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.

179 lines
3.0 KiB

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