File: PackedTermHandler.cpp

package info (click to toggle)
frobby 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,616 kB
  • sloc: cpp: 30,134; sh: 1,184; makefile: 306; ansic: 102; lisp: 10
file content (533 lines) | stat: -rw-r--r-- 14,731 bytes parent folder | download | duplicates (4)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533

class PackedTermHandler {
  class RankCompare;
  class LexComparator;

public:
  class ExternalTerm;

  class Term {
  public:
    unsigned int getIndex() const {
      return index;
    }

  private:
    friend class PackedTermHandler;
    friend class PackedTermHandler::RankCompare;
    friend class PackedTermHandler::ExternalTerm;

    int index;
    mutable const Term* lastDivider;
    unsigned int support;
    Exponent exponents[1];    
  };

  unsigned int getTermSizeInBytes() const {
    return sizeof(Exponent) * (_exponentWords + 3);
  }

  typedef Ranker<Term, LexComparator> Ranker;

  static bool vectorCompare(const vector<Exponent>& a,
			    const vector<Exponent>& b) {
    ASSERT(a.size() == b.size());
    for (int i = 0; i < (int)a.size(); ++i) {
      if (a[i] < b[i])
	return true;
      if (a[i] > b[i])
	return false;
    }
    return false;
  }

  PackedTermHandler(int dimension,
	      const vector<vector<Exponent> >& termsUnsorted):
    _rankers(0) {

    vector<vector<Exponent> > terms(termsUnsorted);
    std::sort(terms.begin(), terms.end(), vectorCompare);

    _size = terms.size();
    _dimension = dimension;
    _exponentWords = (_dimension + 3) >> 2; 

    _termMemory = (Term*)new char[terms.size() * getTermSizeInBytes()];
    for (int i = 0; i < (int)terms.size(); ++i) {
      Term* term = (Term*)indexToMemory(i);

      initializeExponents(term, terms[i].begin(), terms[i].end());
      term->support = computeSupport(term);
      term->lastDivider = 0;
      term->index = i;
    }

    computeRanks();
  }

  ~PackedTermHandler() {
    delete[] _rankers;
    delete[] _termMemory;
  }

  void fillArraySorted(const Term** array) {
    for (int i = 0; i < (int)size(); ++i)
      array[i] = indexToMemory(i);
  }

  Ranker::Predicate getLexPredicate(int position) {
    return _rankers[position].getPredicate();
  }

  int getDimension() const {
    return _dimension;
  }

  size_t size() const {
    return _size;
  }

  void getFirstLarger(const Term**& begin,
		      const Term** end,
		      Exponent largerThanThisE,
		      int position) const {
    unsigned int largerThanThis = (unsigned int)largerThanThisE;
    unsigned int mask = 0xFF000000 >> ((position & 3) << 3);
    largerThanThis = largerThanThis << ((3 - (position & 3)) << 3);
    position = position >> 2;
    /*
    if (begin == end)
      return;

//    cout << "at getFirstLarger begin=";
//    write(*begin, cout);
    cout << " largerThanThis=" << largerThanThis << " largerThanThisE=" << largerThanThisE;
    */
    while (begin != end &&
	   (((*begin)->exponents[position]) & mask) <= largerThanThis)
      ++begin;
  }

  void lcm(ExternalTerm& target,
	   const ExternalTerm& source1,
	   const Term* source2,
	   int position) {
    /*    cout << "doing lcm. target=";
    write(target._term, cout);
    cout << " source1=";
    write(source1._term, cout);
    cout << " source2=";
    write(source2, cout);
    cout << " position=" << position << endl;*/
    

    position = position >> 2;
    
    for (int i = position; i < _exponentWords; ++i) {
      Exponent e1 = source1._term->exponents[i];
      Exponent e2 = source2->exponents[i];

      Exponent exponent = 0xFF000000 & max(e1, e2);
      Exponent result = exponent;

      exponent = max(e1 & 0x00FF0000, e2 & 0x00FF0000);
      result |= exponent;

      exponent = max(e1 & 0x0000FF00, e2 & 0x0000FF00);
      result |= exponent;

      exponent = max(e1 & 0x000000FF, e2 & 0x000000FF);
      result |= exponent;

      target._term->exponents[i] = result;
    }
    
    /*
    
    for (int i = position; i < _dimension; ++i) {
      Exponent e1 = getExponent(source1._term, i);
      Exponent e2 = getExponent(source2, i);
      target.setExponent(i, e1 > e2 ? e1 : e2);
    }
    */

#ifdef PROFILE
    if (_dimension == 1000000) {
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
    }
#endif

  }

