SortUnique

Files CCore/inc/algon/SortUnique.h CCore/src/algon/SortUnique.cpp

Here is a set of algorithms to perform operations on unique elements of some range. When you have some range of elements, you may group adjacent elements, equals in some sense, in groups. Then you may process either first elements of each groups, or the "range" of group subranges. Usually, the initial range is sorted first to make equal elements adjacent.

ApplyUniqueAlgo is an Algorithm Package, parametrized by the generalized range type. The second template parameter is a base range algorithm package, defaulted to the BaseRangeAlgo<R>. The Functor Init Pattern is used to apply functors.


template <class R,class Algo=Algon::BaseRangeAlgo<R> >
struct Algon::ApplyUniqueAlgo : Algo
 {
  template <class FuncInit>
  static void ApplyUnique(R r,FuncInit func_init);
  
  template <class Func,class FuncInit>
  static void ApplyUniqueBy(R r,Func by,FuncInit func_init);
  
  template <class FuncInit>
  static void ApplyUniqueRange(R r,FuncInit func_init);
  
  template <class Func,class FuncInit>
  static void ApplyUniqueRangeBy(R r,Func by,FuncInit func_init);
 };

ApplyUnique() applies the functor to first elements of unique subranges. To compare elements the operator != is used.

ApplyUniqueBy() applies the functor to first elements of unique subranges. To compare elements the given functor by is used. It is applied to elements and results are compared using the operator !=.

ApplyUniqueRange() applies the functor to unique subranges. To compare elements the operator != is used.

ApplyUniqueRangeBy() applies the functor unique subranges. To compare elements the given functor by is used. It is applied to elements and results are compared using the operator !=.

In these algorithms by is a functor, but func_init is a functor initializer. In per-element algorithms the functor is called with an element reference argument, but in per-range algorithms the functor is called with a range of type R argument.

There are number of "stand-alone" derived algorithms, including variants, combined with a sorting.

The first group is sort algorithms with a "by" functor.


/* ...SortBy() */

template <class Ran,class Len,class Func>
void Algon::IncrSortBy(Ran ptr,Len len,Func by);

template <class Ran,class Len,class Func>
void Algon::DecrSortBy(Ran ptr,Len len,Func by);

template <class T,class Func>
void Algon::IncrSortBy(PtrLen<T> range,Func by);

template <class T,class Func>
void Algon::DecrSortBy(PtrLen<T> range,Func by);

template <class T,class Func>
void Algon::IncrSortBy(PtrLenReverse<T> range,Func by);

template <class T,class Func>
void Algon::DecrSortBy(PtrLenReverse<T> range,Func by);

Base variants.


template <class R,class FuncInit>
void Algon::ApplyUnique(R r,FuncInit func_init)
 {
  ApplyUniqueAlgo<R>::ApplyUnique(r,func_init);
 }

template <class R,class FuncInit>
void Algon::SortThenApplyUnique(R r,FuncInit func_init)
 {
  Sort(r);
  ApplyUnique(r,func_init);
 }

"By" variants.


template <class R,class Func,class FuncInit>
void Algon::ApplyUniqueBy(R r,Func by,FuncInit func_init)
 {
  ApplyUniqueAlgo<R>::ApplyUniqueBy(r,by,func_init);
 }

template <class R,class Func,class FuncInit>
void Algon::IncrSortThenApplyUniqueBy(R r,Func by,FuncInit func_init)
 {
  IncrSortBy(r,by);
  ApplyUniqueBy(r,by,func_init);
 }

template <class R,class Func,class FuncInit>
void Algon::DecrSortThenApplyUniqueBy(R r,Func by,FuncInit func_init)
 {
  DecrSortBy(r,by);
  ApplyUniqueBy(r,by,func_init);
 }

Base range variants.


template <class R,class FuncInit>
void Algon::ApplyUniqueRange(R r,FuncInit func_init)
 {
  ApplyUniqueAlgo<R>::ApplyUniqueRange(r,func_init);
 }
  
template <class R,class FuncInit>
void Algon::SortThenApplyUniqueRange(R r,FuncInit func_init)
 {
  Sort(r);
  ApplyUniqueRange(r,func_init);
 }

"By" range variants.


template <class R,class Func,class FuncInit>
void Algon::ApplyUniqueRangeBy(R r,Func by,FuncInit func_init)
 {
  ApplyUniqueAlgo<R>::ApplyUniqueRangeBy(r,by,func_init);
 }

template <class R,class Func,class FuncInit>
void Algon::IncrSortThenApplyUniqueRangeBy(R r,Func by,FuncInit func_init)
 {
  IncrSortBy(r,by);
  ApplyUniqueRangeBy(r,by,func_init);
 }

template <class R,class Func,class FuncInit>
void Algon::DecrSortThenApplyUniqueRangeBy(R r,Func by,FuncInit func_init)
 {
  DecrSortBy(r,by);
  ApplyUniqueRangeBy(r,by,func_init);
 }