示例#1
0
void rb() {
    printf(">> RB\n");
    rb_tree_t tree;
    rb_init(&tree, NULL);

    test_rb_t a = { 1 };
    test_rb_t b = { 2 };
    test_rb_t c = { 3 };

    rb_insert(&tree, &a.node, &test_rb_compare);
    rb_insert(&tree, &b.node, &test_rb_compare);
    rb_insert(&tree, &c.node, &test_rb_compare);

    // Display them
    rb_node_t *node = rb_head(&tree);
    while (node) {
        rb_node_t *next = rb_next(node);
        rb_node_t *prev = rb_prev(node);
        test_rb_t *c = rb_ref(node, test_rb_t, node);
        test_rb_t *n = next ? rb_ref(next, test_rb_t, node) : NULL;
        test_rb_t *p = prev ? rb_ref(prev, test_rb_t, node) : NULL;
        printf("current: %d, next: %d, prev: %d\n",
                c->number,
                n ? n->number : -1,
                p ? p->number : -1);
        node = next;
    }
}
示例#2
0
文件: rbtree.c 项目: coskifu/syslinux
struct rbtree *rb_insert(struct rbtree *tree, struct rbtree *node)
{
    node->left = node->right = NULL;
    node->red = false;

    if (!tree) {
        node->red = true;
        return node;
    }

    if (is_red(tree->left) && is_red(tree->right))
        color_flip(tree);

    if (node->key < tree->key)
        tree->left = rb_insert(tree->left, node);
    else
        tree->right = rb_insert(tree->right, node);

    if (is_red(tree->right))
        tree = rotate_left(tree);

    if (is_red(tree->left) && is_red(tree->left->left))
        tree = rotate_right(tree);

