File: vector.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (156 lines) | stat: -rw-r--r-- 5,135 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
#pragma once

#include <new>

#include <nall/array-span.hpp>
#include <nall/array-view.hpp>
#include <nall/bit.hpp>
#include <nall/function.hpp>
#include <nall/iterator.hpp>
#include <nall/maybe.hpp>
#include <nall/memory.hpp>
#include <nall/merge-sort.hpp>
#include <nall/range.hpp>
#include <nall/traits.hpp>
#include <nall/view.hpp>

namespace nall {

template<typename T>
struct vector_base {
  using type = vector_base;

  //core.hpp
  vector_base() = default;
  vector_base(const initializer_list<T>& values);
  vector_base(const type& source);
  vector_base(type&& source);
  ~vector_base();

  explicit operator bool() const;
  operator array_span<T>();
  operator array_view<T>() const;
  template<typename Cast = T> auto capacity() const -> u64;
  template<typename Cast = T> auto size() const -> u64;
  template<typename Cast = T> auto data() -> Cast*;
  template<typename Cast = T> auto data() const -> const Cast*;

  //assign.hpp
  auto operator=(const type& source) -> type&;
  auto operator=(type&& source) -> type&;

  //compare.hpp
  auto operator==(const type& source) const -> bool;
  auto operator!=(const type& source) const -> bool;

  //memory.hpp
  auto reset() -> void;
  auto acquire(T* data, u64 size, u64 capacity = 0) -> void;
  auto release() -> T*;

  auto reserveLeft(u64 capacity) -> bool;
  auto reserveRight(u64 capacity) -> bool;
  auto reserve(u64 capacity) -> bool { return reserveRight(capacity); }

  auto reallocateLeft(u64 size) -> bool;
  auto reallocateRight(u64 size) -> bool;
  auto reallocate(u64 size) -> bool { return reallocateRight(size); }

  auto resizeLeft(u64 size, const T& value = T()) -> bool;
  auto resizeRight(u64 size, const T& value = T()) -> bool;
  auto resize(u64 size, const T& value = T()) -> bool { return resizeRight(size, value); }

  //access.hpp
  auto operator[](u64 offset) -> T&;
  auto operator[](u64 offset) const -> const T&;

  auto operator()(u64 offset) -> T&;
  auto operator()(u64 offset, const T& value) const -> const T&;

  auto left() -> T&;
  auto first() -> T& { return left(); }
  auto left() const -> const T&;
  auto first() const -> const T& { return left(); }

  auto right() -> T&;
  auto last() -> T& { return right(); }
  auto right() const -> const T&;
  auto last() const -> const T& { return right(); }

  //modify.hpp
  auto prepend(const T& value) -> void;
  auto prepend(T&& value) -> void;
  auto prepend(const type& values) -> void;
  auto prepend(type&& values) -> void;

  auto append(const T& value) -> void;
  auto append(T&& value) -> void;
  auto append(const type& values) -> void;
  auto append(type&& values) -> void;

  auto insert(u64 offset, const T& value) -> void;

  auto removeLeft(u64 length = 1) -> void;
  auto removeFirst(u64 length = 1) -> void { return removeLeft(length); }
  auto removeRight(u64 length = 1) -> void;
  auto removeLast(u64 length = 1) -> void { return removeRight(length); }
  auto remove(u64 offset, u64 length = 1) -> void;
  auto removeByIndex(u64 offset) -> bool;
  auto removeByValue(const T& value) -> bool;

  auto takeLeft() -> T;
  auto takeFirst() -> T { return move(takeLeft()); }
  auto takeRight() -> T;
  auto takeLast() -> T { return move(takeRight()); }
  auto take(u64 offset) -> T;

  //iterator.hpp
  auto begin() -> iterator<T> { return {data(), 0}; }
  auto end() -> iterator<T> { return {data(), size()}; }

  auto begin() const -> iterator_const<T> { return {data(), 0}; }
  auto end() const -> iterator_const<T> { return {data(), size()}; }

  auto rbegin() -> reverse_iterator<T> { return {data(), size() - 1}; }
  auto rend() -> reverse_iterator<T> { return {data(), (u64)-1}; }

  auto rbegin() const -> reverse_iterator_const<T> { return {data(), size() - 1}; }
  auto rend() const -> reverse_iterator_const<T> { return {data(), (u64)-1}; }

  //utility.hpp
  auto fill(const T& value = {}) -> void;
  auto sort(const function<bool (const T& lhs, const T& rhs)>& comparator = [](auto& lhs, auto& rhs) { return lhs < rhs; }) -> void;
  auto reverse() -> void;
  auto find(const function<bool (const T& lhs)>& comparator) -> maybe<u64>;
  auto find(const T& value) const -> maybe<u64>;
  auto findSorted(const T& value) const -> maybe<u64>;
  auto foreach(const function<void (const T&)>& callback) -> void;
  auto foreach(const function<void (u64, const T&)>& callback) -> void;

protected:
  T* _pool = nullptr;   //pointer to first initialized element in pool
  u64 _size = 0;   //number of initialized elements in pool
  u64 _left = 0;   //number of allocated elements free on the left of pool
  u64 _right = 0;  //number of allocated elements free on the right of pool
};

}

#define vector vector_base
#include <nall/vector/core.hpp>
#include <nall/vector/assign.hpp>
#include <nall/vector/compare.hpp>
#include <nall/vector/memory.hpp>
#include <nall/vector/access.hpp>
#include <nall/vector/modify.hpp>
#include <nall/vector/iterator.hpp>
#include <nall/vector/utility.hpp>
#undef vector

namespace nall {
  template<typename T> struct vector : vector_base<T> {
    using vector_base<T>::vector_base;
  };
}

#include <nall/vector/specialization/u8.hpp>