summaryrefslogtreecommitdiff
path: root/casa/covering/bookkeeping/Coverage.H
blob: c0ce2b3e73e98811cf1da7ae314f20624415c9b7 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2008, 2009 Brady J. Garvin

// This file is part of Covering Arrays by Simulated Annealing (CASA).

// CASA is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// CASA is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with CASA.  If not, see <http://www.gnu.org/licenses/>.


#ifndef COVERAGE_H
#define COVERAGE_H

#include "PascalTriangle.H"
#include "Combinadic.H"

#include "SubstitutionArray.H"

#include "covering/bookkeeping/Options.H"

template<class T>class Coverage {
protected:
  unsigned				strength;
  Options				options;
  // The offsets are indices into the contents array; entry offsets[i], where i
  // is the combinadic encoding of a set of columns, is the beginning of the
  // contents for that column set's t-sets.
  Array<unsigned>			offsets;
  SubstitutionArray<T>			contents;

public:
  Coverage(unsigned strength, Options options) :
    strength(strength),
    options(options),
    offsets(triangle.nCr(options.getSize(), strength)) {
    unsigned size = 0;
    unsigned offsetIndex = 0;
    for (Array<unsigned>columns = combinadic.begin(strength);
	 columns[strength - 1] < options.getSize();
	 combinadic.next(columns)) {
      unsigned blockSize = 1;
      for (unsigned i = strength; i--;) {
	blockSize *= options.getSymbolCount(columns[i]);
      }
      offsets[offsetIndex++] = size;
      size += blockSize;
    }
    contents = Array<T>(size);
  }

  Coverage(const Coverage&copy) :
    strength(copy.strength),
    options(copy.options),
    offsets(copy.offsets),
    contents(copy.contents) {}

protected:
  // The method encode, in its various forms, is responsible for converting a
  // sorted t-set to an index into the contents array.

  // These hinted versions of encode are for when some information has already
  // been computed.  indexHint is the combinadic encoding of the set of columns;
  // columnsHint is this set of columns in sorted order; firstsHint is an array
  // of corresponding first symbols; and countsHint is an array of symbol counts
  // for each column.
  unsigned encode
    (unsigned indexHint,
     Array<unsigned>columnsHint,
     Array<unsigned>firstsHint,
     Array<unsigned>countsHint,
     Array<unsigned>sortedCombination) const {
    assert(sortedCombination.getSize() == strength);
    assert(indexHint < offsets.getSize());
    unsigned offset = offsets[indexHint];
    unsigned index = 0;
    for (unsigned i = strength; i--;) {
      unsigned column = columnsHint[i];
      unsigned base = firstsHint[column];
      unsigned count = countsHint[column];
      index *= count;
      index += sortedCombination[i] - base;
    }
    return offset + index;
  }

  unsigned encode
    (Array<unsigned>columnsHint,
     Array<unsigned>firstsHint,
     Array<unsigned>countsHint,
     Array<unsigned>sortedCombination) const {
    return encode
      (combinadic.encode(columnsHint),
       columnsHint,
       firstsHint,
       countsHint,
       sortedCombination);
  }

  unsigned encode(Array<unsigned>sortedCombination) const {
    assert(sortedCombination.getSize() == strength);
    Array<unsigned>columns(strength);
    for (unsigned i = strength; i--;) {
      columns[i] = options.getOption(sortedCombination[i]);
    }
    unsigned offsetIndex = combinadic.encode(columns);
    assert(offsetIndex < offsets.getSize());
    unsigned offset = offsets[offsetIndex];
    unsigned index = 0;
    for (unsigned i = strength; i--;) {
      unsigned column = columns[i];
      unsigned base = options.getFirstSymbol(column);
      unsigned count = options.getSymbolCount(column);
      index *= count;
      index += sortedCombination[i] - base;
    }
    return offset + index;
  }

  // The method decode, of course, does the opposite of encode.
  Array<unsigned>decode(unsigned encoding) const {
    unsigned offsetIndex = offsets.getSize();
    while (offsets[--offsetIndex] > encoding);
    unsigned index = encoding - offsets[offsetIndex];
    Array<unsigned>columns = combinadic.decode(offsetIndex, strength);
    Array<unsigned>result(strength);
    for (unsigned i = 0; i < strength; ++i) {
      unsigned column = columns[i];
      unsigned base = options.getFirstSymbol(column);
      unsigned count = options.getSymbolCount(column);
      unsigned digit = index % count;
      index -= digit;
      index /= count;
      result[i] = base + digit;
    }
    assert(encode(result) == encoding);
    return result;
  }

public:
  unsigned getStrength() const {
    return strength;
  }
  const Options&getOptions() const {
    return options;
  }

  class Entry{
  protected:
    Coverage&				owner;
    unsigned				index;
  public:
    Entry(const Coverage&owner,unsigned index) :
      owner(const_cast<Coverage&>(owner)),
      index(index) {}
    operator T() const {
      return owner.contents[index];
    }
    Entry&operator =(const T&value) {
      owner.contents[index] = value;
      return *this;
    }
    Entry&operator --() {
      --owner.contents[index];
      return *this;
    }
    Entry&operator ++() {
      ++owner.contents[index];
      return *this;
    }
  };

  const Entry operator[](Array<unsigned>sortedCombination) const {
    return Entry(*this, encode(sortedCombination));
  }
  Entry operator [](Array<unsigned>sortedCombination) {
    return Entry(*this, encode(sortedCombination));
  }

  // The methods named hintGet work like the [] operator with the aforementioned
  // hints.
  const Entry hintGet
    (unsigned indexHint,
     Array<unsigned>columnsHint,
     Array<unsigned>firstsHint,
     Array<unsigned>countsHint,
     Array<unsigned>sortedCombination) const {
    return Entry
      (*this,
       encode
         (indexHint, columnsHint, firstsHint, countsHint, sortedCombination));
  }

  Entry hintGet
    (unsigned indexHint,
     Array<unsigned>columnsHint,
     Array<unsigned>firstsHint,
     Array<unsigned>countsHint,
     Array<unsigned>sortedCombination) {
    return Entry
      (*this,
       encode
         (indexHint, columnsHint, firstsHint, countsHint, sortedCombination));
  }

  const Entry hintGet
    (Array<unsigned>columnsHint,
     Array<unsigned>firstsHint,
     Array<unsigned>countsHint,
     Array<unsigned>sortedCombination) const {
    return Entry
      (*this,
       encode(columnsHint, firstsHint, countsHint, sortedCombination));
  }

  Entry hintGet
    (Array<unsigned>columnsHint,
     Array<unsigned>firstsHint,
     Array<unsigned>countsHint,
     Array<unsigned>sortedCombination) {
    return Entry
      (*this,
       encode(columnsHint, firstsHint, countsHint, sortedCombination));
  }

  class iterator {
  protected:
    Coverage&				owner;
    unsigned				index;
  public:
    iterator(Coverage&owner, unsigned index) :
      owner(owner),
      index(index) {}
    const Entry operator *() const {
      return Entry(owner, index);
    }
    Entry operator *() {
      return Entry(owner, index);
    }
    iterator&operator ++() {
      ++index;
      return *this;
    }
    bool operator ==(const iterator&other) const {
      return &owner == &other.owner && index == other.index;
    }
    bool operator !=(const iterator&other) const {
      return &owner != &other.owner || index != other.index;
    }
    Array<unsigned>getCombination() const {
      return owner.decode(index);
    }
  };

  class const_iterator {
  protected:
    const Coverage&			owner;
    unsigned				index;
  public:
    const_iterator(const Coverage&owner, unsigned index) :
      owner(owner),
      index(index) {}
    const_iterator(const iterator&copy) :
      owner(copy.owner),
      index(copy.index) {}
    const Entry operator *() const {
      return Entry(owner, index);
    }
    const_iterator&operator ++() {
      ++index;
      return *this;
    }
    bool operator ==(const const_iterator&other) const {
      return &owner == &other.owner && index == other.index;
    }
    bool operator  != (const const_iterator&other) const {
      return &owner != &other.owner || index != other.index;
    }
    Array<unsigned>getCombination() const {
      return owner.decode(index);
    }
  };

  iterator begin() {
    return iterator(*this, 0);
  }
  const_iterator begin() const {
    return const_iterator(*this, 0);
  }
  iterator end() {
    return iterator(*this, contents.getSize());
  }
  const_iterator end() const {
    return const_iterator(*this, contents.getSize());
  }

  unsigned getSize() const {
    return contents.getSize();
  }

  void fill(const T&filler) {
    contents.fill(filler);
  }
};

#endif