1 //===-- jitcs_adt_slice.h ---------------------------------------*- C++ -*-===//
4 //===----------------------------------------------------------------------===//
6 #ifndef _JITCS_ADT_RANGE_H_
7 #define _JITCS_ADT_RANGE_H_
9 #include "jitcs_base.h"
10 #include "jitcs_typefuncs.h"
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
*> {
30 typedef typename OpDerefType
<T
>::value_type value_type
;
37 Range(const Range
&) = default;
38 Range
& operator =(const Range
&) = default;
40 Range(T start
, T end
) : itStart(start
), itEnd(end
) {}
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(); }
65 typedef typename OpDerefType
<T
>::value_type value_type
;
71 ConstRange() = delete;
72 ConstRange(const ConstRange
&) = default;
73 ConstRange
& operator =(const ConstRange
&) = default;
75 ConstRange(T start
, T end
)
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(); }
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
111 // _JITCS_ADT_RANGE_H_