Skip to content

Commit

Permalink
marl::containers: Add const methods
Browse files Browse the repository at this point in the history
Add const flavors of `front()`, `back()`, `begin()`, `end()`.
  • Loading branch information
ben-clayton committed Jun 5, 2020
1 parent 11f31bf commit 49fe9a1
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/marl/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ class vector {
inline void pop_back();
inline T& front();
inline T& back();
inline const T& front() const;
inline const T& back() const;
inline T* begin();
inline T* end();
inline const T* begin() const;
inline const T* end() const;
inline T& operator[](size_t i);
inline const T& operator[](size_t i) const;
inline size_t size() const;
Expand Down Expand Up @@ -186,6 +190,18 @@ T& vector<T, BASE_CAPACITY>::back() {
return reinterpret_cast<T*>(elements)[count - 1];
}

template <typename T, int BASE_CAPACITY>
const T& vector<T, BASE_CAPACITY>::front() const {
MARL_ASSERT(count > 0, "front() called on empty vector");
return reinterpret_cast<T*>(elements)[0];
}

template <typename T, int BASE_CAPACITY>
const T& vector<T, BASE_CAPACITY>::back() const {
MARL_ASSERT(count > 0, "back() called on empty vector");
return reinterpret_cast<T*>(elements)[count - 1];
}

template <typename T, int BASE_CAPACITY>
T* vector<T, BASE_CAPACITY>::begin() {
return reinterpret_cast<T*>(elements);
Expand All @@ -196,6 +212,16 @@ T* vector<T, BASE_CAPACITY>::end() {
return reinterpret_cast<T*>(elements) + count;
}

template <typename T, int BASE_CAPACITY>
const T* vector<T, BASE_CAPACITY>::begin() const {
return reinterpret_cast<T*>(elements);
}

template <typename T, int BASE_CAPACITY>
const T* vector<T, BASE_CAPACITY>::end() const {
return reinterpret_cast<T*>(elements) + count;
}

template <typename T, int BASE_CAPACITY>
T& vector<T, BASE_CAPACITY>::operator[](size_t i) {
MARL_ASSERT(i < count, "index %d exceeds vector size %d", int(i), int(count));
Expand Down

0 comments on commit 49fe9a1

Please sign in to comment.