libUTL++
Ordering.h
1 #pragma once
2 
4 
5 #include <libutl/FlagsMI.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
13 template <class T>
14 class Vector;
15 class BitArray;
16 
18 // Ordering ////////////////////////////////////////////////////////////////////////////////////////
20 
28 
30 class Ordering : public Object
31 {
34 
35 public:
42  virtual int cmp(const Object* lhs, const Object* rhs) const = 0;
43 
44  virtual const Object& getObjectKey(const Object* object) const;
45 
47  int operator()(const Object* lhs, const Object* rhs) const
48  {
49  return cmp(lhs, rhs);
50  }
51 };
52 
54 // NaturalOrdering /////////////////////////////////////////////////////////////////////////////////
56 
65 
67 class NaturalOrdering : public Ordering
68 {
71 
72 public:
73  virtual int cmp(const Object* lhs, const Object* rhs) const;
74 };
75 
77 
82 extern const NaturalOrdering naturalOrdering;
83 
85 // InvertedNaturalOrdering /////////////////////////////////////////////////////////////////////////
87 
96 
99 {
102 
103 public:
107  virtual int cmp(const Object* lhs, const Object* rhs) const;
108 };
109 
111 
117 
119 // KeyOrdering /////////////////////////////////////////////////////////////////////////////////////
121 
130 
132 class KeyOrdering : public Ordering
133 {
136 
137 public:
138  virtual int cmp(const Object* lhs, const Object* rhs) const;
139 
140  virtual const Object& getObjectKey(const Object* object) const;
141 };
142 
144 
149 extern const KeyOrdering keyOrdering;
150 
152 // InvertedKeyOrdering /////////////////////////////////////////////////////////////////////////////
154 
163 
166 {
169 
170 public:
171  virtual int cmp(const Object* lhs, const Object* rhs) const;
172 
173  virtual const Object& getObjectKey(const Object* object) const;
174 };
175 
177 
183 
185 // TypeOrdering ////////////////////////////////////////////////////////////////////////////////////
187 
196 
198 class TypeOrdering : public Ordering
199 {
201 
202 public:
203  TypeOrdering(Ordering* ordering)
204  : _ordering(ordering)
205  {
206  }
207 
208  virtual int cmp(const Object* lhs, const Object* rhs) const;
209 
210 private:
211  void
212  init()
213  {
214  _ordering = nullptr;
215  }
216  void
217  deInit()
218  {
219  delete _ordering;
220  }
221 
222 private:
223  Ordering* _ordering;
224 };
225 
227 
232 extern const TypeOrdering typeOrdering;
233 
235 
243 
245 class AddressOrdering : public Ordering
246 {
249 
250 public:
251  virtual int cmp(const Object* lhs, const Object* rhs) const;
252 };
253 
255 
260 extern const AddressOrdering addressOrdering;
261 
263 // MultiKeyOrdering ////////////////////////////////////////////////////////////////////////////////
265 
308 
310 class MultiKeyOrdering : public Ordering, protected FlagsMI
311 {
313 
314 public:
315  virtual void copy(const Object& rhs);
316  virtual void serialize(Stream& stream, uint_t io, uint_t mode = ser_default);
317 
323  void addKey(uint_t fieldId, bool ascending = true);
324 
326  void
328  {
329  _numKeys = 0;
330  }
331 
333  size_t
334  numKeys() const
335  {
336  return _numKeys;
337  }
338 
340  uint_t keyId(uint_t idx) const;
341 
342  bool
343  isAscending() const
344  {
345  return _ascending;
346  }
347 
348  bool
349  isDescending() const
350  {
351  return !_ascending;
352  }
353 
355  bool isAscending(uint_t idx) const;
356 
358  bool
359  isDescending(uint_t idx) const
360  {
361  return !isAscending(idx);
362  }
363 
364 private:
365  void init();
366  void deInit();
367  size_t _numKeys;
368  Vector<uint_t>* _fieldIds;
369  BitArray* _ascendingFlags;
370  bool _ascending;
371 };
372 
374 
375 UTL_NS_END;
const NaturalOrdering naturalOrdering
Global instance of NaturalOrdering.
const KeyOrdering keyOrdering
Global instance of KeyOrdering.
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
void deInit()
De-initialize UTL++.
Object comparison abstraction.
Definition: Ordering.h:30
const TypeOrdering typeOrdering
Global instance of TypeOrdering.
int operator()(const Object *lhs, const Object *rhs) const
An alternative to utl::cmp.
Definition: Ordering.h:47
Natural object ordering.
Definition: Ordering.h:67
default representation (via getSerializeMode())
Definition: util.h:75
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
size_t numKeys() const
Get the number of keys.
Definition: Ordering.h:334
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
const InvertedNaturalOrdering invertedNaturalOrdering
Global instance of InvertedNaturalOrdering.
Inverted key-based object ordering.
Definition: Ordering.h:165
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
const AddressOrdering addressOrdering
Global instance of AddressOrdering.
void clear()
Clear all keys.
Definition: Ordering.h:327
Inverted natural object ordering.
Definition: Ordering.h:98
const InvertedKeyOrdering invertedKeyOrdering
Global instance of KeyOrdering.
#define UTL_CLASS_DECL_ABC(DC, BC)
Declaration of standard UTL++ functionality for an abstract base class (ABC).
Definition: macros.h:650
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
bool isDescending(uint_t idx) const
Get the negation of the ascending flag for the given sort key.
Definition: Ordering.h:359
Stream I/O abstraction.
Definition: Stream.h:68
Abstract base for multi-key orderings.
Definition: Ordering.h:310
A sequence of same-type objects.
Definition: Ordering.h:14
Address-based object ordering.
Definition: Ordering.h:245
Root of UTL++ class hierarchy.
Definition: Object.h:52
void init()
Initialize UTL++.
Key-based object ordering.
Definition: Ordering.h:132
Array of n-bit values.
Definition: BitArray.h:36
Order objects by type name first, and use the given ordering to compare objects of the same type...
Definition: Ordering.h:198