forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainers.h
More file actions
178 lines (143 loc) · 4.24 KB
/
Containers.h
File metadata and controls
178 lines (143 loc) · 4.24 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
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
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef REACT_COMMON_CONTAINERS_H_INCLUDED
#define REACT_COMMON_CONTAINERS_H_INCLUDED
#pragma once
#include "react/detail/Defs.h"
#include <algorithm>
#include <array>
#include <type_traits>
#include <vector>
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EnumFlags
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
class EnumFlags
{
public:
using FlagsT = typename std::underlying_type<T>::type;
template <T x>
void Set() { flags_ |= 1 << x; }
template <T x>
void Clear() { flags_ &= ~(1 << x); }
template <T x>
bool Test() const { return (flags_ & (1 << x)) != 0; }
private:
FlagsT flags_ = 0;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// NodeVector
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNode>
class NodeVector
{
private:
typedef std::vector<TNode*> DataT;
public:
void Add(TNode& node)
{
data_.push_back(&node);
}
void Remove(const TNode& node)
{
data_.erase(std::find(data_.begin(), data_.end(), &node));
}
typedef typename DataT::iterator iterator;
typedef typename DataT::const_iterator const_iterator;
iterator begin() { return data_.begin(); }
iterator end() { return data_.end(); }
const_iterator begin() const { return data_.begin(); }
const_iterator end() const { return data_.end(); }
private:
DataT data_;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// NodeBuffer
///////////////////////////////////////////////////////////////////////////////////////////////////
struct SplitTag {};
template <typename T, size_t N>
class NodeBuffer
{
public:
using DataT = std::array<T*,N>;
using iterator = typename DataT::iterator;
using const_iterator = typename DataT::const_iterator;
static const size_t split_size = N / 2;
NodeBuffer() :
size_( 0 ),
front_( nodes_.begin() ),
back_( nodes_.begin() )
{}
NodeBuffer(T* node) :
size_( 1 ),
front_( nodes_.begin() ),
back_( nodes_.begin() + 1 )
{
nodes_[0] = node;
}
template <typename TInput>
NodeBuffer(TInput srcBegin, TInput srcEnd) :
size_( std::distance(srcBegin, srcEnd) ), // parentheses to allow narrowing conversion
front_( nodes_.begin() ),
back_( size_ != N ? nodes_.begin() + size_ : nodes_.begin() )
{
std::copy(srcBegin, srcEnd, front_);
}
// Other must be full
NodeBuffer(NodeBuffer& other, SplitTag) :
size_( split_size ),
front_( nodes_.begin() ),
back_( nodes_.begin() )
{
for (auto i=0; i<split_size; i++)
*(back_++) = other.PopFront();
}
void PushFront(T* e)
{
size_++;
decrement(front_);
*front_ = e;
}
void PushBack(T* e)
{
size_++;
*back_ = e;
increment(back_);
}
T* PopFront()
{
size_--;
auto t = *front_;
increment(front_);
return t;
}
T* PopBack()
{
size_--;
decrement(back_);
return *back_;
}
bool IsFull() const { return size_ == N; }
bool IsEmpty() const { return size_ == 0; }
private:
inline void increment(iterator& it)
{
if (++it == nodes_.end())
it = nodes_.begin();
}
inline void decrement(iterator& it)
{
if (it == nodes_.begin())
it = nodes_.end();
--it;
}
DataT nodes_;
size_t size_;
iterator front_;
iterator back_;
};
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_COMMON_CONTAINERS_H_INCLUDED