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 : #include "index.hxx"
21 :
22 : #include <assert.h>
23 : #include <sal/log.hxx>
24 :
25 9460 : TYPEINIT0(SwIndexReg);
26 :
27 2931463 : SwIndex::SwIndex(SwIndexReg *const pReg, sal_Int32 const nIdx)
28 : : m_nIndex( nIdx )
29 : , m_pIndexReg( pReg )
30 : , m_pNext( 0 )
31 : , m_pPrev( 0 )
32 2931463 : , m_pMark( 0 )
33 : {
34 2931463 : Init(m_nIndex);
35 2931463 : }
36 :
37 156 : SwIndex::SwIndex( const SwIndex& rIdx, short nDiff )
38 : : m_pIndexReg( rIdx.m_pIndexReg )
39 : , m_pNext( 0 )
40 : , m_pPrev( 0 )
41 156 : , m_pMark( 0 )
42 : {
43 156 : ChgValue( rIdx, rIdx.m_nIndex + nDiff );
44 156 : }
45 :
46 3807935 : SwIndex::SwIndex( const SwIndex& rIdx )
47 : : m_nIndex( rIdx.m_nIndex )
48 : , m_pIndexReg( rIdx.m_pIndexReg )
49 : , m_pNext( 0 )
50 : , m_pPrev( 0 )
51 3807935 : , m_pMark( 0 )
52 : {
53 3807935 : ChgValue( rIdx, rIdx.m_nIndex );
54 3807935 : }
55 :
56 3797813 : void SwIndex::Init(sal_Int32 const nIdx)
57 : {
58 3797813 : if (!m_pIndexReg)
59 : {
60 2482915 : m_nIndex = 0; // always 0 if no IndexReg
61 : }
62 1314898 : else if (!m_pIndexReg->m_pFirst) // first Index?
63 : {
64 : assert(!m_pIndexReg->m_pLast);
65 528502 : m_pIndexReg->m_pFirst = m_pIndexReg->m_pLast = this;
66 528502 : m_nIndex = nIdx;
67 : }
68 786396 : else if (nIdx > ((m_pIndexReg->m_pLast->m_nIndex
69 786396 : - m_pIndexReg->m_pFirst->m_nIndex) / 2))
70 : {
71 120587 : ChgValue( *m_pIndexReg->m_pLast, nIdx );
72 : }
73 : else
74 : {
75 665809 : ChgValue( *m_pIndexReg->m_pFirst, nIdx );
76 : }
77 3797813 : }
78 :
79 5935633 : SwIndex& SwIndex::ChgValue( const SwIndex& rIdx, sal_Int32 nNewValue )
80 : {
81 : assert(m_pIndexReg == rIdx.m_pIndexReg);
82 5935633 : if (!m_pIndexReg)
83 : {
84 909140 : return *this; // no IndexReg => no list to sort into; m_nIndex is 0
85 : }
86 5026493 : SwIndex* pFnd = const_cast<SwIndex*>(&rIdx);
87 5026493 : if (rIdx.m_nIndex > nNewValue) // move forwards
88 : {
89 : SwIndex* pPrv;
90 2251264 : while ((0 != (pPrv = pFnd->m_pPrev)) && (pPrv->m_nIndex > nNewValue))
91 1516458 : pFnd = pPrv;
92 :
93 367403 : if( pFnd != this )
94 : {
95 : // remove from list at old position
96 278981 : Remove();
97 :
98 278981 : m_pNext = pFnd;
99 278981 : m_pPrev = pFnd->m_pPrev;
100 278981 : if (m_pPrev)
101 59603 : m_pPrev->m_pNext = this;
102 : else
103 219378 : m_pIndexReg->m_pFirst = this;
104 278981 : pFnd->m_pPrev = this;
105 : }
106 : }
107 4659090 : else if (rIdx.m_nIndex < nNewValue)
108 : {
109 : SwIndex* pNxt;
110 9877948 : while ((0 != (pNxt = pFnd->m_pNext)) && (pNxt->m_nIndex < nNewValue))
111 9327620 : pFnd = pNxt;
112 :
113 275164 : if( pFnd != this )
114 : {
115 : // remove from list at old position
116 173877 : Remove();
117 :
118 173877 : m_pPrev = pFnd;
119 173877 : m_pNext = pFnd->m_pNext;
120 173877 : if (m_pNext)
121 85884 : m_pNext->m_pPrev = this;
122 : else
123 87993 : m_pIndexReg->m_pLast = this;
124 173877 : pFnd->m_pNext = this;
125 : }
126 : }
127 4383926 : else if( pFnd != this )
128 : {
129 : // remove from list at old position
130 4369267 : Remove();
131 :
132 4369267 : m_pPrev = pFnd; // == &rIdx here
133 4369267 : m_pNext = rIdx.m_pNext;
134 4369267 : m_pPrev->m_pNext = this;
135 :
136 4369267 : if (!m_pNext) // last in the list
137 2088245 : m_pIndexReg->m_pLast = this;
138 : else
139 2281022 : m_pNext->m_pPrev = this;
140 : }
141 :
142 5026493 : if (m_pIndexReg->m_pFirst == m_pNext)
143 0 : m_pIndexReg->m_pFirst = this;
144 5026493 : if (m_pIndexReg->m_pLast == m_pPrev)
145 0 : m_pIndexReg->m_pLast = this;
146 :
147 5026493 : m_nIndex = nNewValue;
148 :
149 5026493 : return *this;
150 : }
151 :
152 13233229 : void SwIndex::Remove()
153 : {
154 13233229 : if (!m_pIndexReg)
155 : {
156 : assert(!m_pPrev && !m_pNext);
157 16625192 : return;
158 : }
159 :
160 9841266 : if (m_pPrev)
161 : {
162 4287203 : m_pPrev->m_pNext = m_pNext;
163 : }
164 5554063 : else if (m_pIndexReg->m_pFirst == this)
165 : {
166 1063420 : m_pIndexReg->m_pFirst = m_pNext;
167 : }
168 :
169 9841266 : if (m_pNext)
170 : {
171 3510181 : m_pNext->m_pPrev = m_pPrev;
172 : }
173 6331085 : else if (m_pIndexReg->m_pLast == this)
174 : {
175 1840442 : m_pIndexReg->m_pLast = m_pPrev;
176 : }
177 : }
178 :
179 1124982 : SwIndex& SwIndex::operator=( const SwIndex& rIdx )
180 : {
181 : bool bEqual;
182 1124982 : if (rIdx.m_pIndexReg != m_pIndexReg) // unregister!
183 : {
184 805276 : Remove();
185 805276 : m_pIndexReg = rIdx.m_pIndexReg;
186 805276 : m_pNext = m_pPrev = 0;
187 805276 : bEqual = false;
188 : }
189 : else
190 319706 : bEqual = rIdx.m_nIndex == m_nIndex;
191 :
192 1124982 : if( !bEqual )
193 891224 : ChgValue( rIdx, rIdx.m_nIndex );
194 1124982 : return *this;
195 : }
196 :
197 1975610 : SwIndex& SwIndex::Assign( SwIndexReg* pArr, sal_Int32 nIdx )
198 : {
199 1975610 : if (pArr != m_pIndexReg) // unregister!
200 : {
201 866350 : Remove();
202 866350 : m_pIndexReg = pArr;
203 866350 : m_pNext = m_pPrev = 0;
204 866350 : Init(nIdx);
205 : }
206 1109260 : else if (m_nIndex != nIdx)
207 : {
208 144051 : ChgValue( *this, nIdx );
209 : }
210 1975610 : return *this;
211 : }
212 :
213 309390 : void SwIndex::SetMark(const sw::mark::IMark* pMark)
214 : {
215 309390 : m_pMark = pMark;
216 309390 : }
217 :
218 344021 : SwIndexReg::SwIndexReg()
219 344021 : : m_pFirst( 0 ), m_pLast( 0 )
220 : {
221 344021 : }
222 :
223 343926 : SwIndexReg::~SwIndexReg()
224 : {
225 : assert(!m_pFirst && !m_pLast && "There are still indices registered");
226 343926 : }
227 :
228 249371 : void SwIndexReg::Update(
229 : SwIndex const & rIdx,
230 : const sal_Int32 nDiff,
231 : const bool bNeg,
232 : const bool /* argument is only used in derived class*/ )
233 : {
234 249371 : SwIndex* pStt = const_cast<SwIndex*>(&rIdx);
235 249371 : const sal_Int32 nNewVal = rIdx.m_nIndex;
236 249371 : if( bNeg )
237 : {
238 9104 : const sal_Int32 nLast = rIdx.GetIndex() + nDiff;
239 34519 : while (pStt && pStt->m_nIndex == nNewVal)
240 : {
241 16311 : pStt->m_nIndex = nNewVal;
242 16311 : pStt = pStt->m_pPrev;
243 : }
244 9104 : pStt = rIdx.m_pNext;
245 52012 : while (pStt && pStt->m_nIndex >= nNewVal
246 35326 : && pStt->m_nIndex <= nLast)
247 : {
248 33804 : pStt->m_nIndex = nNewVal;
249 33804 : pStt = pStt->m_pNext;
250 : }
251 228645 : while( pStt )
252 : {
253 210437 : pStt->m_nIndex = pStt->m_nIndex - nDiff;
254 210437 : pStt = pStt->m_pNext;
255 : }
256 : }
257 : else
258 : {
259 853793 : while (pStt && pStt->m_nIndex == nNewVal)
260 : {
261 373259 : pStt->m_nIndex = pStt->m_nIndex + nDiff;
262 373259 : pStt = pStt->m_pPrev;
263 : }
264 240267 : pStt = rIdx.m_pNext;
265 1928232 : while( pStt )
266 : {
267 1447698 : pStt->m_nIndex = pStt->m_nIndex + nDiff;
268 1447698 : pStt = pStt->m_pNext;
269 : }
270 : }
271 249371 : }
272 :
273 249371 : void SwIndexReg::MoveTo( SwIndexReg& rArr )
274 : {
275 249371 : if (this != &rArr && m_pFirst)
276 : {
277 21601 : SwIndex * pIdx = const_cast<SwIndex*>(m_pFirst);
278 : SwIndex * pNext;
279 73155 : while( pIdx )
280 : {
281 29953 : pNext = pIdx->m_pNext;
282 29953 : pIdx->Assign( &rArr, pIdx->GetIndex() );
283 29953 : pIdx = pNext;
284 : }
285 21601 : m_pFirst = 0, m_pLast = 0;
286 : }
287 249371 : }
288 :
289 : #ifdef DBG_UTIL
290 :
291 : // SwIndex
292 :
293 : sal_Int32 SwIndex::operator++(int)
294 : {
295 : SAL_WARN_IF( !(m_nIndex < SAL_MAX_INT32), "sw.core",
296 : "SwIndex::operator++(int) wraps around" );
297 :
298 : const sal_Int32 nOldIndex = m_nIndex;
299 : ChgValue( *this, m_nIndex+1 );
300 : return nOldIndex;
301 : }
302 :
303 : sal_Int32 SwIndex::operator++()
304 : {
305 : SAL_WARN_IF( !(m_nIndex < SAL_MAX_INT32), "sw.core",
306 : "SwIndex::operator++() wraps around" );
307 :
308 : ChgValue( *this, m_nIndex+1 );
309 : return m_nIndex;
310 : }
311 :
312 : sal_Int32 SwIndex::operator--(int)
313 : {
314 : SAL_WARN_IF( !(m_nIndex > 0), "sw.core",
315 : "SwIndex::operator--(int) wraps around" );
316 :
317 : const sal_Int32 nOldIndex = m_nIndex;
318 : ChgValue( *this, m_nIndex-1 );
319 : return nOldIndex;
320 : }
321 :
322 : sal_Int32 SwIndex::operator--()
323 : {
324 : SAL_WARN_IF( !( m_nIndex > 0), "sw.core",
325 : "SwIndex::operator--() wraps around" );
326 : return ChgValue( *this, m_nIndex-1 ).m_nIndex;
327 : }
328 :
329 : sal_Int32 SwIndex::operator+=( sal_Int32 const nVal )
330 : {
331 : SAL_WARN_IF( !(m_nIndex <= SAL_MAX_INT32 - nVal), "sw.core",
332 : "SwIndex SwIndex::operator+=(sal_Int32) wraps around" );
333 : return ChgValue( *this, m_nIndex + nVal ).m_nIndex;
334 : }
335 :
336 : sal_Int32 SwIndex::operator-=( sal_Int32 const nVal )
337 : {
338 : SAL_WARN_IF( !(m_nIndex >= nVal), "sw.core",
339 : "SwIndex::operator-=(sal_Int32) wraps around" );
340 : return ChgValue( *this, m_nIndex - nVal ).m_nIndex;
341 : }
342 :
343 : sal_Int32 SwIndex::operator+=( const SwIndex & rIndex )
344 : {
345 : SAL_WARN_IF( !(m_nIndex <= SAL_MAX_INT32 - rIndex.m_nIndex), "sw.core",
346 : "SwIndex::operator+=(SwIndex) wraps around" );
347 : return ChgValue( *this, m_nIndex + rIndex.m_nIndex ).m_nIndex;
348 : }
349 :
350 : sal_Int32 SwIndex::operator-=( const SwIndex & rIndex )
351 : {
352 : SAL_WARN_IF( !(m_nIndex >= rIndex.m_nIndex), "sw.core",
353 : "SwIndex::operator-=(SwIndex) wraps around" );
354 : return ChgValue( *this, m_nIndex - rIndex.m_nIndex ).m_nIndex;
355 : }
356 :
357 : bool SwIndex::operator< ( const SwIndex & rIndex ) const
358 : {
359 : // Attempt to compare indices into different arrays
360 : assert(m_pIndexReg == rIndex.m_pIndexReg);
361 : return m_nIndex < rIndex.m_nIndex;
362 : }
363 :
364 : bool SwIndex::operator<=( const SwIndex & rIndex ) const
365 : {
366 : // Attempt to compare indices into different arrays
367 : assert(m_pIndexReg == rIndex.m_pIndexReg);
368 : return m_nIndex <= rIndex.m_nIndex;
369 : }
370 :
371 : bool SwIndex::operator> ( const SwIndex & rIndex ) const
372 : {
373 : // Attempt to compare indices into different arrays
374 : assert(m_pIndexReg == rIndex.m_pIndexReg);
375 : return m_nIndex > rIndex.m_nIndex;
376 : }
377 :
378 : bool SwIndex::operator>=( const SwIndex & rIndex ) const
379 : {
380 : // Attempt to compare indices into different arrays
381 : assert(m_pIndexReg == rIndex.m_pIndexReg);
382 : return m_nIndex >= rIndex.m_nIndex;
383 : }
384 :
385 : SwIndex& SwIndex::operator= ( sal_Int32 const nVal )
386 : {
387 : if (m_nIndex != nVal)
388 : ChgValue( *this, nVal );
389 :
390 : return *this;
391 : }
392 :
393 : #endif
394 :
395 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|