File: CanonicalTermConsumer.cpp

package info (click to toggle)
frobby 0.9.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,452 kB
  • ctags: 4,203
  • sloc: cpp: 29,249; sh: 1,121; makefile: 272; ansic: 102
file content (120 lines) | stat: -rwxr-xr-x 3,439 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
/* Frobby: Software for monomial ideal computations.
   Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)

   This program 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 2 of the License, or
   (at your option) any later version.

   This program 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 this program.  If not, see http://www.gnu.org/licenses/.
*/
#include "stdinc.h"
#include "CanonicalTermConsumer.h"
#include "TermTranslator.h"
#include "IdealComparator.h"

#include "Term.h"

CanonicalTermConsumer::CanonicalTermConsumer(auto_ptr<TermConsumer> consumer,
                                             size_t varCount,
                                             TermTranslator* translator):
  _varCount(varCount),
  _storingList(false),
  _ideals(),
  _idealsDeleter(_ideals),
  _consumer(consumer),
  _translator(translator) {
  ASSERT(_consumer.get() != 0);
}

void CanonicalTermConsumer::consumeRing(const VarNames& names) {
  _consumer->consumeRing(names);
}

void CanonicalTermConsumer::beginConsumingList() {
  ASSERT(!_storingList);
  ASSERT(_ideals.empty());

  _storingList = true;
}

void CanonicalTermConsumer::beginConsuming() {
  ASSERT(_storingList || _ideals.empty());

  auto_ptr<Ideal> ideal(new Ideal(_varCount));
  exceptionSafePushBack(_ideals, ideal);
}

void CanonicalTermConsumer::consume(const Term& term) {
  ASSERT(term.getVarCount() == _varCount);
  ASSERT(!_ideals.empty());

  _ideals.back()->insert(term);
}

void CanonicalTermConsumer::doneConsuming() {
  if (!_storingList) {
    ASSERT(_ideals.size() == 1);
    passLastIdeal();
    ASSERT(_ideals.empty());
  }
}

void CanonicalTermConsumer::doneConsumingList() {
  ASSERT(_storingList);

  vector<Ideal*>::iterator end = _ideals.end();
  for (vector<Ideal*>::iterator it = _ideals.begin(); it != end; ++it)
    canonicalizeIdeal(**it);

  // We are sorting in reverse because we are processing the ideals from
  // the back, so they get passed on in the correct order.
  if (_translator == 0) {
    IdealComparator comparator;
    sort(_ideals.rbegin(), _ideals.rend(), comparator);
  } else {
    TranslatedIdealComparator comparator(*_translator);
    sort(_ideals.rbegin(), _ideals.rend(), comparator);
  }

  _consumer->beginConsumingList();
  while (!_ideals.empty())
    passLastIdeal();
  _consumer->doneConsumingList();
}

void CanonicalTermConsumer::passLastIdeal() {
  ASSERT(!_ideals.empty());
  ASSERT(_ideals.back() != 0);

  auto_ptr<Ideal> ideal(_ideals.back());
  _ideals.pop_back();

  canonicalizeIdeal(*ideal);

  _consumer->beginConsuming();
  Term tmp(_varCount);
  Ideal::const_iterator end = ideal->end();
  for (Ideal::const_iterator it = ideal->begin(); it != end; ++it) {
    tmp = *it;
    _consumer->consume(tmp);
  }
  ideal.reset(0);

  _consumer->doneConsuming();
}

void CanonicalTermConsumer::canonicalizeIdeal(Ideal& ideal) {
  if (_translator == 0)
    ideal.sortReverseLex();
  else {
    TranslatedReverseLexComparator comparator(*_translator);
    sort(ideal.begin(), ideal.end(), comparator);
  }
}