Menu

[f613ec]: / src / filter.h  Maximize  Restore  History

Download this file

177 lines (145 with data), 4.8 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
//////////////////////////////////////////////////////////////////////
//
// FILE: filter.h
// Filter and CompressedFilter Classes
//
// Part of: Scid (Shane's Chess Information Database)
// Version: 1.4
//
// Notice: Copyright (c) 2000 Shane Hudson. All rights reserved.
//
// Author: Shane Hudson (sgh@users.sourceforge.net)
//
//////////////////////////////////////////////////////////////////////
#ifndef SCID_FILTER_H
#define SCID_FILTER_H
#include "common.h"
#include "misc.h"
//////////////////////////////////////////////////////////////////////
//
// Filter class:
//
// Holds the results of a database search: one byte per game,
// indicating whether that game is included in the filter or not.
// A value of 0 indicates the game is excluded, or 1-255 indicates
// the game is included, and what position to show when the game
// is loaded: 1 means the start position, 2 means the position after
// Whites first move, etc.
class Filter
{
private:
gamenumT FilterSize; // Number of values in filter.
gamenumT FilterCount; // Number of nonzero values in filter.
gamenumT Capacity; // Number of values allocated for Data[].
byte * Data; // The actual filter data.
Filter(const Filter&);
Filter& operator=(const Filter&);
void Allocate();
void Free();
friend class CompressedFilter;
public:
Filter (gamenumT size) :Data(NULL) { Init (size); }
~Filter() { Free(); }
void Init (gamenumT size);
uint Size() const { return FilterSize; }
uint Count () const { return FilterCount; }
bool isWhole () const { return FilterCount == FilterSize; }
void Set (gamenumT index, byte value); // Sets the value at index.
byte Get (gamenumT index) const; // Gets the value at index.
void Fill (byte value); // Sets all values.
void Append (byte value); // Appends one value.
void SetCapacity (gamenumT size);
};
class HFilter {
Filter* f_;
const Filter* mask_;
public:
explicit HFilter(Filter* f = NULL, const Filter* mask = NULL) : f_(f), mask_(mask) {}
bool operator! () const { return f_ == NULL; }
bool operator* () const { return f_ != NULL; }
bool isWhole() const { return f_->isWhole(); }
uint count() const {
uint res = f_->Count();
if (mask_ == NULL) return res;
if (res == f_->Size()) return mask_->Count();
for (uint i=0, n = f_->Size(); i < n; i++) {
if (f_->Get(i) != 0 && mask_->Get(i) == 0) --res;
}
return res;
}
byte get(gamenumT index) const {
byte res = f_->Get(index);
if (res != 0 && mask_ != NULL) return mask_->Get(index);
return res;
}
void set (gamenumT index, byte value) { return f_->Set(index, value); }
void fill (byte value) { return f_->Fill(value); }
};
inline void
Filter::Set (uint index, byte value)
{
ASSERT (index < FilterSize);
if (Data == NULL){
if (value == 1)
return;
Allocate();
}
if (Data[index] != 0) FilterCount--;
if (value != 0) FilterCount++;
Data[index] = value;
}
inline byte
Filter::Get (uint index) const
{
ASSERT (index < FilterSize);
byte res = (Data == NULL) ? 1 : Data[index];
return res;
}
//////////////////////////////////////////////////////////////////////
//
// CompressedFilter class:
// Holds the same data as a filter, in compressed format.
// Random access to individual values is not possible.
// A CompressedFilter is created from, or restored to, a regular
// filter with the methods CompressFrom() and UncompressTo().
class CompressedFilter
{
private:
uint CFilterSize;
uint CFilterCount;
uint CompressedLength;
byte * CompressedData;
public:
CompressedFilter (void) { Init(); }
~CompressedFilter (void) {
if (CompressedData != NULL) delete[] CompressedData;
}
inline void Init();
inline void Clear();
uint Size() const { return CFilterSize; }
uint Count() { return CFilterCount; }
errorT Verify (Filter * filter);
void CompressFrom (Filter * filter);
errorT UncompressTo(Filter * filter) const;
private:
CompressedFilter(const CompressedFilter&);
void operator=(const CompressedFilter&);
};
inline void
CompressedFilter::Init ()
{
CFilterSize = 0;
CFilterCount = 0;
CompressedLength = 0;
CompressedData = NULL;
}
inline void
CompressedFilter::Clear ()
{
if (CompressedData != NULL) { delete[] CompressedData; }
Init();
}
#endif // #ifndef SCID_FILTER_H
//////////////////////////////////////////////////////////////////////
// EOF: filter.h
//////////////////////////////////////////////////////////////////////