aboutsummaryrefslogtreecommitdiffstats
path: root/src/resp.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2010-12-27 19:20:23 +0000
committerDavid Robillard <d@drobilla.net>2010-12-27 19:20:23 +0000
commit248a2c44e8f8e599d64640cff584a2ef5265b9d9 (patch)
tree94b00f5b858455f3617750d6db623ffd2d859817 /src/resp.hpp
parent0b014dee824646461b7d402bf9bbcf954ff0eba3 (diff)
downloadresp-248a2c44e8f8e599d64640cff584a2ef5265b9d9.tar.gz
resp-248a2c44e8f8e599d64640cff584a2ef5265b9d9.tar.bz2
resp-248a2c44e8f8e599d64640cff584a2ef5265b9d9.zip
Make ATuple a normal LISPey cons cell (it always was anyway).
git-svn-id: http://svn.drobilla.net/resp/resp@360 ad02d1e2-f140-0410-9f75-f8b11f17cedd
Diffstat (limited to 'src/resp.hpp')
-rw-r--r--src/resp.hpp54
1 files changed, 22 insertions, 32 deletions
diff --git a/src/resp.hpp b/src/resp.hpp
index f601ac7..56c24a6 100644
--- a/src/resp.hpp
+++ b/src/resp.hpp
@@ -214,23 +214,15 @@ private:
/// Tuple (heterogeneous sequence of fixed length), e.g. "(a b c)"
struct ATuple : public AST {
- explicit ATuple(Cursor c) : AST(T_TUPLE, c), _len(0), _vec(0) {}
- ATuple(const ATuple& exp) : AST(T_TUPLE, exp.loc), _len(exp._len) {
- _vec = (const AST**)malloc(sizeof(AST*) * _len);
- memcpy(_vec, exp._vec, sizeof(AST*) * _len);
- }
- ATuple(const AST* first, const AST* rest, Cursor c=Cursor()) : AST(T_TUPLE, c), _len(2) {
- _vec = (const AST**)malloc(sizeof(AST*) * _len);
- _vec[0] = first;
- _vec[1] = rest;
- }
- ATuple(Cursor c, AST* ast, va_list args) : AST(T_TUPLE, c), _len(0), _vec(0) {
- if (!ast) return;
+ explicit ATuple(Cursor c) : AST(T_TUPLE, c), _fst(0), _rst(0) {}
+ ATuple(const ATuple& exp) : AST(T_TUPLE, exp.loc), _fst(exp._fst), _rst(exp._rst) {}
- _len = 2;
- _vec = (const AST**)malloc(sizeof(AST*) * _len);
- _vec[0] = ast;
- _vec[1] = NULL;
+ ATuple(const AST* fst, const ATuple* rst, Cursor c=Cursor())
+ : AST(T_TUPLE, c), _fst(fst), _rst(rst) {}
+
+ ATuple(Cursor c, AST* ast, va_list args)
+ : AST(T_TUPLE, c), _fst(ast), _rst(0) {
+ if (!ast) return;
ATuple* tail = this;
for (AST* a = va_arg(args, AST*); a; a = va_arg(args, AST*)) {
@@ -240,11 +232,10 @@ struct ATuple : public AST {
}
}
- const AST* head() const { assert(_len > 0); return _vec[0]; }
- const AST* last() const { return _vec[_len - 1]; }
- bool empty() const { return _len == 0; }
+ const AST* head() const { return _fst; }
+ const AST* last() const { return _rst; }
+ bool empty() const { return _fst == 0 && _rst ==0; }
- size_t tup_len() const { return _len; }
size_t list_len() const {
size_t ret = 0;
for (const_iterator i = begin(); i != end(); ++i, ++ret) {}
@@ -265,14 +256,10 @@ struct ATuple : public AST {
return NULL;
}
- void last(AST* ast) { _vec[_len - 1] = ast; }
+ void last(ATuple* ast) { _rst = ast; }
struct const_iterator {
- explicit const_iterator(const ATuple* n) : node(n) {
- assert(!n || n->tup_len() == 0 || n->tup_len() == 2);
- if (!n || n->tup_len() == 0)
- node = NULL;
- }
+ explicit const_iterator(const ATuple* n) : node(n) {}
inline void increment() {
if (node->last())
node = node->last()->as_tuple();
@@ -291,7 +278,7 @@ struct ATuple : public AST {
return ret;
}
inline bool operator==(const const_iterator& i) const {
- return node == i.node || (!node && i.node->tup_len() == 0);
+ return node == i.node;
}
inline bool operator!=(const const_iterator& i) const {
return !operator==(i);
@@ -300,7 +287,7 @@ struct ATuple : public AST {
const ATuple* node;
};
- const_iterator begin() const { assert(_len == 0 || _len == 2); return const_iterator(this); }
+ const_iterator begin() const { if (empty()) return end(); return const_iterator(this); }
const_iterator end() const { return const_iterator(NULL); }
const_iterator iter_at(unsigned index) const {
@@ -316,9 +303,8 @@ struct ATuple : public AST {
const ATuple* prot() const { return list_ref(1)->as_tuple(); }
private:
- friend class GC;
- size_t _len;
- const AST** _vec;
+ const AST* _fst;
+ const ATuple* _rst;
};
inline ATuple* tup(Cursor c, AST* ast, ...) {
@@ -352,7 +338,11 @@ list_contains(const ATuple* head, const AST* child) {
inline bool
list_equals(const ATuple* lhs, const ATuple* rhs)
{
- if (!rhs || rhs->tup_len() != lhs->tup_len()) return false;
+ if (lhs == rhs)
+ return true;
+ else if (!lhs || !rhs)
+ return false;
+
ATuple::const_iterator l = lhs->begin();
FOREACHP(ATuple::const_iterator, r, rhs)
if (!(*(*l++) == *(*r)))