Skip to content

Commit 547ef79

Browse files
authored
Merge pull request #3 from mdonaka/fix/palindromic-tree-memory
fix: reduce PalindromicTree memory consumption
2 parents 338ae7f + 7145443 commit 547ef79

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
"Test/Range/bit_subset.test.cpp": "2024-12-27 17:07:26 +0900",
6868
"Test/Range/flatten.test.cpp": "2025-12-20 02:07:58 +0900",
6969
"Test/String/LCPArray.test.cpp": "2025-01-23 04:47:08 +0900",
70-
"Test/String/PalindromicTree.test.cpp": "2025-12-20 03:22:27 +0900",
71-
"Test/String/PalindromicTree_large.test.cpp": "2025-12-20 03:22:27 +0900",
70+
"Test/String/PalindromicTree.test.cpp": "2025-12-19 19:38:03 +0000",
71+
"Test/String/PalindromicTree_large.test.cpp": "2025-12-19 19:38:03 +0000",
7272
"Test/String/SuffixArray.test.cpp": "2024-12-27 17:36:05 +0900",
7373
"Test/String/TrieTree.test.cpp": "2024-12-27 17:07:26 +0900",
7474
"Test/String/ZAlgorithm.test.cpp": "2024-12-27 17:07:26 +0900",

Library/String/PalindromicTree.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ namespace mtd {
2929
if (first_itr == -1) {
3030
first_itr = itr;
3131
} else {
32-
rest_itrs.push_back(itr);
32+
rest_itrs.emplace_back(itr);
3333
}
3434
}
3535

3636
auto get_itrs() const -> std::vector<int> {
3737
if (first_itr == -1) { return {}; }
3838
std::vector<int> result;
3939
result.reserve(1 + rest_itrs.size());
40-
result.push_back(first_itr);
40+
result.emplace_back(first_itr);
4141
result.insert(result.end(), rest_itrs.begin(), rest_itrs.end());
4242
return result;
4343
}
@@ -102,8 +102,8 @@ namespace mtd {
102102
template <class Lambda>
103103
auto dfs_edges(const Lambda& lambda) const -> void {
104104
std::stack<int, std::vector<int>> stk;
105-
stk.push(ROOT_ODD);
106-
stk.push(ROOT_EVEN);
105+
stk.emplace(ROOT_ODD);
106+
stk.emplace(ROOT_EVEN);
107107

108108
while (!stk.empty()) {
109109
int idx = stk.top();
@@ -112,7 +112,7 @@ namespace mtd {
112112
const auto& node = m_nodes[idx];
113113
if (node.size > 0) { lambda(node.size, node.get_itrs()); }
114114

115-
for (const auto& [_, next_idx] : node.edges) { stk.push(next_idx); }
115+
for (const auto& [_, next_idx] : node.edges) { stk.emplace(next_idx); }
116116
}
117117
}
118118

@@ -129,14 +129,14 @@ namespace mtd {
129129
int sl_idx = node.suffix_link;
130130
if (sl_idx >= 2 && m_nodes[sl_idx].first_itr != -1) {
131131
int to = m_nodes[sl_idx].first_itr;
132-
graph[from].push_back(to);
132+
graph[from].emplace_back(to);
133133
++order_count[to];
134134
}
135135
}
136136

137137
std::queue<int> q;
138138
for (int i = 0; i < static_cast<int>(m_s.size()); ++i) {
139-
if (order_count[i] == 0) { q.push(i); }
139+
if (order_count[i] == 0) { q.emplace(i); }
140140
}
141141

142142
while (!q.empty()) {
@@ -145,7 +145,7 @@ namespace mtd {
145145
for (int t : graph[f]) {
146146
--order_count[t];
147147
lambda(f, t);
148-
if (order_count[t] == 0) { q.push(t); }
148+
if (order_count[t] == 0) { q.emplace(t); }
149149
}
150150
}
151151
}

0 commit comments

Comments
 (0)