  bool someDivides(const Term** begin,
		   const Term** end,
		   const Term* term,
		   const Term*& divisorParam,
		   int originalPosition) {
    ASSERT(originalPosition != _dimension);

    //    cout << "dpos=" << originalPosition;

    unsigned int mask = 0x80808080 >> ((originalPosition & 3) << 3);
    int position = originalPosition >> 2;

    unsigned int support = term->support;
    const Exponent* exponents = term->exponents + position;
    const Exponent* exponentsEnd = term->exponents + _exponentWords;

    for (const Term** it = begin; it != end; ++it) {
      const Term* divisor = *it;

      if (supportsPrecludeDivision(divisor->support, support, originalPosition)) {
//	cout << " [support " << divisor->support << ' ' << support << ' ' << originalPosition << ']';
	
	goto DoesNotDivide;
      }

      {
      const Exponent* divIt = divisor->exponents + position;
      const Exponent* exp = exponents;

      if ((*exp - *divIt) & mask) {
	//	cout << "[first]";
	goto DoesNotDivide;
      }

      ++divIt;
      ++exp;
      
      for (; exp != exponentsEnd; ++divIt, ++exp)
	if ((*exp - *divIt) & 0x80808080)
	  goto DoesNotDivide;
      
      /*      write(*it, cout);
      cout << " divides ";
      write(term, cout);
      cout << endl;*/
      divisorParam = divisor;
      return true;
      }
    DoesNotDivide:;
      /*      write(*it, cout);
      cout << " does not divide ";
      write(term, cout);
      cout << endl;*/
      }
#ifdef PROFILE
    if (_dimension == 1000000) {
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
    }
#endif
    return false;
    }

  bool someDivides(const Term** begin,
		   const Term** end,
		   const ExternalTerm& term,
		   const Term*& divisorParam,
		   int position) {
    return someDivides(begin, end, term._term, divisorParam, position);
  }

  bool divides(const Term* divisor, const ExternalTerm& divided, int position) {
    ASSERT(position != _dimension);

    /*    cout << "does ";
    write(divisor, cout);
    cout << " divide ";
    write(divided._term, cout);
    cout << "?" << endl;*/

    if ((divisor->support & ~(divided._term->support)) >> position)
      return false;
  
    unsigned int mask = 0x80808080 >> ((position & 3) << 3);
    position = position >> 2;

    const Exponent* divisorIt = divisor->exponents + position;
    const Exponent* dividedIt = divided._term->exponents + position;
    const Exponent* dividedEnd = divided._term->exponents + _exponentWords;

    if ((*dividedIt - *divisorIt) & mask)
      return false;

    ++dividedIt;
    ++divisorIt;

    for (; dividedIt != dividedEnd; ++dividedIt, ++divisorIt)
      if ((*dividedIt - *divisorIt) & 0x80808080)
        return false;
    //    cout << "yes" << endl;

#ifdef PROFILE
    if (_dimension == 1000000) {
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
      combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();combatInlining();
    }
#endif

    return true;
  }
  
  const Term* getLastDivider(const Term* term) {
    return term->lastDivider;
  }

  void setLastDivider(const Term* term, const Term* divider) {
    term->lastDivider = divider;
  }

  Exponent getExponent(const Term* term, int position) const {
    unsigned int adjustment = (3 - (position & 3)) << 3;
    position = position >> 2;

    return (term->exponents[position] >> adjustment) & 0x000000FF;
  }

  Exponent getWord(const Term* term, int position) const {
    return term->exponents[position];
  }

  int getWordCount() const {
    return _exponentWords;
  }

  

  bool write(const Term* term, ostream& out) const {
    out << '(' << getExponent(term, 0);
    for (int i = 1; i < _dimension; ++i)
      out << ", " << getExponent(term, i);
    out << ')';

    /*    out << "~[" << getWord(term, 0);
    for (int i = 1; i < _exponentWords; ++i)
      out << ", " << getWord(term, i);
      out << ']';*/
    return out;
  }

private:
  void initializeExponents(Term* term,
			   vector<Exponent>::iterator begin,
			   vector<Exponent>::iterator end) {
    Exponent* exponent = term->exponents;

    ASSERT(distance(begin,end) == _dimension);
    int variablesToGo = _dimension;

    while (variablesToGo > 0) {
      int value = 0;
      for (int i = 0; i < 4; ++i) {
	value <<= 8;
	if (variablesToGo == 0)
	  continue;

	if (begin != end) {
	  ASSERT(*begin <= 127);

	  value |= *begin;
	  ++begin;
	  --variablesToGo;
	}
      }
      *exponent = value;
      ++exponent;
    }
  }


