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 ) {
167 template< class type >
168 inline int idList<type>::Size( void ) {
177 template< class type >
178 inline void idList<type>::Resize( int size ) {
191 if ( m_size < m_num ) {
195 m_list = new type[ m_size ];
196 for ( i = 0; i < m_num; i++ ) {
197 m_list[ i ] = temp[ i ];
207 idList<type>::operator[] const
210 template< class type >
211 inline type idList<type>::operator[]( int index ) const {
212 assert( index >= 0 );
213 assert( index < m_num );
215 return m_list[ index ];
220 idList<type>::operator[]
223 template< class type >
224 inline type &idList<type>::operator[]( int index ) {
225 assert( index >= 0 );
226 assert( index < m_num );
228 return m_list[ index ];
236 template< class type >
237 inline int idList<type>::Append( type const & obj ) {
239 Resize( m_granularity );
242 if ( m_num == m_size ) {
243 Resize( m_size + m_granularity );
246 m_list[ m_num ] = obj;
254 idList<type>::AddUnique
257 template< class type >
258 inline int idList<type>::AddUnique( type const & obj ) {
261 if ( !Find( obj, &index ) ) {
262 index = Append( obj );
273 template< class type >
274 inline type *idList<type>::Find( type const & obj, int *index ) {
277 for ( i = 0; i < m_num; i++ ) {
278 if ( m_list[ i ] == obj ) {
291 idList<type>::RemoveIndex
294 template< class type >
295 inline bool idList<type>::RemoveIndex( int index ) {
298 if ( !m_list || !m_num ) {
302 assert( index >= 0 );
303 assert( index < m_num );
305 if ( ( index < 0 ) || ( index >= m_num ) ) {
310 for ( i = index; i < m_num; i++ ) {
311 m_list[ i ] = m_list[ i + 1 ];
322 template< class type >
323 inline bool idList<type>::Remove( type const & obj ) {
326 if ( Find( obj, &index ) ) {
327 return RemoveIndex( index );
338 template< class type >
339 inline void idList<type>::Sort( cmp_t *compare ) {
344 qsort( ( void * )m_list, ( size_t )m_num, sizeof( type ), compare );
347 #endif /* !__UTIL_LIST_H__ */