Move folder
This commit is contained in:
91
include/editor/hooks.h
Executable file
91
include/editor/hooks.h
Executable file
@@ -0,0 +1,91 @@
|
||||
#ifndef EDITOR_HOOKS_H
|
||||
#define EDITOR_HOOKS_H
|
||||
|
||||
#include "utils/utils.h"
|
||||
|
||||
struct HookEngine {
|
||||
uint32_t hooks[94];
|
||||
std::vector<std::pair<uint32_t, char>> v;
|
||||
std::vector<std::pair<uint32_t, char>>::iterator it;
|
||||
|
||||
HookEngine() {
|
||||
for (int i = 0; i < 94; i++)
|
||||
hooks[i] = UINT32_MAX;
|
||||
v.reserve(94);
|
||||
}
|
||||
|
||||
inline void edit(uint32_t line, uint32_t removed_rows,
|
||||
uint32_t inserted_rows) {
|
||||
|
||||
int64_t delta = (int64_t)inserted_rows - (int64_t)removed_rows;
|
||||
for (int i = 0; i < 94; i++) {
|
||||
uint32_t &h = hooks[i];
|
||||
if (h == UINT32_MAX)
|
||||
continue;
|
||||
if (h < line)
|
||||
continue;
|
||||
if (removed_rows > 0 && h < line + removed_rows) {
|
||||
h = UINT32_MAX;
|
||||
continue;
|
||||
}
|
||||
h = (uint32_t)((int64_t)h + delta);
|
||||
}
|
||||
}
|
||||
|
||||
inline void set(char c, uint32_t line) {
|
||||
if (c < '!' || c > '~')
|
||||
return;
|
||||
int idx = c - '!';
|
||||
for (int i = 0; i < 94; i++) {
|
||||
if (hooks[i] == line) {
|
||||
hooks[i] = UINT32_MAX;
|
||||
break;
|
||||
}
|
||||
}
|
||||
hooks[idx] = line;
|
||||
}
|
||||
|
||||
inline bool get(char c, uint32_t *out_line) const {
|
||||
if (c < '!' || c > '~')
|
||||
return false;
|
||||
uint32_t h = hooks[c - '!'];
|
||||
if (h == UINT32_MAX)
|
||||
return false;
|
||||
*out_line = h;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void clear_line(uint32_t line) {
|
||||
for (int i = 0; i < 94; i++)
|
||||
if (hooks[i] == line)
|
||||
hooks[i] = UINT32_MAX;
|
||||
}
|
||||
|
||||
inline void clear(char c) {
|
||||
if (c < '!' || c > '~')
|
||||
return;
|
||||
hooks[c - '!'] = UINT32_MAX;
|
||||
}
|
||||
|
||||
void start_iter(uint32_t scroll_line) {
|
||||
for (int i = 0; i < 94; i++)
|
||||
if (hooks[i] != UINT32_MAX)
|
||||
v.push_back({hooks[i], (char)('!' + i)});
|
||||
std::sort(v.begin(), v.end(),
|
||||
[](const auto &a, const auto &b) { return a.first < b.first; });
|
||||
it = std::lower_bound(
|
||||
v.begin(), v.end(), scroll_line,
|
||||
[](const auto &p, uint32_t line) { return p.first < line; });
|
||||
}
|
||||
|
||||
char next(uint32_t line_index) {
|
||||
if (it != v.end() && it->first == line_index) {
|
||||
char c = it->second;
|
||||
it++;
|
||||
return c;
|
||||
}
|
||||
return '\0';
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user