AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
array_pool.h
1#ifndef HEURISTICS_ARRAY_POOL_H
2#define HEURISTICS_ARRAY_POOL_H
3
4#include <cassert>
5#include <vector>
6
7/*
8 ArrayPool is intended as a compact representation of a large collection of
9 arrays that are allocated individually but deallocated together.
10
11 Each array may have a different size, but ArrayPool does not keep track of
12 the array sizes; its user must maintain this information themselves. See the
13 relaxation heuristics for usage examples.
14
15 If the class turns out to be more generally useful, it could be templatized
16 (currently, ValueType = int is hardcoded) and moved to the algorithms
17 directory.
18*/
19namespace array_pool {
20const int INVALID_INDEX = -1;
21
22using Value = int;
23
24class ArrayPoolIndex {
25 friend class ArrayPool;
26 int position;
27 ArrayPoolIndex(int position)
28 : position(position) {
29 }
30public:
31 ArrayPoolIndex()
32 : position(INVALID_INDEX) {
33 }
34};
35
36class ArrayPoolSlice {
37public:
38 using Iterator = std::vector<Value>::const_iterator;
39 Iterator begin() {
40 return first;
41 }
42
43 Iterator end() {
44 return last;
45 }
46private:
47 friend class ArrayPool;
48
49 Iterator first;
50 Iterator last;
51
52 ArrayPoolSlice(Iterator first, Iterator last)
53 : first(first),
54 last(last) {
55 }
56};
57
58class ArrayPool {
59 std::vector<Value> data;
60public:
61 ArrayPoolIndex append(const std::vector<Value> &vec) {
62 ArrayPoolIndex index(data.size());
63 data.insert(data.end(), vec.begin(), vec.end());
64 return index;
65 }
66
67 ArrayPoolSlice get_slice(ArrayPoolIndex index, int size) const {
68 assert(index.position >= 0 &&
69 size >= 0 &&
70 index.position + size <= static_cast<int>(data.size()));
71 return ArrayPoolSlice(data.begin() + index.position, data.begin() + index.position + size);
72 }
73};
74}
75
76#endif