1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: list.hxx,v $
10 * $Revision: 1.6.10.1 $
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 ************************************************************************/
35 * Re-implement a simple container: LinkedList + LinkedListIterator
37 * DO NOT USE EXCEPT FOR REPLACING THE ORIGINAL LinkedList/LinkedListIterator!
38 * USE STL CONTAINERS FOR NEW CODE!
40 * The classes LinkedList and LinkedListIterator were originally
41 * implemented in two files LinkedList.cpp/.h, whose license would not
42 * allow re-distribution through OpenOffice.org. This file
43 * re-implements the same functionality, based on the STL.
51 typedef std::vector
<T
*> list_t
;
55 /// construct list with one element (pItem) or no element (pItem == NULL)
56 LinkedList( T
* pItem
= NULL
);
59 T
* find( const int n
); /// return nth element in list
60 T
* first(); /// return first element in list
61 T
* last(); /// return last element in list
63 int count() const; /// return number of elements in list
64 int empty() const; /// determine whether list contains any elements
66 /// insert pItem into list at position n; at end if n == -1; return count()
67 int insert( T
* pItem
, int n
= -1 );
69 /// remove nth element from list
70 T
* remove( const int n
);
72 /// remove given element from list
73 int remove( T
* pItem
);
76 /** iterator class for LinkedList<T>. Iterator may travel outside of
77 * list using operator++/--, in which case current() must return
80 class LinkedListIterator
82 // iterator state: reference list + position
83 LinkedList
<T
>* mpList
;
87 /// construct list with single element
88 LinkedListIterator( LinkedList
<T
>* pList
= NULL
);
89 ~LinkedListIterator();
91 T
* current(); /// return current element, or NULL if invalid
92 void set( const int n
); /// set iterator to position n
94 void reset( ); /// set iterator to first element
96 // bug-compatible with original LinkedList.h/cpp: Ignore parameter!
97 void operator++( int ); /// advance iterator by one step (ignore n !!!)
98 void operator--( int ); /// go one step backwards (ignore n !!!)
109 // (the implementation of template classes must be accessible to using
110 // code, hence this implementation is in the header.)
115 // define assert based on SAL, so we do not introduce a tools dependency
116 #include <osl/diagnose.h>
117 #define ASSERT(x) OSL_ENSURE((x), " HWP FILTER: " #x)
121 LinkedList
<T
>::LinkedList( T
* pItem
)
124 maList
.push_back( pItem
);
128 LinkedList
<T
>::~LinkedList()
133 T
* LinkedList
<T
>::find( const int n
)
135 ASSERT( n
>= 0 && n
< static_cast<int>( maList
.size() ) );
140 T
* LinkedList
<T
>::first()
146 T
* LinkedList
<T
>::last()
148 return find( count() - 1 );
152 int LinkedList
<T
>::count() const
154 return static_cast<int>( maList
.size() );
158 int LinkedList
<T
>::empty() const
164 int LinkedList
<T
>::insert( T
* pItem
, int n
)
166 ASSERT( pItem
!= NULL
);
167 ASSERT( n
>= -1 && n
<= static_cast<int>( maList
.size() ));
171 maList
.push_back( pItem
);
175 maList
.insert( maList
.begin() + n
, pItem
);
178 return static_cast<int>( maList
.size() );
182 T
* LinkedList
<T
>::remove( const int n
)
184 ASSERT( n
>= -1 && n
<= static_cast<int>( maList
.size() ) );
186 T
* pItem
= maList
[ n
];
187 ASSERT( pItem
!= NULL
);
189 maList
.erase( maList
.begin() + n
);
194 int LinkedList
<T
>::remove( T
* pItem
)
196 ASSERT( pItem
!= NULL
);
199 typename
list_t::iterator aIter
= maList
.begin();
200 typename
list_t::iterator aEnd
= maList
.end();
201 while( aIter
!= aEnd
&& *aIter
!= pItem
)
210 ASSERT( *aIter
== pItem
);
211 maList
.erase( aIter
);
225 LinkedListIterator
<T
>::LinkedListIterator( LinkedList
<T
>* pList
) :
229 ASSERT( pList
!= NULL
);
233 LinkedListIterator
<T
>::~LinkedListIterator()
238 T
* LinkedListIterator
<T
>::current()
240 return valid() ? mpList
->find( mnPosition
) : NULL
;
244 void LinkedListIterator
<T
>::set( const int nIndex
)
246 ASSERT( mpList
!= NULL
);
253 void LinkedListIterator
<T
>::reset()
255 ASSERT( mpList
!= NULL
);
261 void LinkedListIterator
<T
>::operator++( int )
263 ASSERT( mpList
!= NULL
);
265 // bug-compatible with LinkedList.cpp: ignore parameter!
270 void LinkedListIterator
<T
>::operator--( int )
272 ASSERT( mpList
!= NULL
);
274 // bug-compatible with LinkedList.cpp: ignore parameter!
279 bool LinkedListIterator
<T
>::valid()
281 return mpList
!= NULL
283 && mnPosition
< mpList
->count();