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