00001 /* 00002 * Funambol is a mobile platform developed by Funambol, Inc. 00003 * Copyright (C) 2003 - 2007 Funambol, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Affero General Public License version 3 as published by 00007 * the Free Software Foundation with the addition of the following permission 00008 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED 00009 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 00010 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 00011 * 00012 * This program is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00014 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00015 * details. 00016 * 00017 * You should have received a copy of the GNU Affero General Public License 00018 * along with this program; if not, see http://www.gnu.org/licenses or write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00020 * MA 02110-1301 USA. 00021 * 00022 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 00023 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. 00024 * 00025 * The interactive user interfaces in modified source and object code versions 00026 * of this program must display Appropriate Legal Notices, as required under 00027 * Section 5 of the GNU Affero General Public License version 3. 00028 * 00029 * In accordance with Section 7(b) of the GNU Affero General Public License 00030 * version 3, these Appropriate Legal Notices must retain the display of the 00031 * "Powered by Funambol" logo. If the display of the logo is not reasonably 00032 * feasible for technical reasons, the Appropriate Legal Notices must display 00033 * the words "Powered by Funambol". 00034 */ 00035 00036 00037 #ifndef INCL_BASE_UTIL_ARRAY_LIST 00038 #define INCL_BASE_UTIL_ARRAY_LIST 00039 /** @cond DEV */ 00040 00041 #include "base/fscapi.h" 00042 00043 #include "base/util/ArrayElement.h" 00044 #include "base/globalsdef.h" 00045 00046 BEGIN_NAMESPACE 00047 00048 /** 00049 * This class implements a simple linked list that can be accessed by index too. 00050 * This class does not make use of C++ templates by choice, since it must be 00051 * as much easier and portable as possible. 00052 * 00053 * Each list element must be an instance of ArrayElement, which must be 00054 * considered an abstract class. Implementing classes must define the destructor 00055 * and the method clone(), which is used to replicate a given object. This is 00056 * used by insertion methods: they always clone the item and store the cloned 00057 * element so that the version inside the list and the caller can independently 00058 * release their memory. Note that clone methods MUST use the C++ operator to 00059 * allocate new object, since ArrayList will delete them calling the C++ delete 00060 * operator. 00061 */ 00062 00063 struct Element { 00064 ArrayElement* e; // the element value 00065 Element* n; // the next element (NULL for the latest) 00066 }; 00067 00068 class ArrayList { 00069 private: 00070 Element* head; 00071 Element* lastElement; 00072 00073 Element* iterator; 00074 00075 int count; 00076 00077 ArrayList& set (const ArrayList & other); 00078 00079 protected: 00080 00081 /** 00082 * Can be used to reset the iterator, so that next call to next restart from 00083 * the beginning. 00084 */ 00085 void resetIterator() { iterator = 0; } 00086 00087 public: 00088 ArrayList(); 00089 ArrayList(const ArrayList &other); 00090 ~ArrayList(); 00091 00092 /** 00093 * Is this list empty? 00094 */ 00095 bool isEmpty(); 00096 00097 /** 00098 * Adds a new element at the specified index. If index is greater than 00099 * the list size the element is appended. 00100 * The element is dinamically duplicated so that the caller can release 00101 * the element at any time. This method returns the position (0 based) at which 00102 * the element has been inserted. It can be different by index if index is out 00103 * of the array bounds, in that case element is appended as last element. 00104 * It returns -1 in case of errors. 00105 * 00106 * @param index the insertion position 00107 * @param element the element to insert 00108 */ 00109 int add(int index, ArrayElement& element); 00110 00111 /** 00112 * Same as add(index, element, size), but append at the end of the array. 00113 * 00114 * @param element the element to insert 00115 */ 00116 int add(ArrayElement& element); 00117 00118 /** 00119 * Add all the ArrayElement of the given ArrayList to the current 00120 * array list 00121 */ 00122 int add(ArrayList* list); 00123 00124 /** 00125 * Frees the list. All elements are freed as well. 00126 */ 00127 void clear(); 00128 00129 /** 00130 * Frees the list and all its elements, regardless the value of 00131 * autoDeleteElements. 00132 */ 00133 void clearAll(); 00134 00135 int removeElementAt(int index); 00136 00137 /** 00138 * Returns the index-th element of the array or NULL if index is out of 00139 * the array bounds. Note that the retuned element will be released at 00140 * list destruction. Clone it if it must have a different life cycle. 00141 * 00142 * @param index the element position 00143 */ 00144 ArrayElement* get(int index) const; 00145 00146 /** 00147 * Returns the first element of the array and set here the internal iterator. 00148 * 00149 * Note that the retuned element will be released at list destruction. 00150 * Clone it if it must have a different life cycle. 00151 * 00152 * @return - the first element of the array, or NULL if empty. 00153 */ 00154 ArrayElement* front(); 00155 00156 /** 00157 * Returns the next element of the array and increment the internal iterator. 00158 * 00159 * Note that the retuned element will be released at list destruction. 00160 * Clone it if it must have a different life cycle. 00161 * 00162 * @return - the next element of the array, or NULL if beyond the last. 00163 */ 00164 ArrayElement* next(); 00165 00166 /** 00167 * Returns the previous element of the array and decrement the internal iterator. 00168 * 00169 * Note that the retuned element will be released at list destruction. 00170 * Clone it if it must have a different life cycle. 00171 * 00172 * @return - the previous element of the array, or NULL if before the first. 00173 */ 00174 ArrayElement* prev(); 00175 00176 /** 00177 * Returns the last element of the array and set here the internal iterator. 00178 * 00179 * Note that the retuned element will be released at list destruction. 00180 * Clone it if it must have a different life cycle. 00181 * 00182 * @return - the first element of the array, or NULL if empty. 00183 */ 00184 ArrayElement* back(); 00185 00186 /** 00187 * Returns true if it is the last one, false otherwise 00188 * 00189 * @return - true if the iterator is at the last element, false otherwise. 00190 */ 00191 bool last() const; 00192 00193 /** 00194 * Returns the array size. 00195 */ 00196 int size() const; 00197 00198 /** 00199 * Same as get(index) 00200 */ 00201 ArrayElement* operator[] (int index) const; 00202 00203 /** 00204 * Copy the ArrayList 00205 */ 00206 ArrayList& operator= (const ArrayList &v); 00207 00208 00209 /** 00210 * Clones the arrayList a return a pointer to a new one. 00211 */ 00212 ArrayList* clone(); 00213 00214 }; 00215 00216 END_NAMESPACE 00217 00218 /** @endcond */ 00219 #endif