Skip to content

Commit a0d75c0

Browse files
xiao chengxiao cheng
xiao cheng
authored and
xiao cheng
committed
iterator pattern
1 parent 3a87ccf commit a0d75c0

File tree

3 files changed

+208
-1
lines changed

3 files changed

+208
-1
lines changed

IteratorPattern/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ add_executable(Iter_example1 example1.cpp)
88

99
target_link_libraries(Iter_example1 PUBLIC gtest_main ${Boost_LIBRARIES})
1010

11-
install(TARGETS Iter_example1 RUNTIME DESTINATION ../bin)
11+
add_executable(Iter_test iter_test.cpp)
12+
target_link_libraries(Iter_test PUBLIC gtest_main ${Boost_LIBRARIES})
1213

14+
add_executable(Iter_example2 example2.cpp)
15+
target_link_libraries(Iter_example2 PUBLIC gtest_main ${Boost_LIBRARIES})
1316

17+
install(TARGETS Iter_example1 RUNTIME DESTINATION ../bin)
18+
install(TARGETS Iter_test RUNTIME DESTINATION ../bin)
19+
install(TARGETS Iter_example2 RUNTIME DESTINATION ../bin)

IteratorPattern/example2.cpp

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <vector>
4+
#include <string>
5+
using namespace std;
6+
7+
class Container
8+
{
9+
10+
public:
11+
Container(istream &s) : m_stream(s), m_nIndex(0)
12+
{
13+
FillData();
14+
}
15+
16+
class iterator
17+
{
18+
const Container &sln;
19+
size_t m_Index;
20+
21+
public:
22+
iterator(const Container &s) : sln(s), m_Index(0)
23+
{
24+
}
25+
iterator(const Container &s, size_t nSize) : sln(s), m_Index(nSize)
26+
{
27+
}
28+
iterator(const iterator &other) : sln(other.sln), m_Index(other.m_Index)
29+
{
30+
}
31+
void operator++()
32+
{
33+
m_Index++;
34+
}
35+
36+
void operator--()
37+
{
38+
m_Index++;
39+
}
40+
41+
bool operator!=(const iterator &other)
42+
{
43+
return m_Index != other.m_Index;
44+
}
45+
46+
int operator*()
47+
{
48+
return sln.m_Elements[m_Index];
49+
}
50+
};
51+
52+
iterator begin()
53+
{
54+
iterator it(*this);
55+
return it;
56+
}
57+
58+
iterator end()
59+
{
60+
iterator it(*this, m_Elements.size());
61+
return it;
62+
}
63+
64+
private:
65+
void FillData()
66+
{
67+
std::ifstream &in_stream = dynamic_cast<ifstream &>(m_stream);
68+
string line;
69+
vector<string> list;
70+
while (std::getline(in_stream, line))
71+
{
72+
//in_stream >> line;
73+
list.push_back(line);
74+
TrimLeadingSpace(line);
75+
bool bValid = IsValidInteger(line);
76+
if (bValid)
77+
m_Elements.push_back(atoi(line.c_str()));
78+
}
79+
in_stream.close();
80+
}
81+
82+
void TrimLeadingSpace(string &sNum)
83+
{
84+
const char *ws = " \t\n\r\f\v";
85+
sNum.erase(0, sNum.find_first_not_of(ws));
86+
}
87+
88+
bool IsValidInteger(const string &sNum)
89+
{
90+
const int MAX_LIMIT = 1000000000;
91+
const int MIN_LIMIT = -1000000000;
92+
bool bflag = false;
93+
auto startIt = sNum.begin();
94+
if (sNum[0] == '+' || (sNum[0] == '-'))
95+
startIt = sNum.begin() + 1;
96+
97+
for (auto it = startIt; it != sNum.end(); ++it)
98+
{
99+
bflag = true;
100+
char nChar = *it;
101+
if (!isdigit(nChar))
102+
return false;
103+
}
104+
105+
int nNum = atoi(sNum.c_str());
106+
bool bMaxCheck = nNum < MAX_LIMIT && nNum > MIN_LIMIT; //limit check
107+
108+
return (bMaxCheck && bflag);
109+
}
110+
111+
istream &m_stream;
112+
int m_nIndex;
113+
vector<int> m_Elements;
114+
};
115+
116+
int main()
117+
{
118+
std::ifstream file("Test.txt");
119+
Container sobj(file);
120+
121+
for (Container::iterator it = sobj.begin(); it != sobj.end(); ++it)
122+
{
123+
int x = *it;
124+
cout << x << endl;
125+
}
126+
127+
return 0;
128+
}

IteratorPattern/iter_test.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
template <typename T>
6+
struct Node
7+
{
8+
T value;
9+
Node *left{nullptr}, *right{nullptr}, *parent{nullptr};
10+
11+
Node(T value) : value(value) {}
12+
13+
Node(T value, Node<T> *left, Node<T> *right) : value(value), left(left), right(right) {
14+
left->parent = right->parent = this;
15+
}
16+
17+
void preorder_traversal_impl(Node<T>* current, vector<Node<T>*>& result)
18+
{
19+
result.push_back(current);
20+
if (current->left)
21+
{
22+
preorder_traversal_impl(current->left, result);
23+
}
24+
if (current->right)
25+
{
26+
preorder_traversal_impl(current->right, result);
27+
}
28+
}
29+
30+
// traverse the node and its children preorder
31+
// and put all the results into `result`
32+
33+
// this interface does not expose it is a tree
34+
void preorder_traversal(vector<Node<T>*>& result)
35+
{
36+
preorder_traversal_impl(this, result);
37+
}
38+
};
39+
40+
#include "gtest/gtest.h"
41+
42+
//#include "helpers/iohelper.h"
43+
44+
//#include "exercise.cpp"
45+
46+
namespace {
47+
48+
class Evaluate : public ::testing::Test {};
49+
50+
TEST_F(Evaluate, ExampleTest) {
51+
Node<char> c{'c'};
52+
Node<char> d{'d'};
53+
Node<char> e{'e'};
54+
Node<char> b{'b', &c, &d};
55+
Node<char> a{'a', &b, &e};
56+
57+
vector<Node<char>*> result;
58+
a.preorder_traversal(result);
59+
60+
ostringstream oss;
61+
for (auto n : result)
62+
oss << n->value;
63+
ASSERT_EQ("abcde", oss.str());
64+
}
65+
66+
} // namespace
67+
68+
int main(int ac, char* av[])
69+
{
70+
//::testing::GTEST_FLAG(catch_exceptions) = false;
71+
testing::InitGoogleTest(&ac, av);
72+
return RUN_ALL_TESTS();
73+
}

0 commit comments

Comments
 (0)