📄 dce2_list.c
字号:
* void * * Pointer to the data to insert into the queue. * * Returns: * DCE2_Ret * DCE2_RET__ERROR if the queue is full or the queue object * passed in is NULL. * DCE2_RET__SUCCESS if the data is successfully added to * the queue. * ********************************************************************/DCE2_Ret DCE2_CQueueEnqueue(DCE2_CQueue *cqueue, void *data){ if (cqueue == NULL) return DCE2_RET__ERROR; if (cqueue->num_nodes == (uint32_t)cqueue->size) return DCE2_RET__ERROR; if (cqueue->tail_idx == DCE2_SENTINEL) cqueue->tail_idx = cqueue->head_idx; cqueue->queue[cqueue->tail_idx] = data; if ((cqueue->tail_idx + 1) == cqueue->size) cqueue->tail_idx = 0; else cqueue->tail_idx++; cqueue->num_nodes++; return DCE2_RET__SUCCESS;}/******************************************************************** * Function: DCE2_CQueueDequeue() * * Removes and returns the data in the first node in the queue. * Note that the user will have to free the data returned. The * data free function only applies to data that is in the queue * when it is emptied or destroyed. * * Arguments: * DCE2_CQueue * * A pointer to the queue object. * * Returns: * void * * The data in the first node in the queue. * NULL if there are no items in the queue or the queue object * passed in is NULL. * ********************************************************************/void * DCE2_CQueueDequeue(DCE2_CQueue *cqueue){ void *data; if (cqueue == NULL) return NULL; if (cqueue->num_nodes == 0) return NULL; data = cqueue->queue[cqueue->head_idx]; cqueue->queue[cqueue->head_idx] = NULL; if ((cqueue->head_idx + 1) == cqueue->size) cqueue->head_idx = 0; else cqueue->head_idx++; if (cqueue->head_idx == cqueue->tail_idx) cqueue->tail_idx = DCE2_SENTINEL; cqueue->num_nodes--; return data;}/******************************************************************** * Function: DCE2_CQueueFirst() * * Returns a pointer to the data of the first node in the queue. * Sets a current index to the first node in the queue for * iterating over the queue. * * Arguments: * DCE2_CQueue * * A pointer to the queue object. * * Returns: * void * * The data in the first node in the queue. * NULL if the queue object passed in is NULL, or there are * no items in the queue. * ********************************************************************/void * DCE2_CQueueFirst(DCE2_CQueue *cqueue){ if (cqueue == NULL) return NULL; if (cqueue->tail_idx == DCE2_SENTINEL) return NULL; cqueue->cur_idx = cqueue->head_idx; return cqueue->queue[cqueue->cur_idx];}/******************************************************************** * Function: DCE2_CQueueNext() * * Increments the current index in the queue to the next node in * the queue and returns the data associated with it. This in * combination with DCE2_CQueueFirst is useful in a for loop to * iterate over the items in a queue. * * Arguments: * DCE2_CQueue * * A pointer to the queue object. * * Returns: * void * * The data in the next node in the queue. * NULL if the queue object passed in is NULL, or we are at * the end of the queue and there are no next nodes. * ********************************************************************/void * DCE2_CQueueNext(DCE2_CQueue *cqueue){ if (cqueue == NULL) return NULL; if ((cqueue->tail_idx == DCE2_SENTINEL) || (cqueue->cur_idx == DCE2_SENTINEL)) return NULL; if ((cqueue->cur_idx + 1) == cqueue->size) cqueue->cur_idx = 0; else cqueue->cur_idx++; if (cqueue->cur_idx == cqueue->tail_idx) { cqueue->cur_idx = DCE2_SENTINEL; return NULL; } return cqueue->queue[cqueue->cur_idx];}/******************************************************************** * Function: DCE2_CQueueEmpty() * * Removes all of the nodes in a queue. Does not delete the queue * object itself or the storage array. Calls data free function * for data if it is not NULL. * * Arguments: * DCE2_CQueue * * A pointer to the queue object. * * Returns: None * ********************************************************************/void DCE2_CQueueEmpty(DCE2_CQueue *cqueue){ if (cqueue == NULL) return; while (!DCE2_CQueueIsEmpty(cqueue)) { void *data = DCE2_CQueueDequeue(cqueue); if ((data != NULL) && (cqueue->data_free != NULL)) cqueue->data_free(data); } cqueue->num_nodes = 0; cqueue->head_idx = 0; cqueue->tail_idx = DCE2_SENTINEL; cqueue->cur_idx = DCE2_SENTINEL;}/******************************************************************** * Function: DCE2_CQueueDestroy() * * Destroys the queue object and all of the data associated with it. * * Arguments: * DCE2_CQueue * * A pointer to the queue object. * * Returns: None * ********************************************************************/void DCE2_CQueueDestroy(DCE2_CQueue *cqueue){ if (cqueue == NULL) return; DCE2_CQueueEmpty(cqueue); DCE2_Free((void *)cqueue->queue, (cqueue->size * sizeof(void *)), cqueue->mtype); DCE2_Free((void *)cqueue, sizeof(DCE2_CQueue), cqueue->mtype);}/******************************************************************** * Function: DCE2_CStackNew() * * Creates and initializes a new static sized stack object. The * static stack uses a fixed size array and uses indexes to * indicate the start and end of the stack. This type of * stack can become full since it is a fixed size. Used for * performance reasons since new nodes do not need to be * allocated on the fly. * * Arguments: * int * The size that should be allocated for the static * stack storage. * DCE2_CStackDataFree * An optional free function for the data inserted into * the stack. If NULL is passed in, the user will be * responsible for freeing data left in the stack. * DCE2_MemType * The type of memory to associate dynamically allocated * memory with. * * Returns: * DCE2_CStack * * Pointer to a new stack object. * NULL if unable to allocate memory for the object or the * array for storage. * ********************************************************************/DCE2_CStack * DCE2_CStackNew(int size, DCE2_CStackDataFree df, DCE2_MemType mtype){ DCE2_CStack *cstack; if (size <= 0) return NULL; cstack = (DCE2_CStack *)DCE2_Alloc(sizeof(DCE2_CStack), mtype); if (cstack == NULL) return NULL; cstack->data_free = df; cstack->mtype = mtype; cstack->stack = DCE2_Alloc(size * sizeof(void *), mtype); if (cstack->stack == NULL) { DCE2_Free(cstack, sizeof(DCE2_CStack), mtype); return NULL; } cstack->size = size; cstack->tail_idx = DCE2_SENTINEL; cstack->cur_idx = DCE2_SENTINEL; return cstack;}/******************************************************************** * Function: DCE2_CStackPush() * * Inserts data into the static stack. * * Arguments: * DCE2_CStack * * A pointer to the stack object. * void * * Pointer to the data to insert into the stack. * * Returns: * DCE2_Ret * DCE2_RET__ERROR if the stack is full or the stack object * passed in is NULL. * DCE2_RET__SUCCESS if the data is successfully added to * the stack. * ********************************************************************/DCE2_Ret DCE2_CStackPush(DCE2_CStack *cstack, void *data){ if (cstack == NULL) return DCE2_RET__ERROR; if (cstack->num_nodes == (uint32_t)cstack->size) return DCE2_RET__ERROR; if (cstack->tail_idx == DCE2_SENTINEL) cstack->tail_idx = 0; else cstack->tail_idx++; cstack->stack[cstack->tail_idx] = data; cstack->num_nodes++; return DCE2_RET__SUCCESS;}/******************************************************************** * Function: DCE2_CStackPop() * * Removes and returns the data in the last node in the stack. * Note that the user will have to free the data returned. The * data free function only applies to data that is in the stack * when it is emptied or destroyed. * * Arguments: * DCE2_CStack * * A pointer to the stack object. * * Returns: * void * * The data in the last node in the stack. * NULL if there are no items in the stack or the stack object * passed in is NULL. * ********************************************************************/void * DCE2_CStackPop(DCE2_CStack *cstack){ void *data; if (cstack == NULL) return NULL; if (cstack->num_nodes == 0) return NULL; data = cstack->stack[cstack->tail_idx]; cstack->stack[cstack->tail_idx] = NULL; if (cstack->tail_idx == 0) cstack->tail_idx = DCE2_SENTINEL; else cstack->tail_idx--; cstack->num_nodes--; return data;}/******************************************************************** * Function: DCE2_CStackTop() * * Returns the data on top of the stack. Does not remove the data * from the stack. * * Arguments: * DCE2_CStack * * A pointer to the stack object. * * Returns: * void * * The data on top of the stack. * NULL if there are no items in the stack or the stack object * passed in is NULL. * ********************************************************************/void * DCE2_CStackTop(DCE2_CStack *cstack){ if (cstack == NULL) return NULL; if (cstack->num_nodes == 0) return NULL; return cstack->stack[cstack->tail_idx];}/******************************************************************** * Function: DCE2_CStackFirst() * * Returns a pointer to the data of the first node in the stack * array. Sets a current index to the first node in the stack for * iterating over the stack. * * Arguments: * DCE2_CStack * * A pointer to the stack object. * * Returns: * void * * The data in the first node in the stack. * NULL if the stack object passed in is NULL, or there are * no items in the stack. * ********************************************************************/void * DCE2_CStackFirst(DCE2_CStack *cstack){ if (cstack == NULL) return NULL; if (cstack->num_nodes == 0) return NULL; cstack->cur_idx = 0; return cstack->stack[cstack->cur_idx];}/******************************************************************** * Function: DCE2_CStackNext() * * Increments the current index in the stack to the next node in * the stack and returns the data associated with it. This in * combination with DCE2_CStackFirst is useful in a for loop to * iterate over the items in a stack. * * Arguments: * DCE2_CStack * * A pointer to the stack object. * * Returns: * void * * The data in the next node in the stack. * NULL if the stack object passed in is NULL, or we are at * the end of the stack and there are no next nodes. * ********************************************************************/void * DCE2_CStackNext(DCE2_CStack *cstack){ if (cstack == NULL) return NULL; if ((uint32_t)(cstack->cur_idx + 1) == cstack->num_nodes) return NULL; cstack->cur_idx++; return cstack->stack[cstack->cur_idx];}/******************************************************************** * Function: DCE2_CStackEmpty() * * Removes all of the nodes in a stack. Does not delete the stack * object itself or the storage array. Calls data free function * for data if it is not NULL. * * Arguments: * DCE2_CStack * * A pointer to the stack object. * * Returns: None * ********************************************************************/void DCE2_CStackEmpty(DCE2_CStack *cstack){ if (cstack == NULL) return; while (!DCE2_CStackIsEmpty(cstack)) { void *data = DCE2_CStackPop(cstack); if ((data != NULL) && (cstack->data_free != NULL)) cstack->data_free(data); } cstack->num_nodes = 0; cstack->tail_idx = DCE2_SENTINEL; cstack->cur_idx = DCE2_SENTINEL;}/******************************************************************** * Function: DCE2_CStackDestroy() * * Destroys the stack object and all of the data associated with it. * * Arguments: * DCE2_CStack * * A pointer to the stack object. * * Returns: None * ********************************************************************/void DCE2_CStackDestroy(DCE2_CStack *cstack){ if (cstack == NULL) return; DCE2_CStackEmpty(cstack); DCE2_Free((void *)cstack->stack, (cstack->size * sizeof(void *)), cstack->mtype); DCE2_Free((void *)cstack, sizeof(DCE2_CStack), cstack->mtype);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -