-
Notifications
You must be signed in to change notification settings - Fork 2
/
interleaved.h
89 lines (73 loc) · 1.66 KB
/
interleaved.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
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
#ifndef INTERLEAVED_H
#define INTERLEAVED_H
//
// Quasi-STL-compliant interleaved array (random access container)
//
// Leslie Wu
//
// Accesses element of type T from aggregate structure
// STRUCT, where OFFSET is offsetof(STRUCT, <T's name>)
template <typename T, typename STRUCT>
T& TfromSTRUCT(STRUCT& elem, size_t OFFSET)
{
void* data = (void*)((size_t)&elem + OFFSET);
T* pt = (T*)data;
return (T&)(*pt);
}
template <typename T, typename S, size_t OFFSET>
class InterleavedArray
{
public:
explicit InterleavedArray(S* an_array, S* the_end)
{
array_begin = an_array;
array_end = the_end;
}
T& operator[](int index) {
return TfromSTRUCT<T,S>( array_begin[index], OFFSET );
}
// STL container traits
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef size_t difference_type;
typedef size_t size_type_type;
class iterator {
public:
explicit iterator(S* s) : ptr(s) {}
bool operator==(const iterator& rhs) {
return (rhs.ptr == ptr);
}
bool operator!=(const iterator& rhs) {
return !(*this == rhs);
}
iterator& operator++() {
++ptr;
return (*this);
}
iterator& operator--() {
--ptr;
return (*this);
}
reference operator*() const {
return TfromSTRUCT<T,S>(*ptr, OFFSET);
}
private:
S* ptr;
};
// STL container functions
iterator begin() {
return iterator(array_begin);
}
iterator end() {
return iterator(array_end);
}
size_t size() {
return std::distance(array_begin, array_end);
}
private:
S* array_begin;
S* array_end;
};
#endif // INTERLEAVED_H