Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Created July 26, 2017 23:20
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 justinmeiners/ac1617b58d1853286c12641f5ca2d8df to your computer and use it in GitHub Desktop.
Save justinmeiners/ac1617b58d1853286c12641f5ca2d8df to your computer and use it in GitHub Desktop.
#include <iostream>
template <typename T>
struct vector
{
constexpr static int DEFAULT_SIZE = 16;
vector()
{
size = 0;
capacity = 0;
}
void append(const T& x)
{
++size;
if (!buffer)
{
capacity = DEFAULT_SIZE;
buffer = (T*)malloc(sizeof(T) * capacity);
}
if (size > capacity || !buffer)
{
capacity *= 2;
buffer = (T*)realloc(buffer, sizeof(T) * capacity);
}
buffer[size - 1] = x;
}
T* buffer;
size_t size;
size_t capacity;
};
int main(int argc, const char* argv[])
{
vector<float> v;
for (int i = 0; i < 100000; ++i)
{
v.append(i / 2.0f);
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment