Skip to content

Commit 0653894

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

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ option(INSTALL_ONLY "Enable for installation only" OFF)
1010
# Note: update this to your new project's name and version
1111
project(
1212
Py2Cpp
13-
VERSION 1.4.3
13+
VERSION 1.4.4
1414
LANGUAGES CXX
1515
)
1616

include/py2cpp/lict.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@
77
namespace py {
88

99
template <typename T> class Lict {
10+
public:
1011
using key_type = size_t;
1112
using value_type = std::pair<size_t, T>;
1213
using iterator = py::Range<key_type>::iterator;
14+
using const_iterator = py::Range<key_type>::iterator;
1315

1416
private:
15-
std::vector<T> _lst;
1617
py::Range<key_type> _rng;
18+
std::vector<T> _lst;
1719

1820
public:
1921
Lict(std::vector<T> lst)
20-
: _lst(std::move(lst)), _rng{py::range(lst.size())} {}
22+
: _rng{py::range(lst.size())}, _lst(std::move(lst)) {}
2123

2224
T &operator[](const key_type &key) { return this->_lst[key]; }
2325

2426
const T &operator[](const key_type &key) const { return this->_lst[key]; }
2527

2628
// void __delitem__() { throw std::runtime_error("NotImplementedError"); }
2729

28-
iterator begin() { return this->_rng.begin(); }
30+
iterator begin() const { return this->_rng.begin(); }
2931

30-
iterator end() { return this->_rng.end(); }
32+
iterator end() const { return this->_rng.end(); }
3133

32-
bool contains(const key_type& key) const {
33-
return this->_rng.contains(key);
34-
}
34+
bool contains(const key_type &key) const { return this->_rng.contains(key); }
3535

36-
size_t size() { return this->_lst.size(); }
36+
size_t size() const { return this->_rng.size(); }
3737

38-
auto& values() { return this->_lst; }
38+
auto &values() { return this->_lst; }
3939

40-
const auto& values() const { return this->_lst; }
40+
const auto &values() const { return this->_lst; }
4141

4242
auto items() { return py::enumerate(this->_lst); }
4343
};

include/py2cpp/range.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,14 @@ template <typename T> struct Range {
6666
}
6767
};
6868

69-
70-
template <typename T>
71-
CONSTEXPR14 auto range(T start, T stop) -> Range<T> {
69+
template <typename T> CONSTEXPR14 auto range(T start, T stop) -> Range<T> {
7270
if (stop < start) {
7371
stop = start;
7472
}
7573
return Range<T>{start, stop};
7674
}
7775

78-
template <typename T>
79-
CONSTEXPR14 auto range(T stop) -> Range<T> {
76+
template <typename T> CONSTEXPR14 auto range(T stop) -> Range<T> {
8077
return range(T(0), stop);
8178
}
8279

test/source/test_dict.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#include <doctest/doctest.h> // for ResultBuilder, TestCase, CHECK, TEST_CASE
22

3-
// #include <py2cpp/dict.hpp> // for dict, key_iterator
3+
#include <py2cpp/dict.hpp> // for dict, key_iterator
44
// #include <unordered_map> // for operator!=, __hash_map_const_iterator
5-
// #include <utility> // for pair
6-
//
7-
// TEST_CASE("Test set") {
8-
// using E = std::pair<double, int>;
9-
// const auto S = py::dict<double, int>{E{0.1, 1}, E{0.3, 3}, E{0.4, 4}};
10-
// auto count = 0;
11-
// for (const auto& p : S) {
12-
// static_assert(sizeof(p) >= 0, "make compiler happy");
13-
// ++count;
14-
// }
15-
// CHECK(count == 3);
16-
// }
5+
#include <utility> // for pair
6+
7+
TEST_CASE("Test py::dict") {
8+
using E = std::pair<double, int>;
9+
const auto S = py::dict<double, int>{E{0.1, 1}, E{0.3, 3}, E{0.4, 4}};
10+
auto count = 0;
11+
for (const auto& p : S) {
12+
static_assert(sizeof(p) >= 0, "make compiler happy");
13+
++count;
14+
}
15+
CHECK(count == 3);
16+
}

test/source/test_lict.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <doctest/doctest.h> // for ResultBuilder, TestCase, CHECK, TEST_CASE
2+
3+
#include <py2cpp/lict.hpp> // for Lict, key_iterator
4+
#include <vector>
5+
6+
TEST_CASE("Test Lict") {
7+
const auto S = py::Lict<double>{std::vector<double>{0.6, 0.7, 0.8}};
8+
CHECK_EQ(S.size(), 3U);
9+
CHECK(S.contains(1));
10+
auto count = 0;
11+
for (const auto& p : S) {
12+
static_assert(sizeof(p) >= 0, "make compiler happy");
13+
++count;
14+
}
15+
CHECK(count == 3);
16+
}

0 commit comments

Comments
 (0)