The check to see if an item was already on the heap was randomly hitting.
[jitcs.git] / include / jitcs_adt_slice.h
blob33c04212ee170d64ecabba660dd7b6aafba47ab3
1 //===-- jitcs_adt_slice.h ---------------------------------------*- C++ -*-===//
2 //
3 //
4 //===----------------------------------------------------------------------===//
6 #ifndef _JITCS_ADT_SLICE_H_
7 #define _JITCS_ADT_SLICE_H_
9 #include "jitcs_base.h"
10 #include "jitcs_adt_ref.h"
12 namespace jitcs {
14 template <typename T, unsigned FLAGS>
15 struct _Slice {
16 typedef typename std::add_const<T>::type CType;
17 typedef T NCType;
18 static const bool IsConstRef = (FLAGS & REF_Const) != 0;
19 typedef typename std::conditional<IsConstRef, CType, NCType>::type Type;
20 typedef Type* PtrType;
21 typedef Type& RefType;
23 typedef Type* iterator;
24 typedef Type const* const_iterator;
27 _Slice() {
28 _ptr = nullptr;
29 _size = 0;
31 _Slice(const _Slice&) = default;
32 _Slice& operator = (const _Slice&) = default;
33 // RefType operator *() {
34 // guarantee<(FLAGS & REF_Nullable) == 0>
35 // ("dereferencing only for non-nullable refs, remove nullable first");
36 // return *ptr();
37 // }
39 template <typename T2, unsigned FLAGS2>
40 _Slice(_Slice<T2, FLAGS2> o) {
41 static_assert((~FLAGS & FLAGS2) == 0, "incompatible _Slice flags");
42 //static_assert(std::is_base_of<T, T2>::value, "incompatible _Slice types");
43 _ptr = o._ptr;
45 template <typename T2>
46 _Slice(T2* o, size_t n) {
47 typedef typename std::remove_const<T2>::type T2B;
48 //static_assert(std::is_base_of<T, T2B>::value, "incompatible _Slice types");
49 //if ((FLAGS & REF_Nullable) == 0)
50 // assert(o != nullptr);
51 _ptr = o;
52 _size = n;
55 template <typename T2, unsigned FLAGS2>
56 _Slice& operator =(_Slice<T2, FLAGS2> o) {
57 static_assert((~FLAGS & FLAGS2) == 0, "incompatible _Slice flags");
58 //static_assert(std::is_base_of<T, T2>::value, "incompatible _Ref types");
59 _ptr = o._ptr;
60 _size = o._size;
63 // Type* ptr() { return _ptr; }
64 // Type const* ptr() const { return _ptr; }
65 PtrType ptr() const { return _ptr; }
66 size_t size() const { return _size; }
68 bool empty() const { return _size == 0; }
69 bool isEmpty() const { return _size == 0; }
71 bool isValidIndex(size_t r) const { return r >= 0 && r < size(); }
73 RefType operator [](size_t i) const {
74 assert(isValidIndex(i));
75 return _ptr[i];
77 void popFront(size_t n) {
78 _JITCS_DBG_CHECK_(n <= _size);
79 _ptr += n;
80 _size -= n;
82 void popBack(size_t n) {
83 _JITCS_DBG_CHECK_(n <= _size);
84 _size -= n;
86 void fill(const NCType& v) {
87 guarantee<!IsConstRef>("mutation disabled on const container");
88 for (size_t i = 0, n = _size; i < n; ++i)
89 _ptr[i] = v;
92 iterator begin() { return _ptr; }
93 iterator end() { return _ptr + _size; }
94 const_iterator begin() const { return _ptr; }
95 const_iterator end() const { return _ptr + _size; }
96 const_iterator cbegin() const { return _ptr; }
97 const_iterator cend() const { return _ptr + _size; }
99 PtrType _ptr;
100 size_t _size;
103 template <typename T>
104 using Slice = _Slice<typename std::remove_const<T>::type,
105 (std::is_const<T>::value ? REF_Const : REF_Any)>;
107 } // End jitcs namespace
109 #endif
110 // _JITCS_ADT_SLICE_H_