1#ifndef DAWGDIC_RANKED_GUIDE_H
2#define DAWGDIC_RANKED_GUIDE_H
3
4#include "dictionary.h"
5#include "ranked-guide-unit.h"
6
7#include <iostream>
8#include <vector>
9
10namespace dawgdic {
11
12class RankedGuide {
13 public:
14 RankedGuide() : units_(NULL), size_(0), units_buf_() {}
15
16 const RankedGuideUnit *units() const {
17 return units_;
18 }
19 SizeType size() const {
20 return size_;
21 }
22 SizeType total_size() const {
23 return sizeof(RankedGuideUnit) * size_;
24 }
25 SizeType file_size() const {
26 return sizeof(BaseType) + total_size();
27 }
28
29 // The root index.
30 BaseType root() const {
31 return 0;
32 }
33
34 UCharType child(BaseType index) const {
35 return units_[index].child();
36 }
37 UCharType sibling(BaseType index) const {
38 return units_[index].sibling();
39 }
40
41 // Reads a dictionary from an input stream.
42 bool Read(std::istream *input) {
43 BaseType base_size;
44 if (!input->read(reinterpret_cast<char *>(&base_size), sizeof(BaseType))) {
45 return false;
46 }
47
48 SizeType size = static_cast<SizeType>(base_size);
49 std::vector<RankedGuideUnit> units_buf(size);
50 if (!input->read(reinterpret_cast<char *>(&units_buf[0]),
51 sizeof(RankedGuideUnit) * size)) {
52 return false;
53 }
54
55 SwapUnitsBuf(&units_buf);
56 return true;
57 }
58
59 // Writes a dictionry to an output stream.
60 bool Write(std::ostream *output) const {
61 BaseType base_size = static_cast<BaseType>(size_);
62 if (!output->write(reinterpret_cast<const char *>(&base_size),
63 sizeof(BaseType))) {
64 return false;
65 }
66
67 if (!output->write(reinterpret_cast<const char *>(units_),
68 sizeof(RankedGuideUnit) * size_)) {
69 return false;
70 }
71
72 return true;
73 }
74
75 // Maps memory with its size.
76 void Map(const void *address) {
77 Clear();
78 units_ = reinterpret_cast<const RankedGuideUnit *>(
79 static_cast<const BaseType *>(address) + 1);
80 size_ = *static_cast<const BaseType *>(address);
81 }
82 void Map(const void *address, SizeType size) {
83 Clear();
84 units_ = static_cast<const RankedGuideUnit *>(address);
85 size_ = size;
86 }
87
88 // Swaps RankedGuides.
89 void Swap(RankedGuide *guide) {
90 std::swap(units_, guide->units_);
91 std::swap(size_, guide->size_);
92 units_buf_.swap(guide->units_buf_);
93 }
94
95 // Initializes a RankedGuide.
96 void Clear() {
97 units_ = NULL;
98 size_ = 0;
99 std::vector<RankedGuideUnit>(0).swap(units_buf_);
100 }
101
102 public:
103 // Following member function is called from DawgBuilder.
104
105 // Swaps buffers for units.
106 void SwapUnitsBuf(std::vector<RankedGuideUnit> *units_buf) {
107 units_ = &(*units_buf)[0];
108 size_ = static_cast<BaseType>(units_buf->size());
109 units_buf_.swap(*units_buf);
110 }
111
112 private:
113 const RankedGuideUnit *units_;
114 SizeType size_;
115 std::vector<RankedGuideUnit> units_buf_;
116
117 // Disables copies.
118 RankedGuide(const RankedGuide &);
119 RankedGuide &operator=(const RankedGuide &);
120};
121
122} // namespace dawgdic
123
124#endif // DAWGDIC_RANKED_GUIDE_H
125