From d8c26a61aef379c9ad43d4f5cc101ee1ed22440b Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 15 Oct 2002 15:32:32 +0000 Subject: added insert_at method to List template svn-id: r5153 --- common/list.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'common/list.h') diff --git a/common/list.h b/common/list.h index 87e8b5ab34..f5f3e4967a 100644 --- a/common/list.h +++ b/common/list.h @@ -49,12 +49,27 @@ public: delete [] _data; } - void push_back(const T& str) + void push_back(const T& element) { ensureCapacity(_size + 1); - _data[_size++] = str; + _data[_size++] = element; } + void insert_at(int idx, const T& element) + { + assert(idx >= 0 && idx <= _size); + ensureCapacity(_size + 1); + // The following loop is not efficient if you can just memcpy things around. + // e.g. if you have a list of ints. But for real objects (String...), memcpy + // is *not* usually a good idea! + for (int i = _size; i > idx; i--) { + _data[i] = _data[i-1]; + } + _data[idx] = element; + _size++; + } + + // TODO: insert, remove, ... T& operator [](int idx) -- cgit v1.2.3