libUTL++
algorithms.h
1 #pragma once
2 
4 
5 #include <libutl/Predicate.h>
6 #include <libutl/RandIt.h>
7 #include <libutl/Stream.h>
8 #include <libutl/String.h>
9 
11 
12 UTL_NS_BEGIN;
13 
15 
20 enum sort_t
21 {
27 };
28 
30 
42 size_t binarySearch(const RandIt& begin,
43  const RandIt& end,
44  const Object& key,
45  const Ordering* ordering = nullptr,
46  uint_t findType = find_first);
47 
49 
61 void clobber(const FwdIt& lhsBegin,
62  const FwdIt& lhsEnd,
63  const FwdIt& rhsBegin,
64  const FwdIt& rhsEnd,
65  const Ordering* ordering = nullptr);
66 
68 
81 int compare(const FwdIt& lhsBegin,
82  const FwdIt& lhsEnd,
83  const FwdIt& rhsBegin,
84  const FwdIt& rhsEnd,
85  const Ordering* ordering = nullptr);
86 
88 
100 void copy(FwdIt& out,
101  const FwdIt& begin,
102  const FwdIt& end,
103  bool cloning = false,
104  const Predicate* pred = nullptr,
105  bool predVal = true);
106 
108 
120 void copy(Object** out,
121  const FwdIt& begin,
122  const FwdIt& end,
123  bool cloning = false,
124  const Predicate* pred = nullptr,
125  bool predVal = true);
126 
128 
139 void copy(FwdIt& out,
140  const Vector<Object*>& in,
141  bool cloning = false,
142  const Predicate* pred = nullptr,
143  bool predVal = true);
144 
146 
156 size_t
157 count(const FwdIt& begin, const FwdIt& end, const Predicate* pred = nullptr, bool predVal = true);
158 
160 
176 void difference(const FwdIt& lhsBegin,
177  const FwdIt& lhsEnd,
178  const FwdIt& rhsBegin,
179  const FwdIt& rhsEnd,
180  FwdIt& out,
181  const Ordering* ordering = nullptr,
182  bool cloning = false,
183  bool outIsLHS = false);
184 
186 
201 void dump(const FwdIt& begin,
202  const FwdIt& end,
203  Stream& os,
204  uint_t level = uint_t_max,
205  bool key = false,
206  bool printClassName = false,
207  uint_t indent = 0,
208  const char* separator = nullptr);
209 
211 
222 void heapSort(const BidIt& begin,
223  const BidIt& end,
224  const Ordering* ordering = nullptr,
225  size_t size = size_t_max);
226 
228 
239 void heapSort(Object** array, size_t begin, size_t end, const Ordering* ordering = nullptr);
240 
242 
252 void insertionSort(const FwdIt& begin,
253  const FwdIt& end,
254  const Ordering* ordering = nullptr,
255  size_t size = size_t_max);
256 
258 
268 void insertionSort(Object** array, size_t begin, size_t end, const Ordering* ordering = nullptr);
269 
271 
288 void intersect(const FwdIt& lhsBegin,
289  const FwdIt& lhsEnd,
290  const FwdIt& rhsBegin,
291  const FwdIt& rhsEnd,
292  FwdIt& out,
293  const Ordering* ordering = nullptr,
294  bool cloning = false,
295  bool multiSet = false,
296  bool outIsLHS = false);
297 
299 
313 size_t intersectCard(const FwdIt& lhsBegin,
314  const FwdIt& lhsEnd,
315  const FwdIt& rhsBegin,
316  const FwdIt& rhsEnd,
317  const Ordering* ordering = nullptr,
318  bool multiSet = false);
319 
321 
333 bool intersects(const FwdIt& lhsBegin,
334  const FwdIt& lhsEnd,
335  const FwdIt& rhsBegin,
336  const FwdIt& rhsEnd,
337  const Ordering* ordering = nullptr);
338 
340 
350 bool isSorted(const FwdIt& begin, const FwdIt& end, const Ordering* ordering = nullptr);
351 
353 
365 bool isSubSet(const FwdIt& lhsBegin,
366  const FwdIt& lhsEnd,
367  const FwdIt& rhsBegin,
368  const FwdIt& rhsEnd,
369  const Ordering* ordering = nullptr);
370 
372 
384 bool isSuperSet(const FwdIt& lhsBegin,
385  const FwdIt& lhsEnd,
386  const FwdIt& rhsBegin,
387  const FwdIt& rhsEnd,
388  const Ordering* ordering = nullptr);
389 
391 
403 void linearSearch(const FwdIt& begin,
404  const FwdIt& end,
405  const Object& key,
406  FwdIt& out,
407  const Ordering* ordering = nullptr,
408  uint_t findType = find_first);
409 
411 
424 void linearSearchSorted(const FwdIt& begin,
425  const FwdIt& end,
426  const Object& key,
427  FwdIt& out,
428  const Ordering* ordering = nullptr,
429  uint_t findType = find_first);
430 
432 
445 void merge(const FwdIt& lhsBegin,
446  const FwdIt& lhsEnd,
447  const FwdIt& rhsBegin,
448  const FwdIt& rhsEnd,
449  FwdIt& out,
450  const Ordering* ordering = nullptr,
451  bool cloning = false);
452 
454 
464 void mergeSort(const FwdIt& begin,
465  const FwdIt& end,
466  const Ordering* ordering = nullptr,
467  size_t size = size_t_max);
468 
470 
480 void mergeSort(Object** array, size_t begin, size_t end, const Ordering* ordering = nullptr);
481 
483 
496 void multiKeyQuickSort(const FwdIt& begin,
497  const FwdIt& end,
498  bool key = true,
499  bool reverse = false,
500  size_t size = size_t_max);
501 
503 
513 void quickSort(const FwdIt& begin,
514  const FwdIt& end,
515  const Ordering* ordering = nullptr,
516  size_t size = size_t_max);
517 
519 
529 void quickSort(Object** array, size_t begin, size_t end, const Ordering* ordering = nullptr);
530 
532 
543 void remove(FwdIt& begin,
544  const FwdIt& end,
545  bool cmp = false,
546  const Predicate* pred = nullptr,
547  bool predVal = false);
548 
550 
558 void reverse(const BidIt& begin, const BidIt& end);
559 
561 
575 void serializeIn(FwdIt& out,
576  size_t numItems,
577  Stream& is,
578  uint_t mode = ser_default,
579  const RunTimeClass* rtc = nullptr);
580 
582 
594 void serializeOut(
595  const FwdIt& begin, const FwdIt& end, Stream& os, uint_t mode = ser_default, bool boxed = true);
596 
598 
609 void sort(const FwdIt& begin,
610  const FwdIt& end,
611  const Ordering* ordering = nullptr,
612  uint_t algorithm = sort_quickSort,
613  size_t size = size_t_max);
614 
616 
627 void sort(Object** array,
628  size_t begin,
629  size_t end,
630  const Ordering* ordering = nullptr,
631  uint_t algorithm = sort_quickSort);
632 
634 
642 inline void
643 swap(FwdIt& lhs, FwdIt& rhs)
644 {
645  Object* tmp = *lhs;
646  lhs.set(*rhs);
647  rhs.set(tmp);
648 }
649 
651 
660 inline void
661 swap(RandIt& it, size_t lhsIdx, size_t rhsIdx)
662 {
663  Object* tmp = it[lhsIdx];
664  it.set(lhsIdx, it.get(rhsIdx));
665  it.set(rhsIdx, tmp);
666 }
667 
669 
683 void symmetricDifference(const FwdIt& lhsBegin,
684  const FwdIt& lhsEnd,
685  const FwdIt& rhsBegin,
686  const FwdIt& rhsEnd,
687  FwdIt& out,
688  const Ordering* ordering = nullptr,
689  bool cloning = false);
690 
692 
708 void symmetricDifference(const FwdIt& lhsBegin,
709  const FwdIt& lhsEnd,
710  const FwdIt& rhsBegin,
711  const FwdIt& rhsEnd,
712  FwdIt& lhsOut,
713  FwdIt& rhsOut,
714  const Ordering* ordering = nullptr,
715  bool lhsCloning = false,
716  bool rhsCloning = false);
717 
719 
730 String toString(const FwdIt& begin, const FwdIt& end, const String& sep, bool key = false);
731 
733 
745 void unique(const FwdIt& begin,
746  const FwdIt& end,
747  FwdIt& out,
748  const Ordering* ordering = nullptr,
749  bool cloning = false,
750  bool outIsSrc = false);
751 
753 
754 UTL_NS_END;
755 
757 
758 #include <libutl/algorithms_inl.h>
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
void intersect(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false, bool multiSet=false, bool outIsLHS=false)
Determine the intersection of two sorted sequences.
void merge(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false)
Merge two sorted sequences into a single sorted sequence.
Object comparison abstraction.
Definition: Ordering.h:30
bool intersects(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Determine whether the intersection of two sorted sequences is non-empty.
void linearSearch(const FwdIt &begin, const FwdIt &end, const Object &key, FwdIt &out, const Ordering *ordering=nullptr, uint_t findType=find_first)
Linear search a sequence for a key.
void symmetricDifference(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &lhsOut, FwdIt &rhsOut, const Ordering *ordering=nullptr, bool lhsCloning=false, bool rhsCloning=false)
Determine the symmetric difference of two sorted sequences.
void difference(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false, bool outIsLHS=false)
Determine the difference between two sorted sequences.
default representation (via getSerializeMode())
Definition: util.h:75
Store information about a class.
Definition: RunTimeClass.h:23
void multiKeyQuickSort(const FwdIt &begin, const FwdIt &end, bool key=true, bool reverse=false, size_t size=size_t_max)
Multi-key quick-sort a sequence.
void serializeIn(FwdIt &out, size_t numItems, Stream &is, uint_t mode=ser_default, const RunTimeClass *rtc=nullptr)
Serialize objects from an input stream.
quick sort
Definition: algorithms.h:25
sort_t
Specifies a sort algorithm.
Definition: algorithms.h:20
Character string.
Definition: String.h:31
virtual void set(const Object *object)=0
Set the current object.
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
Random-access iterator abstraction.
Definition: RandIt.h:25
void heapSort(Object **array, size_t begin, size_t end, const Ordering *ordering=nullptr)
Heap-sort a sequence.
const size_t size_t_max
Maximum size_t value.
void sort(Object **array, size_t begin, size_t end, const Ordering *ordering=nullptr, uint_t algorithm=sort_quickSort)
Sort a sequence using a given algorithm.
void swap(RandIt &it, size_t lhsIdx, size_t rhsIdx)
Swap two objects in a random-access sequence.
Definition: algorithms.h:661
virtual void set(const Object *object)=0
Set the current object.
bool isSorted(const FwdIt &begin, const FwdIt &end, const Ordering *ordering=nullptr)
Determine whether the given sequence is sorted.
size_t intersectCard(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr, bool multiSet=false)
Determine the cardinality of the intersection of two sorted sequences.
Logical predicate abstraction.
Definition: Predicate.h:26
void quickSort(Object **array, size_t begin, size_t end, const Ordering *ordering=nullptr)
Quick-sort (part of) a sequence.
Forward iterator abstraction.
Definition: FwdIt.h:26
heap sort
Definition: algorithms.h:22
void linearSearchSorted(const FwdIt &begin, const FwdIt &end, const Object &key, FwdIt &out, const Ordering *ordering=nullptr, uint_t findType=find_first)
Linear search a sorted sequence for a key.
void insertionSort(Object **array, size_t begin, size_t end, const Ordering *ordering=nullptr)
Insertion-sort (part of) an array.
do not sort
Definition: algorithms.h:26
int compare(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Compare two sorted sequences.
bool isSuperSet(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Determine whether the lhs sorted sequence is a superset of the rhs sorted sequence.
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
const uint_t uint_t_max
Maximum uint_t value.
Stream I/O abstraction.
Definition: Stream.h:68
Bi-directional iterator abstraction.
Definition: BidIt.h:25
insertion sort
Definition: algorithms.h:23
find first matching object
Definition: util.h:44
A sequence of same-type objects.
Definition: Ordering.h:14
void clobber(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Where lhs and rhs intersect, make the lhs version of the object equal to the rhs version ("clobber" i...
bool isSubSet(const FwdIt &lhsBegin, const FwdIt &lhsEnd, const FwdIt &rhsBegin, const FwdIt &rhsEnd, const Ordering *ordering=nullptr)
Determine whether the lhs sorted sequence is a subset of the rhs sorted sequence. ...
void serializeOut(const FwdIt &begin, const FwdIt &end, Stream &os, uint_t mode=ser_default, bool boxed=true)
Serialize objects to an output stream.
Root of UTL++ class hierarchy.
Definition: Object.h:52
merge sort
Definition: algorithms.h:24
void dump(const FwdIt &begin, const FwdIt &end, Stream &os, uint_t level=uint_t_max, bool key=false, bool printClassName=false, uint_t indent=0, const char *separator=nullptr)
Dump objects to the given stream (with Object::dump()).
void unique(const FwdIt &begin, const FwdIt &end, FwdIt &out, const Ordering *ordering=nullptr, bool cloning=false, bool outIsSrc=false)
Remove duplicate objects from a sorted sequence.
virtual Object * get() const =0
Get the current object.
void mergeSort(Object **array, size_t begin, size_t end, const Ordering *ordering=nullptr)
Merge-sort (part of) an array.
size_t binarySearch(const RandIt &begin, const RandIt &end, const Object &key, const Ordering *ordering=nullptr, uint_t findType=find_first)
Binary search an ordered sequence for a key.
void copy(FwdIt &out, const Vector< Object *> &in, bool cloning=false, const Predicate *pred=nullptr, bool predVal=true)
Copy objects from a Vector to a sequence.
size_t count(const FwdIt &begin, const FwdIt &end, const Predicate *pred=nullptr, bool predVal=true)
Count objects that satisfy a Predicate.