File: vector.hpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (282 lines) | stat: -rw-r--r-- 7,981 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma once

#include <algorithm>
#include <initializer_list>
#include <new>
#include <utility>
#include <nall/algorithm.hpp>
#include <nall/bit.hpp>
#include <nall/maybe.hpp>
#include <nall/memory.hpp>
#include <nall/sort.hpp>
#include <nall/utility.hpp>

namespace nall {

template<typename T> struct vector {
  struct exception_out_of_bounds{};

  explicit operator bool() const { return objectsize; }
  auto data() -> T* { return pool + poolbase; }
  auto data() const -> const T* { return pool + poolbase; }

  auto empty() const -> bool { return objectsize == 0; }
  auto size() const -> unsigned { return objectsize; }
  auto capacity() const -> unsigned { return poolsize; }

  auto release() -> T* {
    T* result = pool + poolbase;
    pool = nullptr;
    poolbase = 0;
    poolsize = 0;
    objectsize = 0;
    return result;
  }

  auto reset() -> void {
    if(pool) {
      for(unsigned n = 0; n < objectsize; n++) pool[poolbase + n].~T();
      memory::free(pool);
    }
    pool = nullptr;
    poolbase = 0;
    poolsize = 0;
    objectsize = 0;
  }

  auto reserve(unsigned size) -> void {
    if(size <= poolsize) return;
    size = bit::round(size);  //amortize growth

    T* copy = (T*)memory::allocate(size * sizeof(T));
    for(unsigned n = 0; n < objectsize; n++) new(copy + n) T(move(pool[poolbase + n]));
    free(pool);
    pool = copy;
    poolbase = 0;
    poolsize = size;
  }

  auto resize(unsigned size, T value = T()) -> void {
    T* copy = (T*)memory::allocate(size * sizeof(T));
    for(unsigned n = 0; n < size && n < objectsize; n++) new(copy + n) T(move(pool[poolbase + n]));
    for(unsigned n = objectsize; n < size; n++) new(copy + n) T(value);
    reset();
    pool = copy;
    poolbase = 0;
    poolsize = size;
    objectsize = size;
  }

  auto reallocate(unsigned size, T value = T()) -> void {
    reset();
    resize(size, value);
  }

  template<typename... Args> auto prepend(const T& data, Args&&... args) -> void {
    prepend(forward<Args>(args)...);
    prepend(data);
  }

  auto prepend(const T& data) -> T& {
    reserve(objectsize + 1);
    if(poolbase == 0) {
      unsigned available = poolsize - objectsize;
      poolbase = max(1u, available >> 1);
      for(signed n = objectsize - 1; n >= 0; n--) {
        pool[poolbase + n] = move(pool[n]);
      }
    }
    new(pool + --poolbase) T(data);
    objectsize++;
    return first();
  }

  template<typename... Args> auto append(const T& data, Args&&... args) -> void {
    append(data);
    append(forward<Args>(args)...);
  }

  auto append(const T& data) -> T& {
    reserve(poolbase + objectsize + 1);
    new(pool + poolbase + objectsize++) T(data);
    return last();
  }

  auto appendOnce(const T& data) -> bool {
    if(find(data)) return false;
    return append(data), true;
  }

  auto insert(unsigned position, const T& data) -> void {
    if(position == 0) {
      prepend(data);
      return;
    }
    append(data);
    if(position == ~0u) return;
    for(signed n = objectsize - 1; n > position; n--) {
      pool[poolbase + n] = move(pool[poolbase + n - 1]);
    }
    pool[poolbase + position] = data;
  }

  auto remove(unsigned position = ~0u, unsigned length = 1) -> void {
    if(position == ~0u) position = objectsize - 1;
    if(position + length > objectsize) throw exception_out_of_bounds{};

    if(position == 0) {
      for(unsigned n = 0; n < length; n++) pool[poolbase + n].~T();
      poolbase += length;
    } else {
      for(unsigned n = position; n < objectsize; n++) {
        if(n + length < objectsize) {
          pool[poolbase + n] = move(pool[poolbase + n + length]);
        } else {
          pool[poolbase + n].~T();
        }
      }
    }
    objectsize -= length;
  }

  auto removeFirst() -> void { return remove(0); }
  auto removeLast() -> void { return remove(~0u); }

  auto take(unsigned position = ~0u) -> T {
    if(position == ~0u) position = objectsize - 1;
    T object = pool[poolbase + position];
    remove(position);
    return object;
  }

  auto takeFirst() -> T { return take(0); }
  auto takeLast() -> T { return take(~0u); }

  auto reverse() -> void {
    unsigned pivot = size() / 2;
    for(unsigned l = 0, r = size() - 1; l < pivot; l++, r--) {
      swap(pool[poolbase + l], pool[poolbase + r]);
    }
  }

  auto sort(const function<bool (const T& lhs, const T& rhs)>& comparator = [](const T& lhs, const T& rhs) -> bool {
    return lhs < rhs;
  }) -> void {
    nall::sort(pool + poolbase, objectsize, comparator);
  }

  auto find(const T& data) const -> maybe<unsigned> {
    for(unsigned n = 0; n < objectsize; n++) if(pool[poolbase + n] == data) return n;
    return nothing;
  }

  auto first() -> T& {
    if(objectsize == 0) throw exception_out_of_bounds();
    return pool[poolbase];
  }

  auto first() const -> const T& {
    if(objectsize == 0) throw exception_out_of_bounds();
    return pool[poolbase];
  }

  auto last() -> T& {
    if(objectsize == 0) throw exception_out_of_bounds();
    return pool[poolbase + objectsize - 1];
  }

  auto last() const -> const T& {
    if(objectsize == 0) throw exception_out_of_bounds();
    return pool[poolbase + objectsize - 1];
  }

  //access
  inline auto operator[](unsigned position) -> T& {
    if(position >= objectsize) throw exception_out_of_bounds();
    return pool[poolbase + position];
  }

  inline auto operator[](unsigned position) const -> const T& {
    if(position >= objectsize) throw exception_out_of_bounds();
    return pool[poolbase + position];
  }

  inline auto operator()(unsigned position) -> T& {
    if(position >= poolsize) reserve(position + 1);
    while(position >= objectsize) append(T());
    return pool[poolbase + position];
  }

  inline auto operator()(unsigned position, const T& data) const -> const T& {
    if(position >= objectsize) return data;
    return pool[poolbase + position];
  }

  //iteration
  struct iterator {
    iterator(vector& source, unsigned position) : source(source), position(position) {}
    auto operator*() -> T& { return source.operator[](position); }
    auto operator!=(const iterator& source) const -> bool { return position != source.position; }
    auto operator++() -> iterator& { position++; return *this; }

  private:
    vector& source;
    unsigned position;
  };

  auto begin() -> iterator { return iterator(*this, 0); }
  auto end() -> iterator { return iterator(*this, size()); }

  struct constIterator {
    constIterator(const vector& source, unsigned position) : source(source), position(position) {}
    auto operator*() const -> const T& { return source.operator[](position); }
    auto operator!=(const constIterator& source) const -> bool { return position != source.position; }
    auto operator++() -> constIterator& { position++; return *this; }

  private:
    const vector& source;
    unsigned position;
  };

  auto begin() const -> const constIterator { return constIterator(*this, 0); }
  auto end() const -> const constIterator { return constIterator(*this, size()); }

  //copy
  inline auto operator=(const vector& source) -> vector& {
    if(this == &source) return *this;
    reset();
    reserve(source.size());
    for(auto& data : source) append(data);
    return *this;
  }

  //move
  inline auto operator=(vector&& source) -> vector& {
    if(this == &source) return *this;
    reset();
    pool = source.pool;
    poolbase = source.poolbase;
    poolsize = source.poolsize;
    objectsize = source.objectsize;
    source.pool = nullptr;
    source.poolbase = 0;
    source.poolsize = 0;
    source.objectsize = 0;
    return *this;
  }

  //construction and destruction
  vector() = default;
  vector(initializer_list<T> list) { for(auto& data : list) append(data); }
  vector(const vector& source) { operator=(source); }
  vector(vector&& source) { operator=(move(source)); }
  ~vector() { reset(); }

protected:
  T* pool = nullptr;
  unsigned poolbase = 0;
  unsigned poolsize = 0;
  unsigned objectsize = 0;
};

}