• R/O
  • SSH
  • HTTPS

tivi: Commit


Commit MetaInfo

Revisão825 (tree)
Hora2012-06-24 23:58:25
Autorfuna-tk

Mensagem de Log

修正

Mudança Sumário

Diff

--- trunk/OLD/exp2/NodeTest.cpp (revision 824)
+++ trunk/OLD/exp2/NodeTest.cpp (revision 825)
@@ -9,12 +9,12 @@
99 CPPUNIT_TEST(child_node_should_leave_the_list_by_itself);
1010 CPPUNIT_TEST(cut_in_a_node_should_be_worked);
1111 CPPUNIT_TEST(cut_in_nodes_should_be_worked);
12+ //CPPUNIT_TEST(insert_with_nodes_should_be_worked);
13+ //CPPUNIT_TEST(delegate_should_be_worked);
1214 //CPPUNIT_TEST(push_back_and_erase_nodes_repeatedly);
1315 //CPPUNIT_TEST(erase_should_be_worked);
1416 //CPPUNIT_TEST(erase_all_should_be_worked);
15- //CPPUNIT_TEST(insert_with_nodes_should_be_worked);
1617 //CPPUNIT_TEST(getParent_should_be_worked);
17- //CPPUNIT_TEST(delegate_should_be_worked);
1818 //CPPUNIT_TEST(iterator_should_be_worked);
1919 //CPPUNIT_TEST(iterator_should_be_worked_using_beginSibling_and_endSibling);
2020 CPPUNIT_TEST_SUITE_END();
@@ -28,11 +28,11 @@
2828 void cut_in_a_node_should_be_worked();
2929 void cut_in_nodes_should_be_worked();
3030 //void push_back_and_erase_nodes_repeatedly();
31+ //void insert_with_nodes_should_be_worked();
32+ //void delegate_should_be_worked();
3133 //void erase_should_be_worked();
3234 //void erase_all_should_be_worked();
33- //void insert_with_nodes_should_be_worked();
3435 //void getParent_should_be_worked();
35- //void delegate_should_be_worked();
3636 //void iterator_should_be_worked();
3737 //void iterator_should_be_worked_using_beginSibling_and_endSibling();
3838 };//
@@ -123,57 +123,43 @@
123123 void
124124 NodeTest::insert_with_nodes_should_be_worked()
125125 {
126- NodeObject rt1;
127- NodeObject rt2;
128- NodeObject rt3;
129- NodeObject rt4;
130- NodeObject rt5;
126+ tivi::Node<int> rt1(1);
127+ tivi::Node<int> rt2(2);
128+ tivi::Node<int> rt3(3);
129+ tivi::Node<int> rt4(4);
130+ tivi::Node<int> rt5(5);
131131
132- rt1.push_back(&rt2);
133- rt1.push_back(&rt3);
134- rt4.push_back(&rt5);
132+ rt1.push_back(rt2);
133+ rt1.push_back(rt3);
134+ rt4.push_back(rt5);
135135
136136 /* r1の子ノードは自動的に離脱する */
137137 rt4.insert(rt4.begin(), rt1.begin(), rt1.end());
138138
139- CPPUNIT_ASSERT_EQUAL(3, rt4.size());
140- CPPUNIT_ASSERT_EQUAL(0, rt1.size());
139+ CPPUNIT_ASSERT_EQUAL(true, 3 == rt4.size());
140+ CPPUNIT_ASSERT_EQUAL(true, 0 == rt1.size());
141141 }
142142
143143 void
144-NodeTest::getParent_should_be_worked()
145-{
146- NodeObject rt1;
147- NodeObject rt2;
148- NodeObject rt3;
149-
150- rt1.push_back(&rt2);
151- rt1.push_back(&rt3);
152-
153- CPPUNIT_ASSERT_EQUAL(true, &rt1 == rt2.getParent());
154- CPPUNIT_ASSERT_EQUAL(true, &rt1 == rt3.getParent());
155-}
156-
157-void
158144 NodeTest::delegate_should_be_worked()
159145 {
160- NodeObject rt1;
161- NodeObject rt2;
162- NodeObject rt3;
163- NodeObject rt4;
164- NodeObject rt5;
165- NodeObject rt6;
146+ tivi::Node<int> a(1);
147+ tivi::Node<int> a1(11);
148+ tivi::Node<int> a2(12);
149+ tivi::Node<int> b(2);
150+ tivi::Node<int> b1(21);
151+ tivi::Node<int> b2(22);
166152
167- rt1.push_back(&rt2);
168- rt1.push_back(&rt3);
153+ a.push_back(a1);
154+ a.push_back(a2);
169155
170- rt4.push_back(&rt5);
171- rt4.push_back(&rt6);
156+ b.push_back(b1);
157+ b.push_back(b2);
172158
173- rt1.delegate(&rt4);
159+ a.delegate(b);
174160
175- CPPUNIT_ASSERT_EQUAL(0, rt1.size());
176- CPPUNIT_ASSERT_EQUAL(4, rt4.size());
161+ CPPUNIT_ASSERT_EQUAL(0, a.size());
162+ CPPUNIT_ASSERT_EQUAL(4, b.size());
177163 }
178164
179165 void
--- trunk/OLD/exp2/Node.h (revision 824)
+++ trunk/OLD/exp2/Node.h (revision 825)
@@ -8,16 +8,16 @@
88
99 template<class T, class C>
1010 std::size_t offset_of(T (C::*pm))
11- {
12- return (std::size_t)(&(((C*)0)->*pm));
13- }
11+ { return (std::size_t)(&(((C*)0)->*pm)); }
1412
1513 template<class T, class C>
1614 C* base_of(T* ptr, T (C::*pm))
17- {
18- return (C*)((const char*)(ptr) - offset_of(pm));
19- }
15+ { return (C*)((const char*)(ptr) - offset_of(pm)); }
2016
17+ template<class T, class C>
18+ const C* base_of(const T* ptr, T (C::*pm))
19+ { return (const C*)((const char*)(ptr) - offset_of(pm)); }
20+
2121 class Link
2222 {
2323 public:
@@ -27,14 +27,10 @@
2727 Link *prev() const { return plink_; }
2828
2929 void link_next(Link* link)
30- {
31- concat(this, link, nlink_);
32- }
30+ { concat(this, link, nlink_); }
3331
3432 void link_prev(Link* link)
35- {
36- concat(plink_, link, this);
37- }
33+ { concat(plink_, link, this); }
3834
3935 void link_next(Link* begin, Link* end)
4036 {
@@ -54,9 +50,7 @@
5450 init_link();
5551 }
5652 static void unlink(Link* start, Link* end)
57- {
58- concat(start->plink_, end->nlink_);
59- }
53+ { concat(start->plink_, end->nlink_); }
6054
6155 public:
6256 static void concat(Link* lhs, Link* rhs)
@@ -106,14 +100,10 @@
106100 }
107101
108102 Node<T>& operator*() const
109- {
110- return *node_;
111- }
103+ { return *node_; }
112104
113105 Node<T>* operator->() const
114- {
115- return node_;
116- }
106+ { return node_; }
117107
118108 self& operator++()
119109 {
@@ -142,14 +132,10 @@
142132 }
143133
144134 bool operator==(const self& x__) const
145- {
146- return node_ == x__.node_;
147- }
135+ { return node_ == x__.node_; }
148136
149137 bool operator!=(const self& x__) const
150- {
151- return node_ != x__.node_;
152- }
138+ { return node_ != x__.node_; }
153139
154140 //operator ConstNodeIterator() const
155141 //{
@@ -159,81 +145,128 @@
159145 Node<T>* node_;
160146 };
161147
148+ template<typename T>
149+ class ConstNodeIterator
150+ {
151+ public:
152+ typedef ConstNodeIterator<T> self;
153+ typedef ptrdiff_t difference_type;
154+ typedef std::bidirectional_iterator_tag iterator_category;
155+ typedef T value_type;
156+ typedef T* pointer;
157+ typedef T& reference;
158+ public:
159+ ConstNodeIterator(const Node<T>* node)
160+ : node_(node) { }
162161
162+ const self& operator=(const self& obj)
163+ {
164+ node_ = obj.node_;
165+ return *this;
166+ }
167+
168+ const Node<T>& operator*() const
169+ { return *node_; }
170+
171+ const Node<T>* operator->() const
172+ { return node_; }
173+
174+ self& operator++()
175+ {
176+ node_ = Node<T>::SiblingToNode(node_->sibling_.next());
177+ return *this;
178+ }
179+
180+ self operator++(int)
181+ {
182+ self tmp = *this;
183+ node_ = Node<T>::SiblingToNode(node_->sibling_.next());
184+ return tmp;
185+ }
186+
187+ self& operator--()
188+ {
189+ node_ = Node<T>::SiblingToNode(node_->sibling_.prev());
190+ return *this;
191+ }
192+
193+ self operator--(int)
194+ {
195+ self tmp = *this;
196+ node_ = Node<T>::SiblingToNode(node_->sibling_.prev());
197+ return tmp;
198+ }
199+
200+ bool operator==(const self& x__) const
201+ { return node_ == x__.node_; }
202+
203+ bool operator!=(const self& x__) const
204+ { return node_ != x__.node_; }
205+ private:
206+ const Node<T>* node_;
207+ };
208+
209+
163210 template <typename T>
164211 class Node
165212 {
166213 public:
167214 friend class NodeIterator<T>;
215+ friend class ConstNodeIterator<T>;
168216 public:
169- typedef Node<T> self;
170- typedef NodeIterator<T> iterator;
217+ typedef Node<T> self;
218+ typedef NodeIterator<T> iterator;
219+ typedef ConstNodeIterator<T> const_iterator;
171220 public:
172221 Node(const T& data)
173222 : data_(data) { }
174223
175224 T get() const
176- {
177- return data_;
178- }
225+ { return data_; }
179226
180227 T& operator*()
181- {
182- return data_;
183- }
228+ { return data_; }
184229
185230 Node& front() const
186- {
187- return *SiblingToNode(child_.next());
188- }
231+ { return *SiblingToNode(child_.next()); }
189232
190233 Node& back() const
191- {
192- return *SiblingToNode(child_.prev());
193- }
234+ { return *SiblingToNode(child_.prev()); }
194235
195- std::size_t size()
236+ std::size_t size() const
196237 {
197238 std::size_t count = 0;
198- for (iterator it = begin(); it != end(); ++it)
239+ for (const_iterator it = begin(); it != end(); ++it)
199240 ++count;
200241 return count;
201242 }
202243
203244 void push_back(Node& node)
204- {
205- this->child_.link_prev(&node.sibling_);
206- }
245+ { this->child_.link_prev(&node.sibling_); }
207246
208247 iterator begin()
209- {
210- return iterator(SiblingToNode(child_.next()));
211- }
248+ { return iterator(SiblingToNode(child_.next())); }
212249
250+ const_iterator begin() const
251+ { return const_iterator(SiblingToNode(child_.next())); }
252+
213253 iterator end()
214- {
215- return iterator(SiblingToNode(&child_));
216- }
254+ { return iterator(SiblingToNode(&child_)); }
217255
256+ const_iterator end() const
257+ { return const_iterator(SiblingToNode(&child_)); }
258+
218259 Node& next() const
219- {
220- return *SiblingToNode(sibling_.next());
221- }
260+ { return *SiblingToNode(sibling_.next()); }
222261
223262 Node& prev() const
224- {
225- return *SiblingToNode(sibling_.prev());
226- }
263+ { return *SiblingToNode(sibling_.prev()); }
227264
228265 void leave()
229- {
230- sibling_.unlink();
231- }
266+ { sibling_.unlink(); }
232267
233268 void cut_in(Node& node)
234- {
235- sibling_.link_next(&node.sibling_);
236- }
269+ { sibling_.link_next(&node.sibling_); }
237270
238271 void cut_in(iterator start, iterator end)
239272 {
@@ -244,15 +277,13 @@
244277 }
245278
246279 bool operator==(Node& node)
247- {
248- return this == &node;
249- }
250-
280+ { return this == &node; }
251281 private:
252282 static Node* SiblingToNode(Link *link)
253- {
254- return base_of(link, &Node::sibling_);
255- }
283+ { return base_of(link, &Node::sibling_); }
284+
285+ static const Node* SiblingToNode(const Link *link)
286+ { return base_of(link, &Node::sibling_); }
256287 private:
257288 T data_;
258289 Link sibling_;
@@ -265,7 +296,6 @@
265296 ost << *node;
266297 return ost;
267298 }
268-
269299 }
270300
271301 #endif
Show on old repository browser