Debian Codegraph @

old-sample/legacy-checkmate/lib/twinTable.cc

raw
1 #include "twinTable.h"
2 #include "twinList.h"
3 #include "checkMoveListProvider.h"
4 #include "osl/stl/hash_set.h"
5
6 namespace osl
7 {
8 namespace checkmate
9 {
10 #ifdef OSL_SMP
11 static const int initial_capacity = 0;
12 #else
13 static const int initial_capacity = 100;
14 #endif
15
16 /**
17 * set にいれたデータの一部を変更するための mutable wrapper.
18 * set の同一性を path だけで見るのでpath さえ変更しなければ
19 * 整合性は保たれる.
20 * TODO: 本質的には hash_map 改造クラスを作るべき
21 */
22 struct TwinAgeMutable
23 {
24 mutable TwinAgeEntry body;
25 TwinAgeMutable(const TwinAgeEntry& e) : body(e)
26 {
27 }
28 };
29
30 struct TwinPathHash
31 {
32 unsigned long operator()(const TwinAgeMutable& e) const
33 {
34 return e.body.entry.path.getPath();
35 }
36 };
37 struct TwinPathEqual
38 {
39 bool operator()(const TwinAgeMutable& l, const TwinAgeMutable& r) const
40 {
41 return l.body.entry.path == r.body.entry.path;
42 }
43 };
44
45 typedef hash_set<TwinAgeMutable,TwinPathHash,TwinPathEqual> hash_set_t;
46
47 struct TwinTable::Table : public hash_set_t
48 {
49 explicit Table(size_t capacity) : hash_set_t(capacity)
50 {
51 }
52 };
53 } // namespace checkmate
54 } // namespace osl
55
56 osl::checkmate::
57 TwinTable::TwinTable() : table(new Table(initial_capacity))
58 {
59 }
60
61 osl::checkmate::
62 TwinTable::~TwinTable()
63 {
64 }
65
66 void osl::checkmate::
67 TwinTable::addTwin(const TwinEntry& e)
68 {
69 const TwinAgeEntry ae(e.path, e.move, e.loopTo);
70 std::pair<Table::iterator,bool> ib = table->insert(TwinAgeMutable(ae));
71 if (! ib.second)
72 {
73 ib.first->body.setTwinEntry(e);
74 }
75 }
76
77 void osl::checkmate::
78 TwinTable::clearTwins()
79 {
80 table->clear();
81 }
82
83 const osl::checkmate::TwinEntry * osl::checkmate::
84 TwinTable::findTwin(const PathEncoding& key) const
85 {
86 Table::const_iterator p=table->find(TwinAgeMutable(TwinAgeEntry(key)));
87 if ((p == table->end())
88 || (! p->body.hasTwinEntry()))
89 return 0;
90 return &(p->body.entry);
91 }
92
93 osl::checkmate::TwinAgeEntry& osl::checkmate::
94 TwinTable::allocateTwin(const PathEncoding& key)
95 {
96 return table->insert(TwinAgeMutable(TwinAgeEntry((key)))).first->body;
97 }
98
99 size_t osl::checkmate::
100 TwinTable::size() const
101 {
102 return table->size();
103 }
104
105 /* ------------------------------------------------------------------------- */
106
107 osl::checkmate::
108 TwinTableHolder::TwinTableHolder() : list_provider(new CheckMoveListProvider())
109 {
110 }
111
112 osl::checkmate::
113 TwinTableHolder::~TwinTableHolder()
114 {
115 }
116
117 void osl::checkmate::
118 TwinTableHolder::clear()
119 {
120 list_provider->clear();
121 clearTwins();
122 }
123
124 /* ------------------------------------------------------------------------- */
125 // ;;; Local Variables:
126 // ;;; mode:c++
127 // ;;; c-basic-offset:2
128 // ;;; End: