Quark  0.1
Algorithms.h++
Go to the documentation of this file.
1 #ifndef __libquark_util_Algorithms_hxx
2 #define __libquark_util_Algorithms_hxx
3 
4 namespace quark {
5 namespace util {
6 
11 {
12  public:
13 
28  template<typename ITER, typename T, typename LESSTHAN>
29  static int orderedInsertPosition(ITER begin, ITER end, const T &item,
30  LESSTHAN lessThan = defaultLessThan)
31  {
32  if(begin == end) return 0;
33 
34  ITER left = begin;
35  ITER right = end - 1;
36  ITER offset = begin;
37 
38  while(left < right)
39  {
40  int half = (right - left) >> 1;
41  ITER mid = left + half;
42 
43  if(lessThan(*mid, item))
44  {
45  offset = left = mid;
46  ++offset;
47  ++left;
48  }
49  else if(lessThan(item, *mid))
50  right = mid;
51  else
52  {
53  offset = mid;
54  break;
55  }
56  }
57 
58  if(lessThan(*offset, item))
59  ++offset;
60 
61  return(offset - begin);
62  }
63 
64  private:
65 
66  template<typename T> static bool defaultLessThan(const T &a, const T &b)
67  { return(a < b); }
68 
69  Algorithms();
70 
71  Q_DISABLE_COPY(Algorithms);
72 };
73 
74 } // namespace util
75 } // namespace quark
76 
77 #endif // __libquark_util_Algorithms_hxx
Definition: BarChartView.h++:6
A collection of general-purpose algorithms.
Definition: Algorithms.h++:10
static int orderedInsertPosition(ITER begin, ITER end, const T &item, LESSTHAN lessThan=defaultLessThan)
Returns the insert position for an item in a (presumably sorted) collection.
Definition: Algorithms.h++:29