static inline void splay_tree_splaying(s_array_stack_t *path_stack) { uint32 path_mask; s_splay_tree_t **iterator; assert_exit(array_stack_structure_legal_p(path_stack)); while (array_stack_size(path_stack) >= 2) { iterator = array_stack_pop(path_stack); path_mask = TREE_PATH_TYPE(iterator); iterator = array_stack_pop(path_stack); path_mask = path_mask + (TREE_PATH_TYPE(iterator) << 1); iterator = TREE_PATH_DECODE(iterator); splay_tree_splaying_i(iterator, path_mask); } if (array_stack_size(path_stack) == 1) { iterator = array_stack_pop(path_stack); path_mask = TREE_PATH_TYPE(iterator); iterator = TREE_PATH_DECODE(iterator); splay_tree_splaying_root(iterator, path_mask); } }
boolean check_parantheses_match(const char *expr) { int i = 0; array_stack_t pstack; boolean valid = TRUE; array_stack_init(&pstack, MAX_EXPR_SIZE); for (i=0; expr[i]; i++) { if (is_opening_parantheses(expr[i])) { array_stack_push(&pstack, expr[i]); } else if (is_closing_parantheses(expr[i])) { if (array_stack_isEmpty(&pstack)) { valid = FALSE; break; } int symb = array_stack_pop(&pstack); /** 'symb' must be matching opener for expr[i] */ if (! is_matching_parantheses(symb, expr[i]) ) { valid = FALSE; break; } } } if (!array_stack_isEmpty(&pstack)) { valid = FALSE; } array_stack_destroy(&pstack); return valid; }
static inline void stacked_queue_stack_dump(s_array_stack_t *from, s_array_stack_t *to) { assert_exit(array_stack_structure_legal_p(to)); assert_exit(array_stack_structure_legal_p(from)); assert_exit(array_stack_empty_p(to)); assert_exit(array_stack_capacity(from) == array_stack_capacity(to)); while (!array_stack_empty_p(from)) { array_stack_push(to, array_stack_pop(from)); } }
void * stacked_queue_leave(s_stacked_queue_t *queue) { if (!stacked_queue_structure_legal_p(queue)) { return PTR_INVALID; } else if (stacked_queue_empty_ip(queue)) { pr_log_warn("Attempt to leave from _EMPTY_ queue.\n"); return PTR_INVALID; } else { if (array_stack_empty_p(queue->leave)) { pr_log_info("Empty leave stack, will dump enter stack to leave.\n"); stacked_queue_stack_dump(queue->enter, queue->leave); } return array_stack_pop(queue->leave); } }
int main (void) { array_stack_t mystack; int i; assert(array_stack_init(&mystack, MAX_STACK_SIZE) == 0); assert(array_stack_isEmpty(&mystack) == TRUE); array_stack_print(&mystack, printAsInt); for(i=0; i<MAX_STACK_SIZE; i++) { assert(array_stack_push(&mystack, (void *)(i+1)) == 0); array_stack_print(&mystack, printAsInt); assert((int)array_stack_peek(&mystack) == (i+1)); assert(array_stack_len(&mystack) == (i+1)); } assert(array_stack_isFull(&mystack) == TRUE); assert(array_stack_isEmpty(&mystack) == FALSE); assert(array_stack_push(&mystack, (void *)(11)) == -EINVAL); for(i=0; i<MAX_STACK_SIZE; i++) { assert((int)array_stack_peek(&mystack) == (MAX_STACK_SIZE-i)); assert(array_stack_len(&mystack) == (MAX_STACK_SIZE-i)); assert(array_stack_pop(&mystack) == (MAX_STACK_SIZE-i)); array_stack_print(&mystack, printAsInt); } assert(array_stack_isFull(&mystack) == FALSE); assert(array_stack_isEmpty(&mystack) == TRUE); array_stack_destroy(&mystack); printf("All tests succeed!\n"); return 0; }
static inline void trie_tree_sequence_remove_i(s_trie_tree_t *trie, uint32 *sequence, uint32 len) { uint32 *seq; s_array_stack_t *stack; s_trie_tree_t *trie_node; assert_exit(!complain_zero_size_p(len)); assert_exit(trie_tree_root_node_p(trie)); assert_exit(trie_tree_structure_legal_p(trie)); assert_exit(!NULL_PTR_P(sequence)); seq = sequence; trie_node = trie; stack = array_stack_create(); while (seq < sequence + len) { trie_node = trie_tree_sub_queue_find(trie_node, *seq); if (trie_node == NULL) { array_stack_destroy(&stack); return; } else { array_stack_push(stack, trie_node); seq++; } } while (!array_stack_empty_p(stack)) { trie_node = array_stack_pop(stack); if (trie_tree_sub_queue_empty_p(trie_node)) { trie_node->is_deleted = true; } } array_stack_destroy(&stack); }