diff --git a/src/runtime/c/pgf/vector.h b/src/runtime/c/pgf/vector.h index 48c17c435..cd999f01c 100644 --- a/src/runtime/c/pgf/vector.h +++ b/src/runtime/c/pgf/vector.h @@ -40,4 +40,55 @@ A *vector_elem(Vector *v, size_t index) return &v->data[index]; } +template class PGF_INTERNAL_DECL vector { +private: + object offset; + + struct V { + size_t size; + A data[]; + }; + + V* v() const { return (V*) (current_base+offset); } + + vector(object o) { + this->offset = o; + } + +public: + class iterator { + public: + object offset; + + iterator(ref r) { + this->offset = r.as_object(); + } + + bool operator!=(iterator other) { + return offset != other.offset; + } + + void operator++() { + offset += sizeof(A); + } + + A &operator*() { + return *((A*) (current_base+offset)); + } + }; + + size_t size() { return v()->size; }; + A &operator[] (size_t i) { return v()->data[i]; }; + iterator begin() { return iterator(ref::from_ptr(&v()->data[0])); } + iterator end() { return iterator(ref::from_ptr(&v()->data[v()->size])); } + + static vector alloc(size_t size) + { + auto res = PgfDB::malloc::V>(size*sizeof(A)); + res->size = size; + return vector(res.as_object()); + } +}; + + #endif // VECTOR_H