|
1 | | -/* Sparse Array |
2 | | - * |
3 | | - * From: https://github.com/PokemonAutomation/ |
4 | | - * |
5 | | - */ |
6 | | - |
7 | | -#include <string.h> |
8 | | -#include "SparseArray.h" |
9 | | - |
10 | | -//#include <iostream> |
11 | | -//using std::cout; |
12 | | -//using std::endl; |
13 | | - |
14 | | -namespace PokemonAutomation{ |
15 | | - |
16 | | - |
17 | | - |
18 | | -void SparseArray::write(size_t address, size_t bytes, const void* data){ |
19 | | - if (m_data.empty()){ |
20 | | - m_data.emplace( |
21 | | - std::piecewise_construct, |
22 | | - std::forward_as_tuple(address), |
23 | | - std::forward_as_tuple((char*)data, (char*)data + bytes) |
24 | | - ); |
25 | | - return; |
26 | | - } |
27 | | - |
28 | | - // Below bottom. |
29 | | - size_t top = address + bytes; |
30 | | - size_t lowest = m_data.begin()->first; |
31 | | - if (top < lowest){ |
32 | | - m_data.emplace( |
33 | | - std::piecewise_construct, |
34 | | - std::forward_as_tuple(address), |
35 | | - std::forward_as_tuple((char*)data, (char*)data + bytes) |
36 | | - ); |
37 | | - return; |
38 | | - } |
39 | | - |
40 | | - // Above top. |
41 | | - size_t highest = m_data.rbegin()->first + m_data.rbegin()->second.size(); |
42 | | - if (highest < address){ |
43 | | - m_data.emplace( |
44 | | - std::piecewise_construct, |
45 | | - std::forward_as_tuple(address), |
46 | | - std::forward_as_tuple((char*)data, (char*)data + bytes) |
47 | | - ); |
48 | | - return; |
49 | | - } |
50 | | - |
51 | | - // Get the first and last blocks that will be affected. |
52 | | - auto lower = m_data.upper_bound(address); |
53 | | - if (lower != m_data.begin()){ |
54 | | - --lower; |
55 | | - } |
56 | | - if (lower->first + lower->second.size() < address){ |
57 | | - ++lower; |
58 | | - } |
59 | | - auto upper = m_data.lower_bound(top); |
60 | | - if (upper != m_data.begin()){ |
61 | | - --upper; |
62 | | - } |
63 | | - |
64 | | - lowest = lower->first; |
65 | | - highest = upper->first + upper->second.size(); |
66 | | - if (lower == upper && lowest <= address && top <= highest){ |
67 | | - // Completely inside. |
68 | | - memcpy(lower->second.data() + address - lowest, data, bytes); |
69 | | - return; |
70 | | - } |
71 | | - |
72 | | - lowest = std::min(lowest, address); |
73 | | - highest = std::max(highest, top); |
74 | | - |
75 | | - std::string block(highest - lowest, 0); |
76 | | - |
77 | | - // Copy in the partial blocks. |
78 | | - bool one_block = lower == upper; |
79 | | - { |
80 | | - size_t shift = lower->first - lowest; |
81 | | - memcpy( |
82 | | - block.data() + shift, |
83 | | - lower->second.data(), |
84 | | - lower->second.size() |
85 | | - ); |
86 | | - } |
87 | | - if (!one_block){ |
88 | | - size_t shift = highest - upper->first - upper->second.size(); |
89 | | - memcpy( |
90 | | - block.data() + block.size() - upper->second.size() - shift, |
91 | | - upper->second.data(), |
92 | | - upper->second.size() |
93 | | - ); |
94 | | - } |
95 | | - |
96 | | -// cout << "lower = " << lower->first << endl; |
97 | | -// cout << "upper = " << upper->first << endl; |
98 | | -// cout << "lowest = " << lowest << endl; |
99 | | -// cout << "highest = " << highest << endl; |
100 | | - |
101 | | - // Copy the newly written block. |
102 | | - memcpy(block.data() + address - lowest, data, bytes); |
103 | | - |
104 | | -// cout << std::string((char*)block.data(), block.size()) << endl; |
105 | | - |
106 | | - // Insert into table. |
107 | | - if (lower->first == lowest){ |
108 | | -// cout << "replace" << endl; |
109 | | - lower->second = std::move(block); |
110 | | - ++lower; |
111 | | - }else{ |
112 | | -// cout << "add" << endl; |
113 | | - auto iter = m_data.emplace( |
114 | | - std::piecewise_construct, |
115 | | - std::forward_as_tuple(lowest), |
116 | | - std::forward_as_tuple(std::move(block)) |
117 | | - ).first; |
118 | | - m_data.erase(lower); |
119 | | - ++iter; |
120 | | - lower = iter; |
121 | | - } |
122 | | - |
123 | | - // Remove all the blocks that it touched. |
124 | | - if (!one_block){ |
125 | | - while (lower != upper){ |
126 | | - lower = m_data.erase(lower); |
127 | | - } |
128 | | - m_data.erase(lower); |
129 | | - } |
130 | | -} |
131 | | -void SparseArray::read(size_t address, size_t bytes, void* data) const{ |
132 | | - if (m_data.empty()){ |
133 | | - return; |
134 | | - } |
135 | | - |
136 | | - // Below bottom. |
137 | | - size_t top = address + bytes; |
138 | | - size_t lowest = m_data.begin()->first; |
139 | | - if (top < lowest){ |
140 | | - return; |
141 | | - } |
142 | | - |
143 | | - // Above top. |
144 | | - size_t highest = m_data.rbegin()->first + m_data.rbegin()->second.size(); |
145 | | - if (highest < address){ |
146 | | - return; |
147 | | - } |
148 | | - |
149 | | - auto lower = m_data.upper_bound(address); |
150 | | - if (lower != m_data.begin()){ |
151 | | - --lower; |
152 | | - } |
153 | | - |
154 | | - while (lower != m_data.end() && bytes > 0){ |
155 | | - size_t block = lower->first; |
156 | | - if (block >= top){ |
157 | | - break; |
158 | | - } |
159 | | - |
160 | | -// cout << "bytes = " << bytes << endl; |
161 | | -// cout << "address = " << address << endl; |
162 | | -// cout << "block = " << block << endl; |
163 | | - |
164 | | - const char* ptr = lower->second.data(); |
165 | | - size_t len = lower->second.size(); |
166 | | - |
167 | | - if (block < address){ |
168 | | -// cout << "block is before" << endl; |
169 | | - size_t shift = address - block; |
170 | | - block = address; |
171 | | - ptr += shift; |
172 | | - len -= shift; // Cannot go negative since it's guaranteed to overlap with region. |
173 | | - } |
174 | | - if (block > address){ |
175 | | -// cout << "block is later" << endl; |
176 | | - size_t shift = block - address; |
177 | | -// cout << "shift = " << shift << endl; |
178 | | - address = block; |
179 | | - data = (char*)data + shift; |
180 | | - bytes -= shift; // Cannot go negative since it's guaranteed to overlap with block. |
181 | | - } |
182 | | - |
183 | | - len = std::min(len, bytes); |
184 | | - |
185 | | -// cout << "len = " << len << endl; |
186 | | - |
187 | | - memcpy(data, ptr, len); |
188 | | - data = (char*)data + len; |
189 | | - address += len; |
190 | | - bytes -= len; |
191 | | - ++lower; |
192 | | - } |
193 | | - |
194 | | -} |
195 | | - |
196 | | - |
197 | | - |
198 | | - |
199 | | -} |
| 1 | +/* Sparse Array |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | +#include "SparseArray.h" |
| 9 | + |
| 10 | +//#include <iostream> |
| 11 | +//using std::cout; |
| 12 | +//using std::endl; |
| 13 | + |
| 14 | +namespace PokemonAutomation{ |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +void SparseArray::write(size_t address, size_t bytes, const void* data){ |
| 19 | + if (m_data.empty()){ |
| 20 | + m_data.emplace( |
| 21 | + std::piecewise_construct, |
| 22 | + std::forward_as_tuple(address), |
| 23 | + std::forward_as_tuple((char*)data, (char*)data + bytes) |
| 24 | + ); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // Below bottom. |
| 29 | + size_t top = address + bytes; |
| 30 | + size_t lowest = m_data.begin()->first; |
| 31 | + if (top < lowest){ |
| 32 | + m_data.emplace( |
| 33 | + std::piecewise_construct, |
| 34 | + std::forward_as_tuple(address), |
| 35 | + std::forward_as_tuple((char*)data, (char*)data + bytes) |
| 36 | + ); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Above top. |
| 41 | + size_t highest = m_data.rbegin()->first + m_data.rbegin()->second.size(); |
| 42 | + if (highest < address){ |
| 43 | + m_data.emplace( |
| 44 | + std::piecewise_construct, |
| 45 | + std::forward_as_tuple(address), |
| 46 | + std::forward_as_tuple((char*)data, (char*)data + bytes) |
| 47 | + ); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Get the first and last blocks that will be affected. |
| 52 | + auto lower = m_data.upper_bound(address); |
| 53 | + if (lower != m_data.begin()){ |
| 54 | + --lower; |
| 55 | + } |
| 56 | + if (lower->first + lower->second.size() < address){ |
| 57 | + ++lower; |
| 58 | + } |
| 59 | + auto upper = m_data.lower_bound(top); |
| 60 | + if (upper != m_data.begin()){ |
| 61 | + --upper; |
| 62 | + } |
| 63 | + |
| 64 | + lowest = lower->first; |
| 65 | + highest = upper->first + upper->second.size(); |
| 66 | + if (lower == upper && lowest <= address && top <= highest){ |
| 67 | + // Completely inside. |
| 68 | + memcpy(lower->second.data() + address - lowest, data, bytes); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + lowest = std::min(lowest, address); |
| 73 | + highest = std::max(highest, top); |
| 74 | + |
| 75 | + std::string block(highest - lowest, 0); |
| 76 | + |
| 77 | + // Copy in the partial blocks. |
| 78 | + bool one_block = lower == upper; |
| 79 | + { |
| 80 | + size_t shift = lower->first - lowest; |
| 81 | + memcpy( |
| 82 | + block.data() + shift, |
| 83 | + lower->second.data(), |
| 84 | + lower->second.size() |
| 85 | + ); |
| 86 | + } |
| 87 | + if (!one_block){ |
| 88 | + size_t shift = highest - upper->first - upper->second.size(); |
| 89 | + memcpy( |
| 90 | + block.data() + block.size() - upper->second.size() - shift, |
| 91 | + upper->second.data(), |
| 92 | + upper->second.size() |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | +// cout << "lower = " << lower->first << endl; |
| 97 | +// cout << "upper = " << upper->first << endl; |
| 98 | +// cout << "lowest = " << lowest << endl; |
| 99 | +// cout << "highest = " << highest << endl; |
| 100 | + |
| 101 | + // Copy the newly written block. |
| 102 | + memcpy(block.data() + address - lowest, data, bytes); |
| 103 | + |
| 104 | +// cout << std::string((char*)block.data(), block.size()) << endl; |
| 105 | + |
| 106 | + // Insert into table. |
| 107 | + if (lower->first == lowest){ |
| 108 | +// cout << "replace" << endl; |
| 109 | + lower->second = std::move(block); |
| 110 | + ++lower; |
| 111 | + }else{ |
| 112 | +// cout << "add" << endl; |
| 113 | + auto iter = m_data.emplace( |
| 114 | + std::piecewise_construct, |
| 115 | + std::forward_as_tuple(lowest), |
| 116 | + std::forward_as_tuple(std::move(block)) |
| 117 | + ).first; |
| 118 | + m_data.erase(lower); |
| 119 | + ++iter; |
| 120 | + lower = iter; |
| 121 | + } |
| 122 | + |
| 123 | + // Remove all the blocks that it touched. |
| 124 | + if (!one_block){ |
| 125 | + while (lower != upper){ |
| 126 | + lower = m_data.erase(lower); |
| 127 | + } |
| 128 | + m_data.erase(lower); |
| 129 | + } |
| 130 | +} |
| 131 | +void SparseArray::read(size_t address, size_t bytes, void* data) const{ |
| 132 | + if (m_data.empty()){ |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + // Below bottom. |
| 137 | + size_t top = address + bytes; |
| 138 | + size_t lowest = m_data.begin()->first; |
| 139 | + if (top < lowest){ |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + // Above top. |
| 144 | + size_t highest = m_data.rbegin()->first + m_data.rbegin()->second.size(); |
| 145 | + if (highest < address){ |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + auto lower = m_data.upper_bound(address); |
| 150 | + if (lower != m_data.begin()){ |
| 151 | + --lower; |
| 152 | + } |
| 153 | + |
| 154 | + while (lower != m_data.end() && bytes > 0){ |
| 155 | + size_t block = lower->first; |
| 156 | + if (block >= top){ |
| 157 | + break; |
| 158 | + } |
| 159 | + |
| 160 | +// cout << "bytes = " << bytes << endl; |
| 161 | +// cout << "address = " << address << endl; |
| 162 | +// cout << "block = " << block << endl; |
| 163 | + |
| 164 | + const char* ptr = lower->second.data(); |
| 165 | + size_t len = lower->second.size(); |
| 166 | + |
| 167 | + if (block < address){ |
| 168 | +// cout << "block is before" << endl; |
| 169 | + size_t shift = address - block; |
| 170 | + block = address; |
| 171 | + ptr += shift; |
| 172 | + len -= shift; // Cannot go negative since it's guaranteed to overlap with region. |
| 173 | + } |
| 174 | + if (block > address){ |
| 175 | +// cout << "block is later" << endl; |
| 176 | + size_t shift = block - address; |
| 177 | +// cout << "shift = " << shift << endl; |
| 178 | + address = block; |
| 179 | + data = (char*)data + shift; |
| 180 | + bytes -= shift; // Cannot go negative since it's guaranteed to overlap with block. |
| 181 | + } |
| 182 | + |
| 183 | + len = std::min(len, bytes); |
| 184 | + |
| 185 | +// cout << "len = " << len << endl; |
| 186 | + |
| 187 | + memcpy(data, ptr, len); |
| 188 | + data = (char*)data + len; |
| 189 | + address += len; |
| 190 | + bytes -= len; |
| 191 | + ++lower; |
| 192 | + } |
| 193 | + |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | +} |
0 commit comments