Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
3.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) 1998-1998 Microsoft Corporation
  6. //
  7. // File: templats.h
  8. //
  9. //--------------------------------------------------------------------------
  10. // templates.h
  11. #ifndef __TEMPLATES_H__
  12. #define __TEMPLATES_H__
  13. template< class T > T* List_Cat( T* p1, T* p2 )
  14. {
  15. T* pScan;
  16. if( p1 == NULL )
  17. {
  18. return p2;
  19. }
  20. for( pScan = p1 ; pScan->pNext != NULL ; pScan = (T*)pScan->pNext );
  21. pScan->pNext = p2;
  22. return p1;
  23. }
  24. template< class T > DWORD List_Len( T* p )
  25. {
  26. DWORD dw;
  27. for( dw = 0 ; p != NULL ; p = (T*)p->pNext )
  28. {
  29. ++dw;
  30. }
  31. return dw;
  32. }
  33. template< class T > BOOL inlist( T* lst, T* p )
  34. {
  35. if( p == NULL )
  36. {
  37. return TRUE;
  38. }
  39. for( ; lst != NULL ; lst = (T*)lst->pNext )
  40. {
  41. if( p == lst )
  42. {
  43. return TRUE;
  44. }
  45. }
  46. return FALSE;
  47. }
  48. template< class T > T* List_Index( T* lst, DWORD dwIndex )
  49. {
  50. T* pScan;
  51. for( pScan = lst ; dwIndex > 0 && pScan != NULL && pScan->pNext != NULL ; --dwIndex, pScan = (T*)pScan->pNext );
  52. return pScan;
  53. }
  54. template< class T > T* List_Insert( T* lst, T* p, DWORD dwIndex )
  55. {
  56. T* pPrev;
  57. if( lst == NULL || dwIndex == 0 )
  58. {
  59. p->pNext = lst;
  60. lst = p;
  61. }
  62. else
  63. {
  64. pPrev = List_Index( lst, dwIndex - 1 );
  65. p->pNext = pPrev->pNext;
  66. pPrev->pNext = p;
  67. }
  68. return lst;
  69. }
  70. template< class T > T* List_Remove( T* lst, T* p )
  71. {
  72. if( lst != NULL )
  73. {
  74. if( lst == p )
  75. {
  76. lst = (T*)lst->pNext;
  77. p->pNext = NULL;
  78. }
  79. else
  80. {
  81. T* pScan;
  82. for( pScan = lst ; pScan->pNext != p && pScan->pNext != NULL ; pScan = (T*)pScan->pNext );
  83. if( pScan->pNext != NULL )
  84. {
  85. pScan->pNext = pScan->pNext->pNext;
  86. p->pNext = NULL;
  87. }
  88. }
  89. }
  90. return lst;
  91. }
  92. template< class T > long List_Position( T* lst, T* p )
  93. {
  94. long lPos;
  95. lPos = 0;
  96. while( lst != NULL && lst != p )
  97. {
  98. lst = lst->pNext;
  99. ++lPos;
  100. }
  101. if( lst == NULL )
  102. {
  103. return -1;
  104. }
  105. return lPos;
  106. }
  107. template< class T > T* List_Clone( T* lst )
  108. {
  109. T* pPrev;
  110. T* lstClone;
  111. T* pCopy;
  112. lstClone = NULL;
  113. pPrev = NULL;
  114. for( ; lst ; lst = (T*)lst->pNext )
  115. {
  116. pCopy = new T;
  117. if( pCopy != NULL )
  118. {
  119. memcpy( pCopy, lst, sizeof( T ) );
  120. pCopy->pNext = NULL;
  121. if( pPrev != NULL )
  122. {
  123. pPrev->pNext = pCopy;
  124. }
  125. else
  126. {
  127. lstClone = pCopy;
  128. }
  129. pPrev = pCopy;
  130. }
  131. }
  132. return lstClone;
  133. }
  134. template< class T > void List_Free( T* lst )
  135. {
  136. T* pNext;
  137. for( ; lst != NULL ; lst = pNext )
  138. {
  139. pNext = (T*)lst->pNext;
  140. delete lst;
  141. }
  142. }
  143. template< class T > T* Clone( T* p )
  144. {
  145. T* pCopy;
  146. pCopy = new T;
  147. if( pCopy != NULL )
  148. {
  149. memcpy( pCopy, p, sizeof( T ) );
  150. pCopy->pNext = NULL;
  151. }
  152. return pCopy;
  153. }
  154. #endif // __TEMPLATES_H__