]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/idlib/containers/StrList.h
hello world
[icculus/iodoom3.git] / neo / idlib / containers / StrList.h
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #ifndef __STRLIST_H__
30 #define __STRLIST_H__
31
32 /*
33 ===============================================================================
34
35         idStrList
36
37 ===============================================================================
38 */
39
40 typedef idList<idStr> idStrList;
41 typedef idList<idStr*> idStrPtrList;
42 typedef idStr *idStrPtr;
43
44 /*
45 ================
46 idListSortCompare<idStrPtr>
47
48 Compares two pointers to strings. Used to sort a list of string pointers alphabetically in idList<idStr>::Sort.
49 ================
50 */
51 template<>
52 ID_INLINE int idListSortCompare<idStrPtr>( const idStrPtr *a, const idStrPtr *b ) {
53         return ( *a )->Icmp( **b );
54 }
55
56 /*
57 ================
58 idStrList::Sort
59
60 Sorts the list of strings alphabetically. Creates a list of pointers to the actual strings and sorts the
61 pointer list. Then copies the strings into another list using the ordered list of pointers.
62 ================
63 */
64 template<>
65 ID_INLINE void idStrList::Sort( cmp_t *compare ) {
66         int i;
67
68         if ( !num ) {
69                 return;
70         }
71
72         idList<idStr>           other;
73         idList<idStrPtr>        pointerList;
74
75         pointerList.SetNum( num );
76         for( i = 0; i < num; i++ ) {
77                 pointerList[ i ] = &( *this )[ i ];
78         }
79
80         pointerList.Sort();
81
82         other.SetNum( num );
83         other.SetGranularity( granularity );
84         for( i = 0; i < other.Num(); i++ ) {
85                 other[ i ] = *pointerList[ i ];
86         }
87
88         this->Swap( other );
89 }
90
91 /*
92 ================
93 idStrList::SortSubSection
94
95 Sorts a subsection of the list of strings alphabetically.
96 ================
97 */
98 template<>
99 ID_INLINE void idStrList::SortSubSection( int startIndex, int endIndex, cmp_t *compare ) {
100         int i, s;
101
102         if ( !num ) {
103                 return;
104         }
105         if ( startIndex < 0 ) {
106                 startIndex = 0;
107         }
108         if ( endIndex >= num ) {
109                 endIndex = num - 1;
110         }
111         if ( startIndex >= endIndex ) {
112                 return;
113         }
114
115         idList<idStr>           other;
116         idList<idStrPtr>        pointerList;
117
118         s = endIndex - startIndex + 1;
119         other.SetNum( s );
120         pointerList.SetNum( s );
121         for( i = 0; i < s; i++ ) {
122                 other[ i ] = ( *this )[ startIndex + i ];
123                 pointerList[ i ] = &other[ i ];
124         }
125
126         pointerList.Sort();
127
128         for( i = 0; i < s; i++ ) {
129                 (*this)[ startIndex + i ] = *pointerList[ i ];
130         }
131 }
132
133 /*
134 ================
135 idStrList::Size
136 ================
137 */
138 template<>
139 ID_INLINE size_t idStrList::Size( void ) const {
140         size_t s;
141         int i;
142
143         s = sizeof( *this );
144         for( i = 0; i < Num(); i++ ) {
145                 s += ( *this )[ i ].Size();
146         }
147
148         return s;
149 }
150
151 /*
152 ===============================================================================
153
154         idStrList path sorting
155
156 ===============================================================================
157 */
158
159 /*
160 ================
161 idListSortComparePaths
162
163 Compares two pointers to strings. Used to sort a list of string pointers alphabetically in idList<idStr>::Sort.
164 ================
165 */
166 template<class idStrPtr>
167 ID_INLINE int idListSortComparePaths( const idStrPtr *a, const idStrPtr *b ) {
168         return ( *a )->IcmpPath( **b );
169 }
170
171 /*
172 ================
173 idStrListSortPaths
174
175 Sorts the list of path strings alphabetically and makes sure folders come first.
176 ================
177 */
178 ID_INLINE void idStrListSortPaths( idStrList &list ) {
179         int i;
180
181         if ( !list.Num() ) {
182                 return;
183         }
184
185         idList<idStr>           other;
186         idList<idStrPtr>        pointerList;
187
188         pointerList.SetNum( list.Num() );
189         for( i = 0; i < list.Num(); i++ ) {
190                 pointerList[ i ] = &list[ i ];
191         }
192
193         pointerList.Sort( idListSortComparePaths<idStrPtr> );
194
195         other.SetNum( list.Num() );
196         other.SetGranularity( list.GetGranularity() );
197         for( i = 0; i < other.Num(); i++ ) {
198                 other[ i ] = *pointerList[ i ];
199         }
200
201         list.Swap( other );
202 }
203
204 #endif /* !__STRLIST_H__ */