Peano
Loading...
Searching...
No Matches
vec.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <x86intrin.h>
5
6template<typename Tp, typename Alloc = std::allocator<Tp>>
7class Vector : public std::vector<Tp, Alloc> {
8
9public:
10 Vector() noexcept = default;
11
12 Vector(const std::initializer_list<Tp> &list) {
13 this->reserve(list.size());
14 for (auto &element : list) this->push_back(element);
15 }
16
17 Tp unstableTake(std::size_t idx) {
18 auto &idxItem = this->operator[](idx);
19 auto &lastItem = this->operator[](this->size() - 1);
20 std::swap(idxItem, lastItem);
21
22 auto item = this->back();
23 this->pop_back();
24 return item;
25 }
26};
27
28template<typename ITEM, typename ALLOCATOR>
29class Vec {
30
31 ITEM* dataPtr = nullptr;
32
33 ALLOCATOR allocator;
34
35 unsigned int capacity = 0;
36 unsigned int freeIdx = 0;
37
38public:
39 using value_type = ITEM;
40
41 explicit Vec(unsigned int capacity = 0, ALLOCATOR allocator = ALLOCATOR()) : allocator(allocator), capacity(capacity) {
42 if (capacity == 0) return;
43 this->dataPtr = this->allocator.template alloc<ITEM>(capacity);
44 }
45
46 void reserve(unsigned int newCapacity) {
47 assert(this->capacity == 0)
48 this->capacity = newCapacity;
49 this->dataPtr = this->allocator.template alloc<ITEM>(newCapacity);
50 }
51
52 void prefetch() const {
53 char *data = (char*) this->dataPtr;
54 auto laneCount = (sizeof(ITEM) * this->size() + 63) / 64;
55 for (int i = 0; i < laneCount; i++) _mm_prefetch(data + i * 64, _MM_HINT_NTA);
56 }
57
58 void push_back(ITEM &item) {
59 if (this->freeIdx == this->capacity) {
60 auto newCapacity = std::max(this->capacity * 2, 4U); // insures against capacity 0
61 auto newData = this->allocator.template alloc<ITEM>(newCapacity);
62 memcpy(newData, this->dataPtr, sizeof(ITEM) * this->capacity);
63 this->allocator.del(this->dataPtr);
64 this->capacity = newCapacity;
65 this->dataPtr = newData;
66 }
67
68 this->dataPtr[this->freeIdx] = item;
69 this->freeIdx++;
70 }
71
72 ITEM& operator [](int idx) const {
73 return this->dataPtr[idx];
74 }
75
76 void clear() {
77 this->freeIdx = 0;
78 }
79
80 unsigned int size() const {
81 return this->freeIdx;
82 }
83
84 ITEM* data() const {
85 return this->dataPtr;
86 }
87
88 Vec(const Vec&) = delete;
89
90 Vec& operator=(Vec&& other) noexcept {
91 this->allocator.del(this->dataPtr);
92
93 this->dataPtr = other.dataPtr;
94 this->capacity = other.capacity;
95 this->freeIdx = other.freeIdx;
96
97 other.dataPtr = nullptr;
98 other.freeIdx = 0;
99 other.capacity = 0;
100 return *this;
101 }
102
104 this->allocator.del(this->dataPtr);
105 }
106
107 ITEM* begin() {
108 return this->dataPtr;
109 }
110
111 ITEM* end() {
112 return this->dataPtr + this->freeIdx;
113 }
114
115 const ITEM* begin() const {
116 return this->dataPtr;
117 }
118
119 const ITEM* end() const {
120 return this->dataPtr + this->freeIdx;
121 }
122};
#define assert(...)
Definition LinuxAMD.h:28
Definition vec.h:29
Vec(const Vec &)=delete
void reserve(unsigned int newCapacity)
Definition vec.h:46
void prefetch() const
Definition vec.h:52
ITEM * data() const
Definition vec.h:84
ALLOCATOR allocator
Definition vec.h:33
ITEM * end()
Definition vec.h:111
~Vec()
Definition vec.h:103
unsigned int size() const
Definition vec.h:80
ITEM * begin()
Definition vec.h:107
void push_back(ITEM &item)
Definition vec.h:58
const ITEM * end() const
Definition vec.h:119
Vec(unsigned int capacity=0, ALLOCATOR allocator=ALLOCATOR())
Definition vec.h:41
ITEM & operator[](int idx) const
Definition vec.h:72
void clear()
Definition vec.h:76
ITEM value_type
Definition vec.h:39
unsigned int capacity
Definition vec.h:35
ITEM * dataPtr
Definition vec.h:31
unsigned int freeIdx
Definition vec.h:36
Vec & operator=(Vec &&other) noexcept
Definition vec.h:90
const ITEM * begin() const
Definition vec.h:115
Definition vec.h:7
Vector() noexcept=default
Tp unstableTake(std::size_t idx)
Definition vec.h:17
STL namespace.