22#include "scid/database/common.h"
23#include "scid/database/game_id.h"
39 std::unique_ptr<scid::core::byte[]> data_;
45 explicit Filter(gamenumT size)
46 : size_(size), nonzero_(size), capacity_(0) {}
48 void Init(gamenumT size) {
50 nonzero_ = size_ = size;
54 scid::core::byte*
data() {
return data_.get(); }
57 gamenumT
Count()
const {
return nonzero_; }
60 gamenumT
Size()
const {
return size_; }
66 }
else if (size < size_) {
67 nonzero_ = size -
static_cast<gamenumT
>(std::count(
68 data_.get(), data_.get() + size, 0));
69 }
else if (size > size_) {
70 if (size > capacity_) {
71 auto tmp(std::move(data_));
73 std::copy_n(tmp.get(), size_, data_.get());
75 scid::core::byte val = 0;
80 std::fill(data_.get() + size_, data_.get() + size, val);
86 scid::core::byte
Get(gamenumT index)
const {
87 ASSERT(index <
Size());
88 return data_ ? data_[index] : 1;
92 void Set(gamenumT index, scid::core::byte value) {
93 ASSERT(index <
Size());
96 if (data_[index] != 0)
98 }
else if (data_[index] == 0) {
101 data_[index] = value;
102 }
else if (value != 1) {
104 std::fill(data_.get(), data_.get() + size_, 1);
105 data_[index] = value;
112 void Fill(scid::core::byte value) {
120 std::fill(data_.get(), data_.get() + size_, value);
121 nonzero_ = (value == 0) ? 0 : size_;
126 void allocate(
size_t size) {
127 auto capacity = (size | 63) + 1;
128 data_ = std::make_unique<scid::core::byte[]>(capacity);
129 capacity_ = capacity;
176 typedef std::forward_iterator_tag iterator_category;
177 typedef std::ptrdiff_t difference_type;
178 typedef gamenumT value_type;
179 typedef const gamenumT* pointer;
180 typedef const gamenumT& reference;
183 bool inFilter =
true)
184 : gnum_(gnum), end_(end), hfilter_(hfilter), inFilter_(inFilter) {
185 ASSERT(hfilter != 0);
187 bool included = (hfilter_->get(gnum_) != 0);
188 if (included != inFilter_)
193 reference operator*()
const {
return gnum_; }
196 while (++gnum_ != end_) {
197 bool included = (hfilter_->get(gnum_) != 0);
198 if (included == inFilter_)
205 return gnum_ != b.gnum_ || hfilter_ != b.hfilter_;
208 return !operator!=(b);
215 const_iterator end()
const {
216 return const_iterator(main_->
Size(), main_->
Size(),
this);
218 const_iterator beginInverted()
const {
219 return const_iterator(0, main_->
Size(),
this,
false);
221 const_iterator endInverted()
const {
222 return const_iterator(main_->
Size(), main_->
Size(),
this,
false);
224 size_t sizeInverted()
const {
return main_->
Size() - size(); }
227 bool operator==(
const Filter* b)
const {
return main_ == b; }
228 bool operator!=(
const Filter* b)
const {
return main_ != b; }
229 HFilter* operator->() {
return this; }
230 const HFilter* operator->()
const {
return this; }
231 HFilter& operator*() {
return *
this; }
232 const HFilter& operator*()
const {
return *
this; }
235 explicit HFilter(Filter* main,
const Filter* mask = 0)
236 : main_(main), mask_(mask) {}
238 Filter* mainFilter()
const {
return main_; }
239 const Filter* maskFilter()
const {
return mask_; }
241 void clear() {
return main_->
Fill(0); }
242 void erase(gamenumT gnum) {
return main_->
Set(gnum, 0); }
243 void insert_or_assign(gamenumT gnum, uint8_t ply) {
244 return main_->
Set(gnum, ply + 1);
246 gamenumT size()
const {
247 if (!mask_ || mask_->
Count() == mask_->
Size())
248 return main_->
Count();
250 return mask_->
Count();
251 return static_cast<gamenumT
>(std::distance(begin(), end()));
261 void includeAll() {
return main_->
Fill(1); }
268 scid::core::byte get(gamenumT gnum)
const {
269 scid::core::byte res = main_->
Get(gnum);
270 if (res != 0 && mask_ != 0)
271 res = mask_->
Get(gnum);
282 void set(gamenumT gnum, scid::core::byte value) {
return main_->
Set(gnum, value); }
302 ASSERT(hfilter != 0);
306 size_t size()
const {
return hfilter_.sizeInverted(); }
void Fill(scid::core::byte value)
Sets all values.
Definition hfilter.h:112
scid::core::byte Get(gamenumT index) const
Gets the value at index.
Definition hfilter.h:86
gamenumT Size() const
Return the number of elements in filter.
Definition hfilter.h:60
gamenumT Count() const
Return the number of nonzero values in filter.
Definition hfilter.h:57
void Resize(gamenumT size)
Changes the number of elements stored.
Definition hfilter.h:63
scid::core::byte * data()
Return a pointer to the allocated data.
Definition hfilter.h:54
void Set(gamenumT index, scid::core::byte value)
Sets the value at index.
Definition hfilter.h:92
class HFilterInverted - iterate through games excluded from a filter
Definition hfilter.h:297
class const_iterator - iterator for HFilter objects
Definition hfilter.h:169
size_t mainSize() const
Returns the number of games included in the main filter.
Definition hfilter.h:255
class StrRange - parse a string interpreting its content as 1 or 2 integers separated by whitespace.
Definition common.h:30