Menu

[13a134]: / src / filter.h  Maximize  Restore  History

Download this file

205 lines (174 with data), 5.3 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//////////////////////////////////////////////////////////////////////
//
// 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();
void SetCapacity (gamenumT size);
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.
};
//////////////////////////////////////////////////////////////////////
//
// 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 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 Init();
};
inline void Filter::Init (uint size) {
Free();
FilterSize = size;
FilterCount = size;
Capacity = size;
Data = NULL;
}
inline void Filter::Allocate()
{
Free();
Capacity = FilterSize > Capacity ? FilterSize : Capacity;
Data = new byte [Capacity];
byte * pb = Data;
for (uint i=0; i < FilterSize; i++) { *pb++ = 1; }
}
inline void Filter::Free()
{
if(Data != NULL) {
delete[] Data;
Data = NULL;
}
}
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;
}
inline void Filter::Fill (byte value)
{
if (value == 1) {
if (Data != NULL) Free();
FilterCount = FilterSize;
} else {
if (Data == NULL) Allocate();
FilterCount = (value != 0) ? FilterSize : 0;
memset(Data, value, FilterSize);
}
}
inline void Filter::Append (byte value)
{
if (value != 0) FilterCount++;
if (value != 1 && Data == NULL) Allocate();
if (Data != NULL) {
if (FilterSize >= Capacity) {
SetCapacity(FilterSize > 512 ? FilterSize * 2 : 1024);
}
Data[FilterSize] = value;
}
FilterSize++;
}
inline void Filter::SetCapacity(gamenumT size)
{
if (size > Capacity) {
Capacity = size;
if (Data != NULL) {
byte * newData = new byte [Capacity];
for (uint i=0; i < FilterSize; i++) newData[i] = Data[i];
delete[] Data;
Data = newData;
}
}
}
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
//////////////////////////////////////////////////////////////////////