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