    return tree;
}
示例#3
0
void insert_CLRS(NODE * t, int v)
{
	if (dbg_level == 0) {
		fprintf(stderr, "%s%d%s, ", GREEN, v, NOCOLOR);
		rb_insert(t, alloc_node(v, R));
		verify_rbtree(t->L, 0);
	} else {
		printf("%sinsert %d%s\n", GREEN, v, NOCOLOR);
		rb_insert(t, alloc_node(v, R));
		______________________________("./fig/", t, t, "inserted %d", v);
	}
}
示例#4
0
/* If hk==NULL to register, new is attempted to be created. */
WHook *mainloop_register_hook(const char *name, WHook *hk)
{
    char *nnm;
    
    if(hk==NULL)
        return NULL;
    
    if(named_hooks==NULL){
        named_hooks=make_rb();
        if(named_hooks==NULL)
            return NULL;
    }
    
    nnm=scopy(name);
    
    if(nnm==NULL)
        return NULL;
    
    if(!rb_insert(named_hooks, nnm, hk)){
        free(nnm);
        destroy_obj((Obj*)hk);
    }
    
    return hk;
}
示例#5
0
文件: median.c 项目: elvinxiao/dml
static void m_insert_right(MTrace * m, void * pdata){
    rb_insert(m->rb2, pdata);
    m->cnt2 += 1;
    if ((m->right_min && m->cmp_fn(pdata, m->right_min) < 0) || (!m->right_min)){
        m->right_min = pdata;
    }
}
示例#6
0
文件: median.c 项目: elvinxiao/dml
static void m_insert_left(MTrace * m, void *pdata){
    rb_insert(m->rb1, pdata);
    m->cnt1 += 1;
    if ((m->left_max && m->cmp_fn(pdata, m->left_max) > 0) || (!m->left_max)){
        m->left_max = pdata;
    }
}
示例#7
0
StringId stringstore_alloc_n(const char *str, uint l)
{
    Rb_node node=(Rb_node)stringstore_find_n(str, l);
    char *s;
    
    if(node!=NULL){
        node->v.ival++;
        return node;
    }
    
    if(stringstore==NULL){
        stringstore=make_rb();
        if(stringstore==NULL)
            return STRINGID_NONE;
    }
    
    s=scopyn(str, l);
    
    if(s==NULL)
        return STRINGID_NONE;
    
    node=rb_insert(stringstore, s, NULL);
    
    if(node==NULL)
        return STRINGID_NONE;
    
    node->v.ival=1;
        
    return (StringId)node;
}
示例#8
0
BOOL vmm_ld_mapped(struct pm_task *task, UINT32 vlow, UINT32 vhigh)
{
    struct vmm_memory_region *mreg = NULL;

    // I'll only create a memory region for LD on the task..
    // I won't check collitions because this is always the first lib loaded.
    mreg = kmalloc(sizeof(struct vmm_memory_region));

    if(!mreg) return FALSE;

    if(!rb_free_value(&task->vmm_info.regions_id, &mreg->tsk_id_node.value))
	{
		kfree(mreg);
		return FALSE;
	}

    mreg->owner_task = task->id;
    mreg->next = mreg->prev = NULL;
	mreg->tsk_node.high = TRANSLATE_ADDR(vhigh, UINT32);
	mreg->tsk_node.low = TRANSLATE_ADDR(vlow, UINT32);
	mreg->flags = VMM_MEM_REGION_FLAG_NONE;
	mreg->type = VMM_MEMREGION_LIB;
    mreg->descriptor = NULL;

    ma_insert(&task->vmm_info.regions, &mreg->tsk_node);
    rb_insert(&task->vmm_info.regions_id, &mreg->tsk_id_node, FALSE);

    return TRUE;
}
示例#9
0
int
main(void)
{
	rbt root;
	int i;
	int key[] = { 1, 2, 4, 5, 7, 8, 11, 14, 15, 39, 29, 73, 24, 18, 18, 18, 18};
	int key2[] = {15, 14, 11, 8, 7, 5, 4, 2, 1, 18, 73, 24, 39, 29, 18, 18, 18};
	/*int key[] = {41, 38, 31, 12, 19, 8};
	int key2[] = { 8, 12, 19, 31, 38, 41};*/
	rbn nil = { BLACK, 0, NULL, NULL, NULL};
	root.nil = &nil;
	root.root = &nil;
	rbn *tmp;
	for (i = 0; i < sizeof(key2) / sizeof(int); i++) {
		tmp = malloc(sizeof(rbn));
		tmp->key = key2[i];
		rb_insert(&root, tmp);
		printf("insert:%d\n", tmp->key);
	}
	for (i = 0; i < sizeof(key) / sizeof(int); i++) {
		tmp = rb_search(root.root, key[i]);
		rb_delete(&root, tmp);
		printf("deleted:%d\n", tmp->key);
	}
	printf("\n");
	return 0;
}
示例#10
0
文件: playlist.c 项目: jonsafari/mocp
/* Add a file to the list. Return the index of the item. */
int plist_add (struct plist *plist, const char *file_name)
{
	assert (plist != NULL);
	assert (plist->items != NULL);

	if (plist->allocated == plist->num) {
		plist->allocated *= 2;
		plist->items = (struct plist_item *)xrealloc (plist->items,
				sizeof(struct plist_item) * plist->allocated);
	}

	plist->items[plist->num].file = xstrdup (file_name);
	plist->items[plist->num].type = file_name ? file_type (file_name)
		: F_OTHER;
	plist->items[plist->num].deleted = 0;
	plist->items[plist->num].title_file = NULL;
	plist->items[plist->num].title_tags = NULL;
	plist->items[plist->num].tags = NULL;
	plist->items[plist->num].mtime = (file_name ? get_mtime(file_name)
			: (time_t)-1);
	plist->items[plist->num].queue_pos = 0;

	if (file_name) {
		rb_delete (plist->search_tree, file_name);
		rb_insert (plist->search_tree, (void *)(intptr_t)plist->num);
	}

	plist->num++;
	plist->not_deleted++;

	return plist->num - 1;
}
示例#11
0
int main(int argc, char *argv[])
{
	struct rb_node sentinel;
	struct rb_tree rbtree;
	struct rb_node node[1000];
	int i, j;

	rb_init(&rbtree, &sentinel);
	srand(time(0));

	for (i = 0; i < 10; i++) {
		for (;;) {
			node[i].key = rand()%10000;
			for (j = 0; j < i; j++) {
				if (node[i].key == node[j].key)
					break;
			}
			if (j < i)
				continue;
			else
				break;
		}
		rb_insert(&rbtree, &node[i]);
	}
	for (i = 0; i < 10; i++)
		rb_delete(&rbtree, &node[i]);

	exit(0);
}
示例#12
0
static bool
node_insert_at(struct rbtree_elem **root, struct rbtree_elem *node,
    struct rbtree_elem *new_node, rbtree_less_func *less)
{
  bool inserted;

