diff options
-rw-r--r-- | backends/fs/fs.h | 4 | ||||
-rw-r--r-- | backends/wince/CEkeys/KeysBuffer.h | 1 | ||||
-rw-r--r-- | base/plugins.h | 8 | ||||
-rw-r--r-- | common/array.h | 170 | ||||
-rw-r--r-- | common/config-manager.h | 4 | ||||
-rw-r--r-- | common/list.h | 153 | ||||
-rw-r--r-- | common/str.h | 4 | ||||
-rw-r--r-- | gui/PopUpWidget.h | 4 | ||||
-rw-r--r-- | gui/TabWidget.h | 4 |
9 files changed, 251 insertions, 101 deletions
diff --git a/backends/fs/fs.h b/backends/fs/fs.h index c6b35e6a02..ba9cb6dd4c 100644 --- a/backends/fs/fs.h +++ b/backends/fs/fs.h @@ -55,7 +55,7 @@ * i.e. the root dir is usually not the best starting point for browsing. */ -#include "common/list.h" +#include "common/array.h" #include "common/str.h" class FSList; @@ -149,7 +149,7 @@ public: /** * Sorted list of multiple file system nodes. E.g. the contents of a given directory. */ -class FSList : private Common::List<FilesystemNode *> { +class FSList : private Common::Array<FilesystemNode *> { public: class const_iterator { friend class FSList; diff --git a/backends/wince/CEkeys/KeysBuffer.h b/backends/wince/CEkeys/KeysBuffer.h index b8c56109d0..05da70296a 100644 --- a/backends/wince/CEkeys/KeysBuffer.h +++ b/backends/wince/CEkeys/KeysBuffer.h @@ -25,7 +25,6 @@ #include "common/stdafx.h" #include "common/scummsys.h" #include "common/system.h" -#include "common/list.h" #include "Key.h" diff --git a/base/plugins.h b/base/plugins.h index 9fe4a287cb..5c5e94559f 100644 --- a/base/plugins.h +++ b/base/plugins.h @@ -23,7 +23,7 @@ #ifndef COMMON_PLUGINS_H #define COMMON_PLUGINS_H -#include "common/list.h" +#include "common/array.h" #include "common/singleton.h" #include "common/util.h" @@ -34,7 +34,7 @@ class OSystem; struct GameSettings; /** List of games. */ -typedef Common::List<GameSettings> GameList; +typedef Common::Array<GameSettings> GameList; /** * A detected game. Carries the GameSettings, but also (optionally) @@ -51,7 +51,7 @@ struct DetectedGame : GameSettings { }; /** List of detected games. */ -typedef Common::List<DetectedGame> DetectedGameList; +typedef Common::Array<DetectedGame> DetectedGameList; /** @@ -100,7 +100,7 @@ public: /** List of plugins. */ -typedef Common::List<Plugin *> PluginList; +typedef Common::Array<Plugin *> PluginList; /** diff --git a/common/array.h b/common/array.h new file mode 100644 index 0000000000..9a40a392c4 --- /dev/null +++ b/common/array.h @@ -0,0 +1,170 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2002-2004 The ScummVM project + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Header$ + */ + +#ifndef COMMON_ARRAY_H +#define COMMON_ARRAY_H + +#include "common/scummsys.h" +#include <assert.h> + +namespace Common { + +template <class T> +class Array { +protected: + int _capacity; + int _size; + T *_data; + +public: + typedef T *iterator; + typedef const T *const_iterator; + +public: + Array<T>() : _capacity(0), _size(0), _data(0) {} + Array<T>(const Array<T>& array) : _capacity(0), _size(0), _data(0) { + _size = array._size; + _capacity = _size + 32; + _data = new T[_capacity]; + for (int i = 0; i < _size; i++) + _data[i] = array._data[i]; + } + + ~Array<T>() { + if (_data) + delete [] _data; + } + + void push_back(const T& element) { + ensureCapacity(_size + 1); + _data[_size++] = element; + } + + void push_back(const Array<T>& array) { + ensureCapacity(_size + array._size); + for (int i = 0; i < array._size; i++) + _data[_size++] = array._data[i]; + } + + 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 + // usually isn't correct (specifically, for any class which has a non-default + // copy behaviour. E.g. the String class uses a refCounter which has to be + // updated whenever a String is copied. + for (int i = _size; i > idx; i--) { + _data[i] = _data[i-1]; + } + _data[idx] = element; + _size++; + } + + T& remove_at(int idx) { + T& tmp; + + assert(idx >= 0 && idx < _size); + tmp = _data[idx]; + for (int i = idx; i < _size - 1; i++) + _data[i] = _data[i+1]; + _size--; + return tmp; + } + + // TODO: insert, remove, ... + + T& operator [](int idx) { + assert(idx >= 0 && idx < _size); + return _data[idx]; + } + + const T& operator [](int idx) const { + assert(idx >= 0 && idx < _size); + return _data[idx]; + } + + Array<T>& operator =(const Array<T>& array) { + if (_data) + delete [] _data; + _size = array._size; + _capacity = _size + 32; + _data = new T[_capacity]; + for (int i = 0; i < _size; i++) + _data[i] = array._data[i]; + + return *this; + } + + uint size() const { + return _size; + } + + void clear() { + if (_data) { + delete [] _data; + _data = 0; + } + _size = 0; + _capacity = 0; + } + + bool isEmpty() const { + return (_size == 0); + } + + + iterator begin() { + return _data; + } + + iterator end() { + return _data + _size; + } + + const_iterator begin() const { + return _data; + } + + const_iterator end() const { + return _data + _size; + } + +protected: + void ensureCapacity(int new_len) { + if (new_len <= _capacity) + return; + + T *old_data = _data; + _capacity = new_len + 32; + _data = new T[_capacity]; + + if (old_data) { + // Copy old data + for (int i = 0; i < _size; i++) + _data[i] = old_data[i]; + delete [] old_data; + } + } +}; + +} // End of namespace Common + +#endif diff --git a/common/config-manager.h b/common/config-manager.h index 7f01ad01e9..2488e40696 100644 --- a/common/config-manager.h +++ b/common/config-manager.h @@ -23,7 +23,7 @@ #ifndef COMMON_CONFIG_H #define COMMON_CONFIG_H -#include "common/list.h" +#include "common/array.h" #include "common/map.h" #include "common/singleton.h" #include "common/str.h" @@ -127,7 +127,7 @@ private: DomainMap _globalDomains; Domain _defaultsDomain; - List<Domain *> _searchOrder; + Array<Domain *> _searchOrder; String _activeDomain; String _filename; diff --git a/common/list.h b/common/list.h index fd09ea0d9f..eabf8a4701 100644 --- a/common/list.h +++ b/common/list.h @@ -26,25 +26,48 @@ namespace Common { +/* +TODO: Add a true list class, based on a linked list data structure + template <class T> class List { protected: - int _capacity; - int _size; - T *_data; + template <class T> + class Node { + Node<T> *_prev; + Node<T> *_next; + T _data; + } + + template <class T> + class Iterator { + friend class List<T>; + private: + Node<T> *_node; + + public: + Node<T> &operator++() { + _node = _node->_next; + } + + T& operator*() const { + return _node->_data; + } + T* operator->() const { + return &(_node->_data); + } + }; + + Iterator<T> *_anchor; public: - typedef T *iterator; - typedef const T *const_iterator; + typedef Node<T> *iterator; + typedef const Node<T> *const_iterator; public: - List<T>() : _capacity(0), _size(0), _data(0) {} - List<T>(const List<T>& list) : _capacity(0), _size(0), _data(0) { - _size = list._size; - _capacity = _size + 32; - _data = new T[_capacity]; - for (int i = 0; i < _size; i++) - _data[i] = list._data[i]; + List<T>() : _anchor(...) {} + List<T>(const List<T>& list) : _anchor(...) { + ... copy list ... } ~List<T>() { @@ -53,117 +76,75 @@ public: } void push_back(const T& element) { - ensureCapacity(_size + 1); - _data[_size++] = element; + ... } void push_back(const List<T>& list) { - ensureCapacity(_size + list._size); - for (int i = 0; i < list._size; i++) - _data[_size++] = list._data[i]; - } - - 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 - // usually isn't correct (specifically, for any class which has a non-default - // copy behaviour. E.g. the String class uses a refCounter which has to be - // updated whenever a String is copied. - for (int i = _size; i > idx; i--) { - _data[i] = _data[i-1]; - } - _data[idx] = element; - _size++; + ... } - T& remove_at(int idx) { - T& tmp; - - assert(idx >= 0 && idx < _size); - tmp = _data[idx]; - for (int i = idx; i < _size - 1; i++) - _data[i] = _data[i+1]; - _size--; - return tmp; + void insert(iterator pos, const T& element) { + ... } - // TODO: insert, remove, ... + void insert(iterator pos, iterator beg, Iterator end) { + ... + } - T& operator [](int idx) { - assert(idx >= 0 && idx < _size); - return _data[idx]; + void erase(iterator beg, Iterator end) { + ... } - const T& operator [](int idx) const { - assert(idx >= 0 && idx < _size); - return _data[idx]; + void remove(const T &val) { } - List<T>& operator =(const List<T>& list) { - if (_data) - delete [] _data; - _size = list._size; - _capacity = _size + 32; - _data = new T[_capacity]; - for (int i = 0; i < _size; i++) - _data[i] = list._data[i]; - return *this; + List<T>& operator =(const List<T>& list) { + // Careful here: handle self-assignment properly! I.e. situations like + // List x; + // ... + // x = x; + // In particular, we can't just do this: + // clear(); + // insert(_first, list.begin(), list.end()); + ... } uint size() const { - return _size; + int size = 0; + for (const_iterator i = begin(); i != end(); ++i) + size++; + return size; } void clear() { - if (_data) { - delete [] _data; - _data = 0; - } - _size = 0; - _capacity = 0; + erase(begin(), end()); } bool isEmpty() const { - return (_size == 0); + return (_anchor._node == _anchor._node._next); } iterator begin() { - return _data; + Iterator iter = _anchor; + return ++iter; } iterator end() { - return _data + _size; + return _anchor; } const_iterator begin() const { - return _data; + Iterator iter = _anchor; + return ++iter; } const_iterator end() const { - return _data + _size; - } - -protected: - void ensureCapacity(int new_len) { - if (new_len <= _capacity) - return; - - T *old_data = _data; - _capacity = new_len + 32; - _data = new T[_capacity]; - - if (old_data) { - // Copy old data - for (int i = 0; i < _size; i++) - _data[i] = old_data[i]; - delete [] old_data; - } + return _anchor; } }; +*/ } // End of namespace Common diff --git a/common/str.h b/common/str.h index 2534dabd90..8697ca2871 100644 --- a/common/str.h +++ b/common/str.h @@ -22,7 +22,7 @@ #define COMMON_STRING_H #include "common/scummsys.h" -#include "common/list.h" +#include "common/array.h" #include <assert.h> #include <string.h> @@ -126,7 +126,7 @@ protected: bool operator == (const char *x, const ConstString &y); bool operator != (const char *x, const ConstString &y); -class StringList : public List<String> { +class StringList : public Array<String> { public: void push_back(const char *str) { ensureCapacity(_size + 1); diff --git a/gui/PopUpWidget.h b/gui/PopUpWidget.h index 9a5e63ad09..65be3b8daa 100644 --- a/gui/PopUpWidget.h +++ b/gui/PopUpWidget.h @@ -23,7 +23,7 @@ #include "gui/widget.h" #include "common/str.h" -#include "common/list.h" +#include "common/array.h" namespace GUI { @@ -46,7 +46,7 @@ class PopUpWidget : public Widget, public CommandSender { String name; uint32 tag; }; - typedef Common::List<Entry> EntryList; + typedef Common::Array<Entry> EntryList; protected: EntryList _entries; int _selectedItem; diff --git a/gui/TabWidget.h b/gui/TabWidget.h index ea49fed891..f425bc7884 100644 --- a/gui/TabWidget.h +++ b/gui/TabWidget.h @@ -23,7 +23,7 @@ #include "widget.h" #include "common/str.h" -#include "common/list.h" +#include "common/array.h" namespace GUI { @@ -33,7 +33,7 @@ class TabWidget : public Widget { String title; Widget *firstWidget; }; - typedef Common::List<Tab> TabList; + typedef Common::Array<Tab> TabList; protected: int _activeTab; TabList _tabs; |