1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef __LISTEN_123456__ 29 #define __LISTEN_123456__ 30 31 #include <string.h> 32 #include <iostream> 33 #include <stdlib.h> 34 35 template <class XX> 36 class List 37 { 38 public : 39 typedef XX * iterator; 40 typedef const XX * const_iterator; 41 42 // LIFECYCLE 43 List(); 44 virtual ~List() { delete [] inhalt; } 45 46 // OPERATORS 47 const XX & operator[]( 48 unsigned n) const 49 { return elem(n); } 50 XX & operator[]( 51 unsigned n) 52 { return elem(n); } 53 // OPERATIONS 54 void reserve( 55 unsigned i_nSize ) 56 { alloc(i_nSize,true); } 57 virtual void insert( 58 unsigned pos, 59 const XX & elem ); 60 void push_back( 61 const XX & elem_) 62 { insert(size(),elem_); } 63 64 virtual void remove( 65 unsigned pos ); 66 void pop_back() { remove(size()-1); } 67 void erase_all() { while (size()) remove(size()-1); } 68 69 // INQUIRY 70 const XX & front() const { return elem(0); } 71 const XX & back() const { return elem(len-1); } 72 73 unsigned size() const { return len; } 74 unsigned space() const { return allocated; } 75 bool is_valid_index( 76 unsigned n) const 77 { return n < len; } 78 // ACCESS 79 XX & front() { return elem(0); } 80 XX & back() { return elem(len-1); } 81 82 protected: 83 void checkSize( 84 unsigned newLength); 85 void alloc( 86 unsigned newSpace, 87 bool re = false ); 88 89 const XX & elem( 90 unsigned n ) const 91 { return inhalt[n]; } 92 XX & elem( 93 unsigned n ) 94 { return inhalt[n]; } 95 // DATA 96 XX * inhalt; 97 unsigned len; 98 unsigned allocated; 99 100 private: 101 // forbidden functions 102 List(const List<XX> & L); 103 List<XX> & operator=( 104 const List<XX> & L); 105 106 }; 107 108 template <class XY> 109 class DynamicList : public List<XY*> 110 { 111 public: 112 virtual ~DynamicList(); 113 114 virtual void insert( 115 unsigned pos, 116 XY * const & elem ); 117 virtual void remove( 118 unsigned pos ); 119 }; 120 121 122 123 template <class XX> 124 List<XX>::List() 125 : inhalt(0), 126 len(0), 127 allocated(0) 128 129 { 130 alloc(1); 131 } 132 133 134 template <class XX> 135 void 136 List<XX>::insert(unsigned pos, const XX & elem_) 137 { 138 if ( pos > len ) 139 return; 140 141 checkSize(len+2); 142 for ( unsigned p = len; p > pos; --p) 143 { 144 inhalt[p] = inhalt[p-1]; 145 } 146 inhalt[pos] = elem_; 147 len++; 148 } 149 150 151 template <class XX> 152 void 153 List<XX>::remove(unsigned pos) 154 { 155 if ( pos >= len ) 156 return; 157 len--; 158 for ( unsigned p = pos; p < len; ++p) 159 { 160 inhalt[p] = inhalt[p+1]; 161 } 162 } 163 164 165 // Protected: 166 template <class XX> 167 void 168 List<XX>::checkSize(unsigned newLength) 169 { 170 // neuen Platzbedarf pruefen: 171 unsigned newSpace = space(); 172 if (newLength > newSpace) 173 { 174 if (!newSpace) 175 newSpace = 1; 176 const unsigned nBorder = 65536 / 2; 177 while(newLength > newSpace) 178 { 179 if (newSpace < nBorder) 180 newSpace <<= 1; 181 else 182 { 183 std::cerr << "List becomes too big" << std::endl; 184 exit(1); 185 } 186 } 187 } 188 189 // Veraenderung ?: 190 if (newSpace != space()) 191 alloc(newSpace,true); 192 } 193 194 template <class XX> 195 void 196 List<XX>::alloc( unsigned newSpace, 197 bool re ) 198 { 199 XX * pNew = new XX[newSpace]; 200 201 if (inhalt != 0) 202 { 203 if (re) 204 { 205 for (unsigned i = 0; i < len; ++i) 206 { 207 pNew[i] = inhalt[i]; 208 } // end for 209 } 210 delete [] inhalt; 211 } 212 213 inhalt = pNew; 214 allocated = newSpace; 215 } 216 217 218 template <class XY> 219 DynamicList<XY>::~DynamicList() 220 { 221 this->erase_all(); 222 } 223 224 template <class XY> 225 void 226 DynamicList<XY>::insert(unsigned pos, XY * const & elem_) 227 { 228 if ( pos > this->len ) 229 return; 230 231 checkSize(this->len+2); 232 memmove(this->inhalt[pos+1], this->inhalt[pos], (this->len-pos) * sizeof(XY*) ); 233 this->inhalt[pos] = elem_; 234 this->len++; 235 } 236 237 template <class XY> 238 void 239 DynamicList<XY>::remove( unsigned pos ) 240 { 241 if (!this->is_valid_index(pos) ) 242 return; 243 this->len--; 244 delete this->inhalt[pos]; 245 memmove(this->inhalt[pos], this->inhalt[pos+1], (this->len-pos) * sizeof(XY*) ); 246 } 247 248 249 250 #endif 251 252