-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringPool.h
executable file
·157 lines (133 loc) · 4.84 KB
/
stringPool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Created by admin on 2022/7/8.
//
#ifndef MIDILIB_STRINGPOOL_H
#define MIDILIB_STRINGPOOL_H
#include <string>
#include <unordered_map>
#include <memory>
#include <vector>
#include <algorithm>
#include <string.h>
namespace mgnr {
struct stringPool {
struct stringNode {
int count;
std::string value;
stringPool *parent_pool = nullptr;
};
struct stringPtr {
stringNode *parent_node = nullptr;
inline stringPtr() {
parent_node = nullptr;
}
inline stringPtr(stringPool *parent_pool, const std::string &value) {
if (value.empty()) {
parent_node = nullptr;
return;
}
//查找
auto it = parent_pool->indexer.find(value);
if (it == parent_pool->indexer.end()) {
//创建
std::unique_ptr<stringNode> p(new stringNode);
p->count = 1;
p->parent_pool = parent_pool;
p->value = value;
this->parent_node = p.get();
parent_pool->indexer[value] = std::move(p);
} else {
//计数
++it->second->count;
this->parent_node = it->second.get();
}
}
inline stringPtr(const stringPtr &c) {
parent_node = c.parent_node;
if (parent_node) {
++parent_node->count;
}
}
inline const stringPtr &operator=(const stringPtr &c) {
drop();
parent_node = c.parent_node;
if (parent_node) {
++parent_node->count;
}
return *this;
}
const std::string &value() const;
inline bool operator<(const stringPtr &c) const {
return this->value() < c.value();
}
inline bool operator>(const stringPtr &c) const {
return this->value() > c.value();
}
inline bool operator==(const stringPtr &c) const {
return this->value() == c.value();
}
inline bool operator!=(const stringPtr &c) const {
return this->value() != c.value();
}
inline bool operator==(const std::string &c) const {
return this->value() == c;
}
inline bool operator!=(const std::string &c) const {
return this->value() != c;
}
inline bool empty() const {
return this->value().empty();
}
inline const char &at(int i) const {
return this->value().at(i);
}
inline const char &operator[](int i) const {
return this->value().at(i);
}
inline const char *c_str() const {
return this->value().c_str();
}
inline size_t size() const {
return this->value().size();
}
inline void clear() {
drop();
}
inline void drop() {
if (parent_node) {
auto pp = parent_node->parent_pool;
--parent_node->count;
if (parent_node->count <= 0) {
std::string val(value());
//::__android_log_print(ANDROID_LOG_INFO,
// "stringPool",
// "release '%s':%d",
// val.c_str(),parent_node->count);
pp->indexer.erase(val);
}
parent_node = nullptr;
}
}
inline ~stringPtr() {
drop();
}
};
std::unordered_map<std::string, std::unique_ptr<stringNode> > indexer;
inline stringPtr create(const std::string &value) {
return stringPtr(this, value);
}
inline void getStrings(std::vector<stringNode *> &res, bool sort = true) {
res.clear();
for (auto &it:indexer) {
res.push_back(it.second.get());
}
if (sort) {
std::sort(res.begin(), res.end(),
[](const stringNode *x, const stringNode *y) {
return x->count > y->count;
});
}
}
};
}
#endif //MIDILIB_STRINGPOOL_H