Macaulay2 Engine
Loading...
Searching...
No Matches
monomLex.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright (C) 2006-2011 by Mikhail V. Zinin *
3 * mzinin@gmail.com *
4 * *
5 * You may redistribute this file under the terms of the GNU General *
6 * Public License as published by the Free Software Foundation, either *
7 * version 2 of the License, or any later version. *
8 *****************************************************************************/
9
10#ifndef BIBASIS_MONOM_LEX_HPP
11#define BIBASIS_MONOM_LEX_HPP
12
44
45#include <set>
46#include "allocator.hpp"
47#include "monom.hpp"
48
49namespace BIBasis
50{
62 class MonomLex : public Monom
63 {
64 private:
66
67 public:
69
70 public:
71 MonomLex();
72 MonomLex(const MonomLex& anotherMonom);
73 ~MonomLex();
74
75 void* operator new(std::size_t);
76 void operator delete(void* ptr);
77
78 void SetOne();
79 Integer operator[](Integer var) const;
80
81 const MonomLex& operator=(const MonomLex& anotherMonom);
82 const MonomLex& operator*=(Integer var);
83 const MonomLex& operator*=(const MonomLex& anotherMonom);
84 const MonomLex& operator/=(const MonomLex& anotherMonom);
85 void SetQuotientOf(const MonomLex& monomA, const MonomLex& monomB);
86
87 bool operator==(const MonomLex& anotherMonom) const;
88 bool operator!=(const MonomLex& anotherMonom) const;
89
90 bool operator<(const MonomLex& anotherMonom) const;
91 bool operator>(const MonomLex& anotherMonom) const;
92 int Compare(const MonomLex& anotherMonom);
93
94 bool IsDivisibleBy(const MonomLex& anotherMonom) const;
95 bool IsTrueDivisibleBy(const MonomLex& anotherMonom) const;
96 bool IsPommaretDivisibleBy(const MonomLex& anotherMonom) const;
97
98 Integer FirstMultiVar() const;
99 std::set<Integer> GetVariablesSet() const;
100
101 private:
102 void MultiplyBy(Integer var);
103 VarsListNode* Find(const Integer var) const;
104 };
105
107 : Monom()
108 , Next(nullptr)
109 {
110 }
111
112 inline MonomLex::MonomLex(const MonomLex& anotherMonom)
113 : Monom()
114 , Next(nullptr)
115 {
116 if (!anotherMonom.ListHead)
117 {
118 return;
119 }
120 else
121 {
122 TotalDegree = anotherMonom.TotalDegree;
123 VarsListNode **iterator = &ListHead,
124 *iteratorAnother = anotherMonom.ListHead;
125 while (iteratorAnother)
126 {
127 *iterator = new VarsListNode();
128 (*iterator)->Value = iteratorAnother->Value;
129
130 iterator = &((*iterator)->Next);
131 iteratorAnother = iteratorAnother->Next;
132 }
133 }
134 }
135
137 {
138 SetOne();
139 }
140
141 inline void* MonomLex::operator new(std::size_t)
142 {
143 return Allocator.Allocate();
144 }
145
146 inline void MonomLex::operator delete(void* ptr)
147 {
148 Allocator.Free(ptr);
149 }
150
152 {
153 if (!ListHead || ListHead->Value > var)
154 {
155 return nullptr;
156 }
157
158 VarsListNode* position = ListHead;
159 while (position && position->Next && position->Next->Value <= var)
160 {
161 position = position->Next;
162 }
163 return position;
164 }
165
166 inline void MonomLex::SetOne()
167 {
168 TotalDegree = 0;
169 if (ListHead)
170 {
171 VarsListNode* tmpNode;
172 while (ListHead)
173 {
174 tmpNode = ListHead;
175 ListHead = ListHead->Next;
176 delete tmpNode;
177 }
178 }
179 }
180
182 {
183 VarsListNode* varPosition = Find(var);
184 return varPosition && varPosition->Value == var;
185 }
186
187 inline const MonomLex& MonomLex::operator=(const MonomLex& anotherMonom)
188 {
189 if (this == &anotherMonom)
190 {
191 return *this;
192 }
193
194 if (!anotherMonom.ListHead)
195 {
196 SetOne();
197 }
198 else
199 {
200 TotalDegree = anotherMonom.TotalDegree;
201
202 VarsListNode *iteratorAnother = anotherMonom.ListHead,
203 **iterator = &ListHead;
204 while (*iterator && iteratorAnother)
205 {
206 (*iterator)->Value = iteratorAnother->Value;
207 iterator = &((*iterator)->Next);
208 iteratorAnother = iteratorAnother->Next;
209 }
210
211 if (*iterator)
212 {
213 VarsListNode *nodeToDelete = (*iterator)->Next;
214 *iterator = nullptr;
215 while (nodeToDelete)
216 {
217 iteratorAnother = nodeToDelete;
218 nodeToDelete = nodeToDelete->Next;
219 delete iteratorAnother;
220 }
221 }
222 else while (iteratorAnother)
223 {
224 *iterator = new VarsListNode();
225 (*iterator)->Value = iteratorAnother->Value;
226
227 iterator = &((*iterator)->Next);
228 iteratorAnother = iteratorAnother->Next;
229 }
230 }
231 return *this;
232 }
233
235 {
236 //inserted variable is the only one
237 if (!ListHead)
238 {
239 ListHead = new VarsListNode();
240 ListHead->Value = var;
241 ++TotalDegree;
242 }
243 else
244 {
245 VarsListNode* position = Find(var);
246 //inserted variable is the eldest one
247 if (!position)
248 {
249 position = new VarsListNode();
250 position->Value = var;
251 position->Next = ListHead;
252 ListHead = position;
253 ++TotalDegree;
254 }
255 //all other cases
256 else if(position->Value != var)
257 {
258 VarsListNode* newNode = new VarsListNode();
259 newNode->Value = var;
260 newNode->Next = position->Next;
261 position->Next = newNode;
262 ++TotalDegree;
263 }
264 }
265 }
266
268 {
269 MultiplyBy(var);
270 return *this;
271 }
272
273 inline const MonomLex& MonomLex::operator*=(const MonomLex& anotherMonom)
274 {
275 if (!ListHead)
276 {
277 *this = anotherMonom;
278 }
279 else
280 {
281 if (anotherMonom.ListHead)
282 {
283 VarsListNode **iterator = &ListHead,
284 *anotherIterator = anotherMonom.ListHead;
285
286 while (*iterator && anotherIterator)
287 {
288 if ((*iterator)->Value == anotherIterator->Value)
289 {
290 iterator = &((*iterator)->Next);
291 anotherIterator = anotherIterator->Next;
292 }
293 else if ((*iterator)->Value < anotherIterator->Value)
294 {
295 iterator = &((*iterator)->Next);
296 }
297 else
298 {
299 VarsListNode* newNode = new VarsListNode();
300 newNode->Value = anotherIterator->Value;
301 newNode->Next = *iterator;
302 *iterator = newNode;
303 ++TotalDegree;
304
305 iterator = &(newNode->Next);
306 anotherIterator = anotherIterator->Next;
307 }
308 }
309
310 while (anotherIterator)
311 {
312 *iterator = new VarsListNode();
313 (*iterator)->Value = anotherIterator->Value;
314 ++TotalDegree;
315
316 iterator = &((*iterator)->Next);
317 anotherIterator = anotherIterator->Next;
318 }
319 }
320 }
321
322 return *this;
323 }
324
325 inline const MonomLex& MonomLex::operator/=(const MonomLex& anotherMonom)
326 {
327 VarsListNode **iterator = &ListHead,
328 *anotherIterator = anotherMonom.ListHead;
329
330 while (*iterator && anotherIterator)
331 {
332 if ((*iterator)->Value == anotherIterator->Value)
333 {
334 VarsListNode* nodeToDelete = *iterator;
335 *iterator = (*iterator)->Next;
336 delete nodeToDelete;
337 --TotalDegree;
338 anotherIterator = anotherIterator->Next;
339 }
340 else if ((*iterator)->Value < anotherIterator->Value)
341 {
342 iterator = &((*iterator)->Next);
343 }
344 }
345
346 return *this;
347 }
348
349 inline void MonomLex::SetQuotientOf(const MonomLex& monomA, const MonomLex& monomB)
350 {
351 SetOne();
352 VarsListNode **iterator = &ListHead,
353 *iteratorA = monomA.ListHead,
354 *iteratorB = monomB.ListHead;
355
356 while (iteratorA && iteratorB)
357 {
358 if (iteratorA->Value == iteratorB->Value)
359 {
360 iteratorA = iteratorA->Next;
361 iteratorB = iteratorB->Next;
362 }
363 else
364 {
365 ++TotalDegree;
366 *iterator = new VarsListNode();
367 (*iterator)->Value = iteratorA->Value;
368 iterator = &((*iterator)->Next);
369 if (iteratorA->Value < iteratorB->Value)
370 {
371 iteratorA = iteratorA->Next;
372 }
373 }
374 }
375
376 while (iteratorA)
377 {
378 ++TotalDegree;
379 *iterator = new VarsListNode();
380 (*iterator)->Value = iteratorA->Value;
381 iterator = &((*iterator)->Next);
382 iteratorA = iteratorA->Next;
383 }
384 }
385
386 inline bool MonomLex::operator==(const MonomLex& anotherMonom) const
387 {
388 if (TotalDegree != anotherMonom.TotalDegree)
389 {
390 return false;
391 }
392 else
393 {
394 VarsListNode *iterator(ListHead),
395 *anotherIterator(anotherMonom.ListHead);
396 while (anotherIterator)
397 {
398 if (iterator->Value != anotherIterator->Value)
399 {
400 break;
401 }
402 iterator = iterator->Next;
403 anotherIterator = anotherIterator->Next;
404 }
405 return !anotherIterator;
406 }
407 }
408
409 inline bool MonomLex::operator!=(const MonomLex& anotherMonom) const
410 {
411 if (TotalDegree != anotherMonom.TotalDegree)
412 {
413 return true;
414 }
415 else
416 {
417 VarsListNode *iterator(ListHead),
418 *anotherIterator(anotherMonom.ListHead);
419 while (anotherIterator)
420 {
421 if (iterator->Value != anotherIterator->Value)
422 {
423 break;
424 }
425 iterator = iterator->Next;
426 anotherIterator = anotherIterator->Next;
427 }
428 return anotherIterator;
429 }
430 }
431
432 inline bool MonomLex::operator<(const MonomLex& anotherMonom) const
433 {
434 VarsListNode *iterator = ListHead,
435 *anotherIterator = anotherMonom.ListHead;
436 while (anotherIterator && iterator)
437 {
438 if (iterator->Value < anotherIterator->Value)
439 {
440 return false;
441 }
442 if (iterator->Value > anotherIterator->Value)
443 {
444 return true;
445 }
446 iterator = iterator->Next;
447 anotherIterator = anotherIterator->Next;
448 }
449 return anotherIterator;
450 }
451
452 inline bool MonomLex::operator>(const MonomLex& anotherMonom) const
453 {
454 VarsListNode *iterator = ListHead,
455 *anotherIterator = anotherMonom.ListHead;
456 while (anotherIterator && iterator)
457 {
458 if (iterator->Value < anotherIterator->Value)
459 {
460 return true;
461 }
462 if (iterator->Value > anotherIterator->Value)
463 {
464 return false;
465 }
466 iterator = iterator->Next;
467 anotherIterator = anotherIterator->Next;
468 }
469 return iterator;
470 }
471
472 inline bool MonomLex::IsDivisibleBy(const MonomLex& anotherMonom) const
473 {
474 VarsListNode *iterator = ListHead,
475 *anotherIterator = anotherMonom.ListHead;
476 while (iterator && anotherIterator)
477 {
478 if (iterator->Value == anotherIterator->Value)
479 {
480 iterator = iterator->Next;
481 anotherIterator = anotherIterator->Next;
482 }
483 else if (iterator->Value < anotherIterator->Value)
484 {
485 iterator = iterator->Next;
486 }
487 else
488 {
489 break;
490 }
491 }
492
493 return !anotherIterator;
494 }
495
496 inline bool MonomLex::IsTrueDivisibleBy(const MonomLex& anotherMonom) const
497 {
498 if (TotalDegree <= anotherMonom.TotalDegree)
499 {
500 return false;
501 }
502
503 VarsListNode *iterator(ListHead),
504 *anotherIterator(anotherMonom.ListHead);
505 while (iterator && anotherIterator)
506 {
507 if (iterator->Value == anotherIterator->Value)
508 {
509 iterator = iterator->Next;
510 anotherIterator = anotherIterator->Next;
511 }
512 else if (iterator->Value < anotherIterator->Value)
513 {
514 iterator = iterator->Next;
515 }
516 else
517 {
518 break;
519 }
520 }
521
522 return !anotherIterator;
523 }
524
525 inline bool MonomLex::IsPommaretDivisibleBy(const MonomLex& anotherMonom) const
526 {
527 if (TotalDegree < anotherMonom.TotalDegree)
528 {
529 return false;
530 }
531 if (!anotherMonom.TotalDegree)
532 {
533 return true;
534 }
535
536 VarsListNode *iterator = ListHead,
537 *anotherIterator = anotherMonom.ListHead;
538 while (iterator && anotherIterator)
539 {
540 if (iterator->Value != anotherIterator->Value)
541 {
542 break;
543 }
544 iterator = iterator->Next;
545 anotherIterator = anotherIterator->Next;
546 }
547
548 return !anotherIterator;
549 }
550
552 {
553 if (!ListHead)
554 {
555 return 0;
556 }
557
558 VarsListNode* iterator(ListHead);
559 while (iterator->Next)
560 {
561 iterator = iterator->Next;
562 }
563 return iterator->Value;
564 }
565
566 inline std::set<MonomLex::Integer> MonomLex::GetVariablesSet() const
567 {
568 std::set<Integer> result;
569 VarsListNode *iterator = ListHead;
570 while (iterator)
571 {
572 result.insert(iterator->Value);
573 iterator = iterator->Next;
574 }
575 return result;
576 }
577}
578
579#endif // BIBASIS_MONOM_LEX_HPP
BIBasis::FastAllocator — per-size-class slab allocator for BIBasis's small objects.
Slab allocator handing out fixed-size blocks for one BIBasis type per instance.
Definition allocator.hpp:57
short int Integer
Definition monom.hpp:72
Integer TotalDegree
Definition monom.hpp:106
VarsListNode * ListHead
Definition monom.hpp:105
Integer operator[](Integer var) const
Definition monomLex.hpp:181
void MultiplyBy(Integer var)
Definition monomLex.hpp:234
VarsListNode * Find(const Integer var) const
Definition monomLex.hpp:151
const MonomLex & operator/=(const MonomLex &anotherMonom)
Definition monomLex.hpp:325
bool operator>(const MonomLex &anotherMonom) const
Definition monomLex.hpp:452
bool IsDivisibleBy(const MonomLex &anotherMonom) const
Definition monomLex.hpp:472
int Compare(const MonomLex &anotherMonom)
Definition monomLex.cpp:18
static FastAllocator Allocator
Definition monomLex.hpp:65
Integer FirstMultiVar() const
Definition monomLex.hpp:551
bool operator!=(const MonomLex &anotherMonom) const
Definition monomLex.hpp:409
std::set< Integer > GetVariablesSet() const
Definition monomLex.hpp:566
void SetQuotientOf(const MonomLex &monomA, const MonomLex &monomB)
Definition monomLex.hpp:349
bool IsTrueDivisibleBy(const MonomLex &anotherMonom) const
Definition monomLex.hpp:496
const MonomLex & operator=(const MonomLex &anotherMonom)
Definition monomLex.hpp:187
MonomLex * Next
Definition monomLex.hpp:68
bool IsPommaretDivisibleBy(const MonomLex &anotherMonom) const
Definition monomLex.hpp:525
const MonomLex & operator*=(Integer var)
Definition monomLex.hpp:267
bool operator==(const MonomLex &anotherMonom) const
Definition monomLex.hpp:386
bool operator<(const MonomLex &anotherMonom) const
Definition monomLex.hpp:432
VALGRIND_MAKE_MEM_DEFINED & result(result)
BIBasis::Monom — abstract squarefree-monomial base for the three Janet orderings.
Singly linked-list node of a Monom's variable list, with a per-class slab allocator.
Definition monom.hpp:94