Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #ifndef __LISTEN_123456__
21 : : #define __LISTEN_123456__
22 : :
23 : : #include <string.h>
24 : : #include <iostream>
25 : : #include <stdlib.h>
26 : :
27 : : template <class XX>
28 : : class List
29 : : {
30 : : public :
31 : : typedef XX * iterator;
32 : : typedef const XX * const_iterator;
33 : :
34 : : // LIFECYCLE
35 : : List();
36 [ + - ]: 1232 : virtual ~List() { delete [] inhalt; }
[ + + + ]
[ - + ][ # # ]
[ # # ][ + - ]
[ - + ]
37 : :
38 : : // OPERATORS
39 : 436 : const XX & operator[](
40 : : unsigned n) const
41 : 436 : { return elem(n); }
42 : 772 : XX & operator[](
43 : : unsigned n)
44 : 772 : { return elem(n); }
45 : : // OPERATIONS
46 : : void reserve(
47 : : unsigned i_nSize )
48 : : { alloc(i_nSize,true); }
49 : : virtual void insert(
50 : : unsigned pos,
51 : : const XX & elem );
52 : 940 : void push_back(
53 : : const XX & elem_)
54 : 940 : { insert(size(),elem_); }
55 : :
56 : : virtual void remove(
57 : : unsigned pos );
58 : : void pop_back() { remove(size()-1); }
59 [ + + ]: 274 : void erase_all() { while (size()) remove(size()-1); }
60 : :
61 : : // INQUIRY
62 : : const XX & front() const { return elem(0); }
63 : : const XX & back() const { return elem(len-1); }
64 : :
65 : 1532 : unsigned size() const { return len; }
66 : 1880 : unsigned space() const { return allocated; }
67 : 638 : bool is_valid_index(
68 : : unsigned n) const
69 : 638 : { return n < len; }
70 : : // ACCESS
71 : : XX & front() { return elem(0); }
72 : : XX & back() { return elem(len-1); }
73 : :
74 : : protected:
75 : : void checkSize(
76 : : unsigned newLength);
77 : : void alloc(
78 : : unsigned newSpace,
79 : : bool re = false );
80 : :
81 : 436 : const XX & elem(
82 : : unsigned n ) const
83 : 436 : { return inhalt[n]; }
84 : 772 : XX & elem(
85 : : unsigned n )
86 : 772 : { return inhalt[n]; }
87 : : // DATA
88 : : XX * inhalt;
89 : : unsigned len;
90 : : unsigned allocated;
91 : :
92 : : private:
93 : : // forbidden functions
94 : : List(const List<XX> & L);
95 : : List<XX> & operator=(
96 : : const List<XX> & L);
97 : :
98 : : };
99 : :
100 : : template <class XY>
101 : 42 : class DynamicList : public List<XY*>
102 : : {
103 : : public:
104 : : virtual ~DynamicList();
105 : :
106 : : virtual void insert(
107 : : unsigned pos,
108 : : XY * const & elem );
109 : : virtual void remove(
110 : : unsigned pos );
111 : : };
112 : :
113 : :
114 : :
115 : : template <class XX>
116 : 148 : List<XX>::List()
117 : : : inhalt(0),
118 : : len(0),
119 : 148 : allocated(0)
120 : :
121 : : {
122 : 148 : alloc(1);
123 : 148 : }
124 : :
125 : :
126 : : template <class XX>
127 : : void
128 : 708 : List<XX>::insert(unsigned pos, const XX & elem_)
129 : : {
130 [ - + ][ - + ]: 708 : if ( pos > len )
131 : 708 : return;
132 : :
133 : 708 : checkSize(len+2);
134 [ - + ][ - + ]: 708 : for ( unsigned p = len; p > pos; --p)
[ - + ]
135 : : {
136 : 0 : inhalt[p] = inhalt[p-1];
137 : : }
138 : 690 : inhalt[pos] = elem_;
139 : 708 : len++;
140 : : }
141 : :
142 : :
143 : : template <class XX>
144 : : void
145 : 0 : List<XX>::remove(unsigned pos)
146 : : {
147 [ # # ][ # # ]: 0 : if ( pos >= len )
148 : 0 : return;
149 : 0 : len--;
150 [ # # ][ # # ]: 0 : for ( unsigned p = pos; p < len; ++p)
[ # # ]
151 : : {
152 : 0 : inhalt[p] = inhalt[p+1];
153 : : }
154 : : }
155 : :
156 : :
157 : : // Protected:
158 : : template <class XX>
159 : : void
160 : 940 : List<XX>::checkSize(unsigned newLength)
161 : : {
162 : : // test new size requirement:
163 : 940 : unsigned newSpace = space();
164 [ + + + + ]: 940 : if (newLength > newSpace)
165 : : {
166 [ - + ][ - + ]: 398 : if (!newSpace)
167 : 0 : newSpace = 1;
168 : 398 : const unsigned nBorder = 65536 / 2;
169 [ + + ][ + + ]: 796 : while(newLength > newSpace)
170 : : {
171 [ + - ][ + - ]: 398 : if (newSpace < nBorder)
172 : 398 : newSpace <<= 1;
173 : : else
174 : : {
175 : 0 : std::cerr << "List becomes too big" << std::endl;
176 : 0 : exit(1);
177 : : }
178 : : }
179 : : }
180 : :
181 : : // change?
182 [ + + ][ + + ]: 940 : if (newSpace != space())
183 : 398 : alloc(newSpace,true);
184 : 940 : }
185 : :
186 : : template <class XX>
187 : : void
188 : 546 : List<XX>::alloc( unsigned newSpace,
189 : : bool re )
190 : : {
191 [ + - ][ # # ]: 2612 : XX * pNew = new XX[newSpace];
[ + + # #
# # ][ # #
# # # # ]
192 : :
193 [ + + + + ]: 546 : if (inhalt != 0)
[ + + ]
194 : : {
195 [ + - ][ + - ]: 398 : if (re)
196 : : {
197 [ + + ][ + + ]: 1416 : for (unsigned i = 0; i < len; ++i)
[ + + ]
198 : : {
199 : 748 : pNew[i] = inhalt[i];
200 : : } // end for
201 : : }
202 [ + - ]: 1380 : delete [] inhalt;
[ + + # ]
[ # # ][ + - ]
203 : : }
204 : :
205 : 546 : inhalt = pNew;
206 : 546 : allocated = newSpace;
207 : 546 : }
208 : :
209 : :
210 : : template <class XY>
211 : 42 : DynamicList<XY>::~DynamicList()
212 : : {
213 [ + - - + ]: 84 : this->erase_all();
214 : 84 : }
215 : :
216 : : template <class XY>
217 : : void
218 : 232 : DynamicList<XY>::insert(unsigned pos, XY * const & elem_)
219 : : {
220 [ - + ]: 232 : if ( pos > this->len )
221 : 232 : return;
222 : :
223 : 232 : this->checkSize(this->len+2);
224 : 232 : memmove(&this->inhalt[pos+1], &this->inhalt[pos], (this->len-pos) * sizeof(XY*) );
225 : 232 : this->inhalt[pos] = elem_;
226 : 232 : this->len++;
227 : : }
228 : :
229 : : template <class XY>
230 : : void
231 : 232 : DynamicList<XY>::remove( unsigned pos )
232 : : {
233 [ - + ]: 232 : if (!this->is_valid_index(pos) )
234 : 232 : return;
235 : 232 : this->len--;
236 [ + - ]: 232 : delete this->inhalt[pos];
237 : 232 : memmove(&this->inhalt[pos], &this->inhalt[pos+1], (this->len-pos) * sizeof(XY*) );
238 : : }
239 : :
240 : :
241 : :
242 : : #endif
243 : :
244 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|