From 54cee059a533433091a4b65cf47b821bcbb0840c Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 4 Jul 2009 02:10:59 +0000 Subject: Use raw memory instead of std::vector. git-svn-id: http://svn.drobilla.net/resp/tuplr@180 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- src/tuplr.hpp | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/tuplr.hpp b/src/tuplr.hpp index aaa0867..57481bb 100644 --- a/src/tuplr.hpp +++ b/src/tuplr.hpp @@ -23,6 +23,7 @@ #define TUPLR_HPP #include +#include #include #include #include @@ -278,27 +279,35 @@ private: }; /// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)" -struct ATuple : public AST, private vector { - ATuple(Cursor c) : AST(c) {} - ATuple(Cursor c, const ATuple& t) : AST(c), vector(t) {} - ATuple(Cursor c, AST* ast, va_list args) : AST(c) { +struct ATuple : public AST { + ATuple(Cursor c) : AST(c), _len(0), _vec(0) {} + ATuple(Cursor c, const ATuple& t) : AST(c), _len(t._len) { + _vec = (AST**)malloc(sizeof(AST*) * _len); + memcpy(_vec, t._vec, sizeof(AST*) * _len); + } + ATuple(Cursor c, AST* ast, va_list args) : AST(c), _len(0), _vec(0) { if (!ast) return; push_back(ast); for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*)) push_back(a); } - void push_back(AST* ast) { vector::push_back(ast); } - const AST* at(size_t i) const { return vector::at(i); } - AST*& at(size_t i) { return vector::at(i); } - size_t size() const { return vector::size(); } - bool empty() const { return vector::empty(); } - - typedef vector::iterator iterator; - typedef vector::const_iterator const_iterator; - const_iterator begin() const { return vector::begin(); } - iterator begin() { return vector::begin(); } - const_iterator end() const { return vector::end(); } - iterator end() { return vector::end(); } + ~ATuple() { free(_vec); } + void push_back(AST* ast) { + AST** newvec = (AST**)realloc(_vec, sizeof(AST*) * (_len + 1)); + newvec[_len++] = ast; + _vec = newvec; + } + const AST* at(size_t i) const { assert(i < _len); return _vec[i]; } + AST*& at(size_t i) { assert(i < _len); return _vec[i]; } + size_t size() const { return _len; } + bool empty() const { return _len == 0; } + + typedef AST** iterator; + typedef AST* const * const_iterator; + const_iterator begin() const { return _vec; } + iterator begin() { return _vec; } + const_iterator end() const { return _vec + _len; } + iterator end() { return _vec + _len; } bool value() const { return false; } bool operator==(const AST& rhs) const { const ATuple* rt = rhs.to(); @@ -320,6 +329,10 @@ struct ATuple : public AST, private vector { void lift(CEnv& cenv) { FOREACH(iterator, t, *this) (*t)->lift(cenv); } CValue compile(CEnv& cenv) { throw Error(loc, "tuple compiled"); } + +private: + size_t _len; + AST** _vec; }; /// Type Expression, e.g. "Int", "(Fn (Int Int) Float)" -- cgit v1.2.1