  inserted = false;

  while (!inserted) {
    ASSERT(node!=nil);
    if (less(node, new_node)) /*(node->high < lowIP)*/ {
      if (node->right == nil) {
        node->right = new_node;
        new_node->parent = node;
        inserted = true;
      } else {
        node = node->right;
      }
    } else {
      ASSERT(is_less(new_node, node));
      if (node->left == nil) {
        node->left = new_node;
        new_node->parent = node;
        inserted = true;
      } else {
        node = node->left;
      }
    }
  }

  rb_insert(root, new_node) ;
  return true;
}
示例#13
0
int Write(unsigned long *random_seed, param_t *params)
{
    long int_value;
    void *value;

    int_value = (get_random(random_seed) % params->size) + 1;

    // make sure we have an odd value
    int_value |= 0x0001;

    value = rb_remove(My_Tree, int_value);
    if (value == NULL)
    {
        printf("Failure to remove %ld\n", int_value);
        exit(-2);
    }

    if (!rb_insert(My_Tree, int_value, (void *)int_value) )
    {
        printf("Failure to insert %ld\n", int_value);
        exit(-3);
    }

    return 0;
}
示例#14
0
int Write(unsigned long *random_seed, param_t *params)
{
    int errors = 0;
    int write_elem;
    long int_value;
    void *value;

    write_elem = get_random(random_seed) % params->size;
#ifdef DEBUG
    check_tree();
    printf("Remove %ld\n", Values[write_elem]);
#endif
    value = rb_remove(My_Tree, Values[write_elem]);
    if (value == NULL) errors++;

    int_value = get_random(random_seed) % params->scale + 1;
#ifdef DEBUG
    check_tree();
    printf("Insert %ld\n", int_value);
#endif
    while ( !rb_insert(My_Tree, int_value, (void *)int_value) )
    {
        int_value = get_random(random_seed) % params->scale + 1;
#ifdef DEBUG
        printf("Insert %ld\n", int_value);
#endif
    }
    Values[write_elem] = int_value;

    return errors;
}
示例#15
0
文件: rbtree.c 项目: jpcoles/ZM
int main() {
    RB_TREE root = NULL;
    RB_TYPE rbt;
    int v,i;

    rb_type_create( &rbt, sizeof(VNODE), 0, cmp, NULL, NULL );

    srand(2);
    for( i=0; i<1000; i++ ) {
	v = rand();
	//v = 1000000-i;
	rb_insert( &rbt, &root, &v );
	}
    rb_assert(&rbt,root);

    printf( "Tree has %d elements\n", rb_size(&root) );

    srand(2);
    for( i=0; i<1000; i++ ) {
	v = rand();
	//v = 1000000-i;
	rb_remove( &rbt, &root, &v );
	}
    rb_assert(&rbt,root);
    printf( "Tree has %d elements\n", rb_size(&root) );

    rb_free(&rbt,&root);

    return 0;
    }
