Skip to content

Commit bf17e61

Browse files
committed
add lict.hpp
1 parent cf8052d commit bf17e61

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

include/py2cpp/dict.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class dict : public std::unordered_map<Key, T> {
3838

3939
public:
4040
using value_type = std::pair<const Key, T>;
41+
using key_type = Key;
4142

4243
/**
4344
* @brief Construct a new dict object

include/py2cpp/lict.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include "enumerate.hpp"
4+
#include "range.hpp"
5+
#include <vector>
6+
7+
namespace py {
8+
9+
template <typename T> class Lict {
10+
using key_type = size_t;
11+
using value_type = std::pair<size_t, T>;
12+
using iterator = py::Range<key_type>::iterator;
13+
14+
private:
15+
std::vector<T> _lst;
16+
py::Range<key_type> _rng;
17+
18+
public:
19+
Lict(std::vector<T> lst)
20+
: _lst(std::move(lst)), _rng{py::range(lst.size())} {}
21+
22+
T &operator[](const key_type &key) { return this->_lst[key]; }
23+
24+
const T &operator[](const key_type &key) const { return this->_lst[key]; }
25+
26+
// void __delitem__() { throw std::runtime_error("NotImplementedError"); }
27+
28+
iterator begin() { return this->_rng.begin(); }
29+
30+
iterator end() { return this->_rng.end(); }
31+
32+
bool contains(const key_type& key) const {
33+
return this->_rng.contains(key);
34+
}
35+
36+
size_t size() { return this->_lst.size(); }
37+
38+
auto& values() { return this->_lst; }
39+
40+
const auto& values() const { return this->_lst; }
41+
42+
auto items() { return py::enumerate(this->_lst); }
43+
};
44+
45+
} // namespace py
46+
47+
// int main() {
48+
// Lict<int> a(std::vector<int>(8, 0));
49+
// for (int i : a) {
50+
// a[i] = i * i;
51+
// }
52+
// for (auto i : a) {
53+
// std::cout << i << ": " << a[i] << std::endl;
54+
// }
55+
// std::cout << a.__contains__(3) << std::endl;
56+
// return 0;
57+
// }

include/py2cpp/range.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
namespace py {
1414

15-
namespace detail {
16-
1715
template <typename T> struct RangeIterator {
1816
using iterator_category = std::output_iterator_tag;
1917
using difference_type = std::ptrdiff_t;
@@ -43,7 +41,7 @@ template <typename T> struct RangeIterator {
4341
}
4442
};
4543

46-
template <typename T> struct RangeIterableWrapper {
44+
template <typename T> struct Range {
4745
public:
4846
using iterator = RangeIterator<T>;
4947
using value_type = T;
@@ -68,18 +66,17 @@ template <typename T> struct RangeIterableWrapper {
6866
}
6967
};
7068

71-
} // namespace detail
7269

7370
template <typename T>
74-
CONSTEXPR14 auto range(T start, T stop) -> detail::RangeIterableWrapper<T> {
71+
CONSTEXPR14 auto range(T start, T stop) -> Range<T> {
7572
if (stop < start) {
7673
stop = start;
7774
}
78-
return detail::RangeIterableWrapper<T>{start, stop};
75+
return Range<T>{start, stop};
7976
}
8077

8178
template <typename T>
82-
CONSTEXPR14 auto range(T stop) -> detail::RangeIterableWrapper<T> {
79+
CONSTEXPR14 auto range(T stop) -> Range<T> {
8380
return range(T(0), stop);
8481
}
8582

0 commit comments

Comments
 (0)