Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created November 18, 2014 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaiorabbit/6f775bb338130718947f to your computer and use it in GitHub Desktop.
Save vaiorabbit/6f775bb338130718947f to your computer and use it in GitHub Desktop.
std::vector-like interface for array of plain old data structure.
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