home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / wlist.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  10.1 KB  |  318 lines

  1.  
  2. //=============================================================================
  3. //  Microsoft (R) Bloodhound (tm). Copyright (C) 1991-1993.
  4. //
  5. //  MODULE: list.h
  6. //
  7. //  Modification History
  8. //
  9. //  raypa           03/17/93    Created.
  10. //=============================================================================
  11.  
  12. #if !defined(_LIST_)
  13. #pragma option push -b -a8 -pc -A- /*P_O_Push*/
  14.  
  15. #define _LIST_
  16. #pragma pack(1)
  17.  
  18. //=============================================================================
  19. //  The LINK structure is used to chain structures together into a list.
  20. //=============================================================================
  21.  
  22. typedef struct _LINK *LPLINK;
  23.  
  24. typedef struct _LINK
  25. {
  26.     LPLINK     PrevLink;                    //... Previous or back pointer.
  27.     LPLINK     NextLink;                    //... Next or forward pointer.
  28. } LINK;
  29.  
  30. //=============================================================================
  31. //  The LIST data structure.
  32. //=============================================================================
  33.  
  34. typedef struct _LIST
  35. {
  36.     LPLINK      Tail;                       //... List Tail pointer.
  37.     LPLINK      Head;                       //... List Head pointer.
  38.     DWORD       Length;                     //... List Length.
  39. } LIST;
  40.  
  41. typedef LIST *LPLIST;
  42.  
  43.  
  44. #ifndef NO_INLINE
  45.  
  46. #ifndef INLINE
  47. #define INLINE __inline
  48. #endif
  49.  
  50. //=============================================================================
  51. //  FUNCTIONS.
  52. //=============================================================================
  53.  
  54. INLINE LPLINK WINAPI GetPrevLink(LPLINK Link)
  55. {
  56.     return Link->PrevLink;
  57. }
  58.  
  59. INLINE LPLINK WINAPI GetNextLink(LPLINK Link)
  60. {
  61.     return Link->NextLink;
  62. }
  63.  
  64. INLINE LPLINK WINAPI GetHeadOfList(LPLIST List)
  65. {
  66.     return List->Head;
  67. }
  68.  
  69. INLINE LPLINK WINAPI GetTailOfList(LPLIST List)
  70. {
  71.     return List->Tail;
  72. }
  73.  
  74. INLINE DWORD WINAPI GetListLength(LPLIST List)
  75. {
  76.     return List->Length;
  77. }
  78.  
  79. //=============================================================================
  80. //  FUNCTION: InitializeList()
  81. //
  82. //  Modification History
  83. //                               
  84. //  raypa           04/15/93        Created
  85. //=============================================================================
  86.  
  87. INLINE LPLIST WINAPI InitializeList(LPLIST List)
  88. {
  89.     List->Head    = (LPLINK) 0L;
  90.     List->Tail    = (LPLINK) 0L;
  91.     List->Length  = 0;
  92.  
  93.     return List;
  94. }
  95.  
  96. //=============================================================================
  97. //  FUNCTION: AddLinkToLink()
  98. //
  99. //  Modification History
  100. //                               
  101. //  raypa           04/15/93        Created
  102. //=============================================================================
  103.  
  104. INLINE VOID WINAPI AddLinkToLink(LPLINK DstLink, LPLINK SrcLink)
  105. {
  106.     //=========================================================================
  107.     //  Make the source link point at the destination link.
  108.     //=========================================================================
  109.  
  110.     SrcLink->PrevLink = DstLink;
  111.     SrcLink->NextLink = DstLink->NextLink;
  112.  
  113.     //=========================================================================
  114.     //  Make the destination link point at the source link.
  115.     //=========================================================================
  116.  
  117.     DstLink->NextLink->PrevLink = SrcLink;
  118.     DstLink->NextLink = SrcLink;
  119. }
  120.  
  121. //=============================================================================
  122. //  FUNCTION: AddToList()
  123. //
  124. //  Modification History
  125. //                               
  126. //  raypa           04/15/93        Created
  127. //=============================================================================
  128.  
  129. INLINE LPLINK WINAPI AddToList(LPLIST List, LPLINK DstLink, LPLINK SrcLink)
  130. {
  131.     //=========================================================================
  132.     //  Grow the list length by one.
  133.     //=========================================================================
  134.  
  135.     List->Length++;
  136.  
  137.     //=========================================================================
  138.     //  If SrcLink is NULL then add DstLink to the end of the list.
  139.     //=========================================================================
  140.  
  141.     if ( SrcLink == (LPLINK) 0L )
  142.     {
  143.         //=====================================================================
  144.         //  If the tail pointer is NULL then the list is empty.
  145.         //=====================================================================
  146.  
  147.         if ( List->Tail != (LPLINK) 0L )
  148.         {
  149.             AddLinkToLink(List->Tail, DstLink);
  150.         }
  151.         else
  152.         {
  153.             DstLink->PrevLink = DstLink;
  154.             DstLink->NextLink = DstLink;
  155.  
  156.             List->Head = DstLink;
  157.         }
  158.  
  159.         return (List->Tail = DstLink);
  160.     }
  161.  
  162.     //=========================================================================
  163.     //  If DstLink is NULL then add SrcLink to the front of the list.
  164.     //=========================================================================
  165.  
  166.     if ( DstLink == (LPLINK) 0L )
  167.     {
  168.         //=====================================================================
  169.         //  If the head pointer is NULL then the list is empty.
  170.         //=====================================================================
  171.  
  172.         if ( List->Head != (LPLINK) 0L )
  173.         {
  174.             AddLinkToLink(List->Head, SrcLink);
  175.         }
  176.         else
  177.         {
  178.             SrcLink->PrevLink = SrcLink;
  179.             SrcLink->NextLink = SrcLink;
  180.  
  181.             List->Tail = SrcLink;
  182.         }
  183.  
  184.         return (List->Head = SrcLink);
  185.     }
  186.  
  187.     //=========================================================================
  188.     //  Neither DstLink nor SrcLink is NULL so link them together.
  189.     //=========================================================================
  190.  
  191.     AddLinkToLink(DstLink, SrcLink);
  192.  
  193.     return SrcLink;
  194. }
  195.  
  196. //=============================================================================
  197. //  FUNCTION: DeleteFromList()
  198. //
  199. //  Modification History
  200. //                               
  201. //  raypa           04/15/93        Created
  202. //=============================================================================
  203.  
  204. INLINE LPLINK WINAPI DeleteFromList(LPLIST List, LPLINK Link)
  205. {
  206.     //=========================================================================
  207.     //  If the list is empty then return NULL.
  208.     //=========================================================================
  209.  
  210.     if ( List->Length != 0 )
  211.     {
  212.         //=====================================================================
  213.         //  If the list length is not zero then we may need to fixup head and
  214.         //  tail pointers in the event we delete the first or last link,
  215.         //  respectively.
  216.         //=====================================================================
  217.  
  218.         if ( --List->Length != 0 )
  219.         {
  220.             //=================================================================
  221.             //  If we are deleting the front link then fixup the head pointer.
  222.             //=================================================================
  223.  
  224.             if ( List->Head == Link )
  225.             {
  226.                 List->Head = List->Head->NextLink;
  227.             }
  228.  
  229.             //=================================================================
  230.             //  If we are deleting the end link then fixup the tail pointer.
  231.             //=================================================================
  232.  
  233.             if ( List->Tail == Link )
  234.             {
  235.                 List->Tail = List->Tail->PrevLink;
  236.             }
  237.  
  238.             //=================================================================
  239.             //  Now we can unlink this link from the list.
  240.             //=================================================================
  241.  
  242.             Link->NextLink->PrevLink = Link->PrevLink;
  243.             Link->PrevLink->NextLink = Link->NextLink;
  244.         }
  245.         else
  246.         {
  247.             //=================================================================
  248.             //  There is only one link on the list and we just deleted it.
  249.             //=================================================================
  250.  
  251.             List->Head = (LPLINK) 0L;
  252.             List->Tail = (LPLINK) 0L;
  253.         }
  254.  
  255.         return Link;
  256.     }
  257.  
  258.     return (LPLINK) 0L;
  259. }
  260.  
  261. //=============================================================================
  262. //  FUNCTION: AddToFrontOfList()
  263. //
  264. //  Modification History
  265. //                               
  266. //  raypa           04/15/93        Created
  267. //=============================================================================
  268.  
  269. INLINE LPLINK WINAPI AddToFrontOfList(LPLIST List, LPLINK Link)
  270. {
  271.     return AddToList(List, (LPLINK) 0L, Link);
  272. }
  273.  
  274. //=============================================================================
  275. //  FUNCTION: AddToEndOfList()
  276. //
  277. //  Modification History
  278. //                               
  279. //  raypa           04/15/93        Created
  280. //=============================================================================
  281.  
  282. INLINE LPLINK WINAPI AddToEndOfList(LPLIST List, LPLINK Link)
  283. {
  284.     return AddToList(List, Link, (LPLINK) 0L);
  285. }
  286.  
  287. //=============================================================================
  288. //  FUNCTION: DeleteFromFrontOfList()
  289. //
  290. //  Modification History
  291. //                               
  292. //  raypa           04/15/93        Created
  293. //=============================================================================
  294.  
  295. INLINE LPLINK WINAPI DeleteFromFrontOfList(LPLIST List)
  296. {
  297.     return DeleteFromList(List, GetHeadOfList(List));
  298. }
  299.  
  300. //=============================================================================
  301. //  FUNCTION: DeleteFromEndOfList()
  302. //
  303. //  Modification History
  304. //                               
  305. //  raypa           04/15/93        Created
  306. //=============================================================================
  307.  
  308. INLINE LPLINK WINAPI DeleteFromEndOfList(LPLIST List)
  309. {
  310.     return DeleteFromList(List, GetTailOfList(List));
  311. }
  312.  
  313. #endif
  314.  
  315. #pragma pack()
  316. #pragma option pop /*P_O_Pop*/
  317. #endif
  318.