Frobby  0.9.5
Term.cpp
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see http://www.gnu.org/licenses/.
16 */
17 #include "stdinc.h"
18 #include "Term.h"
19 
20 #include "TermPredicate.h"
21 #include <sstream>
22 #include <vector>
23 
24 const unsigned int PoolCount = 50;
25 const unsigned int ObjectPoolSize = 1000;
26 
27 Term::Term(const string& str):
28  _exponents(0), _varCount(0) {
29  istringstream in(str);
30 
31  vector<Exponent> exponents;
32  mpz_class ex;
33  while (in >> ex) {
34  ASSERT(ex.fits_uint_p());
35  exponents.push_back(ex.get_ui());
36  }
37 
38  if (!exponents.empty())
39  initialize(&(exponents[0]), exponents.size());
40 }
41 
42 namespace {
43  struct ObjectPool {
44  ObjectPool(): objectsStored(0), objects(0) {}
45 
46  void ensureInit() {
47  if (objects == 0)
48  objects = new Exponent*[ObjectPoolSize];
49  }
50 
51  bool empty() const {
52  return objectsStored == 0;
53  }
54 
55  bool canStoreMore() const {
56  return objectsStored < ObjectPoolSize;
57  }
58 
59  Exponent* removeObject() {
60  ASSERT(!empty());
61  --objectsStored;
62  return objects[objectsStored];
63  }
64 
65  void addObject(Exponent* object) {
66  ASSERT(canStoreMore());
67  ASSERT(objects != 0);
68 
69  objects[objectsStored] = object;
70  ++objectsStored;
71  }
72 
73  ~ObjectPool() {
74  if (objects == 0)
75  return;
76  for (size_t i = 0; i < objectsStored; ++i)
77  delete[] objects[i];
78  delete[] objects;
79  }
80 
81  unsigned int objectsStored;
82  Exponent** objects;
83  } pools[PoolCount];
84 }
85 
86 Exponent* Term::allocate(size_t size) {
87  ASSERT(size > 0);
88 
89  if (size < PoolCount) {
90  pools[size].ensureInit();
91  if (!pools[size].empty())
92  return pools[size].removeObject();
93  }
94 
95  return new Exponent[size];
96 }
97 
98 void Term::deallocate(Exponent* p, size_t size) {
99  if (p == 0)
100  return;
101 
102  ASSERT(size > 0);
103 
104  if (size < PoolCount && pools[size].canStoreMore())
105  pools[size].addObject(p);
106  else
107  delete[] p;
108 }
109 
110 void Term::print(FILE* file, const Exponent* e, size_t varCount) {
111  ostringstream out;
112  print(out, e, varCount);
113  fputs(out.str().c_str(), file);
114 }
115 
116 void Term::print(ostream& out, const Exponent* e, size_t varCount) {
117  ASSERT(e != 0 || varCount == 0);
118 
119  out << '(';
120  for (size_t var = 0; var < varCount; ++var) {
121  if (var != 0)
122  out << ", ";
123  out << e[var];
124  }
125  out << ')';
126 }
127 
128 bool Term::operator==(const Exponent* term) const {
129  return equals(begin(), term, getVarCount());
130 }
const unsigned int ObjectPoolSize
Definition: Term.cpp:25
const unsigned int PoolCount
Definition: Term.cpp:24
bool operator==(const Term &term) const
Definition: Term.h:125
static Exponent * allocate(size_t size)
Definition: Term.cpp:86
static void deallocate(Exponent *p, size_t size)
Definition: Term.cpp:98
Exponent * begin()
Definition: Term.h:79
Term()
Definition: Term.h:51
size_t getVarCount() const
Definition: Term.h:85
static void print(FILE *file, const Exponent *e, size_t varCount)
Writes e to file in a format suitable for debug output.
Definition: Term.cpp:110
void initialize(const Exponent *exponents, size_t varCount)
Definition: Term.h:587
bool equals(const Word *a, const Word *b, size_t varCount)
Returns true if a equals b.
unsigned int Exponent
Definition: stdinc.h:89
#define ASSERT(X)
Definition: stdinc.h:86