-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed_size_array_tracker.hpp
35 lines (24 loc) · 1.1 KB
/
fixed_size_array_tracker.hpp
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
#ifndef FIXED_SIZE_ARRAY_TRACKER_HPP
#define FIXED_SIZE_ARRAY_TRACKER_HPP
#include <unordered_map>
#include <set>
#include <optional>
#include <iostream>
class FixedSizeArrayTracker {
public:
FixedSizeArrayTracker(unsigned int size, bool logging_enabled = false);
void log(const std::string &message) const;
std::optional<unsigned int> find_contiguous_space(unsigned int length);
void add_metadata(int id, unsigned int start, unsigned int length);
void remove_metadata(int id);
std::optional<std::pair<unsigned int, unsigned int>> get_metadata(int id) const;
void compact();
const std::unordered_map<int, std::pair<unsigned int, unsigned int>> &get_all_metadata() const;
friend std::ostream &operator<<(std::ostream &os, const FixedSizeArrayTracker &tracker);
private:
unsigned int size;
bool logging_enabled;
std::unordered_map<int, std::pair<unsigned int, unsigned int>> metadata; // id -> {start, length}
std::set<std::pair<unsigned int, unsigned int>> occupied_intervals; // sorted intervals of occupied spaces
};
#endif // FIXED_SIZE_ARRAY_TRACKER_HPP