  unsigned int computeSupport(const Term* term) const {
    unsigned int support = 0;
    for (int i = _exponentWords - 1; i >= 0; --i) {
      // hex 7F is binary 0111111, so adding 7F to 0aaaaaaa will give
      // 1bbbbbbb exactly if any a is different from 0. So doing this
      // and anding with hex 80 (which is 10000000) tests for zero.

      unsigned int spreadSubSupport = term->exponents[i] + 0x7F7F7F7F;
      unsigned int subSupport =
	((spreadSubSupport & 0x80000000) >> 31) |
	((spreadSubSupport & 0x00800000) >> 22) |
	((spreadSubSupport & 0x00008000) >> 13) |
	((spreadSubSupport & 0x00000080) >> 4);

      // subSupport now contains the support of exponents[i] in
      // the lower 4 bits.

      if (i == _exponentWords - 1) {
	int bits = ((_dimension - 1) & 3) + 1;
	support = (support << bits) | (subSupport >> (4 - bits));
      } else
	support = (support << 4) | subSupport;
    }
    /*    cout << hex << "the support of";
    write(term, cout);
    cout << " is " << support << endl;*/
    return support;
  }

  static unsigned int supportsPrecludeDivision(unsigned int divisor,
					       unsigned int divided,
					       int position) {
    return (divisor & ~divided) >> position;
  }
  
  class LexComparator {
  public:
    LexComparator(PackedTermHandler& handler, int position):
      _handler(handler),
      _position(position) {
    }

    bool operator()(const Term* a, const Term* b) const {
      /*      _handler.write(a, cout);
      cout << "<";
      _handler.write(b,cout);
      cout << " for position=" << _position << "? ";*/

      unsigned int mask = 0xFFFFFFFF >> ((_position & 3) << 3);

      for (int i = _position >> 2; i < _handler.getWordCount(); ++i) {
	Exponent aEntry = _handler.getWord(a, i) & mask;
	Exponent bEntry = _handler.getWord(b, i) & mask;
	mask = 0xFFFFFFFF;
	
	// Yes, this actually does lex comparison on the exponents
	// contained within.
	if (aEntry < bEntry) {
	  //	  cout << "true" << endl;
	  return true;
	}
	if (aEntry > bEntry) {
	  //	  cout << "false" << endl;
	  return false;
	}
      }
      //      cout << "false" << endl;
      return false;    
    }

  private:
    PackedTermHandler& _handler;
    int _position;
  };

  void computeRanks() {
    const Term** buffer = new const Term*[_size];
    fillArraySorted(buffer);

    _rankers = new Ranker[_dimension];
    for (int position = 0; position < _dimension; ++position)
      _rankers[position].init(buffer, buffer + _size,
			      LexComparator(*this, position));

    delete[] buffer;
  }

  int indexOf(const Term* term) const {
    return term->index;
  }

  const Term* indexToMemory(int index) const {
    return (const Term*)((char*)_termMemory + index * getTermSizeInBytes());
  }

  void combatInlining() const {
    ((PackedTermHandler*)this)->computeRanks();
  }

  void setExponent(Term* term, int position, Exponent value) const {
    //    cout << "(setting " << value << ")";
    value = value << ((3 - (position & 3)) << 3);
    unsigned int mask = 0xFF000000 >> ((position & 3) << 3);
    position = position >> 2;

    term->exponents[position] = (term->exponents[position] & ~mask) | value;
  }

  int _dimension;
  int _exponentWords;
  Ranker* _rankers;
  Term* _termMemory;
  size_t _size;
  
  
public:
  friend class ExternalTerm;

  class ExternalTerm {
    friend class PackedTermHandler;

  public:
    ExternalTerm(const ExternalTerm& term):
      _handler(term._handler) {
      _term = (Term*)new char[_handler.getTermSizeInBytes()];
      _term->support = term._term->support;
      copy(term._term->exponents, term._term->exponents + _handler.getWordCount(),
	   _term->exponents);
    }

    ExternalTerm(const PackedTermHandler& handler):
      _handler(handler) {
      _term = (Term*)new char[_handler.getTermSizeInBytes()];
      _term->support = 0;
      fill_n(_term->exponents, _handler.getWordCount(), 0);
    }

    ~ExternalTerm() {
      delete[] _term;
    }

    const Exponent* begin(int position) const {
      return _term->exponents + position;
    }

    const Exponent* end() const {
      return _term->exponents + _handler.getDimension();
    }

    bool write(ostream& out) const {
      return _handler.write(_term, out);
    }

    Exponent operator[](int position) const {
      return _handler.getExponent(_term, position);
    }

    void setExponent(int position, Exponent value) const {
      _handler.setExponent(_term, position, value);
    }
  
    unsigned int getSupport() const {
      return _term->support;
    }

    void updateSupport() {
      _term->support = _handler.computeSupport(_term);
    }

  private:
    Term* _term;
    const PackedTermHandler& _handler;
  };  
};
/*
typedef PackedTermHandler TermHandler;
typedef PackedTermHandler::Term Term;
typedef PackedTermHandler::ExternalTerm ExternalTerm;
*/
ostream& operator<<(ostream& out, const PackedTermHandler::ExternalTerm& term) {
  term.write(out);
  return out;
}