From 7ead107290a29811aee2cd6d170dacec45e0d67f Mon Sep 17 00:00:00 2001 From: Michael Bebenita Date: Tue, 27 Jul 2010 23:05:13 -0700 Subject: [PATCH] array_list improvements. --- src/rt/util/array_list.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/rt/util/array_list.h b/src/rt/util/array_list.h index 04fd833f8b6..e6ce55ab792 100644 --- a/src/rt/util/array_list.h +++ b/src/rt/util/array_list.h @@ -14,20 +14,24 @@ public: ~array_list(); size_t size(); int32_t append(T value); + int32_t push(T value); + T pop(); T replace(T old_value, T new_value); int32_t index_of(T value); + bool is_empty(); T & operator[](size_t index); }; template array_list::array_list() { + _size = 0; _capacity = INITIAL_CAPACITY; _data = (T *) malloc(sizeof(T) * _capacity); } template array_list::~array_list() { - delete _data; + free(_data); } template size_t @@ -37,6 +41,11 @@ array_list::size() { template int32_t array_list::append(T value) { + return push(value); +} + +template int32_t +array_list::push(T value) { if (_size == _capacity) { _capacity = _capacity * 2; _data = (T *) realloc(_data, _capacity * sizeof(T)); @@ -45,6 +54,12 @@ array_list::append(T value) { return _size - 1; } +template T +array_list::pop() { + T value = _data[-- _size]; + return value; +} + /** * Replaces the old_value in the list with the new_value. * Returns the old_value if the replacement succeeded, or NULL otherwise. @@ -74,4 +89,9 @@ array_list::operator[](size_t index) { return _data[index]; } +template bool +array_list::is_empty() { + return _size == 0; +} + #endif /* ARRAY_LIST_H */