示例#16
0
int register_event_prio(int fd, event_handler_t h, void *data, int prio)
{
	int ret;
	struct epoll_event ev;
	struct event_info *ei;

	ei = xzalloc(sizeof(*ei));
	ei->fd = fd;
	ei->handler = h;
	ei->data = data;
	ei->prio = prio;

	memset(&ev, 0, sizeof(ev));
	ev.events = EPOLLIN;
	ev.data.ptr = ei;

	ret = epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
	if (ret) {
		sd_err("failed to add epoll event for fd %d: %m", fd);
		free(ei);
	} else
		rb_insert(&events_tree, ei, rb, event_cmp);

	return ret;
}
示例#17
0
void *rb_put(struct rb_tree *t, void *key, void *value)
{
  struct rb_tree_node *n = rb_insert(t, key);
  void *old_value = n->value;
  n->value = value;
  return old_value;
}
示例#18
0
void *LCUIMM_Alloc( size_t size, unsigned int class_id )
{
	rb_node_t *node;
	mem_data_t mem_data;

	mem_data.mem_info.class_id = class_id;
	/* 查找该类别的结点 */
	node = rb_search( global_mem_class_info.root,
			&mem_data, RB_DATA_TYLE_INFO );
	/* 如果不存在,则用默认类别的结点 */
	if( node == NULL ) {
		mem_data.mem_info.class_id = 0;
		node = rb_search( global_mem_class_info.root,
				&mem_data, RB_DATA_TYLE_INFO );
		/* 如果默认类别的结点不存在,则说明出问题了 */
		if( node == NULL ) {
			abort();
		}
	}
	/* 分配内存空间,并记录它 */
	mem_data.mem_blk.mem_addr = malloc( size );
	/* 分配失败则返回NULL */
	if( mem_data.mem_blk.mem_addr == NULL ) {
		return NULL;
	}
	mem_data.mem_blk.mem_size = size;
	/* 累计该类别的总内存空间大小 */
	node->mem_data.mem_info.total_size += size;
	/* 插入该内存的信息 */
	rb_insert( &global_mem_data, &mem_data, RB_DATA_TYPE_ADDR );
	return mem_data.mem_blk.mem_addr;
}
示例#19
0
文件: playlist.c 项目: jonsafari/mocp
/* Swap the first item on the playlist with the item with file fname. */
void plist_swap_first_fname (struct plist *plist, const char *fname)
{
	int i;

	assert (plist != NULL);
	assert (fname != NULL);

	i = plist_find_fname (plist, fname);

	if (i != -1 && i != 0) {
		rb_delete (plist->search_tree, fname);
		rb_delete (plist->search_tree, plist->items[0].file);
		plist_swap (plist, 0, i);
		rb_insert (plist->search_tree, NULL);
		rb_insert (plist->search_tree, (void *)(intptr_t)i);
	}
}
示例#20
0
static void add_suggestion(rb_tree *suggs, string word){
  if(!(rb_lookup(suggs, &word))){
    DEBUG_PRINTF("adding suggestion %.*s\n", (int)word.len, word.str);
    string *ptr = xmalloc(sizeof(string) + word.sz);
    ptr->sz = word.sz; ptr->mem = (((void*)ptr)+sizeof(string));
    memcpy(ptr->mem, word.mem, word.sz);
    rb_insert(suggs, ptr);
  }
}
示例#21
0
文件: vmm.c 项目: jefjin/ucore
// insert_vma_rb - insert vma in rb tree according vma->start_addr
static inline void
insert_vma_rb(rb_tree *tree, struct vma_struct *vma, struct vma_struct **vma_prevp) {
    rb_node *node = &(vma->rb_link), *prev;
    rb_insert(tree, node);
    if (vma_prevp != NULL) {
        prev = rb_node_prev(tree, node);
        *vma_prevp = (prev != NULL) ? rbn2vma(prev, rb_link) : NULL;
    }
}
示例#22
0
//*******************************
void *Init_Data(int count, void *lock, param_t *params)
{
    int ii;
    unsigned long seed = init_random_seed(); // random();
    unsigned long value;

    Values = (unsigned long *)malloc(count*sizeof(unsigned long));
    My_Tree = (rbtree_t *)malloc(sizeof(rbtree_t));
    rb_create(My_Tree, lock);

#ifndef VALIDATE
    if (params->mode == MODE_TRAVERSE || params->mode == MODE_TRAVERSENLN)
    {
        rb_insert(My_Tree, -1, (void *)-1);
        rb_insert(My_Tree, params->scale+1, (void *)(long)params->scale+1);
        //count -= 2;
    }
#endif

#ifdef VALIDATE
    for (value=1; value<=count; value++)
    {
        rb_insert(My_Tree, value, (void *)(value));
    }
#else
    for (ii=0; ii<count; ii++)
    {
        value = get_random(&seed) % params->scale + 1;
        while ( !rb_insert(My_Tree, value, (void *)value) )
        {
            value = get_random(&seed) % params->scale + 1;
        }
#ifdef DEBUG_INSERT
        if (ii%1000 == 0)
        printf("Insert %d %ld\n", ii, value);
        //check_tree();
#endif

        Values[ii] = value;
    }
#endif

    return My_Tree;
}
示例#23
0
static struct fs_entry *vfs_lookup(struct fs_entry *dir, const char *name,
			bool create, int *rc)
{
	struct fs_node *node = dir->node;
	struct rb_node **plink = &dir->children.root;
	struct rb_node *parent = 0;

	if (!node->ops->lookup) {
		*rc = -ENOTSUP;
		return 0;
	}

	const bool enabled = spin_lock_irqsave(&dir->lock);

	while (*plink) {
		struct fs_entry *entry = TREE_ENTRY(*plink, struct fs_entry,
			link);

		const int cmp = strcmp(entry->name, name);

		if (!cmp) {
			*rc = 0;
			vfs_entry_get(entry);
			spin_unlock_irqrestore(&dir->lock, enabled);
			return entry;
		}

		parent = *plink;
		if (cmp < 0)
			plink = &parent->right;
		else
			plink = &parent->left;
	}

	struct fs_entry *entry = vfs_entry_create(name);

	if (!entry) {
		spin_unlock_irqrestore(&dir->lock, enabled);
		*rc = -ENOMEM;
		return 0;
	}

	rb_link(&entry->link, parent, plink);
	rb_insert(&entry->link, &dir->children);
	entry->parent = vfs_entry_get(dir);
	entry->cached = true;
	spin_unlock_irqrestore(&dir->lock, enabled);

	*rc = node->ops->lookup(node, entry);
	if (*rc && !create) {
		vfs_entry_put(entry);
		return 0;
	}

	return entry;
}
示例#24
0
int Insert(unsigned long *random_seed, param_t *params)
{
    int errors = 0;
    long int_value;

    int_value = get_random(random_seed) % params->scale + 1;
    if (!rb_insert(My_Tree, int_value, (void *)int_value) ) errors++;

    return errors;
}
示例#25
0
void
node_insert(RB_TREE *T, int key)
{
    RB_NODE     *node;

    node = node_init(T);
    node->key = key;

    rb_insert(T, node);
}
示例#26
0
文件: usart.c 项目: bubi-007/ardurevo
uint32_t usart_tx(usart_dev *dev, const uint8_t *buf, uint32_t len)
    {
    /* Check the parameters */
    assert_param(IS_USART_ALL_PERIPH(USARTx));
    assert_param(IS_USART_DATA(Data));

    uint32_t tosend = len;
    uint32_t sent = 0;

    if (dev->usetxrb)
	{
	while (tosend)
	    {
	    if (rb_is_full(dev->txrb))
		break;
	    rb_insert(dev->txrb, *buf++);
	    sent++;
	    tosend--;
	    }
	if (dev->txbusy == 0 && sent > 0)
	    {
	    dev->txbusy = 1;
	    USART_ITConfig(dev->USARTx, USART_IT_TXE, ENABLE);
	    }
	}
    else
	{
#if 0
	uint32_t rtime;
	if (dev->use_timeout)
	stopwatch_reset();
#endif
	while (tosend)
	    {
	    while (!(dev->USARTx->SR & USART_FLAG_TXE))
		{
#if 0
		if (dev->use_timeout)
		    {
		    rtime = stopwatch_getus();
		    if (rtime >= dev->tx_timeout)
			{
			return sent;
			}
		    }
#endif
		}
	    dev->USARTx->DR = *buf++;
	    tosend--;
	    sent++;
	    }
	}

    return sent;
    }
