-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed_size_array_tracker.cpp
108 lines (89 loc) · 3.34 KB
/
fixed_size_array_tracker.cpp
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
#include "fixed_size_array_tracker.hpp"
#include <vector>
#include <string>
FixedSizeArrayTracker::FixedSizeArrayTracker(unsigned int size, bool logging_enabled)
: size(size), logging_enabled(logging_enabled) {}
void FixedSizeArrayTracker::log(const std::string &message) const {
if (logging_enabled) {
std::cout << "[LOG]: " << message << "\n";
std::cout << *this << "\n";
}
}
std::optional<unsigned int> FixedSizeArrayTracker::find_contiguous_space(unsigned int length) {
unsigned int last_end = 0;
// Traverse through sorted intervals
for (const auto &[start, end] : occupied_intervals) {
if (start - last_end >= length) {
return last_end; // Found space before this interval
}
last_end = end;
}
// Check if there's enough space after the last interval
if (size - last_end >= length) {
return last_end;
}
return std::nullopt;
}
void FixedSizeArrayTracker::add_metadata(int id, unsigned int start, unsigned int length) {
if (metadata.count(id)) {
log("ID '" + std::to_string(id) + "' already exists. Use a unique ID.");
return;
}
if (start + length > size) {
log("Error: Metadata exceeds array bounds.");
return;
}
// Add metadata and update occupied intervals
metadata[id] = {start, length};
occupied_intervals.insert({start, start + length});
log("Added metadata: ID=" + std::to_string(id) + ", start=" + std::to_string(start) +
", length=" + std::to_string(length));
}
void FixedSizeArrayTracker::remove_metadata(int id) {
auto it = metadata.find(id);
if (it != metadata.end()) {
// Remove metadata and update intervals
auto &[start, length] = it->second;
occupied_intervals.erase({start, start + length});
metadata.erase(it);
log("Removed metadata for ID=" + std::to_string(id));
} else {
log("ID '" + std::to_string(id) + "' not found.");
}
}
std::optional<std::pair<unsigned int, unsigned int>> FixedSizeArrayTracker::get_metadata(int id) const {
auto it = metadata.find(id);
if (it != metadata.end()) {
return it->second;
}
return std::nullopt;
}
void FixedSizeArrayTracker::compact() {
unsigned int current_index = 0;
std::unordered_map<int, std::pair<unsigned int, unsigned int>> new_metadata;
// Reassign metadata to eliminate gaps
for (const auto &[id, range] : metadata) {
unsigned int length = range.second;
new_metadata[id] = {current_index, length};
current_index += length;
}
metadata = std::move(new_metadata); // Move the new metadata
occupied_intervals.clear(); // Clear the current intervals
for (const auto &[id, range] : metadata) {
unsigned int start = range.first;
unsigned int length = range.second;
occupied_intervals.insert({start, start + length});
}
log("Compacted metadata.");
}
const std::unordered_map<int, std::pair<unsigned int, unsigned int>> &FixedSizeArrayTracker::get_all_metadata() const {
return metadata;
}
std::ostream &operator<<(std::ostream &os, const FixedSizeArrayTracker &tracker) {
os << "Metadata: {";
for (const auto &[id, range] : tracker.metadata) {
os << id << ": (start=" << range.first << ", length=" << range.second << "), ";
}
os << "}";
return os;
}