-
Notifications
You must be signed in to change notification settings - Fork 11
/
fast_iterable.h
61 lines (47 loc) · 1.83 KB
/
fast_iterable.h
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
//---------------------------------------------------------------------------
#pragma once
#include "interval.h"
#include "iterable.h"
#include <stdlib.h>
//---------------------------------------------------------------------------
// FastIterable stores a start and end pointer, so that the calculation
// end = start + count
// is not required at the start of each loop.
template <typename T> class FastIterable : protected Interval<T *> {
private:
using super = Interval<T *>;
public:
FastIterable() = default;
FastIterable(T *p, T *pEnd) : super(p, pEnd) {}
template <size_t N> FastIterable(T (&t)[N]) : super(t, (T *)t + N) {}
template <typename S> FastIterable(S &s) : super(begin(s), end(s)) {}
bool IsEmpty() const { return super::min == super::max; }
bool IsNotEmpty() const { return super::min != super::max; }
size_t GetCount() const { return super::max - super::min; }
T &Front() { return super::min[0]; }
T &Back() { return super::max[-1]; }
friend T *begin(const FastIterable &it) { return it.min; }
friend T *end(const FastIterable &it) { return it.max; }
ReverseIterableData<T> Reverse() {
return ReverseIterableData<T>{.count = GetCount(), .data = super::min};
}
ReverseIterableData<const T> Reverse() const {
return ReverseIterableData<const T>{
.count = GetCount(),
.data = super::min,
};
}
};
//---------------------------------------------------------------------------
template <typename T, size_t CAPACITY>
class FastIterableStaticList : public FastIterable<T> {
private:
using super = FastIterable<T>;
public:
FastIterableStaticList() : super(data, data) {}
void Add(T t) { *super::max++ = t; }
bool IsFull() const { return super::max == data + CAPACITY; }
private:
T data[CAPACITY];
};
//---------------------------------------------------------------------------