2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef __UTIL_LIST_H__
23 #define __UTIL_LIST_H__
28 template< class type >
37 idList( int granularity = 16 );
41 void SetNum( int num );
42 void SetGranularity( int granularity );
43 void Condense( void );
45 void Resize( int size );
46 type operator[]( int index ) const;
47 type &operator[]( int index );
48 int Append( type const & obj );
49 int AddUnique( type const & obj );
50 type *Find( type const & obj, int *index = NULL );
51 bool RemoveIndex( int index );
52 bool Remove( type const & obj );
53 typedef int cmp_t(const void *, const void *);
54 void Sort( cmp_t *compare );
59 idList<type>::idList( int )
62 template< class type >
63 inline idList<type>::idList( int granularity ) {
64 assert( granularity > 0 );
67 m_granularity = granularity;
73 idList<type>::~idList<type>
76 template< class type >
77 inline idList<type>::~idList() {
86 template< class type >
87 inline void idList<type>::Clear( void ) {
102 template< class type >
103 inline int idList<type>::Num( void ) {
112 template< class type >
113 inline void idList<type>::SetNum( int num ) {
115 if ( num > m_size ) {
116 // resize it up to the closest level of granularity
117 Resize( ( ( num + m_granularity - 1 ) / m_granularity ) * m_granularity );
124 idList<type>::SetGranularity
127 template< class type >
128 inline void idList<type>::SetGranularity( int granularity ) {
131 assert( granularity > 0 );
132 m_granularity = granularity;
135 // resize it to the closest level of granularity
136 newsize = ( ( m_num + m_granularity - 1 ) / m_granularity ) * m_granularity;
137 if ( newsize != m_size ) {
145 idList<type>::Condense
147 Resizes the array to exactly the number of elements it contains
150 template< class type >
151 inline void idList<type>::Condense( void ) {
166 template< class type >
167 inline int idList<type>::Size( void ) {
176 template< class type >
177 inline void idList<type>::Resize( int size ) {
190 if ( m_size < m_num ) {
194 m_list = new type[ m_size ];
195 for( i = 0; i < m_num; i++ ) {
196 m_list[ i ] = temp[ i ];
206 idList<type>::operator[] const
209 template< class type >
210 inline type idList<type>::operator[]( int index ) const {
211 assert( index >= 0 );
212 assert( index < m_num );
214 return m_list[ index ];
219 idList<type>::operator[]
222 template< class type >
223 inline type &idList<type>::operator[]( int index ) {
224 assert( index >= 0 );
225 assert( index < m_num );
227 return m_list[ index ];
235 template< class type >
236 inline int idList<type>::Append( type const & obj ) {
238 Resize( m_granularity );
241 if ( m_num == m_size ) {
242 Resize( m_size + m_granularity );
245 m_list[ m_num ] = obj;
253 idList<type>::AddUnique
256 template< class type >
257 inline int idList<type>::AddUnique( type const & obj ) {
260 if ( !Find( obj, &index ) ) {
261 index = Append( obj );
272 template< class type >
273 inline type *idList<type>::Find( type const & obj, int *index ) {
276 for( i = 0; i < m_num; i++ ) {
277 if ( m_list[ i ] == obj ) {
290 idList<type>::RemoveIndex
293 template< class type >
294 inline bool idList<type>::RemoveIndex( int index ) {
297 if ( !m_list || !m_num ) {
301 assert( index >= 0 );
302 assert( index < m_num );
304 if ( ( index < 0 ) || ( index >= m_num ) ) {
309 for( i = index; i < m_num; i++ ) {
310 m_list[ i ] = m_list[ i + 1 ];
321 template< class type >
322 inline bool idList<type>::Remove( type const & obj ) {
325 if ( Find( obj, &index ) ) {
326 return RemoveIndex( index );
337 template< class type >
338 inline void idList<type>::Sort( cmp_t *compare ) {
343 qsort( ( void * )m_list, ( size_t )m_num, sizeof( type ), compare );
346 #endif /* !__UTIL_LIST_H__ */