xref: /AOO41X/main/svl/source/items/nranges.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // compiled via include from itemset.cxx only!
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir //========================================================================
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #ifdef DBG_UTIL
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #define DBG_CHECK_RANGES(NUMTYPE, pArr)									\
38*cdf0e10cSrcweir 	for ( const NUMTYPE *pRange = pArr; *pRange; pRange += 2 )          \
39*cdf0e10cSrcweir 	{                                                                   \
40*cdf0e10cSrcweir 		DBG_ASSERT( pRange[0] <= pRange[1], "ranges must be sorted" );  \
41*cdf0e10cSrcweir 		DBG_ASSERT( !pRange[2] || ( pRange[2] - pRange[1] ) > 1,        \
42*cdf0e10cSrcweir 					"ranges must be sorted and discrete" );             \
43*cdf0e10cSrcweir 	}
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #else
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #define DBG_CHECK_RANGES(NUMTYPE,pArr)
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #endif
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //============================================================================
52*cdf0e10cSrcweir inline void Swap_Impl(const NUMTYPE *& rp1, const NUMTYPE *& rp2)
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir 	const NUMTYPE * pTemp = rp1;
55*cdf0e10cSrcweir 	rp1 = rp2;
56*cdf0e10cSrcweir 	rp2 = pTemp;
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir //========================================================================
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir NUMTYPE InitializeRanges_Impl( NUMTYPE *&rpRanges, va_list pArgs,
62*cdf0e10cSrcweir 							   NUMTYPE nWh1, NUMTYPE nWh2, NUMTYPE nNull )
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir /**	<H3>Description</H3>
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	Creates an sal_uInt16-ranges-array in 'rpRanges' using 'nWh1' and 'nWh2' as
67*cdf0e10cSrcweir 	first range, 'nNull' as terminator or start of 2nd range and 'pArgs' as
68*cdf0e10cSrcweir 	remaider.
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	It returns the number of NUMTYPEs which are contained in the described
71*cdf0e10cSrcweir 	set of NUMTYPEs.
72*cdf0e10cSrcweir */
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	NUMTYPE nSize = 0, nIns = 0;
76*cdf0e10cSrcweir     sal_uInt16 nCnt = 0;
77*cdf0e10cSrcweir 	SvNums aNumArr( 11, 8 );
78*cdf0e10cSrcweir 	aNumArr.Insert( nWh1, nCnt++ );
79*cdf0e10cSrcweir 	aNumArr.Insert( nWh2, nCnt++ );
80*cdf0e10cSrcweir 	DBG_ASSERT( nWh1 <= nWh2, "Ungueltiger Bereich" );
81*cdf0e10cSrcweir 	nSize += nWh2 - nWh1 + 1;
82*cdf0e10cSrcweir 	aNumArr.Insert( nNull, nCnt++ );
83*cdf0e10cSrcweir 	while ( 0 !=
84*cdf0e10cSrcweir             ( nIns =
85*cdf0e10cSrcweir               sal::static_int_cast< NUMTYPE >(
86*cdf0e10cSrcweir                   va_arg( pArgs, NUMTYPE_ARG ) ) ) )
87*cdf0e10cSrcweir 	{
88*cdf0e10cSrcweir 		aNumArr.Insert( nIns, nCnt++ );
89*cdf0e10cSrcweir 		if ( 0 == (nCnt & 1) )		 // 4,6,8, usw.
90*cdf0e10cSrcweir 		{
91*cdf0e10cSrcweir 			DBG_ASSERT( aNumArr[ nCnt-2 ] <= nIns, "Ungueltiger Bereich" );
92*cdf0e10cSrcweir 			nSize += nIns - aNumArr[ nCnt-2 ] + 1;
93*cdf0e10cSrcweir 		}
94*cdf0e10cSrcweir 	}
95*cdf0e10cSrcweir 	va_end( pArgs );
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	DBG_ASSERT( 0 == (nCnt & 1), "ungerade Anzahl von Which-Paaren!" );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	// so, jetzt sind alle Bereiche vorhanden und
100*cdf0e10cSrcweir 	rpRanges = new NUMTYPE[ nCnt+1 ];
101*cdf0e10cSrcweir 	memcpy( rpRanges, aNumArr.GetData(), sizeof(NUMTYPE) * nCnt );
102*cdf0e10cSrcweir 	*(rpRanges+nCnt) = 0;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	return nSize;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir //------------------------------------------------------------------------
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir NUMTYPE Count_Impl( const NUMTYPE *pRanges )
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir /**	<H3>Description</H3>
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	Determines the number of NUMTYPEs in an 0-terminated array of pairs of
114*cdf0e10cSrcweir 	NUMTYPEs. The terminating 0 is not included in the count.
115*cdf0e10cSrcweir */
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	NUMTYPE nCount = 0;
119*cdf0e10cSrcweir 	while ( *pRanges )
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		nCount += 2;
122*cdf0e10cSrcweir 		pRanges += 2;
123*cdf0e10cSrcweir 	}
124*cdf0e10cSrcweir 	return nCount;
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir //------------------------------------------------------------------------
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir NUMTYPE Capacity_Impl( const NUMTYPE *pRanges )
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir /**	<H3>Description</H3>
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	Determines the total number of NUMTYPEs described in an 0-terminated
134*cdf0e10cSrcweir 	array of pairs of NUMTYPEs, each representing an range of NUMTYPEs.
135*cdf0e10cSrcweir */
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir 	NUMTYPE nCount = 0;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	if ( pRanges )
141*cdf0e10cSrcweir 	{
142*cdf0e10cSrcweir 		while ( *pRanges )
143*cdf0e10cSrcweir 		{
144*cdf0e10cSrcweir 			nCount += pRanges[1] - pRanges[0] + 1;
145*cdf0e10cSrcweir 			pRanges += 2;
146*cdf0e10cSrcweir 		}
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir 	return nCount;
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir //------------------------------------------------------------------------
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir SfxNumRanges::SfxNumRanges( const SfxNumRanges &rOrig )
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir /**	<H3>Description</H3>
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 	Copy-Ctor.
158*cdf0e10cSrcweir */
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	if ( rOrig._pRanges )
162*cdf0e10cSrcweir 	{
163*cdf0e10cSrcweir 		NUMTYPE nCount = Count_Impl( rOrig._pRanges ) + 1;
164*cdf0e10cSrcweir 		_pRanges = new NUMTYPE[nCount];
165*cdf0e10cSrcweir 		memcpy( _pRanges, rOrig._pRanges, sizeof(NUMTYPE) * nCount );
166*cdf0e10cSrcweir 	}
167*cdf0e10cSrcweir 	else
168*cdf0e10cSrcweir 		_pRanges = 0;
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir //------------------------------------------------------------------------
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir SfxNumRanges::SfxNumRanges( NUMTYPE nWhich1, NUMTYPE nWhich2 )
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir /**	<H3>Description</H3>
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	Constructs an SfxNumRanges-instance from one range of NUMTYPEs.
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	precondition:
180*cdf0e10cSrcweir 		nWhich1 <= nWhich2
181*cdf0e10cSrcweir */
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir :   _pRanges( new NUMTYPE[3] )
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 	_pRanges[0] = nWhich1;
186*cdf0e10cSrcweir 	_pRanges[1] = nWhich2;
187*cdf0e10cSrcweir 	_pRanges[2] = 0;
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir //------------------------------------------------------------------------
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir SfxNumRanges::SfxNumRanges( NUMTYPE_ARG nWh0, NUMTYPE_ARG nWh1, NUMTYPE_ARG nNull, ... )
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir /**	<H3>Description</H3>
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 	Constructs an SfxNumRanges-instance from more than one sorted ranges of
197*cdf0e10cSrcweir 	NUMTYPEs terminated with one 0.
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	precondition: for each n >= 0 && n < nArgs
200*cdf0e10cSrcweir 		nWh(2n) <= nWh(2n+1) && ( nWh(2n+2)-nWh(2n+1) ) > 1
201*cdf0e10cSrcweir */
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir 	va_list pArgs;
205*cdf0e10cSrcweir 	va_start( pArgs, nNull );
206*cdf0e10cSrcweir 	InitializeRanges_Impl(
207*cdf0e10cSrcweir         _pRanges, pArgs, sal::static_int_cast< NUMTYPE >(nWh0),
208*cdf0e10cSrcweir         sal::static_int_cast< NUMTYPE >(nWh1),
209*cdf0e10cSrcweir         sal::static_int_cast< NUMTYPE >(nNull));
210*cdf0e10cSrcweir 	DBG_CHECK_RANGES(NUMTYPE, _pRanges);
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir //------------------------------------------------------------------------
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir SfxNumRanges::SfxNumRanges( const NUMTYPE* pArr )
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir /**	<H3>Description</H3>
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	Constcurts an SfxNumRanges-instance from an sorted ranges of NUMTYPEs,
220*cdf0e10cSrcweir 	terminates with on 0.
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 	precondition: for each n >= 0 && n < (sizeof(pArr)-1)
223*cdf0e10cSrcweir 		pArr[2n] <= pArr[2n+1] && ( pArr[2n+2]-pArr[2n+1] ) > 1
224*cdf0e10cSrcweir */
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir 	DBG_CHECK_RANGES(NUMTYPE, pArr);
228*cdf0e10cSrcweir 	NUMTYPE nCount = Count_Impl(pArr) + 1;
229*cdf0e10cSrcweir 	_pRanges = new NUMTYPE[ nCount ];
230*cdf0e10cSrcweir 	memcpy( _pRanges, pArr, sizeof(NUMTYPE) * nCount );
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir //------------------------------------------------------------------------
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir sal_Bool SfxNumRanges::operator==( const SfxNumRanges &rOther ) const
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir 	// Object pointers equal?
238*cdf0e10cSrcweir 	if ( this == &rOther )
239*cdf0e10cSrcweir 		return sal_True;
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 	// Ranges pointers equal?
242*cdf0e10cSrcweir 	if ( _pRanges == rOther._pRanges )
243*cdf0e10cSrcweir 		return sal_True;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 	// Counts equal?
246*cdf0e10cSrcweir 	NUMTYPE nCount = Count();
247*cdf0e10cSrcweir 	if ( nCount != rOther.Count() )
248*cdf0e10cSrcweir 		return sal_False;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 	// Check arrays.
251*cdf0e10cSrcweir 	NUMTYPE n = 0;
252*cdf0e10cSrcweir 	while( _pRanges[ n ] != 0 )
253*cdf0e10cSrcweir 	{
254*cdf0e10cSrcweir 		// Elements at current position equal?
255*cdf0e10cSrcweir 		if ( _pRanges[ n ] != rOther._pRanges[ n ] )
256*cdf0e10cSrcweir 			return sal_False;
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 		++n;
259*cdf0e10cSrcweir 	}
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 	return sal_True;
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir //------------------------------------------------------------------------
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir SfxNumRanges& SfxNumRanges::operator =
267*cdf0e10cSrcweir (
268*cdf0e10cSrcweir 	const SfxNumRanges &rRanges
269*cdf0e10cSrcweir )
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir /**	<H3>Description</H3>
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 	Assigns ranges from 'rRanges' to '*this'.
274*cdf0e10cSrcweir */
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir {
277*cdf0e10cSrcweir 	// special case: assign itself
278*cdf0e10cSrcweir 	if ( &rRanges == this )
279*cdf0e10cSrcweir 		return *this;
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir 	delete[] _pRanges;
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 	// special case: 'rRanges' is empty
284*cdf0e10cSrcweir 	if ( rRanges.IsEmpty() )
285*cdf0e10cSrcweir 		_pRanges = 0;
286*cdf0e10cSrcweir 	else
287*cdf0e10cSrcweir 	{
288*cdf0e10cSrcweir 		// copy ranges
289*cdf0e10cSrcweir 		NUMTYPE nCount = Count_Impl( rRanges._pRanges ) + 1;
290*cdf0e10cSrcweir 		_pRanges = new NUMTYPE[ nCount ];
291*cdf0e10cSrcweir 		memcpy( _pRanges, rRanges._pRanges, sizeof(NUMTYPE) * nCount );
292*cdf0e10cSrcweir 	}
293*cdf0e10cSrcweir 	return *this;
294*cdf0e10cSrcweir }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir //------------------------------------------------------------------------
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir SfxNumRanges& SfxNumRanges::operator +=
299*cdf0e10cSrcweir (
300*cdf0e10cSrcweir 	const SfxNumRanges &rRanges
301*cdf0e10cSrcweir )
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir /**	<H3>Description</H3>
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 	Merges *this with 'rRanges'.
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir 	for each NUMTYPE n:
308*cdf0e10cSrcweir 		this->Contains( n ) || rRanges.Contains( n ) => this'->Contains( n )
309*cdf0e10cSrcweir 		!this->Contains( n ) && !rRanges.Contains( n ) => !this'->Contains( n )
310*cdf0e10cSrcweir */
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir 	// special cases: one is empty
314*cdf0e10cSrcweir 	if ( rRanges.IsEmpty() )
315*cdf0e10cSrcweir 		return *this;
316*cdf0e10cSrcweir 	if ( IsEmpty() )
317*cdf0e10cSrcweir 		return *this = rRanges;
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir 	// First, run thru _pRanges and rRanges._pRanges and determine the size of
320*cdf0e10cSrcweir 	// the new, merged ranges:
321*cdf0e10cSrcweir 	NUMTYPE nCount = 0;
322*cdf0e10cSrcweir 	const NUMTYPE * pRA = _pRanges;
323*cdf0e10cSrcweir 	const NUMTYPE * pRB = rRanges._pRanges;
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 	for (;;)
326*cdf0e10cSrcweir 	{
327*cdf0e10cSrcweir 		// The first pair of pRA has a lower lower bound than the first pair
328*cdf0e10cSrcweir 		// of pRB:
329*cdf0e10cSrcweir 		if (pRA[0] > pRB[0])
330*cdf0e10cSrcweir 			Swap_Impl(pRA, pRB);
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 		// We are done with the merging if at least pRA is exhausted:
333*cdf0e10cSrcweir 		if (!pRA[0])
334*cdf0e10cSrcweir 			break;
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 		for (;;)
337*cdf0e10cSrcweir 		{
338*cdf0e10cSrcweir 			// Skip those pairs in pRB that completely lie in the first pair
339*cdf0e10cSrcweir 			// of pRA:
340*cdf0e10cSrcweir 			while (pRB[1] <= pRA[1])
341*cdf0e10cSrcweir 			{
342*cdf0e10cSrcweir 				pRB += 2;
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir 				// Watch out for exhaustion of pRB:
345*cdf0e10cSrcweir 				if (!pRB[0])
346*cdf0e10cSrcweir 				{
347*cdf0e10cSrcweir 					Swap_Impl(pRA, pRB);
348*cdf0e10cSrcweir 					goto count_rest;
349*cdf0e10cSrcweir 				}
350*cdf0e10cSrcweir 			}
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 			// If the next pair of pRA does not at least touch the current new
353*cdf0e10cSrcweir 			// pair, we are done with the current new pair:
354*cdf0e10cSrcweir 			if (pRB[0] > pRA[1] + 1)
355*cdf0e10cSrcweir 				break;
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir 			// The next pair of pRB extends the current new pair; first,
358*cdf0e10cSrcweir 			// extend the current new pair (we are done if pRB is then
359*cdf0e10cSrcweir 			// exhausted); second, switch the roles of pRA and pRB in order to
360*cdf0e10cSrcweir 			// merge in those following pairs of the original pRA that will
361*cdf0e10cSrcweir 			// lie in the (now larger) current new pair or will even extend it
362*cdf0e10cSrcweir 			// further:
363*cdf0e10cSrcweir 			pRA += 2;
364*cdf0e10cSrcweir 			if (!pRA[0])
365*cdf0e10cSrcweir 				goto count_rest;
366*cdf0e10cSrcweir 			Swap_Impl(pRA, pRB);
367*cdf0e10cSrcweir 		}
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir 		// Done with the current new pair:
370*cdf0e10cSrcweir 		pRA += 2;
371*cdf0e10cSrcweir 		nCount += 2;
372*cdf0e10cSrcweir 	}
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir 	// Only pRB has more pairs available, pRA is already exhausted:
375*cdf0e10cSrcweir count_rest:
376*cdf0e10cSrcweir 	for (; pRB[0]; pRB += 2)
377*cdf0e10cSrcweir 		nCount += 2;
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 	// Now, create new ranges of the correct size and, on a second run thru
380*cdf0e10cSrcweir 	// _pRanges and rRanges._pRanges, copy the merged pairs into the new
381*cdf0e10cSrcweir 	// ranges:
382*cdf0e10cSrcweir 	NUMTYPE * pNew = new NUMTYPE[nCount + 1];
383*cdf0e10cSrcweir 	pRA = _pRanges;
384*cdf0e10cSrcweir 	pRB = rRanges._pRanges;
385*cdf0e10cSrcweir 	NUMTYPE * pRN = pNew;
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir 	for (;;)
388*cdf0e10cSrcweir 	{
389*cdf0e10cSrcweir 		// The first pair of pRA has a lower lower bound than the first pair
390*cdf0e10cSrcweir 		// of pRB:
391*cdf0e10cSrcweir 		if (pRA[0] > pRB[0])
392*cdf0e10cSrcweir 			Swap_Impl(pRA, pRB);
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir 		// We are done with the merging if at least pRA is exhausted:
395*cdf0e10cSrcweir 		if (!pRA[0])
396*cdf0e10cSrcweir 			break;
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir 		// Lower bound of current new pair is already known:
399*cdf0e10cSrcweir 		*pRN++ = pRA[0];
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir 		for (;;)
402*cdf0e10cSrcweir 		{
403*cdf0e10cSrcweir 			// Skip those pairs in pRB that completely lie in the first pair
404*cdf0e10cSrcweir 			// of pRA:
405*cdf0e10cSrcweir 			while (pRB[1] <= pRA[1])
406*cdf0e10cSrcweir 			{
407*cdf0e10cSrcweir 				pRB += 2;
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir 				// Watch out for exhaustion of pRB:
410*cdf0e10cSrcweir 				if (!pRB[0])
411*cdf0e10cSrcweir 				{
412*cdf0e10cSrcweir 					Swap_Impl(pRA, pRB);
413*cdf0e10cSrcweir 					++pRB;
414*cdf0e10cSrcweir 					goto copy_rest;
415*cdf0e10cSrcweir 				}
416*cdf0e10cSrcweir 			}
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 			// If the next pair of pRA does not at least touch the current new
419*cdf0e10cSrcweir 			// pair, we are done with the current new pair:
420*cdf0e10cSrcweir 			if (pRB[0] > pRA[1] + 1)
421*cdf0e10cSrcweir 				break;
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir 			// The next pair of pRB extends the current new pair; first,
424*cdf0e10cSrcweir 			// extend the current new pair (we are done if pRB is then
425*cdf0e10cSrcweir 			// exhausted); second, switch the roles of pRA and pRB in order to
426*cdf0e10cSrcweir 			// merge in those following pairs of the original pRA that will
427*cdf0e10cSrcweir 			// lie in the (now larger) current new pair or will even extend it
428*cdf0e10cSrcweir 			// further:
429*cdf0e10cSrcweir 			pRA += 2;
430*cdf0e10cSrcweir 			if (!pRA[0])
431*cdf0e10cSrcweir 			{
432*cdf0e10cSrcweir 				++pRB;
433*cdf0e10cSrcweir 				goto copy_rest;
434*cdf0e10cSrcweir 			}
435*cdf0e10cSrcweir 			Swap_Impl(pRA, pRB);
436*cdf0e10cSrcweir 		}
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir 		// Done with the current new pair, now upper bound is also known:
439*cdf0e10cSrcweir 		*pRN++ = pRA[1];
440*cdf0e10cSrcweir 		pRA += 2;
441*cdf0e10cSrcweir 	}
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 	// Only pRB has more pairs available (which are copied to the new ranges
444*cdf0e10cSrcweir 	// unchanged), pRA is already exhausted:
445*cdf0e10cSrcweir copy_rest:
446*cdf0e10cSrcweir 	for (; *pRB;)
447*cdf0e10cSrcweir 		*pRN++ = *pRB++;
448*cdf0e10cSrcweir 	*pRN = 0;
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir 	delete[] _pRanges;
451*cdf0e10cSrcweir 	_pRanges = pNew;
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir 	return *this;
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir //------------------------------------------------------------------------
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir SfxNumRanges& SfxNumRanges::operator -=
459*cdf0e10cSrcweir (
460*cdf0e10cSrcweir 	const SfxNumRanges &rRanges
461*cdf0e10cSrcweir )
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir /**	<H3>Description</H3>
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir 	Removes 'rRanges' from '*this'.
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir 	for each NUMTYPE n:
468*cdf0e10cSrcweir 		this->Contains( n ) && rRanges.Contains( n ) => !this'->Contains( n )
469*cdf0e10cSrcweir 		this->Contains( n ) && !rRanges.Contains( n ) => this'->Contains( n )
470*cdf0e10cSrcweir 		!this->Contains( n ) => !this'->Contains( n )
471*cdf0e10cSrcweir */
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir {
474*cdf0e10cSrcweir 	// special cases: one is empty
475*cdf0e10cSrcweir 	if ( rRanges.IsEmpty() || IsEmpty() )
476*cdf0e10cSrcweir 		return *this;
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir 	// differentiate 'rRanges' in a temporary copy of '*this'
479*cdf0e10cSrcweir 	// (size is computed for maximal possibly split-count plus terminating 0)
480*cdf0e10cSrcweir 	NUMTYPE nThisSize = Count_Impl(_pRanges);
481*cdf0e10cSrcweir 	NUMTYPE nTargetSize = 1 + (  nThisSize + Count_Impl(rRanges._pRanges) );
482*cdf0e10cSrcweir 	NUMTYPE *pTarget = new NUMTYPE[ nTargetSize ];
483*cdf0e10cSrcweir 	memset( pTarget, 0, sizeof(NUMTYPE)*nTargetSize );
484*cdf0e10cSrcweir 	memcpy( pTarget, _pRanges, sizeof(NUMTYPE)*nThisSize );
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir 	NUMTYPE nPos1 = 0, nPos2 = 0, nTargetPos = 0;
487*cdf0e10cSrcweir 	while( _pRanges[ nPos1 ] )
488*cdf0e10cSrcweir 	{
489*cdf0e10cSrcweir 		NUMTYPE l1 = _pRanges[ nPos1 ]; 	 // lower bound of interval 1
490*cdf0e10cSrcweir 		NUMTYPE u1 = _pRanges[ nPos1+1 ];	 // upper bound of interval 1
491*cdf0e10cSrcweir 		NUMTYPE l2 = rRanges._pRanges[ nPos2 ]; 	 // lower bound of interval 2
492*cdf0e10cSrcweir 		NUMTYPE u2 = rRanges._pRanges[ nPos2+1 ];	 // upper bound of interval 2
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir 		// boundary cases
495*cdf0e10cSrcweir 		// * subtrahend is empty -> copy the minuend
496*cdf0e10cSrcweir 		if( !l2 )
497*cdf0e10cSrcweir 		{
498*cdf0e10cSrcweir 			pTarget[ nTargetPos ] = l1;
499*cdf0e10cSrcweir 			pTarget[ nTargetPos+1 ] = u1;
500*cdf0e10cSrcweir 			nTargetPos += 2;
501*cdf0e10cSrcweir 			nPos1 += 2;
502*cdf0e10cSrcweir 			continue;
503*cdf0e10cSrcweir 		}
504*cdf0e10cSrcweir 		// * next subtrahend interval is completely higher -> copy the minuend
505*cdf0e10cSrcweir 		if( u1 < l2 )
506*cdf0e10cSrcweir 		{
507*cdf0e10cSrcweir 			pTarget[ nTargetPos ] = l1;
508*cdf0e10cSrcweir 			pTarget[ nTargetPos+1 ] = u1;
509*cdf0e10cSrcweir 			nTargetPos += 2;
510*cdf0e10cSrcweir 			nPos1 += 2;
511*cdf0e10cSrcweir 			continue;
512*cdf0e10cSrcweir 		}
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir 		// * next subtrahend interval is completely lower -> try next
515*cdf0e10cSrcweir 		if( u2 < l1 )
516*cdf0e10cSrcweir 		{
517*cdf0e10cSrcweir 			nPos2 += 2;
518*cdf0e10cSrcweir 			continue;
519*cdf0e10cSrcweir 		}
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir 		// intersecting cases
522*cdf0e10cSrcweir 		// * subtrahend cuts out from the beginning of the minuend
523*cdf0e10cSrcweir 		if( l2 <= l1 && u2 <= u1 )
524*cdf0e10cSrcweir 		{
525*cdf0e10cSrcweir 			// reduce minuend interval, try again (minuend might be affected by other subtrahend intervals)
526*cdf0e10cSrcweir 			_pRanges[ nPos1 ] = u2 + 1;
527*cdf0e10cSrcweir 			nPos2 += 2; // this cannot hurt any longer
528*cdf0e10cSrcweir 			continue;
529*cdf0e10cSrcweir 		}
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir 		// * subtrahend cuts out from the end of the minuend
532*cdf0e10cSrcweir 		if( l1 <= l2 && u1 <= u2 )
533*cdf0e10cSrcweir 		{
534*cdf0e10cSrcweir 			// copy remaining part of minuend (cannot be affected by other intervals)
535*cdf0e10cSrcweir 			if( l1 < l2 ) // anything left at all?
536*cdf0e10cSrcweir 			{
537*cdf0e10cSrcweir 				pTarget[ nTargetPos ] = l1;
538*cdf0e10cSrcweir 				pTarget[ nTargetPos+1 ] = l2 - 1;
539*cdf0e10cSrcweir 				nTargetPos += 2;
540*cdf0e10cSrcweir 				// do not increment nPos2, might affect next minuend interval, too
541*cdf0e10cSrcweir 			}
542*cdf0e10cSrcweir 			nPos1 += 2; // nothing left at all
543*cdf0e10cSrcweir 			continue;
544*cdf0e10cSrcweir 		}
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir 		// * subtrahend completely deletes minuend (larger or same at both ends)
547*cdf0e10cSrcweir 		if( l1 >= l2 && u1 <= u2 )
548*cdf0e10cSrcweir 		{
549*cdf0e10cSrcweir 			nPos1 += 2; // minuend deleted
550*cdf0e10cSrcweir 			// do not increment nPos2, might affect next minuend interval, too
551*cdf0e10cSrcweir 			continue;
552*cdf0e10cSrcweir 		}
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir 		// * subtrahend divides minuend into two pieces
555*cdf0e10cSrcweir 		if( l1 <= l2 && u1 >= u2 ) // >= and <= since they may be something left only at one side
556*cdf0e10cSrcweir 		{
557*cdf0e10cSrcweir 			// left side
558*cdf0e10cSrcweir 			if( l1 < l2 ) // anything left at all
559*cdf0e10cSrcweir 			{
560*cdf0e10cSrcweir 				pTarget[ nTargetPos ] = l1;
561*cdf0e10cSrcweir 				pTarget[ nTargetPos+1 ] = l2 - 1;
562*cdf0e10cSrcweir 				nTargetPos += 2;
563*cdf0e10cSrcweir 			}
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir 			// right side
566*cdf0e10cSrcweir 			if( u1 > u2 ) // anything left at all
567*cdf0e10cSrcweir 			{
568*cdf0e10cSrcweir 				// reduce minuend interval, try again (minuend might be affected by other subtrahend itnervals )
569*cdf0e10cSrcweir 				_pRanges[ nPos1 ] = u2 + 1;
570*cdf0e10cSrcweir 			}
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir 			// subtrahend is completely used
573*cdf0e10cSrcweir 			nPos2 += 2;
574*cdf0e10cSrcweir 			continue;
575*cdf0e10cSrcweir 		}
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir 		// we should never be here
578*cdf0e10cSrcweir 		DBG_ERROR( "SfxNumRanges::operator-=: internal error" );
579*cdf0e10cSrcweir 	} // while
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir 	pTarget[ nTargetPos ] = 0;
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir 	// assign the differentiated ranges
584*cdf0e10cSrcweir 	delete[] _pRanges;
585*cdf0e10cSrcweir 
586*cdf0e10cSrcweir 	NUMTYPE nUShorts = Count_Impl(pTarget) + 1;
587*cdf0e10cSrcweir 	if ( 1 != nUShorts )
588*cdf0e10cSrcweir 	{
589*cdf0e10cSrcweir 		_pRanges = new NUMTYPE[ nUShorts ];
590*cdf0e10cSrcweir 		memcpy( _pRanges, pTarget, nUShorts * sizeof(NUMTYPE) );
591*cdf0e10cSrcweir 	}
592*cdf0e10cSrcweir 	else
593*cdf0e10cSrcweir 		_pRanges = 0;
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir 	delete [] pTarget;
596*cdf0e10cSrcweir 	return *this;
597*cdf0e10cSrcweir 
598*cdf0e10cSrcweir 	/* untested code from MI commented out (MDA, 28.01.97)
599*cdf0e10cSrcweir 	do
600*cdf0e10cSrcweir 	{
601*cdf0e10cSrcweir 		// 1st range is smaller than 2nd range?
602*cdf0e10cSrcweir 		if ( pRange1[1] < pRange2[0] )
603*cdf0e10cSrcweir 			// => keep 1st range
604*cdf0e10cSrcweir 			pRange1 += 2;
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir 		// 2nd range is smaller than 1st range?
607*cdf0e10cSrcweir 		else if ( pRange2[1] < pRange1[0] )
608*cdf0e10cSrcweir 			// => skip 2nd range
609*cdf0e10cSrcweir 			pRange2 += 2;
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir 		// 2nd range totally overlaps the 1st range?
612*cdf0e10cSrcweir 		else if ( pRange2[0] <= pRange1[0] && pRange2[1] >= pRange1[1] )
613*cdf0e10cSrcweir 			// => remove 1st range
614*cdf0e10cSrcweir 			memmove( pRange1, pRange1+2, sizeof(NUMTYPE) * (pEndOfTarget-pRange1+2) );
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir 		// 2nd range overlaps only the beginning of 1st range?
617*cdf0e10cSrcweir 		else if ( pRange2[0] <= pRange1[0] && pRange2[1] < pRange1[1] )
618*cdf0e10cSrcweir 		{
619*cdf0e10cSrcweir 			// => cut the beginning of 1st range and goto next 2nd range
620*cdf0e10cSrcweir 			pRange1[0] = pRange2[1] + 1;
621*cdf0e10cSrcweir 			pRange2 += 2;
622*cdf0e10cSrcweir 		}
623*cdf0e10cSrcweir 
624*cdf0e10cSrcweir 		// 2nd range overlaps only the end of 1st range?
625*cdf0e10cSrcweir 		else if ( pRange2[0] > pRange1[0] && pRange2[1] >= pRange1[0] )
626*cdf0e10cSrcweir 			// => cut the beginning of 1st range
627*cdf0e10cSrcweir 			pRange1[0] = pRange2[1]+1;
628*cdf0e10cSrcweir 
629*cdf0e10cSrcweir 		// 2nd range is a real subset of 1st range
630*cdf0e10cSrcweir 		else
631*cdf0e10cSrcweir 		{
632*cdf0e10cSrcweir 			// => split 1st range and goto next 2nd range
633*cdf0e10cSrcweir 			memmove( pRange1+3, pRange1+1, sizeof(NUMTYPE) * (pEndOfTarget-pRange1-1) );
634*cdf0e10cSrcweir 			pRange1[1] = pRange2[0] - 1;
635*cdf0e10cSrcweir 			pRange1[2] = pRange2[1] + 1;
636*cdf0e10cSrcweir 			pRange1 += 2;
637*cdf0e10cSrcweir 			pRange2 += 2;
638*cdf0e10cSrcweir 		}
639*cdf0e10cSrcweir 	}
640*cdf0e10cSrcweir 	while ( *pRange1 && *pRange2 );
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir 	// assign the differentiated ranges
643*cdf0e10cSrcweir 	delete[] _pRanges;
644*cdf0e10cSrcweir 	NUMTYPE nUShorts = Count_Impl(pTarget) + 1;
645*cdf0e10cSrcweir 	if ( 1 != nUShorts )
646*cdf0e10cSrcweir 	{
647*cdf0e10cSrcweir 		_pRanges = new NUMTYPE[ nUShorts ];
648*cdf0e10cSrcweir 		memcpy( _pRanges, pTarget, nUShorts * sizeof(NUMTYPE) );
649*cdf0e10cSrcweir 		_pRanges[ nUShorts-1 ] = 0;
650*cdf0e10cSrcweir 	}
651*cdf0e10cSrcweir 	else
652*cdf0e10cSrcweir 		_pRanges = 0;
653*cdf0e10cSrcweir 	return *this;
654*cdf0e10cSrcweir 	*/
655*cdf0e10cSrcweir }
656*cdf0e10cSrcweir 
657*cdf0e10cSrcweir //------------------------------------------------------------------------
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir SfxNumRanges& SfxNumRanges::operator /=
660*cdf0e10cSrcweir (
661*cdf0e10cSrcweir 	const SfxNumRanges &rRanges
662*cdf0e10cSrcweir )
663*cdf0e10cSrcweir 
664*cdf0e10cSrcweir /**	<H3>Description</H3>
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir 	Determines intersection of '*this' with 'rRanges'.
667*cdf0e10cSrcweir 
668*cdf0e10cSrcweir 	for each NUMTYPE n:
669*cdf0e10cSrcweir 		this->Contains( n ) && rRanges.Contains( n ) => this'->Contains( n )
670*cdf0e10cSrcweir 		!this->Contains( n ) => !this'->Contains( n )
671*cdf0e10cSrcweir 		!rRanges.Contains( n ) => !this'->Contains( n )
672*cdf0e10cSrcweir */
673*cdf0e10cSrcweir 
674*cdf0e10cSrcweir {
675*cdf0e10cSrcweir 	// boundary cases
676*cdf0e10cSrcweir 	// * first set is empty -> nothing to be done
677*cdf0e10cSrcweir 	// * second set is empty -> delete first set
678*cdf0e10cSrcweir 	if( rRanges.IsEmpty() )
679*cdf0e10cSrcweir 	{
680*cdf0e10cSrcweir 		delete[] _pRanges;
681*cdf0e10cSrcweir 
682*cdf0e10cSrcweir 		_pRanges = new NUMTYPE[1];
683*cdf0e10cSrcweir 		_pRanges[0] = 0;
684*cdf0e10cSrcweir 
685*cdf0e10cSrcweir 		return *this;
686*cdf0e10cSrcweir 	}
687*cdf0e10cSrcweir 
688*cdf0e10cSrcweir 	// intersect 'rRanges' in a temporary copy of '*this'
689*cdf0e10cSrcweir 	// (size is computed for maximal possibly split-count plus terminating 0)
690*cdf0e10cSrcweir 	NUMTYPE nThisSize = Count_Impl(_pRanges);
691*cdf0e10cSrcweir 	NUMTYPE nTargetSize = 1 + (  nThisSize + Count_Impl(rRanges._pRanges) );
692*cdf0e10cSrcweir 	NUMTYPE *pTarget = new NUMTYPE[ nTargetSize ];
693*cdf0e10cSrcweir 	memset( pTarget, 0, sizeof(NUMTYPE)*nTargetSize );
694*cdf0e10cSrcweir 	memcpy( pTarget, _pRanges, sizeof(NUMTYPE)*nThisSize );
695*cdf0e10cSrcweir 
696*cdf0e10cSrcweir 	NUMTYPE nPos1 = 0, nPos2 = 0, nTargetPos = 0;
697*cdf0e10cSrcweir 	while( _pRanges[ nPos1 ] != 0 && rRanges._pRanges[ nPos2 ] != 0 )
698*cdf0e10cSrcweir 	{
699*cdf0e10cSrcweir 		NUMTYPE l1 = _pRanges[ nPos1 ]; 	 // lower bound of interval 1
700*cdf0e10cSrcweir 		NUMTYPE u1 = _pRanges[ nPos1+1 ];	 // upper bound of interval 1
701*cdf0e10cSrcweir 		NUMTYPE l2 = rRanges._pRanges[ nPos2 ]; 	 // lower bound of interval 2
702*cdf0e10cSrcweir 		NUMTYPE u2 = rRanges._pRanges[ nPos2+1 ];	 // upper bound of interval 2
703*cdf0e10cSrcweir 
704*cdf0e10cSrcweir 		if( u1 < l2 )
705*cdf0e10cSrcweir 		{
706*cdf0e10cSrcweir 			// current interval in s1 is completely before ci in s2
707*cdf0e10cSrcweir 			nPos1 += 2;
708*cdf0e10cSrcweir 			continue;
709*cdf0e10cSrcweir 		}
710*cdf0e10cSrcweir 		if( u2 < l1 )
711*cdf0e10cSrcweir 		{
712*cdf0e10cSrcweir 			// ci in s2 is completely before ci in s1
713*cdf0e10cSrcweir 			nPos2 += 2;
714*cdf0e10cSrcweir 			continue;
715*cdf0e10cSrcweir 		}
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir 		// assert: there exists an intersection between ci1 and ci2
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir 		if( l1 <= l2 )
720*cdf0e10cSrcweir 		{
721*cdf0e10cSrcweir 			// c1 "is more to the left" than c2
722*cdf0e10cSrcweir 
723*cdf0e10cSrcweir 			if( u1 <= u2 )
724*cdf0e10cSrcweir 			{
725*cdf0e10cSrcweir 				pTarget[ nTargetPos ] = l2;
726*cdf0e10cSrcweir 				pTarget[ nTargetPos+1 ] = u1;
727*cdf0e10cSrcweir 				nTargetPos += 2;
728*cdf0e10cSrcweir 				nPos1 += 2;
729*cdf0e10cSrcweir 				continue;
730*cdf0e10cSrcweir 			}
731*cdf0e10cSrcweir 			else
732*cdf0e10cSrcweir 			{
733*cdf0e10cSrcweir 				pTarget[ nTargetPos ] = l2;
734*cdf0e10cSrcweir 				pTarget[ nTargetPos+1 ] = u2;
735*cdf0e10cSrcweir 				nTargetPos += 2;
736*cdf0e10cSrcweir 				nPos2 += 2;
737*cdf0e10cSrcweir 			}
738*cdf0e10cSrcweir 		}
739*cdf0e10cSrcweir 		else
740*cdf0e10cSrcweir 		{
741*cdf0e10cSrcweir 			// c2 "is more to the left" than c1"
742*cdf0e10cSrcweir 
743*cdf0e10cSrcweir 			if( u1 > u2 )
744*cdf0e10cSrcweir 			{
745*cdf0e10cSrcweir 				pTarget[ nTargetPos ] = l1;
746*cdf0e10cSrcweir 				pTarget[ nTargetPos+1 ] = u2;
747*cdf0e10cSrcweir 				nTargetPos += 2;
748*cdf0e10cSrcweir 				nPos2 += 2;
749*cdf0e10cSrcweir 			}
750*cdf0e10cSrcweir 			else
751*cdf0e10cSrcweir 			{
752*cdf0e10cSrcweir 				pTarget[ nTargetPos ] = l1;
753*cdf0e10cSrcweir 				pTarget[ nTargetPos+1 ] = u1;
754*cdf0e10cSrcweir 				nTargetPos += 2;
755*cdf0e10cSrcweir 				nPos1 += 2;
756*cdf0e10cSrcweir 			}
757*cdf0e10cSrcweir 		}
758*cdf0e10cSrcweir 	}; // while
759*cdf0e10cSrcweir 	pTarget[ nTargetPos ] = 0;
760*cdf0e10cSrcweir 
761*cdf0e10cSrcweir 	// assign the intersected ranges
762*cdf0e10cSrcweir 	delete[] _pRanges;
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir 	NUMTYPE nUShorts = Count_Impl(pTarget) + 1;
765*cdf0e10cSrcweir 	if ( 1 != nUShorts )
766*cdf0e10cSrcweir 	{
767*cdf0e10cSrcweir 		_pRanges = new NUMTYPE[ nUShorts ];
768*cdf0e10cSrcweir 		memcpy( _pRanges, pTarget, nUShorts * sizeof(NUMTYPE) );
769*cdf0e10cSrcweir 	}
770*cdf0e10cSrcweir 	else
771*cdf0e10cSrcweir 		_pRanges = 0;
772*cdf0e10cSrcweir 
773*cdf0e10cSrcweir 	delete [] pTarget;
774*cdf0e10cSrcweir 	return *this;
775*cdf0e10cSrcweir }
776*cdf0e10cSrcweir 
777*cdf0e10cSrcweir //------------------------------------------------------------------------
778*cdf0e10cSrcweir 
779*cdf0e10cSrcweir sal_Bool SfxNumRanges::Intersects( const SfxNumRanges &rRanges ) const
780*cdf0e10cSrcweir 
781*cdf0e10cSrcweir /**	<H3>Description</H3>
782*cdf0e10cSrcweir 
783*cdf0e10cSrcweir 	Determines if at least one range in 'rRanges' intersects with one
784*cdf0e10cSrcweir 	range in '*this'.
785*cdf0e10cSrcweir 
786*cdf0e10cSrcweir 	sal_True, if there is at least one with:
787*cdf0e10cSrcweir 		this->Contains( n ) && rRanges.Contains( n )
788*cdf0e10cSrcweir */
789*cdf0e10cSrcweir 
790*cdf0e10cSrcweir {
791*cdf0e10cSrcweir 	// special cases: one is empty
792*cdf0e10cSrcweir 	if ( rRanges.IsEmpty() || IsEmpty() )
793*cdf0e10cSrcweir 		return sal_False;
794*cdf0e10cSrcweir 
795*cdf0e10cSrcweir 	// find at least one intersecting range
796*cdf0e10cSrcweir 	const NUMTYPE *pRange1 = _pRanges;
797*cdf0e10cSrcweir 	const NUMTYPE *pRange2 = rRanges._pRanges;
798*cdf0e10cSrcweir 
799*cdf0e10cSrcweir 	do
800*cdf0e10cSrcweir 	{
801*cdf0e10cSrcweir 		// 1st range is smaller than 2nd range?
802*cdf0e10cSrcweir 		if ( pRange1[1] < pRange2[0] )
803*cdf0e10cSrcweir 			// => keep 1st range
804*cdf0e10cSrcweir 			pRange1 += 2;
805*cdf0e10cSrcweir 
806*cdf0e10cSrcweir 		// 2nd range is smaller than 1st range?
807*cdf0e10cSrcweir 		else if ( pRange2[1] < pRange1[0] )
808*cdf0e10cSrcweir 			// => skip 2nd range
809*cdf0e10cSrcweir 			pRange2 += 2;
810*cdf0e10cSrcweir 
811*cdf0e10cSrcweir 		// the ranges are overlappung
812*cdf0e10cSrcweir 		else
813*cdf0e10cSrcweir 			return sal_True;
814*cdf0e10cSrcweir 	}
815*cdf0e10cSrcweir 	while ( *pRange2 );
816*cdf0e10cSrcweir 
817*cdf0e10cSrcweir 	// no intersection found
818*cdf0e10cSrcweir 	return sal_False;
819*cdf0e10cSrcweir }
820*cdf0e10cSrcweir 
821*cdf0e10cSrcweir //------------------------------------------------------------------------
822*cdf0e10cSrcweir 
823*cdf0e10cSrcweir NUMTYPE SfxNumRanges::Count() const
824*cdf0e10cSrcweir 
825*cdf0e10cSrcweir /**	<H3>Description</H3>
826*cdf0e10cSrcweir 
827*cdf0e10cSrcweir 	Determines the number of USHORTs in the set described by the ranges
828*cdf0e10cSrcweir 	of USHORTs in '*this'.
829*cdf0e10cSrcweir */
830*cdf0e10cSrcweir 
831*cdf0e10cSrcweir {
832*cdf0e10cSrcweir 	return Capacity_Impl( _pRanges );
833*cdf0e10cSrcweir }
834*cdf0e10cSrcweir 
835*cdf0e10cSrcweir //------------------------------------------------------------------------
836*cdf0e10cSrcweir 
837*cdf0e10cSrcweir sal_Bool SfxNumRanges::Contains( NUMTYPE n ) const
838*cdf0e10cSrcweir 
839*cdf0e10cSrcweir /**	<H3>Description</H3>
840*cdf0e10cSrcweir 
841*cdf0e10cSrcweir 	Determines if '*this' contains 'n'.
842*cdf0e10cSrcweir */
843*cdf0e10cSrcweir 
844*cdf0e10cSrcweir {
845*cdf0e10cSrcweir 	for ( NUMTYPE *pRange = _pRanges; *pRange && *pRange <= n; pRange += 2 )
846*cdf0e10cSrcweir 		if ( pRange[0] <= n && n <= pRange[1] )
847*cdf0e10cSrcweir 			return sal_True;
848*cdf0e10cSrcweir 	return sal_False;
849*cdf0e10cSrcweir 
850*cdf0e10cSrcweir }
851