The check to see if an item was already on the heap was randomly hitting.
[jitcs.git] / include / jitcs_adt_range.h
blobea42603090557275e8c1fab93d364a830f39707f
1 //===-- jitcs_adt_slice.h ---------------------------------------*- C++ -*-===//
2 //
3 //
4 //===----------------------------------------------------------------------===//
6 #ifndef _JITCS_ADT_RANGE_H_
7 #define _JITCS_ADT_RANGE_H_
9 #include "jitcs_base.h"
10 #include "jitcs_typefuncs.h"
12 namespace jitcs {
13 // simple range for array-like slices
14 // instead of keeping a size around, it is only using
15 // a start and end pointer, decreasing the number of
16 // changed memory positions per traversal step
17 // size calculation is more costly though
19 template <typename T> struct OpDerefType {
20 typedef typename T::value_type value_type;
22 template <typename T> struct OpDerefType<T*> {
23 typedef T value_type;
27 template <typename T>
28 struct Range {
29 public:
30 typedef typename OpDerefType<T>::value_type value_type;
31 public:
32 T itStart;
33 T itEnd;
35 public:
36 Range() = delete;
37 Range(const Range&) = default;
38 Range& operator =(const Range&) = default;
40 Range(T start, T end) : itStart(start), itEnd(end) {}
42 public:
43 // range interface
44 bool isEmpty() const { return itStart == itEnd; }
46 value_type& getFront() { _JITCS_DBG_CHECK_(!isEmpty()); return *itStart; }
47 value_type const& getFront() const { _JITCS_DBG_CHECK_(!isEmpty()); return *itStart; }
48 value_type& getBack() { _JITCS_DBG_CHECK_(!isEmpty()); T it = itEnd; --it; return *it; }
49 value_type const& getBack() const { _JITCS_DBG_CHECK_(!isEmpty()); T it = itEnd; --it; return *it; }
51 void popFront() { _JITCS_DBG_CHECK_(!isEmpty()); ++itStart; }
52 void popBack() { _JITCS_DBG_CHECK_(!isEmpty()); --itEnd; }
54 bool operator !() const { return isEmpty(); }
55 value_type& operator *() { return getFront(); }
56 value_type const& operator *() const { return getFront(); }
57 value_type* operator ->() { return &getFront(); }
58 value_type const* operator ->() const { return &getFront(); }
59 void operator ++() { popFront(); }
62 template <typename T>
63 struct ConstRange {
64 public:
65 typedef typename OpDerefType<T>::value_type value_type;
66 public:
67 T itStart;
68 T itEnd;
70 public:
71 ConstRange() = delete;
72 ConstRange(const ConstRange&) = default;
73 ConstRange& operator =(const ConstRange&) = default;
75 ConstRange(T start, T end)
76 : itStart(start)
77 , itEnd(end) {
80 public:
81 // range interface
82 bool isEmpty() const { return itStart == itEnd; }
84 value_type const& getFront() const { _JITCS_DBG_CHECK_(!isEmpty()); return *itStart; }
85 value_type const& getBack() const { _JITCS_DBG_CHECK_(!isEmpty()); T it = itEnd; --it; return *it; }
87 void popFront() { _JITCS_DBG_CHECK_(!isEmpty()); ++itStart; }
88 void popBack() { _JITCS_DBG_CHECK_(!isEmpty()); --itEnd; }
90 bool operator !() const { return isEmpty(); }
91 value_type const& operator *() const { return getFront(); }
92 value_type const* operator ->() const { return &getFront(); }
93 void operator ++() { popFront(); }
96 template <typename U>
97 inline Range<typename U::const_iterator> getRange(U const& u) {
98 return Range<typename U::const_iterator>(u.begin(), u.end());
100 template <typename U>
101 inline ConstRange<typename U::const_iterator> getConstRange(U const& u) {
102 return ConstRange<typename U::const_iterator>(u.begin(), u.end());
104 template <typename U>
105 inline Range<typename U::iterator> getRange(U& u) {
106 return Range<typename U::iterator>(u.begin(), u.end());
108 } // end of namespace jitcs
110 #endif
111 // _JITCS_ADT_RANGE_H_