Scid  4.6.5
filter.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////
2 //
3 // FILE: filter.h
4 // Filter and CompressedFilter Classes
5 //
6 // Part of: Scid (Shane's Chess Information Database)
7 // Version: 1.4
8 //
9 // Notice: Copyright (c) 2000 Shane Hudson. All rights reserved.
10 //
11 // Author: Shane Hudson (sgh@users.sourceforge.net)
12 //
13 //////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef SCID_FILTER_H
17 #define SCID_FILTER_H
18 
19 #include "common.h"
20 #include <algorithm>
21 
22 //////////////////////////////////////////////////////////////////////
23 //
24 // Filter class:
25 //
26 // Holds the results of a database search: one byte per game,
27 // indicating whether that game is included in the filter or not.
28 // A value of 0 indicates the game is excluded, or 1-255 indicates
29 // the game is included, and what position to show when the game
30 // is loaded: 1 means the start position, 2 means the position after
31 // Whites first move, etc.
32 class Filter
33 {
34  private:
35  gamenumT FilterSize; // Number of values in filter.
36  gamenumT FilterCount; // Number of nonzero values in filter.
37  gamenumT Capacity; // Number of values allocated for Data[].
38  byte * Data; // The actual filter data.
39 
40  Filter(const Filter&);
41  Filter& operator=(const Filter&);
42  void Allocate();
43  void Free();
44  void SetCapacity (gamenumT size);
45  friend class CompressedFilter;
46 
47  public:
48  Filter (gamenumT size) :Data(NULL) { Init (size); }
49  ~Filter() { Free(); }
50 
51  void Init (gamenumT size);
52  uint Size() const { return FilterSize; }
53  uint Count () const { return FilterCount; }
54  bool isWhole () const { return FilterCount == FilterSize; }
55  void Set (gamenumT index, byte value); // Sets the value at index.
56  byte Get (gamenumT index) const; // Gets the value at index.
57  void Fill (byte value); // Sets all values.
58  void Append (byte value); // Appends one value.
59 };
60 
61 //////////////////////////////////////////////////////////////////////
62 //
63 // CompressedFilter class:
64 // Holds the same data as a filter, in compressed format.
65 // Random access to individual values is not possible.
66 // A CompressedFilter is created from, or restored to, a regular
67 // filter with the methods CompressFrom() and UncompressTo().
69 {
70  private:
71 
72  uint CFilterSize;
73  uint CFilterCount;
74  uint CompressedLength;
75  byte * CompressedData;
76 
77  public:
78  CompressedFilter (void) { Init(); }
80  if (CompressedData != NULL) delete[] CompressedData;
81  }
82 
83  inline void Clear();
84 
85  uint Size() const { return CFilterSize; }
86  uint Count() { return CFilterCount; }
87 
88  errorT Verify (Filter * filter);
89 
90  void CompressFrom (Filter * filter);
91  errorT UncompressTo(Filter * filter) const;
92 
93  private:
95  void operator=(const CompressedFilter&);
96 
97  inline void Init();
98 };
99 
100 
101 inline void Filter::Init (uint size) {
102  Free();
103  FilterSize = size;
104  FilterCount = size;
105  Capacity = size;
106  Data = NULL;
107 }
108 
109 inline void Filter::Allocate()
110 {
111  Free();
112  Capacity = FilterSize > Capacity ? FilterSize : Capacity;
113  Data = new byte [Capacity];
114  byte * pb = Data;
115  for (uint i=0; i < FilterSize; i++) { *pb++ = 1; }
116 }
117 
118 inline void Filter::Free()
119 {
120  if(Data != NULL) {
121  delete[] Data;
122  Data = NULL;
123  }
124 }
125 
126 inline void Filter::Set (uint index, byte value)
127 {
128  ASSERT (index < FilterSize);
129  if (Data == NULL) {
130  if (value == 1) return;
131  Allocate();
132  }
133  if (Data[index] != 0) FilterCount--;
134  if (value != 0) FilterCount++;
135  Data[index] = value;
136 }
137 
138 inline byte Filter::Get (uint index) const
139 {
140  ASSERT (index < FilterSize);
141  byte res = (Data == NULL) ? 1 : Data[index];
142  return res;
143 }
144 
145 inline void Filter::Fill (byte value)
146 {
147  if (value == 1) {
148  if (Data != NULL) Free();
149  FilterCount = FilterSize;
150  } else {
151  if (Data == NULL) Allocate();
152  FilterCount = (value != 0) ? FilterSize : 0;
153  std::fill(Data, Data + FilterSize, value);
154  }
155 }
156 
157 inline void Filter::Append (byte value)
158 {
159  if (value != 0) FilterCount++;
160  if (value != 1 && Data == NULL) Allocate();
161  if (Data != NULL) {
162  if (FilterSize >= Capacity) {
163  SetCapacity(FilterSize > 512 ? FilterSize * 2 : 1024);
164  }
165  Data[FilterSize] = value;
166  }
167  FilterSize++;
168 }
169 
170 inline void Filter::SetCapacity(gamenumT size)
171 {
172  if (size > Capacity) {
173  Capacity = size;
174  if (Data != NULL) {
175  byte * newData = new byte [Capacity];
176  for (uint i=0; i < FilterSize; i++) newData[i] = Data[i];
177  delete[] Data;
178  Data = newData;
179  }
180  }
181 }
182 
183 inline void
184 CompressedFilter::Init ()
185 {
186  CFilterSize = 0;
187  CFilterCount = 0;
188  CompressedLength = 0;
189  CompressedData = NULL;
190 }
191 
192 inline void
194 {
195  if (CompressedData != NULL) { delete[] CompressedData; }
196  Init();
197 }
198 
199 
200 #endif // #ifndef SCID_FILTER_H
201 
202 //////////////////////////////////////////////////////////////////////
203 // EOF: filter.h
204 //////////////////////////////////////////////////////////////////////
unsigned char byte
Definition: common.h:97
void Fill(byte value)
Definition: filter.h:145
bool isWhole() const
Definition: filter.h:54
friend class CompressedFilter
Definition: filter.h:45
~CompressedFilter(void)
Definition: filter.h:79
#define ASSERT(f)
Definition: common.h:67
void Init(gamenumT size)
Definition: filter.h:101
Filter(gamenumT size)
Definition: filter.h:48
uint Count()
Definition: filter.h:86
uint Count() const
Definition: filter.h:53
sizew
Definition: board.tcl:619
uint Size() const
Definition: filter.h:85
uint32_t uint
Definition: common.h:99
uint Size() const
Definition: filter.h:52
void Append(byte value)
Definition: filter.h:157
Clear
Definition: game.tcl:8
unsigned short errorT
Definition: error.h:20
void Set(gamenumT index, byte value)
Definition: filter.h:126
CompressedFilter(void)
Definition: filter.h:78
Definition: filter.h:32
uint gamenumT
Definition: common.h:159
byte Get(gamenumT index) const
Definition: filter.h:138
void Clear()
Definition: filter.h:193
~Filter()
Definition: filter.h:49