示例#27
0
rb_node_t* rb_create(int n, int arr[])
{
	rb_node_t* root;
	int i = 0;
	root = &nil;
	for(i = 0;i<n;i++)
	{
		root = rb_insert(root,arr[i]);
	}
  return root;
}
示例#28
0
void fbt_memprotect_add_valid(void *addr_begin, int len)
{
  struct mem_info *info = (struct mem_info*) malloc(sizeof(struct mem_info));
  info->node.addr_begin = addr_begin;
  info->node.addr_end = addr_begin + len;
  info->flags = INFO_RFLAG | INFO_XFLAG;

  info->lib_index = 0;    // TODO check if we can set this -1 or so
  info->sec_name = ralloc_str;
  sections_root = rb_insert(sections_root, (struct rb_node*) info);
}
示例#29
0
/*
 * mm_malloc - Allocate a block
 *
 * If there exists a free block where the request fits, get the smallest one, segment it and allocate.
 * If there is no such block, increase brk.
 */
void* mm_malloc(size_t size)
{
    size_t block_size, next_block_size;
    void *free_block, *next_block;
    
    block_size = ALIGN(HEADER_SIZE + size);
    block_size = block_size < MIN_BLOCK_SIZE ? MIN_BLOCK_SIZE : block_size;

    free_block = rb_find(block_size);
    if(free_block == rb_null){ // proper free block not found
        /* set free_block to the end of last block in heap */
        free_block = mem_heap_hi() - 3;
        if(PREV_FREE(free_block)){ // if the last block is free
            /* set free_block to the last block */
            free_block -= PREV_SIZE_MASKED(free_block);
            if(IS_IN_RB(free_block)){
                rb_delete(free_block);
            }
            /* this block is smaller than request, so increase brk */
            mem_sbrk(block_size - CUR_SIZE_MASKED(free_block));
        }else{ // if the last block is not free
            mem_sbrk(block_size);
        }
    }else{
        /* will be allocated, so delete from tree first */
        rb_delete(free_block);
        /* if the block is bigger than request, segment it */
        if((next_block_size = CUR_SIZE_MASKED(free_block) - block_size) > 0){
            next_block = NEXT_BLOCK(free_block, block_size);
            CUR_SIZE(next_block) = PREV_SIZE(NEXT_BLOCK(next_block, next_block_size)) = next_block_size | 1;
            if(IS_IN_RB(next_block)){
                rb_insert(next_block);
            }
        }
    }
    CUR_SIZE(free_block) = PREV_SIZE(NEXT_BLOCK(free_block, block_size)) = block_size;

#ifdef DEBUG
    printf("mm_malloc(%u) called\n", size);
    printf("free_block = %p\n", free_block);
    rb_print_preorder();
    printf("\n");
#endif /* DEBUG */

#ifdef CHECK
    if(!mm_check()){
        rb_print_preorder();
        exit(0);
    }
#endif /* CHECK */

    return USER_BLOCK(free_block);
}
示例#30
0
文件: main.c 项目: godspeed1989/misc
int main()
{
	int i;
	rb_tree_t tree;
	rb_node_t *node;
	rb_tree_create(&tree, NULL);

	for (i = 0; i < MAX; i++)
		rb_insert(&tree, i, NULL);
	rb_clear(&tree);
	for (i = 0; i < MAX; i+=2)
		rb_insert(&tree, i, NULL);
	for (i = 0; i < MAX; i+=4)
	{
		node = rb_find(&tree, i);
		rb_delete(&tree, node, FALSE);
	}

	rb_tree_destroy(&tree);
	return 0;
}