StrList.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #ifndef __STRLIST_H__
  21. #define __STRLIST_H__
  22. /*
  23. ===============================================================================
  24. idStrList
  25. ===============================================================================
  26. */
  27. typedef idList<idStr> idStrList;
  28. typedef idList<idStr*> idStrPtrList;
  29. typedef idStr *idStrPtr;
  30. /*
  31. ================
  32. idListSortCompare<idStrPtr>
  33. Compares two pointers to strings. Used to sort a list of string pointers alphabetically in idList<idStr>::Sort.
  34. ================
  35. */
  36. template<>
  37. ID_INLINE int idListSortCompare<idStrPtr>( const idStrPtr *a, const idStrPtr *b ) {
  38. return ( *a )->Icmp( **b );
  39. }
  40. /*
  41. ================
  42. idStrList::Sort
  43. Sorts the list of strings alphabetically. Creates a list of pointers to the actual strings and sorts the
  44. pointer list. Then copies the strings into another list using the ordered list of pointers.
  45. ================
  46. */
  47. template<>
  48. ID_INLINE void idStrList::Sort( cmp_t *compare ) {
  49. int i;
  50. if ( !num ) {
  51. return;
  52. }
  53. idList<idStr> other;
  54. idList<idStrPtr> pointerList;
  55. pointerList.SetNum( num );
  56. for( i = 0; i < num; i++ ) {
  57. pointerList[ i ] = &( *this )[ i ];
  58. }
  59. pointerList.Sort();
  60. other.SetNum( num );
  61. other.SetGranularity( granularity );
  62. for( i = 0; i < other.Num(); i++ ) {
  63. other[ i ] = *pointerList[ i ];
  64. }
  65. this->Swap( other );
  66. }
  67. /*
  68. ================
  69. idStrList::SortSubSection
  70. Sorts a subsection of the list of strings alphabetically.
  71. ================
  72. */
  73. template<>
  74. ID_INLINE void idStrList::SortSubSection( int startIndex, int endIndex, cmp_t *compare ) {
  75. int i, s;
  76. if ( !num ) {
  77. return;
  78. }
  79. if ( startIndex < 0 ) {
  80. startIndex = 0;
  81. }
  82. if ( endIndex >= num ) {
  83. endIndex = num - 1;
  84. }
  85. if ( startIndex >= endIndex ) {
  86. return;
  87. }
  88. idList<idStr> other;
  89. idList<idStrPtr> pointerList;
  90. s = endIndex - startIndex + 1;
  91. other.SetNum( s );
  92. pointerList.SetNum( s );
  93. for( i = 0; i < s; i++ ) {
  94. other[ i ] = ( *this )[ startIndex + i ];
  95. pointerList[ i ] = &other[ i ];
  96. }
  97. pointerList.Sort();
  98. for( i = 0; i < s; i++ ) {
  99. (*this)[ startIndex + i ] = *pointerList[ i ];
  100. }
  101. }
  102. /*
  103. ================
  104. idStrList::Size
  105. ================
  106. */
  107. template<>
  108. ID_INLINE size_t idStrList::Size( void ) const {
  109. size_t s;
  110. int i;
  111. s = sizeof( *this );
  112. for( i = 0; i < Num(); i++ ) {
  113. s += ( *this )[ i ].Size();
  114. }
  115. return s;
  116. }
  117. /*
  118. ===============================================================================
  119. idStrList path sorting
  120. ===============================================================================
  121. */
  122. /*
  123. ================
  124. idListSortComparePaths
  125. Compares two pointers to strings. Used to sort a list of string pointers alphabetically in idList<idStr>::Sort.
  126. ================
  127. */
  128. template<class idStrPtr>
  129. ID_INLINE int idListSortComparePaths( const idStrPtr *a, const idStrPtr *b ) {
  130. return ( *a )->IcmpPath( **b );
  131. }
  132. /*
  133. ================
  134. idStrListSortPaths
  135. Sorts the list of path strings alphabetically and makes sure folders come first.
  136. ================
  137. */
  138. ID_INLINE void idStrListSortPaths( idStrList &list ) {
  139. int i;
  140. if ( !list.Num() ) {
  141. return;
  142. }
  143. idList<idStr> other;
  144. idList<idStrPtr> pointerList;
  145. pointerList.SetNum( list.Num() );
  146. for( i = 0; i < list.Num(); i++ ) {
  147. pointerList[ i ] = &list[ i ];
  148. }
  149. pointerList.Sort( idListSortComparePaths<idStrPtr> );
  150. other.SetNum( list.Num() );
  151. other.SetGranularity( list.GetGranularity() );
  152. for( i = 0; i < other.Num(); i++ ) {
  153. other[ i ] = *pointerList[ i ];
  154. }
  155. list.Swap( other );
  156. }
  157. #endif /* !__STRLIST_H__ */