1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // ScopedMap.h -- |
---|
8 | // |
---|
9 | // Author : Aaron B. Moss |
---|
10 | // Created On : Wed Feb 1 16:55:00 2017 |
---|
11 | // Last Modified By : Aaron B. Moss |
---|
12 | // Last Modified On : Wed Feb 1 16:55:00 2017 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef _VECTORMAP_H |
---|
17 | #define _VECTORMAP_H |
---|
18 | |
---|
19 | #include <iterator> |
---|
20 | #include <utility> |
---|
21 | #include <vector> |
---|
22 | |
---|
23 | /// Maps integers from a contiguous range to T's |
---|
24 | template<typename T> |
---|
25 | class VectorMap { |
---|
26 | std::vector<T> data; |
---|
27 | |
---|
28 | public: |
---|
29 | typedef typename std::vector<T>::size_type size_type; |
---|
30 | typedef size_type key_type; |
---|
31 | typedef T mapped_type; |
---|
32 | typedef std::pair<size_type, T&> value_type; |
---|
33 | typedef std::pair<size_type, const T&> const_value_type; |
---|
34 | typedef typename std::vector<T>::difference_type difference_type; |
---|
35 | typedef const value_type& reference; |
---|
36 | typedef const const_value_type& const_reference; |
---|
37 | typedef const value_type* pointer; |
---|
38 | typedef const const_value_type* const_pointer; |
---|
39 | |
---|
40 | class iterator : public std::iterator< std::bidirectional_iterator_tag, |
---|
41 | value_type, |
---|
42 | difference_type, |
---|
43 | pointer, |
---|
44 | reference > { |
---|
45 | friend class VectorMap; |
---|
46 | friend class const_iterator; |
---|
47 | |
---|
48 | value_type data; |
---|
49 | |
---|
50 | iterator(size_type i, std::vector<T>& v) : data(i, v[i]) {} |
---|
51 | public: |
---|
52 | iterator(const iterator& that) : data(that.data) {} |
---|
53 | iterator& operator= (const iterator& that) { |
---|
54 | new(&data) value_type{ that.data }; |
---|
55 | return *this; |
---|
56 | } |
---|
57 | |
---|
58 | reference operator* () { return data; } |
---|
59 | pointer operator-> () { return &data; } |
---|
60 | |
---|
61 | iterator& operator++ () { |
---|
62 | // SHENANIGANS: relies on pair<unsigned, T&> having a trivial destructor and |
---|
63 | // vector<T> having contiguous layout |
---|
64 | new(&data) value_type{ (data.first + 1), *(&data.second + 1) }; |
---|
65 | return *this; |
---|
66 | } |
---|
67 | iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } |
---|
68 | |
---|
69 | iterator& operator-- () { |
---|
70 | // SHENANIGANS: same reasons as operator++ |
---|
71 | new(&data) value_type{ (data.first - 1), *(&data.second - 1) }; |
---|
72 | return *this; |
---|
73 | } |
---|
74 | iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; } |
---|
75 | |
---|
76 | bool operator== (const iterator& o) const { |
---|
77 | return data.first == o.data.first && &data.second == &o.data.second; |
---|
78 | } |
---|
79 | bool operator!= (const iterator& that) const { return !(*this == that); } |
---|
80 | }; |
---|
81 | |
---|
82 | class const_iterator : public std::iterator< std::bidirectional_iterator_tag, |
---|
83 | const_value_type, |
---|
84 | difference_type, |
---|
85 | const_pointer, |
---|
86 | const_reference > { |
---|
87 | friend class VectorMap; |
---|
88 | const_value_type data; |
---|
89 | |
---|
90 | const_iterator(size_type i, const std::vector<T>& v) : data(i, v[i]) {} |
---|
91 | public: |
---|
92 | const_iterator(const iterator& that) : data(that.data) {} |
---|
93 | const_iterator(const const_iterator& that) : data(that.data) {} |
---|
94 | const_iterator& operator= (const iterator& that) { |
---|
95 | new(&data) const_value_type{ that.data }; |
---|
96 | return *this; |
---|
97 | } |
---|
98 | const_iterator& operator= (const const_iterator& that) { |
---|
99 | new(&data) const_value_type{ that.data }; |
---|
100 | return *this; |
---|
101 | } |
---|
102 | |
---|
103 | const_reference operator* () { return data; } |
---|
104 | const_pointer operator-> () { return &data; } |
---|
105 | |
---|
106 | const_iterator& operator++ () { |
---|
107 | // SHENANIGANS: same reasons as iterator::operator++ |
---|
108 | new(&data) const_value_type{ (data.first + 1), *(&data.second + 1) }; |
---|
109 | return *this; |
---|
110 | } |
---|
111 | const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } |
---|
112 | |
---|
113 | const_iterator& operator-- () { |
---|
114 | // SHENANIGANS: same reasons as iterator::operator++ |
---|
115 | new(&data) const_value_type{ (data.first - 1), *(&data.second - 1) }; |
---|
116 | return *this; |
---|
117 | } |
---|
118 | const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } |
---|
119 | |
---|
120 | bool operator== (const const_iterator& o) const { |
---|
121 | return data.first == o.data.first && &data.second == &o.data.second; |
---|
122 | } |
---|
123 | bool operator!= (const const_iterator& that) const { return !(*this == that); } |
---|
124 | }; |
---|
125 | |
---|
126 | /// Reserve space for n elements |
---|
127 | void reserve(size_type n) { |
---|
128 | if ( n > data.size() ) { data.insert( data.end(), n - data.size(), T{} ); } |
---|
129 | } |
---|
130 | |
---|
131 | /// Unsafe access; no bounds checking |
---|
132 | T& operator[] (size_type i) { return data[i]; } |
---|
133 | const T& operator[] (size_type i) const { return data[i]; } |
---|
134 | |
---|
135 | /// Safe access; will insert new values if needed |
---|
136 | T& at(size_type i) { |
---|
137 | reserve(i+1); |
---|
138 | return data[i]; |
---|
139 | } |
---|
140 | |
---|
141 | /// Number of stored values |
---|
142 | unsigned size() const { return data.size(); } |
---|
143 | |
---|
144 | /// No stored values |
---|
145 | bool empty() const { return data.empty(); } |
---|
146 | |
---|
147 | /// Empties the map |
---|
148 | void clear() { data.clear(); } |
---|
149 | |
---|
150 | /// Returns 1 if element in map, 0 otherwise |
---|
151 | size_type count( size_type i ) const { return i < size() ? 1 : 0; } |
---|
152 | |
---|
153 | iterator begin() { return iterator{ 0, data }; } |
---|
154 | const_iterator begin() const { return const_iterator{ 0, data }; } |
---|
155 | const_iterator cbegin() const { return const_iterator{ 0, data }; } |
---|
156 | |
---|
157 | iterator end() { return iterator{ data.size(), data }; } |
---|
158 | const_iterator end() const { return const_iterator{ data.size(), data }; } |
---|
159 | const_iterator cend() const { return const_iterator{ data.size(), data }; } |
---|
160 | |
---|
161 | iterator find( size_type i ) { return i < size() ? iterator{ i, data } : end(); } |
---|
162 | const_iterator find( size_type i ) const { return i < size() ? const_iterator{ i, data } : end(); } |
---|
163 | }; |
---|
164 | |
---|
165 | #endif // _VECTORMAP_H |
---|
166 | |
---|
167 | // Local Variables: // |
---|
168 | // tab-width: 4 // |
---|
169 | // mode: c++ // |
---|
170 | // compile-command: "make install" // |
---|
171 | // End: // |
---|