Update ooo320-m1
[ooovba.git] / xml2cmp / source / support / list.hxx
blob73a44b327990c033871f0e55f56a952d175003df
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: list.hxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __LISTEN_123456__
32 #define __LISTEN_123456__
34 #include <string.h>
35 #include <iostream>
36 #include <stdlib.h>
38 template <class XX>
39 class List
41 public :
42 typedef XX * iterator;
43 typedef const XX * const_iterator;
45 // LIFECYCLE
46 List();
47 virtual ~List() { delete [] inhalt; }
49 // OPERATORS
50 const XX & operator[](
51 unsigned n) const
52 { return elem(n); }
53 XX & operator[](
54 unsigned n)
55 { return elem(n); }
56 // OPERATIONS
57 void reserve(
58 unsigned i_nSize )
59 { alloc(i_nSize,true); }
60 virtual void insert(
61 unsigned pos,
62 const XX & elem );
63 void push_back(
64 const XX & elem_)
65 { insert(size(),elem_); }
67 virtual void remove(
68 unsigned pos );
69 void pop_back() { remove(size()-1); }
70 void erase_all() { while (size()) remove(size()-1); }
72 // INQUIRY
73 const XX & front() const { return elem(0); }
74 const XX & back() const { return elem(len-1); }
76 unsigned size() const { return len; }
77 unsigned space() const { return allocated; }
78 bool is_valid_index(
79 unsigned n) const
80 { return n < len; }
81 // ACCESS
82 XX & front() { return elem(0); }
83 XX & back() { return elem(len-1); }
85 protected:
86 void checkSize(
87 unsigned newLength);
88 void alloc(
89 unsigned newSpace,
90 bool re = false );
92 const XX & elem(
93 unsigned n ) const
94 { return inhalt[n]; }
95 XX & elem(
96 unsigned n )
97 { return inhalt[n]; }
98 // DATA
99 XX * inhalt;
100 unsigned len;
101 unsigned allocated;
103 private:
104 // forbidden functions
105 List(const List<XX> & L);
106 List<XX> & operator=(
107 const List<XX> & L);
111 template <class XY>
112 class DynamicList : public List<XY*>
114 public:
115 virtual ~DynamicList();
117 virtual void insert(
118 unsigned pos,
119 XY * const & elem );
120 virtual void remove(
121 unsigned pos );
126 template <class XX>
127 List<XX>::List()
128 : inhalt(0),
129 len(0),
130 allocated(0)
133 alloc(1);
137 template <class XX>
138 void
139 List<XX>::insert(unsigned pos, const XX & elem_)
141 if ( pos > len )
142 return;
144 checkSize(len+2);
145 for ( unsigned p = len; p > pos; --p)
147 inhalt[p] = inhalt[p-1];
149 inhalt[pos] = elem_;
150 len++;
154 template <class XX>
155 void
156 List<XX>::remove(unsigned pos)
158 if ( pos >= len )
159 return;
160 len--;
161 for ( unsigned p = pos; p < len; ++p)
163 inhalt[p] = inhalt[p+1];
168 // Protected:
169 template <class XX>
170 void
171 List<XX>::checkSize(unsigned newLength)
173 // neuen Platzbedarf pruefen:
174 unsigned newSpace = space();
175 if (newLength > newSpace)
177 if (!newSpace)
178 newSpace = 1;
179 const unsigned nBorder = 65536 / 2;
180 while(newLength > newSpace)
182 if (newSpace < nBorder)
183 newSpace <<= 1;
184 else
186 std::cerr << "List becomes too big" << std::endl;
187 exit(1);
192 // Veraenderung ?:
193 if (newSpace != space())
194 alloc(newSpace,true);
197 template <class XX>
198 void
199 List<XX>::alloc( unsigned newSpace,
200 bool re )
202 XX * pNew = new XX[newSpace];
204 if (inhalt != 0)
206 if (re)
208 for (unsigned i = 0; i < len; ++i)
210 pNew[i] = inhalt[i];
211 } // end for
213 delete [] inhalt;
216 inhalt = pNew;
217 allocated = newSpace;
221 template <class XY>
222 DynamicList<XY>::~DynamicList()
224 this->erase_all();
227 template <class XY>
228 void
229 DynamicList<XY>::insert(unsigned pos, XY * const & elem_)
231 if ( pos > this->len )
232 return;
234 checkSize(this->len+2);
235 memmove(this->inhalt[pos+1], this->inhalt[pos], (this->len-pos) * sizeof(XY*) );
236 this->inhalt[pos] = elem_;
237 this->len++;
240 template <class XY>
241 void
242 DynamicList<XY>::remove( unsigned pos )
244 if (!this->is_valid_index(pos) )
245 return;
246 this->len--;
247 delete this->inhalt[pos];
248 memmove(this->inhalt[pos], this->inhalt[pos+1], (this->len-pos) * sizeof(XY*) );
253 #endif