Macaulay2 Engine
Loading...
Searching...
No Matches
monomDRL.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_DRL_HPP
11#define BIBASIS_MONOM_DRL_HPP
12
41
42#include <set>
43#include "allocator.hpp"
44#include "monom.hpp"
45
46namespace BIBasis
47{
59 class MonomDRL : public Monom
60 {
61 private:
63
64 public:
66
67 public:
68 MonomDRL();
69 MonomDRL(const MonomDRL& anotherMonom);
70 ~MonomDRL();
71
72 void* operator new(std::size_t);
73 void operator delete(void* ptr);
74
75 void SetOne();
76 Integer operator[](const Integer var) const;
77
78 const MonomDRL& operator=(const MonomDRL& anotherMonom);
79 const MonomDRL& operator*=(Integer var);
80 const MonomDRL& operator*=(const MonomDRL& anotherMonom);
81 const MonomDRL& operator/=(const MonomDRL& anotherMonom);
82 void SetQuotientOf(const MonomDRL& monomA, const MonomDRL& monomB);
83
84 bool operator==(const MonomDRL& anotherMonom) const;
85 bool operator!=(const MonomDRL& anotherMonom) const;
86
87 bool operator<(const MonomDRL& anotherMonom) const;
88 bool operator>(const MonomDRL& anotherMonom) const;
89 int Compare(const MonomDRL& anotherMonom);
90
91 bool IsDivisibleBy(const MonomDRL& anotherMonom) const;
92 bool IsTrueDivisibleBy(const MonomDRL& anotherMonom) const;
93 bool IsPommaretDivisibleBy(const MonomDRL& 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 MonomDRL::MonomDRL(const MonomDRL& 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* MonomDRL::operator new(std::size_t)
140 {
141 return Allocator.Allocate();
142 }
143
144 inline void MonomDRL::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 MonomDRL::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 MonomDRL& MonomDRL::operator=(const MonomDRL& 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 MonomDRL& MonomDRL::operator*=(const MonomDRL& 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 MonomDRL& MonomDRL::operator/=(const MonomDRL& 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 MonomDRL::SetQuotientOf(const MonomDRL& monomA, const MonomDRL& 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 MonomDRL::operator==(const MonomDRL& 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 MonomDRL::operator!=(const MonomDRL& 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 MonomDRL::operator<(const MonomDRL& 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 MonomDRL::operator>(const MonomDRL& 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 MonomDRL::IsDivisibleBy(const MonomDRL& 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 MonomDRL::IsTrueDivisibleBy(const MonomDRL& 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 MonomDRL::IsPommaretDivisibleBy(const MonomDRL& 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 && iterator->Value > anotherIterator->Value)
559 {
560 iterator = iterator->Next;
561 }
562
563 while (iterator && anotherIterator)
564 {
565 if (iterator->Value != anotherIterator->Value)
566 {
567 break;
568 }
569 iterator = iterator->Next;
570 anotherIterator = anotherIterator->Next;
571 }
572
573 return !iterator && !anotherIterator;
574 }
575
577 {
578 if (!ListHead)
579 {
580 return 0;
581 }
582 else
583 {
584 return ListHead->Value;
585 }
586 }
587
588 inline std::set<MonomDRL::Integer> MonomDRL::GetVariablesSet() const
589 {
590 std::set<Integer> result;
591 VarsListNode *iterator = ListHead;
592 while (iterator)
593 {
594 result.insert(iterator->Value);
595 iterator = iterator->Next;
596 }
597 return result;
598 }
599}
600
601#endif // BIBASIS_MONOM_DRL_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
bool operator!=(const MonomDRL &anotherMonom) const
Definition monomDRL.hpp:407
Integer operator[](const Integer var) const
Definition monomDRL.hpp:179
MonomDRL * Next
Definition monomDRL.hpp:65
const MonomDRL & operator*=(Integer var)
Definition monomDRL.hpp:265
bool IsPommaretDivisibleBy(const MonomDRL &anotherMonom) const
Definition monomDRL.hpp:545
bool operator<(const MonomDRL &anotherMonom) const
Definition monomDRL.hpp:430
int Compare(const MonomDRL &anotherMonom)
Definition monomDRL.cpp:18
const MonomDRL & operator/=(const MonomDRL &anotherMonom)
Definition monomDRL.hpp:323
bool operator>(const MonomDRL &anotherMonom) const
Definition monomDRL.hpp:461
bool operator==(const MonomDRL &anotherMonom) const
Definition monomDRL.hpp:384
void MultiplyBy(Integer var)
Definition monomDRL.hpp:232
Integer FirstMultiVar() const
Definition monomDRL.hpp:576
static FastAllocator Allocator
Definition monomDRL.hpp:62
VarsListNode * Find(const Integer var) const
Definition monomDRL.hpp:149
bool IsTrueDivisibleBy(const MonomDRL &anotherMonom) const
Definition monomDRL.hpp:516
std::set< Integer > GetVariablesSet() const
Definition monomDRL.hpp:588
const MonomDRL & operator=(const MonomDRL &anotherMonom)
Definition monomDRL.hpp:185
bool IsDivisibleBy(const MonomDRL &anotherMonom) const
Definition monomDRL.hpp:492
void SetQuotientOf(const MonomDRL &monomA, const MonomDRL &monomB)
Definition monomDRL.hpp:347
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