-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket_array.hpp
More file actions
93 lines (76 loc) · 2.31 KB
/
bucket_array.hpp
File metadata and controls
93 lines (76 loc) · 2.31 KB
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
#ifndef BUCKET_ARRAY_HPP
#define BUCKET_ARRAY_HPP
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <forward_list>
#include <cassert>
template <typename T_, uint32_t BucketSize_>
class BucketArray
{
public:
using Bucket = std::vector<T_>;
using BucketList = std::forward_list<Bucket>;
using Type = T_;
static constexpr bucket_size() { return BucketSize_; }
public:
BucketArray()
: list_(1), size_(), bucketCount_(1)
{}
const T_& operator [](uint32_t i) const;
T_& operator [](uint32_t i);
uint32_t size() const { return size_; }
uint32_t storage_size() const { return bucketCount_ * BucketSize_ * sizeof(T_); }
uint32_t bucket_count() const { return bucketCount_; }
void push_back(const T_& t);
void push_back(T_&& t);
private:
BucketList list_;
uint32_t size_;
uint32_t bucketCount_;
};
template <typename T_, uint32_t BucketSize_>
const T_& BucketArray<T_, BucketSize_>::operator[](uint32_t index) const
{
assert(index <= size_);
uint32_t fromFront = bucketCount_ - index/BucketSize_;
auto iter = list_.begin();
while (fromFront--) ++iter;
return iter[index];
}
template <typename T_, uint32_t BucketSize_>
T_& BucketArray<T_, BucketSize_>::operator[](uint32_t index)
{
assert(index <= size_);
uint32_t fromFront = bucketCount_ - index/BucketSize_;
auto iter = list_.begin();
while (fromFront--) ++iter;
return iter[index];
}
template <typename T_, uint32_t BucketSize_>
void BucketArray<T_, BucketSize_>::push_back(const T_& t)
{
Bucket* currentBucket = &list_.front();
if (currentBucket->size() == BucketSize_) {
list_.emplace_front();
currentBucket = &list_.front();
currentBucket->reserve(BucketSize_);
bucketCount_++;
}
currentBucket->push_back(t);
size_++;
}
template <typename T_, uint32_t BucketSize_>
void BucketArray<T_, BucketSize_>::push_back(T_&& t)
{
Bucket* currentBucket = &list_.front();
if (currentBucket->size() == BucketSize_) {
list_.emplace_front();
currentBucket = &list_.front();
currentBucket->reserve(BucketSize_);
bucketCount_++;
}
currentBucket->push_back(t);
size_++;
}
#endif // BUCKET_ARRAY_HPP