Created
November 18, 2014 14:49
-
-
Save vaiorabbit/6f775bb338130718947f to your computer and use it in GitHub Desktop.
std::vector-like interface for array of plain old data structure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename POD> | |
struct PODContainer | |
{ | |
POD* buf; | |
size_t cap, cnt; | |
typedef POD* iterator; | |
PODContainer(size_t capacity) : cnt(0), cap(capacity) { buf = (POD*)malloc(cap * sizeof(POD)); } | |
~PODContainer() { free(buf); } | |
size_t size() { return cnt; } | |
size_t capacity() { return cap; } | |
bool empty() { return cnt == 0; } | |
POD* begin() { return buf; } | |
POD* end() { return buf + cnt; } | |
POD& front() { return buf[0]; } | |
POD& back() { return buf[cnt-1]; } | |
POD& operator [](size_t index) { return buf[index]; } | |
void push_back(const POD& val) { if (cnt < cap) { buf[cnt++] = val; } } | |
void pop_back() { if (cnt > 0) { --cnt; } } | |
void erase(POD* first, POD* last) { | |
if (first < last) { | |
memmove( first, last, size_t(end()-last)*sizeof(POD) ); | |
cnt -= size_t(last-first); | |
} | |
} | |
void erase(POD* pos) { erase(pos, pos+1); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment