int add_at_first(linkedlist_t* _list, void* data) { linkedlist_t* list = (linkedlist_t*) _list; node* n, *slider; if(list->size == 0) { return add_at_last(list, data); } else if(list->size == 1) { n = node_new(data); set_head(list, n); set_prev(get_tail(list), n); set_next(n, get_tail(list)); list->size++; } else // list->size > 1 { slider = get_head(list); n = node_new(data); set_next(n, slider); set_prev(slider, n); set_head(list, n); list->size++; } return list->size; }
uint32_t get_available(char* buffer) { if (is_empty(buffer)) return 0; if (get_head(buffer) > get_tail(buffer)) { return get_head(buffer) - get_tail(buffer); } return get_size(buffer) - get_tail(buffer) + get_head(buffer); }
int inject(linked_element **el, int i) { check(el, "Invalid pointer to *linked_element."); linked_element *_el = *el; check(_el, "Empty pointer to linked_element."); linked_element *tail_element = get_tail(_el); if(tail_element && !tail_element->set) { tail_element->val = i; tail_element->set = true; return 0; } else { linked_element *el_new = malloc(sizeof (linked_element)); check_mem(el_new); el_new->next = NULL; el_new->val = i; el_new->set = true; if(!tail_element) { el_new->prev = NULL; tail_element = el_new; } else { el_new->prev = tail_element; tail_element->next = el_new; } return 0; } error: return 1; }
int do_cmd_1_arg(QSP_ARG_DECL Cmd_Index cmd_index, int data_word) { u_short len; char pkt[20]; USB2000_Cmd_Def *ucdp; ucdp = &usb2000_cmd_tbl[cmd_index]; make_pkt( pkt, ucdp->ucd_cmd, data_word ); send_pkt(QSP_ARG pkt); len = strlen(pkt); if( get_echo(QSP_ARG pkt) < 0 ) return -1; if ( recv_a_byte(SINGLE_QSP_ARG) != ACK ) { WARN("ERROR: no ACK received"); return -1; } if( get_tail(SINGLE_QSP_ARG) < 0 ) return -1; return 0; }
void test_prepend ( void ) { uint32_t arr[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; uint32_t i; list * new_list = make_new_list(); node * cur = NULL; CU_ASSERT_PTR_NOT_NULL(new_list); for(i = 0; i < 10; i++) { prepend(new_list, &(arr[i])); } cur = get_head(new_list); CU_ASSERT_PTR_NOT_NULL(cur); i = 9; while(cur != NULL) { CU_ASSERT_EQUAL(*(uint32_t *)cur->data, arr[i]); cur = cur->next; i--; } cur = NULL; cur = get_tail(new_list); CU_ASSERT_PTR_NOT_NULL(cur); i = 0; while(cur != NULL) { CU_ASSERT_EQUAL(*(uint32_t *)cur->data, arr[i]); cur = cur->prev; i++; } CU_ASSERT_EQUAL(new_list->size, 10); }
int add_end_dcl_list(List **list, char *str) { List *node_ptr; /* malloc space for node, including elements housed by node (ptr values) */ node_ptr = malloc(sizeof(List)); if (node_ptr == NULL) return 1; /* set node str to a copy of the string using strdup */ node_ptr->str = strdup(str); if (node_ptr->str == NULL) return 1; /* if list is empty, make node's prev. element point to self; * reassign list to point to node_ptr */ if (*list == NULL) { node_ptr->prev = node_ptr; *list = node_ptr; } else { /* make prev pointer equal to pointer to last node in list */ node_ptr->prev = get_tail(list); /* make previous node's next element point to this node*/ node_ptr->prev->next = node_ptr; } /* make next pointer equal to first node in list */ node_ptr->next = *list; /* make first element's prev element equal to new node */ (*list)->prev = node_ptr; return 0; }
int main() { //创建队列并且初始化 Queue queue; queue.head = NULL; queue.tail = NULL; push(&queue, 12); push(&queue, 22); push(&queue, 32); push(&queue, 42); push(&queue, 52); push(&queue, 62); push(&queue, 72); push(&queue, 82); push(&queue, 92); printf("--------------------\n"); travel(&queue); printf("--------------------\n"); printf("出队的元素是: %d\n", pop(&queue)); printf("%s\n", empty(&queue) ? "队列空" : "队列没空"); printf("%s\n", full(&queue) ? "队列满" : "队列没满"); printf("队列中节点元素个数 :%d\n", size(&queue)); travel(&queue); printf("--------------------\n"); printf("队首元素: %d\n", get_head(&queue)); printf("队尾元素: %d\n", get_tail(&queue)); printf("--------------------\n"); clean(&queue); travel(&queue); return 0; }
bool mutex::try_lock_until( const fc::time_point& abs_time ) { fc::context* n = 0; fc::context* cc = fc::thread::current().my->current; { // lock scope fc::unique_lock<fc::spin_yield_lock> lock(m_blist_lock,abs_time); if( !lock ) return false; if( !m_blist ) { m_blist = cc; return true; } // allow recusive locks if ( get_tail( m_blist, n ) == cc ) return true; cc->next_blocked_mutex = m_blist; m_blist = cc; } // end lock scope try { fc::thread::current().my->yield_until( abs_time, false ); return( 0 == cc->next_blocked_mutex ); } catch (...) { cleanup( *this, m_blist_lock, m_blist, cc); throw; } }
int get_ver(QSP_ARG_DECL char *ver) { const char *pkt = "v"; int data_value; u_short len; len = 1; //send_serial( usb2000_fd, pkt, len ); send_usb2000_packet(QSP_ARG pkt,len); if( get_echo(QSP_ARG pkt) < 0 ) return -1; if ( recv_a_byte(SINGLE_QSP_ARG) != ACK ) { WARN("ERROR: no ACK received"); return -1; } if( ( data_value = recv_a_value(SINGLE_QSP_ARG) ) < 0 ) return -1; if( get_tail(SINGLE_QSP_ARG) < 0 ) return -1; sprintf(ver,"%d",data_value); sprintf( ver, "%c.%c%c.%c",ver[0], ver[1], ver[2], ver[3]); return 0; }
bool mutex::try_lock_until( const boost::chrono::system_clock::time_point& abs_time ) { cmt::context* n = 0; cmt::context* cc = cmt::thread::current().current_context(); { // lock scope boost::unique_lock<cmt::spin_yield_lock> lock(m_blist_lock,abs_time); if( !lock ) return false; if( !m_blist ) { m_blist = cc; return true; } // allow recusive locks if ( get_tail( m_blist, n ) == cc ) return true; cc->next_blocked = m_blist; m_blist = cc; } // end lock scope try { cmt::thread::current().yield_until( abs_time, false ); return( 0 == cc->next_blocked ); } catch (...) { cleanup( *this, m_blist_lock, m_blist, cc); throw; } }
POETCode* EvaluatePOET:: eval_readInput_nosyntax(POETCode* inputFiles, POETCode* codeType, POETCode* inputInline) { inputFiles = eval_AST(inputFiles); std::cerr << "Using ROSE Parser for " << inputFiles->toString() << "\n"; std::vector<std::string> argvList; argvList.push_back("pcg_rose"); for (std::list<std::string>::const_iterator p_lib = lib_dir.begin(); p_lib != lib_dir.end(); ++p_lib) { std::string cur_dir=(*p_lib); argvList.push_back("-I"+cur_dir); } for (POETCode* p = inputFiles; p != 0; p = get_tail(p)) { std::string curname = p->toString(OUTPUT_NO_DEBUG); assert(curname != ""); if (curname.find("/") == curname.npos) { for (std::list<std::string>::const_iterator p_lib = lib_dir.begin(); p_lib != lib_dir.end(); ++p_lib) { std::string cur_dir=(*p_lib) + "/" + curname; if (access(cur_dir.c_str(), R_OK) == 0) { curname = cur_dir; break;} } } argvList.push_back(curname); } assert(argvList.size() > 1); SgProject *sageProject = new SgProject ( argvList); /*TODO: must add support for inserting trace handles. optimization won't work otherwise*/ return POETAstInterface::Ast2POET(sageProject); }
// Macro is evil, inline function is more reliable. static inline void insert_node(int level, void * bp) { char **flist_head = get_head(level); char **flist_tail = get_tail(level); if (!(*flist_head)) { // empty list *flist_head = bp; *flist_tail = bp; set_prev_free(bp, NULL); set_next_free(bp, NULL); } else { if ((char *)bp < (*flist_head)) { // insert at head set_prev_free(*flist_head, bp); set_next_free(bp, *flist_head); set_prev_free(bp, NULL); *flist_head = bp; } else if ((*flist_tail) < (char *)bp) { // insert to tail set_next_free(*flist_tail, bp); set_prev_free(bp, *flist_tail); set_next_free(bp, NULL); *flist_tail = bp; } else { // find some place in the list char * c = *flist_head; while (c < (char *)bp) { c = next_free(c); } set_next_free(prev_free(c), bp); set_prev_free(bp, prev_free(c)); set_prev_free(c, bp); set_next_free(bp, c); } } }
int main (void) { auto l1 = create_and_populate_list<int> (0, 5); auto l2 = create_and_populate_list<int> (0, 7); auto l3 = create_and_populate_list<int> (10, 15); auto tail1 = l1.get_tail(); auto tail2 = l2.get_tail(); tail1->next = l3.head; tail2->next = l3.head; auto x = overlapping_list(l1, l2); std::cout << *x << std::endl; return 0; }
t_file *quick_recur(t_file *head, t_file *end) { t_file *nhead; t_file *n_end; t_file *pivot; t_file *tmp; nhead = NULL; n_end = NULL; if (!head || head == end) return (head); pivot = partition(head, end, &nhead, &n_end); if (nhead != pivot) { tmp = nhead; while (tmp->next != pivot) tmp = tmp->next; tmp->next = NULL; nhead = quick_recur(nhead, tmp); tmp = get_tail(nhead); tmp->next = pivot; } pivot->next = quick_recur(pivot->next, n_end); return (nhead); }
std::unique_ptr<node> try_pop_head() { std::lock_guard<std::mutex> head_lock(head_mutex); if (head.get() == get_tail()) { return std::unique_ptr<node>(); } return pop_head(); }
std::unique_ptr<node> pop_head() { std::lock_guard<std::mutex> head_lock(_head_mutex); if (_head.get() == get_tail()) return nullptr; std::unique_ptr<node> old_head = std::move(_head); _head = std::move(old_head->next); return old_head; }
void append(struct exec_token *xt_list, struct exec_token *xt) { struct exec_token *tail; tail = get_tail(xt_list); tail->next = new_param_exec_token(xt); }
POETCode* MatchOp(POETCode* curop, POETCode* input) { for (; curop != 0; (curop=get_tail(curop),input=NextToken(input))) { if (get_head(curop) != get_head(input)) break; } if (curop == 0) return (input==0)? EMPTY : input; return 0; }
std::unique_ptr<node> try_pop_head(T& value) { std::lock_guard<std::mutex> head_lock(head_mutex); if (head.get() == get_tail()) { return std::unique_ptr<node>(); } value = std::move(*head->data); return pop_head(); }
unit *get_tail(unit *head) { if (head != NULL && head->next != NULL) { return get_tail(head->next); } return(head); }
virtual void visitType(POETType *t) { if (t == ANY && fullmatch == 0) { /*QY: go over annotations */ POETCode* r1_save = r1, *r1_tail=get_tail(r1); r1 = r1_tail; if (r1 != 0) { POETCode* input2 = apply(t, 0); if (input2 == r1_tail) res = r1_save; else res = fac->new_list(get_head(r1_save), input2); } else res = r1_save; return; } POETCode* input = (fullmatch==0)? r1 : get_head(r1); res = match_Type(input, t, true); if (res == 0) PARSE_MISMATCH(input,t,lineno); if (fullmatch != 0) r1 = get_tail(r1); else r1 = 0; }
int set_data_mode(QSP_ARG_DECL Cmd_Index data_mode) { u_short len; char pkt[3]; USB2000_Cmd_Def *ucdp; u_short n_to_get; u_short i; ucdp = &usb2000_cmd_tbl[data_mode]; sprintf(pkt, "%s", ucdp->ucd_cmd); len = strlen(pkt); //send_serial( usb2000_fd, pkt, len ); send_usb2000_packet(QSP_ARG pkt,len); /**************************************************************** bin->ascii ascii->bin ascii->ascii bin->bin cmd: aA bB aA bB reply: A b a B ACK B A ACK LF ACK ACK LF CR LF LF CR > CR CR > SPACE > > SPACE SPACE SPACE *****************************************************************/ if( ascii_mode ) n_to_get = len; else n_to_get = len-1; for( i=0; i<n_to_get; i++ ) { if( recv_a_byte(SINGLE_QSP_ARG) < 0 ) return -1; } if ( recv_a_byte(SINGLE_QSP_ARG) != ACK ) { WARN("ERROR: no ACK received"); clear_input_buf(SINGLE_QSP_ARG); return -1; } if(data_mode == ASCII) ascii_mode=TRUE; else ascii_mode=FALSE; if( get_tail(SINGLE_QSP_ARG) < 0 ) return -1; return 0; }
std::unique_ptr<node> pop_head() { std::lock_guard<std::mutex> head_lock(head_mutex); if(head.get()==get_tail()) { return nullptr; } std::unique_ptr<node> const old_head=std::move(head); head=std::move(old_head->next); return old_head; }
POETCode* extract_parameter_names(POETCode* pars) { if (pars == 0) return 0; POETCode* head = get_head(pars); head = split_string("", head); POETCode* tmp = EvaluatePOET::NextToken(head); while (tmp != EMPTY && tmp != 0) { head = tmp; tmp = EvaluatePOET::NextToken(head); } head = EvaluatePOET::FirstToken(head); POETCode* tail = extract_parameter_names(get_tail(pars)); return LIST(head, tail); }
int add_at_last(linkedlist_t* list, void* data) { node* n; n = node_new(data); // empty? if(NULL == get_head(list)) { set_head(list, n); set_tail(list, n); list->size = 1; return list->size; } set_next(get_tail(list), n); set_prev(n, get_tail(list)); set_tail(list, n); list->size++; return list->size; }
void test_get_tail ( void ) { uint32_t arr[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; uint32_t i; list * new_list = make_new_list(); node * cur = get_tail(new_list); CU_ASSERT_PTR_NOT_NULL(new_list); CU_ASSERT_PTR_NULL(cur); for(i = 0; i < 10; i++) { add(new_list, &(arr[i])); } cur = get_tail(new_list); CU_ASSERT_PTR_NOT_NULL(cur); CU_ASSERT_EQUAL(*(uint32_t *)cur->data, arr[9]); CU_ASSERT_PTR_NULL(cur->next); CU_ASSERT_PTR_NOT_NULL(cur->prev); }
/* void print_int(void *value); void print_int(void *value) { printf("%d ",*(int *)value); } */ int main(int argc, char **argv) { int i = 0; int a[]={1,2,3,4,5}; void *value; Dlist *dlist = NULL; dlist = init_dlist(); printf("3.push_front(dlist, &a[i]);\n"); for(i=0; i< sizeof(a)/sizeof(a[0]);++i) { push_front(dlist, &a[i]); } show_dlist(dlist, print_int); printf("5.pop_front(dlist);\n"); pop_front(dlist); show_dlist(dlist, print_int); printf("4.push_back(dlist, &a[i]);\n"); for(i=0; i< sizeof(a)/sizeof(a[0]);++i) { push_back(dlist, &a[i]); } show_dlist(dlist, print_int); printf("6.pop_back(dlist);\n"); pop_back(dlist); show_dlist(dlist, print_int); printf("7.insert_prev(dlist, dlist->head->next->next, &a[4]);\n"); insert_prev(dlist, dlist->head->next->next, &a[4]); show_dlist(dlist, print_int); printf("8.insert_next(dlist, dlist->head->next->next, &a[4]);\n"); insert_next(dlist, dlist->head->next->next, &a[4]); show_dlist(dlist, print_int); printf("9.remove_dlist_node(dlist, dlist->head->next->next->next);\n"); remove_dlist_node(dlist, dlist->head->next->next->next); show_dlist(dlist, print_int); get_front(dlist, &value); printf("\n11.get_front:\n"); print_int(value); get_tail(dlist, &value); printf("\n12.get_tail:\n"); print_int(value); printf("\n13.get_dlist_count:\n"); printf("%d \n",get_dlist_count(dlist)); destroy_dlist(&dlist); return 0; }
void mutex::unlock() { cmt::context* next = 0; { boost::unique_lock<cmt::spin_yield_lock> lock(m_blist_lock); get_tail(m_blist, next); if( next ) { next->next_blocked = 0; next->ctx_thread->unblock( next ); } else { m_blist = 0; } } }
void mutex::unlock() { fc::context* context_to_unblock = 0; { fc::unique_lock<fc::spin_yield_lock> lock(m_blist_lock); get_tail(m_blist, context_to_unblock); if( context_to_unblock ) { context_to_unblock->next_blocked_mutex = 0; context_to_unblock->ctx_thread->my->unblock( context_to_unblock ); } else { m_blist = 0; } } }
unsigned EvaluatePOET:: compute_exp_lookahead(std::vector<POETCode*>& res) { res.push_back(fac->new_string("(")); if (exp_item->get_entry().get_code() == 0) SYM_UNDEFINED("EXP_BASE"); compute_lookaheadInfo(exp_item->get_entry().get_code(), res); POETCode* uop = exp_uop->get_entry().get_code(); while (uop != 0) { res.push_back(get_head(uop)); uop = get_tail(uop); } return 1; }