Changeset 6e93323 in mainline for uspace/lib/cpp/include/impl/deque.hpp
- Timestamp:
- 2018-07-05T21:41:20Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 2215fd0
- Parents:
- 3a06cc6
- git-author:
- Dzejrou <dzejrou@…> (2018-04-11 14:42:31)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:20)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/impl/deque.hpp
r3a06cc6 r6e93323 41 41 namespace aux 42 42 { 43 /** 44 * Note: We decided that these iterators contain a 45 * reference to the container and an index, which 46 * allows us to use the already implemented operator[] 47 * on deque and also allows us to conform to the requirement 48 * of the standard that functions such as push_back 49 * invalidate the .end() iterator. 50 */ 51 52 template<class T, class Allocator> 53 class deque_const_iterator 54 { 55 public: 56 using size_type = typename deque<T, Allocator>::size_type; 57 using value_type = typename deque<T, Allocator>::value_type; 58 using reference = typename deque<T, Allocator>::const_reference; 59 using difference_type = size_type; 60 using pointer = const value_type*; 61 using iterator_category = random_access_iterator_tag; 62 63 deque_const_iterator(deque<T, Allocator>& deq, size_type idx) 64 : deq_{deq}, idx_{idx} 65 { /* DUMMY BODY */ } 66 67 deque_const_iterator& operator=(const deque_const_iterator& other) 68 { 69 deq_ = other.deq_; 70 idx_ = other.idx_; 71 72 return *this; 73 } 74 75 reference operator*() const 76 { 77 return deq_[idx_]; 78 } 79 80 pointer operator->() const 81 { 82 return addressof(deq_[idx_]); 83 } 84 85 deque_const_iterator& operator++() 86 { 87 ++idx_; 88 89 return *this; 90 } 91 92 deque_const_iterator operator++(int) 93 { 94 return deque_const_iterator{deq_, idx_++}; 95 } 96 97 deque_const_iterator& operator--() 98 { 99 --idx_; 100 101 return *this; 102 } 103 104 deque_const_iterator operator--(int) 105 { 106 return deque_const_iterator{deq_, idx_--}; 107 } 108 109 deque_const_iterator operator+(difference_type n) 110 { 111 return deque_const_iterator{deq_, idx_ + n}; 112 } 113 114 deque_const_iterator& operator+=(difference_type n) 115 { 116 idx_ += n; 117 118 return *this; 119 } 120 121 deque_const_iterator operator-(difference_type n) 122 { 123 return deque_const_iterator{deq_, idx_ - n}; 124 } 125 126 deque_const_iterator& operator-=(difference_type n) 127 { 128 idx_ -= n; 129 130 return *this; 131 } 132 133 reference operator[](difference_type n) const 134 { 135 return deq_[idx_ + n]; 136 } 137 138 size_type idx() const 139 { 140 return idx_; 141 } 142 143 private: 144 deque<T, Allocator>& deq_; 145 size_type idx_; 146 }; 147 148 template<class T, class Allocator> 149 bool operator==(const deque_const_iterator<T, Allocator>& lhs, 150 const deque_const_iterator<T, Allocator>& rhs) 151 { 152 return lhs.idx() == rhs.idx(); 153 } 154 155 template<class T, class Allocator> 156 bool operator!=(const deque_const_iterator<T, Allocator>& lhs, 157 const deque_const_iterator<T, Allocator>& rhs) 158 { 159 return !(lhs == rhs); 160 } 161 43 162 template<class T, class Allocator> 44 163 class deque_iterator 45 164 { 46 using size_type = typename std::deque<T, Allocator>::size_type;47 using value_type = typename std::deque<T, Allocator>::value_type;48 using difference_type = size_type;49 50 165 public: 51 deque_iterator(deque<T, Allocator>* deq, size_type idx) 166 using size_type = typename deque<T, Allocator>::size_type; 167 using value_type = typename deque<T, Allocator>::value_type; 168 using reference = typename deque<T, Allocator>::reference; 169 using difference_type = size_type; 170 using pointer = value_type*; 171 using iterator_category = random_access_iterator_tag; 172 173 deque_iterator(deque<T, Allocator>& deq, size_type idx) 52 174 : deq_{deq}, idx_{idx} 53 175 { /* DUMMY BODY */ } 54 176 177 deque_iterator(const deque_const_iterator<T, Allocator>& other) 178 : deq_{other.deq_}, idx_{other.idx_} 179 { /* DUMMY BODY */ } 180 181 deque_iterator& operator=(const deque_iterator& other) 182 { 183 deq_ = other.deq_; 184 idx_ = other.idx_; 185 186 return *this; 187 } 188 189 deque_iterator& operator=(const deque_const_iterator<T, Allocator>& other) 190 { 191 deq_ = other.deq_; 192 idx_ = other.idx_; 193 194 return *this; 195 } 196 197 reference operator*() 198 { 199 return deq_[idx_]; 200 } 201 202 pointer operator->() 203 { 204 return addressof(deq_[idx_]); 205 } 206 207 deque_iterator& operator++() 208 { 209 ++idx_; 210 211 return *this; 212 } 213 214 deque_iterator operator++(int) 215 { 216 return deque_iterator{deq_, idx_++}; 217 } 218 219 deque_iterator& operator--() 220 { 221 --idx_; 222 223 return *this; 224 } 225 226 deque_iterator operator--(int) 227 { 228 return deque_iterator{deq_, idx_--}; 229 } 230 231 deque_iterator operator+(difference_type n) 232 { 233 return deque_iterator{deq_, idx_ + n}; 234 } 235 236 deque_iterator& operator+=(difference_type n) 237 { 238 idx_ += n; 239 240 return *this; 241 } 242 243 deque_iterator operator-(difference_type n) 244 { 245 return deque_iterator{deq_, idx_ - n}; 246 } 247 248 deque_iterator& operator-=(difference_type n) 249 { 250 idx_ -= n; 251 252 return *this; 253 } 254 255 reference operator[](difference_type n) const 256 { 257 return deq_[idx_ + n]; 258 } 259 260 size_type idx() const 261 { 262 return idx_; 263 } 264 55 265 private: 56 deque<T, Allocator> *deq_;266 deque<T, Allocator>& deq_; 57 267 size_type idx_; 58 268 }; 59 269 60 270 template<class T, class Allocator> 61 class deque_const_iterator 271 bool operator==(const deque_iterator<T, Allocator>& lhs, 272 const deque_iterator<T, Allocator>& rhs) 62 273 { 63 // TODO: implement 64 }; 274 return lhs.idx() == rhs.idx(); 275 } 276 277 template<class T, class Allocator> 278 bool operator!=(const deque_iterator<T, Allocator>& lhs, 279 const deque_iterator<T, Allocator>& rhs) 280 { 281 return !(lhs == rhs); 282 } 65 283 } 66 284 … … 190 408 iterator begin() noexcept 191 409 { 192 // TODO: implement410 return aux::deque_iterator{*this, 0}; 193 411 } 194 412 195 413 const_iterator begin() const noexcept 196 414 { 197 // TODO: implement415 return aux::deque_const_iterator{*this, 0}; 198 416 } 199 417 200 418 iterator end() noexcept 201 419 { 202 // TODO: implement420 return aux::deque_iterator{*this, size_}; 203 421 } 204 422 205 423 const_iterator end() const noexcept 206 424 { 207 // TODO: implement425 return aux::deque_const_iterator{*this, size_}; 208 426 } 209 427 210 428 reverse_iterator rbegin() noexcept 211 429 { 212 // TODO: implement430 return make_reverse_iterator(end()); 213 431 } 214 432 215 433 const_reverse_iterator rbegin() const noexcept 216 434 { 217 // TODO: implement435 return make_reverse_iterator(cend()); 218 436 } 219 437 220 438 reverse_iterator rend() noexcept 221 439 { 222 // TODO: implement440 return make_reverse_iterator(begin()); 223 441 } 224 442 225 443 const_reverse_iterator rend() const noexcept 226 444 { 227 // TODO: implement445 return make_reverse_iterator(cbegin()); 228 446 } 229 447 230 448 const_iterator cbegin() const noexcept 231 449 { 232 // TODO: implement450 return aux::deque_const_iterator{*this, 0}; 233 451 } 234 452 235 453 const_iterator cend() const noexcept 236 454 { 237 // TODO: implement455 return aux::deque_const_iterator{*this, size_}; 238 456 } 239 457 240 458 const_reverse_iterator crbegin() const noexcept 241 459 { 242 // TODO: implement460 return make_reverse_iterator(cend()); 243 461 } 244 462 245 463 const_reverse_iterator crend() const noexcept 246 464 { 247 // TODO: implement465 return make_reverse_iterator(cbegin()); 248 466 } 249 467
Note:
See TracChangeset
for help on using the changeset viewer.