queue.h

  1. #ifndef QUEUE_H
  2. #define QUEUE_H
  3. /*===========================================================================
  4.             Q U E U E    S E R V I C E S    H E A D E R    F I L E
  5. DESCRIPTION
  6.  This file contains types and declarations associated with the Queue
  7.  Services.与Queue Services相关的类型与声明
  8. ===========================================================================*/
  9. /*===========================================================================
  10.                      INCLUDE FILES FOR MODULE
  11. ===========================================================================*/
  12. #ifdef CUST_H
  13.   #include "customer.h"
  14. #endif
  15. /*===========================================================================
  16.                         DATA DECLARATIONS
  17. ===========================================================================*/
  18. /* -------------------------------------------------------------------------
  19. ** Queue Link Structure这个东东像一个钩子,有了它对象就可以把排在自己的前一个或后一个对象钩起来了
  20. **
  21. ** The following link structure is really the heart of the Queue Services. 下面的链式结构实际上是Queue Service的核心
  22. ** It is used as a link field with variables which allows them to be moved 它被用作一个链域,其中的变量允许它们被移入/出队列
  23. ** on and off queues. It is also used in the definition of queues themselves. 其也被用于queue们本身的定义中
  24. **
  25. ** Do NOT directly access the fields of a link! Only the Queue Services should
  26. ** do this.
  27. ** ------------------------------------------------------------------------- */
  28. typedef struct q_link_struct
  29. {
  30.   struct q_link_struct  *next_ptr;
  31.     /* Ptr to next link in list. If NULL, there is no next link. */
  32. #ifndef FEATURE_Q_NO_SELF_QPTR//自我包含
  33.   void                  *self_ptr;
  34.     /* Ptr to item which contains this link. */
  35.   struct q_struct       *q_ptr;
  36.     /* Ptr to the queue on which this item is enqueued. NULL if the
  37.        item is not on a queue. While not strictly necessary, this field
  38.        is helpful for debugging.*/
  39. #endif
  40. #ifndef FEATURE_Q_SINGLE_LINK
  41.   struct q_link_struct  *prev_ptr;//双向链表
  42.     /* Ptr to prev link in list. If NULL, there is no prev link. */
  43. #endif
  44. } q_link_type;
  45. /* ------------------------------------------------------------------------
  46. ** Queue Head Link Structure
  47. **
  48. ** When queue items are linked in a singly link list, q_head_link_type is
  49. ** used instead of q_link_type. This avoids the overhead of traversing 
  50. ** the whole of link list when queueing at the end of the link list.
  51. ** This structure should be accessed only by Queue services.
  52. ** ------------------------------------------------------------------------ */
  53. typedef struct q_head_link_struct
  54. {
  55.   struct q_link_struct  *next_ptr;//指向后一个的钩子
  56.     /* Ptr to head of the link list */
  57. #ifndef FEATURE_Q_NO_SELF_QPTR//自我包含
  58.   void                  *self_ptr;
  59.     /* Ptr to item which contains this link. */
  60.   struct q_struct       *q_ptr;
  61.     /* Ptr to the queue on which this item is enqueued. NULL if the
  62.        item is not on a queue. While not strictly necessary, this field
  63.        is helpful for debugging.*/
  64. #endif
  65.   struct q_link_struct  *prev_ptr;//头指针比较特殊,它有指向尾部的钩子
  66.     /* Ptr to the tail of link list */
  67. } q_head_link_type;
  68. //
  69. //双向链表没有用到q_head_link_type,这里的q_head_link_type相当于双向链表中的q_link_type
  70. //
  71. //
  72. /* ------------------------------------------------------------------------
  73. ** Queue Structure
  74. **
  75. ** The following structure format is used by the Queue Services to represent
  76. ** a queue.下面的结构被Queue Service用来代表一个队列--队列结构
  77. ** 
  78. ** Do NOT access the fields of a queue directly. Only the Queue Services should
  79. ** do this.不要直接访问一个队列的这个域,只有Queue Service才可以这样做
  80. ** ------------------------------------------------------------------------ */
  81. typedef struct q_struct
  82. {
  83.     //link代表头指针(的钩子)
  84. #ifdef FEATURE_Q_SINGLE_LINK
  85.   q_head_link_type  link;//单向链表的头指针比其它指针多了一个前向指针元素
  86. #else
  87.   q_link_type  link;//双向链表,头指针与其它指针相同
  88. #endif
  89.     /* Used for linking items into queue. 用于把items链到队列内*/
  90.   int          cnt;
  91.     /* Keeps track of number of items enqueued. Though not necessary, having
  92.        this field is tremendously helpful for debugging. *///跟踪队列中元素的数目,虽不必需但对调试很有用
  93. #ifdef FEATURE_Q_WIN32_LOCKS
  94.   void        *mutex;//线程安全
  95. #endif
  96. } q_type;
  97. /* ------------------------------------------------------------------------
  98. ** Queue Generic Item
  99. **   Generic items must have q_link_type as the first element.  This allows 每个item必需把它作为自己的第一个(域?)
  100. **   the linear search function to traverse the list without knowing 这可以让线性查找函数在不了解它的情况下遍历此链
  101. **   anything about the elements
  102. ** ------------------------------------------------------------------------ */
  103. typedef struct {
  104.    q_link_type link;
  105. } q_generic_item_type;
  106. /* ------------------------------------------------------------------------
  107. ** Queue Compare Function
  108. **    Used by the searching functions to determine if an item is in
  109. **       the queue.查找函数用它判断是否某个元素在链表中
  110. **    Returns non zero if the element should be operated upon, 0 otherwise
  111. **    For linear searching, the operation is to return a pointer to the
  112. **       item and terminate the search
  113. **    For linear deleting, the operation is to remove the item from the
  114. **       queue and continue the traversal
  115. ** ------------------------------------------------------------------------ */
  116. typedef int (*q_compare_func_type)( void* item_ptr, void* compare_val );
  117. /* ------------------------------------------------------------------------
  118. ** Queue Action Function
  119. **    Used by q_linear_delete to perform an action on an item which is 
  120. **    being deleted from the list AFTER the item is deleted.  To perform
  121. **    an action BEFORE the item is deleted, the user can call the action
  122. **    function directly in the compare function call back.
  123. ** ------------------------------------------------------------------------ */
  124. typedef void (*q_action_func_type)( void *item_ptr, void* param );
  125. /*===========================================================================
  126.                              Macro Definitions
  127. ===========================================================================*/
  128. #ifdef PC_EMULATOR_H//pc机上模拟
  129.   #ifdef Q_RENAME
  130.     #define PC_EMULATOR_QUEUE
  131.     #include PC_EMULATOR_H
  132.   #endif
  133.   #ifdef Q_XCEPT
  134.     #define PC_EMULATOR_Q_XCEPT
  135.     #include PC_EMULATOR_H
  136.   #endif
  137. #endif
  138. #if !defined(PC_EMULATOR_H) || !defined(Q_XCEPT)
  139.   #define Q_XCEPT_Q_INIT( q_ptr )
  140.   #define Q_XCEPT_Q_LINK( q_ptr, link_ptr )
  141.   #define Q_XCEPT_Q_PUT( q_ptr, link_ptr )
  142.   #define Q_XCEPT_Q_GET( q_ptr )
  143.   #define Q_XCEPT_Q_LAST_GET( q_ptr )
  144.   #define Q_XCEPT_Q_CNT( q_ptr )
  145.   #define Q_XCEPT_Q_CHECK( q_ptr )
  146.   #define Q_XCEPT_Q_LAST_CHECK( q_ptr )
  147.   #define Q_XCEPT_Q_NEXT( q_ptr, link_ptr )
  148.   #ifdef FEATURE_Q_NO_SELF_QPTR
  149.     #define Q_XCEPT_Q_INSERT( q_ptr, q_insert_ptr, q_item_ptr )
  150.   #else
  151.     #define Q_XCEPT_Q_INSERT( q_insert_ptr, q_item_ptr )
  152.   #endif
  153.   #ifdef FEATURE_Q_NO_SELF_QPTR
  154.     #define Q_XCEPT_Q_DELETE( q_ptr, q_delete_ptr )
  155.   #else
  156.     #define Q_XCEPT_Q_DELETE( q_delete_ptr )
  157.   #endif
  158. #endif
  159. /* ========================================================================
  160. MACRO Q_ALREADY_QUEUED
  161. DESCRIPTION
  162.    Evaluates to true if the item passed in is already in a queue and to
  163.    false otherwise.
  164. =========================================================================== */
  165. #define Q_ALREADY_QUEUED( q_link_ptr ) /
  166.    ((q_link_ptr)->next_ptr != NULL)//q_link_ptr表示一链表而非链表中的一元素///错误!
  167. /*===========================================================================
  168.                             Function Declarations
  169. ===========================================================================*/
  170. #ifdef __cplusplus
  171.    extern "C" {
  172. #endif
  173. /*==========================================================================
  174. FUNCTION Q_INIT
  175. DESCRIPTION
  176.   This function initializes a specified queue. It should be called for each
  177.   queue prior to using the queue with the other Queue Services.
  178. DEPENDENCIES
  179.   None.
  180. RETURN VALUE
  181.   A pointer to the initialized queue.
  182. SIDE EFFECTS
  183.   The specified queue is initialized for use with Queue Services.
  184. ===========================================================================*/
  185. q_type* q_init ( q_type *q_ptr );
  186. /*===========================================================================
  187. FUNCTION Q_LINK
  188. DESCRIPTION
  189.   This function initializes a specified link. It should be called for each
  190.   link prior to using the link with the other Queue Services.
  191. DEPENDENCIES
  192.   None.
  193. RETURN VALUE
  194.   A pointer to the initialized link.
  195. SIDE EFFECTS
  196.   The specified link is initialized for use with the Queue Services.
  197. ===========================================================================*/
  198. q_link_type* q_link ( void *item_ptr, q_link_type *link_ptr );
  199. /*===========================================================================
  200. FUNCTION Q_PUT
  201. DESCRIPTION
  202.   This function enqueues an item onto a specified queue using a specified
  203.   link.
  204. DEPENDENCIES
  205.   The specified queue should have been previously initialized via a call
  206.   to q_init. The specified link field of the item should have been prev-
  207.   iously initialized via a call to q_init_link.
  208. RETURN VALUE
  209.   None.
  210. SIDE EFFECTS
  211.   The specified item is placed at the tail of the specified queue.
  212. ===========================================================================*/
  213. void  q_put  ( q_type *q_ptr, q_link_type *link_ptr );
  214. /*===========================================================================
  215. FUNCTION Q_GET
  216. DESCRIPTION
  217.   This function removes an item from the head of a specified queue.
  218. DEPENDENCIES
  219.   The specified queue should have been initialized previously via a call
  220.   to q_init.
  221. RETURN VALUE
  222.   A pointer to the dequeued item. If the specified queue is empty, then
  223.   NULL is returned.
  224. SIDE EFFECTS
  225.   The head item, if any, is removed from the specified queue.
  226. ===========================================================================*/
  227. void* q_get ( q_type *q_ptr );
  228. /*===========================================================================
  229. FUNCTION Q_LAST_GET
  230. DESCRIPTION
  231.   This function removes an item from the tail of a specified queue.
  232. DEPENDENCIES
  233.   The specified queue should have been initialized previously via a call
  234.   to q_init.
  235. RETURN VALUE
  236.   A pointer to the dequeued item. If the specified queue is empty, then
  237.   NULL is returned.
  238. SIDE EFFECTS
  239.   The head item, if any, is removed from the specified queue.
  240. ===========================================================================*/
  241. void* q_last_get ( q_type *q_ptr );
  242. /*===========================================================================
  243. FUNCTION Q_CNT
  244. DESCRIPTION
  245.   This function returns the number of items currently queued on a specified
  246.   queue.
  247. DEPENDENCIES
  248.   The specified queue should have been initialized previously via a call
  249.   to q_init.
  250. RETURN VALUE
  251.   The number of items currently queued on the specified queue.
  252. SIDE EFFECTS
  253.   None.
  254. ===========================================================================*/
  255. int q_cnt  ( q_type *q_ptr );
  256. /*===========================================================================
  257. FUNCTION Q_CHECK
  258. DESCRIPTION
  259.   This function returns a pointer to the data block at the head of the queue.
  260.   The data block is not removed from the queue.
  261. DEPENDENCIES
  262.   The specified queue should have been initialized previously via a call
  263.   to q_init.
  264. RETURN VALUE
  265.   A pointer to the queue item. If the specified queue is empty, then
  266.   NULL is returned.
  267. SIDE EFFECTS
  268.   None
  269. ===========================================================================*/
  270. void* q_check (q_type  *q_ptr);
  271. /*===========================================================================
  272. FUNCTION Q_LAST_CHECK
  273. DESCRIPTION
  274.   This function returns a pointer to the data block at the tail of the queue.
  275.   The data block is not removed from the queue.
  276. DEPENDENCIES
  277.   The specified queue should have been initialized previously via a call
  278.   to q_init.
  279. RETURN VALUE
  280.   A pointer to the queue item. If the specified queue is empty, then
  281.   NULL is returned.
  282. SIDE EFFECTS
  283.   The head item, if any, is removed from the specified queue.
  284. ===========================================================================*/
  285. void* q_last_check ( q_type *q_ptr );
  286. /*===========================================================================
  287. FUNCTION Q_NEXT
  288. DESCRIPTION
  289.   This function returns a pointer to the next item on the queue.
  290. DEPENDENCIES
  291.   The specified queue should have been initialized previously via a call
  292.   to q_init.
  293. RETURN VALUE
  294.   A pointer to the next item on the queue. If the end of the queue is reached, 
  295.   then NULL is returned.
  296. SIDE EFFECTS
  297.   None.
  298. ===========================================================================*/
  299. void* q_next  ( q_type *q_ptr, q_link_type *link_ptr );
  300. /*===========================================================================
  301. FUNCTION Q_INSERT
  302. DESCRIPTION
  303.   This function inserts an item before a specified item on a queue.
  304. DEPENDENCIES
  305.   The specified queue should have been initialized previously via a call
  306.   to q_init.
  307. RETURN VALUE
  308.   None.
  309. SIDE EFFECTS
  310.   Input item is inserted before input item.
  311. ===========================================================================*/
  312. #ifdef FEATURE_Q_NO_SELF_QPTR
  313.    void q_insert  ( q_type *q_ptr, q_link_type *q_insert_ptr, q_link_type *q_item_ptr );
  314. #else
  315.    void q_insert  ( q_link_type *q_insert_ptr, q_link_type *q_item_ptr );
  316. #endif
  317. /*===========================================================================
  318. FUNCTION Q_DELETE
  319. DESCRIPTION
  320.   This function removes an item from a specified queue.
  321. DEPENDENCIES
  322.   The specified queue should have been initialized previously via a call
  323.   to q_init.
  324. RETURN VALUE
  325.   None.
  326. SIDE EFFECTS
  327.   Input item is delete from the queue.
  328. ===========================================================================*/
  329. #ifdef FEATURE_Q_NO_SELF_QPTR
  330.    void q_delete  ( q_type *q_ptr, q_link_type *q_delete_ptr );
  331. #else
  332.    void q_delete  ( q_link_type *q_delete_ptr );
  333. #endif
  334. /*===========================================================================
  335. FUNCTION Q_LINEAR_SEARCH
  336. DESCRIPTION
  337.   Given a comparison function, this function traverses the elements in
  338.   a queue, calls the compare function, and returns a pointer to the
  339.   current element being compared if the user passed compare function
  340.   returns non zero.
  341.   The user compare function should return 0 if the current element is
  342.   not the element in which the compare function is interested.
  343. DEPENDENCIES
  344.   The specified queue should have been initialized previously via a call
  345.   to q_init.
  346.   The user's queue elements must have q_link_type as the first element
  347.   of the queued structure.
  348. RETURN VALUE
  349.   A pointer to the found element
  350. SIDE EFFECTS
  351.   None.
  352. ===========================================================================*/
  353. void* q_linear_search(
  354.   q_type             *q_ptr,  
  355.   q_compare_func_type compare_func,
  356.   void               *compare_val
  357. );
  358. #if defined FEATURE_Q_NO_SELF_QPTR && defined FEATURE_Q_SINGLE_LINK
  359. /*===========================================================================
  360. FUNCTION Q_LINEAR_DELETE
  361. DESCRIPTION
  362.   Given a comparison function, this function traverses the elements in
  363.   a queue, calls the compare function, and returns a pointer to the
  364.   current element being compared if the user passed compare function
  365.   returns non zero.  In addition, the item will be removed from the queue.
  366.   The user compare function should return 0 if the current element is
  367.   not the element in which the compare function is interested.
  368. DEPENDENCIES
  369.   The specified queue should have been initialized previously via a call
  370.   to q_init.
  371.   The user's queue elements must have q_link_type as the first element
  372.   of the queued structure.
  373.   The user's compare function will be passed NULL for the compare value.
  374. RETURN VALUE
  375.   None
  376. SIDE EFFECTS
  377.   None.
  378. ===========================================================================*/
  379. void q_linear_delete(
  380.   q_type             *q_ptr,  
  381.   q_compare_func_type compare_func,
  382.   void               *param,
  383.   q_action_func_type  action_func
  384. );
  385. #endif
  386. #ifdef __cplusplus
  387.    }
  388. #endif
  389. #endif /* QUEUE_H */
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值