Esempio n. 1
0
typename BSTree<KeyType, ValueType>::BSTreeNode*
BSTree<KeyType, ValueType>::_insert(typename BSTree<KeyType, ValueType>::BSTreeNode *parent, typename BSTree<KeyType, ValueType>::BSTreeNode **root, KeyType key, ValueType value)
{
	if(*root == 0){
        typename BSTree<KeyType, ValueType>::BSTreeNode *pNode = _newNodeImpl();
		pNode->key = key;
        pNode->value = value;
		pNode->parent = parent;
		(*root) = pNode;
        if(parent == NULL){
            (*root)->pre = (*root)->next = NULL;
            firstNode = *root;
        }else{
            if(&(parent->lchild) == root){
                (*root)->pre = parent->pre;
                (*root)->next = parent;
                if(parent->pre) parent->pre->next = *root;
                else firstNode = *root;
                parent->pre = *root;
            }else{
                (*root)->pre = parent;
                (*root)->next = parent->next;
                if(parent->next) parent->next->pre = *root;
                parent->next = *root;
            }
        }
		return parent;
	}else if(key < (*root)->key) return _insert(*root, &((*root)->lchild),key, value);
	else return _insert(*root, &((*root)->rchild), key, value);
}
Esempio n. 2
0
Tptr _insert(Tobj *pTernary, Tptr p, char *s, char *insertstr) {
  if (p == 0) {
    p = (Tptr) malloc(sizeof(Tnode));
    p->splitchar = *s;
    p->lokid = p->eqkid = p->hikid = 0;
    pTernary->nodes++;
  }
  if (*s < p->splitchar)
    p->lokid = _insert(pTernary, p->lokid, s, insertstr);
  else if (*s == p->splitchar) {
    if (*s == 0) {
      if (p->eqkid) {
	free(p->eqkid);
	p->eqkid = (Tptr) insertstr;
      } else {
	p->eqkid = (Tptr) insertstr;
	pTernary->terminals++;
      }
    }
    else
      p->eqkid = _insert(pTernary, p->eqkid, ++s, insertstr);
  } else
    p->hikid = _insert(pTernary, p->hikid, s, insertstr);
  return p;
}
Esempio n. 3
0
Boolean bset::_insert(void* x, bsetnode *rt)
{
/*
debug(cerr, "in bsert _insert");
debug(cerr, int(x));
debug(cerr, int(rt));
*/

/*
debug(cerr, int(f_cmp_func_eq));
debug(cerr, int(f_cmp_func_ls));
*/

   if ( (*f_cmp_func_eq)(x, rt -> v_element) == true )
      return false;

   if ( (*f_cmp_func_ls)(x, rt -> v_element) == true ) 
      if ( rt -> v_left == 0 ) {
         rt -> v_left = new bsetnode(x);
         return true;
      } else
         return _insert(x, rt -> v_left);
   else
      if ( rt -> v_right == 0 ) {
         rt -> v_right = new bsetnode(x);
         return true;
      } else
         return _insert(x, rt -> v_right);
}
Esempio n. 4
0
	void bst<type>::_insert(np cur, const type& x) {
		if (x > cur->_data) {
			if (cur->right == NULL) {
				np tmp = new nt();
				cur->right = tmp;
				tmp->_data = x;
			}
			else {
				_insert(cur->right, x);
			}
		}
		if (x < cur->_data) {
			if (cur->left == NULL) {
				np tmp = new nt();
				cur->left = tmp;
				cur->height++;
				tmp->_data = x;
			}
			else {
				_insert(cur->left, x);
			}
		}
		cur->height = (
			_height(cur->left) > _height(cur->right) ?
			_height(cur->left) : _height(cur->right)
		) + 1;
	}
Esempio n. 5
0
void *str2ptr_set(struct str2ptr *map, char *key, void *value) {
    unsigned int hash = _hash(key), index = _get_index(map, key, hash);
    void *old = NULL;
    if(index <= map->mask) {
        old = map->pair[index].value;
        map->pair[index].value = value;
    } else if(map->fill == map->mask + 1) {
        struct str2ptr new_map;
        unsigned int size = (map->mask + 1) << 2;

        memset(&new_map, 0, sizeof(new_map));
        new_map.mask = size - 1;
        new_map.pair = (struct str2ptr_pair *)calloc(size, sizeof(*new_map.pair));

        for(index = 0; index < map->mask + 1; ++index) {
            _insert(&new_map, map->pair[index].key, map->pair[index].hash, map->pair[index].value);
        }
        _insert(&new_map, key, hash, value);

        free(map->pair);
        *map = new_map;
    } else {
        _insert(map, key, hash, value);
    }
    return old;
}
Esempio n. 6
0
	//returns the node that shall take its place after rotations and stuff
	treap::node* treap::_insert(int val, treap::node *pos) {
		if (pos == nullptr) {
			return new node(val);
		}

		//node already exists, ignore
		if (pos->val == val) {
			return pos;
		}

		//insert left
		if (val < pos->val) {
			pos->left = _insert(val, pos->left);
			if (pos->rnd > pos->left->rnd) {
				return pos->rotR();
			} else {
				return pos;
			}
		} else { //insert right
			pos->right = _insert(val, pos->right);
			if (pos->rnd > pos->right->rnd) {
				return pos->rotL();
			} else {
				return pos;
			}
		}

	}
Esempio n. 7
0
	void _insert(Node*& node, const Key& key, const Value& value)
	{
		if (! node) node = new Node(key, value);
		
		else if (node == _end)
		{
			node = new Node(key, value);
			
			node->right = _end;
			_end->parent = node;
		}
		
		else if (key < node->key)
		{
			_insert(node->left, key, value);
			
			node->left->parent = node;
			
			if (node == _begin) _begin = node->left;
			
			node->resize();
		}
		
		else if (key > node->key)
		{
			_insert(node->right, key, value);
			
			node->right->parent = node;
			
			node->resize();
		}
	}
Esempio n. 8
0
File: AVL.c Progetto: GitMMM/C_Study
void _insert(TreeNode *tree_node, int num)
{
	if (num > tree_node->num) {
		if (tree_node->right) {
			_insert(tree_node->right, num);
		} else {
			TreeNode *new_node;
			new_node	= (TreeNode *)malloc(sizeof(TreeNode));
			new_node->num	= num;
			new_node->height	= 0;
			new_node->parent	= tree_node;
			tree_node->right	= new_node;
		}
	} else if (num < tree_node->num) {
		if (tree_node->left) {
			_insert(tree_node->left, num);
		} else {
			TreeNode *new_node;
			new_node	= (TreeNode *)malloc(sizeof(TreeNode));
			new_node->num	= num;
			new_node->height	= 0;
			new_node->parent	= tree_node;
			tree_node->left	= new_node;
		}
	} else {
		return ;
	}

	int l_height, r_height;
	l_height	= tree_node->left ? tree_node->left->height : -1;
	r_height	= tree_node->right ? tree_node->right->height : -1;

	if (l_height - r_height > 1) {
		l_height	= tree_node->left->left ? tree_node->left->left->height : -1;
		r_height	= tree_node->left->right ? tree_node->left->right->height : -1;
		if (l_height > r_height) {
			_single_rotate(tree_node, 0);
		} else {
			_double_rotate(tree_node, 0);
		}
	} else if (r_height - l_height > 1) {
		r_height	= tree_node->right->right ? tree_node->right->right->height : -1;
		l_height	= tree_node->right->left ? tree_node->right->left->height : -1;
		if (r_height > l_height) {
			_single_rotate(tree_node, 1);
		} else {
			_double_rotate(tree_node, 1);
		}
	}

	l_height	= tree_node->left ? tree_node->left->height : -1;
	r_height	= tree_node->right ? tree_node->right->height : -1;
	if (l_height > r_height) {
		tree_node->height	= l_height + 1;
	} else {
		tree_node->height	= r_height + 1;
	}
}
Esempio n. 9
0
int _insert(RBT * u,RNode * & x,RNode * y)
{
 if(x == u->nil) { x = y ; return 1; }
 int ret = 0;
 if(y->val > x->val) ret = _insert(u, x->R , y);
 else ret = _insert(u , x->L , y);
 if(ret) y->F = x;
 return 0;
}
Esempio n. 10
0
Leaf * _insert(Leaf *leaf, int item) {
  // Inserts the new item via recursion.
  if(leaf == NULL)
    return new_leaf(item);

  if(item < leaf->data)
    leaf->left = _insert(leaf->left, item);
  else
    leaf->right = _insert(leaf->right, item);

  return leaf;
}
Esempio n. 11
0
//assumes write lock is held and the src is vbbm
//and that dest->{numHashBuckets, vbCapacity, vbLWM} have been set.
void VBBM::copyVBBM(VBShmsegHeader *dest)
{
	int i;
	int *newHashtable;
	VBBMEntry *newStorage;
	VBFileMetadata *newFiles;
	char *cDest = reinterpret_cast<char *>(dest);

	// copy metadata
	dest->nFiles = vbbm->nFiles;
	dest->vbCurrentSize = vbbm->vbCurrentSize;

	newFiles = reinterpret_cast<VBFileMetadata*>(&cDest[sizeof(VBShmsegHeader)]);
	newHashtable = reinterpret_cast<int*>(&cDest[sizeof(VBShmsegHeader) +
			dest->nFiles*sizeof(VBFileMetadata)]);
	newStorage = reinterpret_cast<VBBMEntry*>(&cDest[sizeof(VBShmsegHeader) +
			dest->nFiles*sizeof(VBFileMetadata) +
			dest->numHashBuckets*sizeof(int)]);

	memcpy(newFiles, files, sizeof(VBFileMetadata)*vbbm->nFiles);

	//initialize new storage & hash
	for (i = 0; i < dest->numHashBuckets; i++)
		newHashtable[i] = -1;

	for (i = 0; i < dest->vbCapacity; i++)
		newStorage[i].lbid = -1;

	//walk the storage & re-hash all entries;
	for (i = 0; i < vbbm->vbCurrentSize; i++)
		if (storage[i].lbid != -1) {
			_insert(storage[i], dest, newHashtable, newStorage, true);
			//confirmChanges();
		}
}
Esempio n. 12
0
void* read_loop(void* p)
{
    int readpipe = *(int *)p;
    fd_set rfds;

    char c;

    for (;;)
    {
        FD_ZERO(&rfds);
        FD_SET(STDIN_FILENO, &rfds);
        FD_SET(readpipe, &rfds);
        while (select(readpipe + 1, &rfds, NULL, NULL, NULL) == 0);
        if (FD_ISSET(readpipe, &rfds))
        {
            close(readpipe);
            break;
        }
        if (FD_ISSET(STDIN_FILENO, &rfds))
        {
            if (read(STDIN_FILENO, &c, sizeof(c)) > 0)
            {
                _insert(c);
            }
        }
    }

    printf("Thread terminating\n");

    pthread_exit(NULL);
}
Esempio n. 13
0
 int insert (const char *string, size_t strlen, int32_t ip4_address) {
   // Skip strings of length 0
   int rc;
   if (strlen == 0)
   return 0;
   int rv;
   // get root lock
   rc = pthread_mutex_lock(&(root_lock));
   assert(rc == 0);
   if (root == NULL) {
     root = new_leaf (string, strlen, ip4_address);
     rv =  1;
     rc = pthread_mutex_unlock(&(root_lock));
     assert(rc == 0);
   }
   else
   {
     rv = _insert (string, strlen, ip4_address, root, NULL, NULL);
     rc = pthread_mutex_unlock(&(root_lock));
     assert(rc == 0);
     if (!rv && allow_squatting)
     {
       search_and_squat(string, strlen, ip4_address);
     }
   }
   return rv;
 }
Esempio n. 14
0
void graph::insert(item_wrapper  it_p){
    if(nodes.count(it_p->name())){
        std::cout << "Warning: variable " << it_p->name() << "has already been entered\n";
        std::cout <<"Overwriting with new value\n";
    }
    _insert(it_p);
}
Esempio n. 15
0
	bool trie_tree::insert(const string& word){
		if (is_existed(word))
			return true;
		if (word.empty())
			return false;
		char ch = word[0];
		auto root = get_root();
		auto res = root->map_childs.find(ch);
		if (res != root->map_childs.end()){
			return _insert(word.substr(1), res->second);
		}else{
			auto is_a_word = (word.size() == 1 ? true : false);
			auto node = make_node(ch, is_a_word);
			root->map_childs[ch] = std::move(node);
			return _insert(word.substr(1), root->map_childs[ch]);
		}
	}
Esempio n. 16
0
	Value& operator[](const Key& key)
	{
		Node* node = _insert(_root, nullptr, key);
		
		if (! _begin) _begin = node;
		
		return node->value;
	}
Esempio n. 17
0
void insert(char *key,char *value){
  fprintf(stderr, "inserting %s => %s\n", key, value);
  Node* node = search(key);
  if( node = _insert(key, value, node) ){
    root_node = node;
  }
  return;
}
Esempio n. 18
0
typename LRUCacheH4<K, V>::Val * LRUCacheH4<K, V>::_update_or_insert(const K & key)
{
	typename MAP_TYPE::iterator it = _map.find(key);
	if (it != _map.end())
		return _update(it);
	else
		return _insert(key);
}
Esempio n. 19
0
	bool trie_tree::_insert(const string& word, const node_ptr& up){
		if (word.empty()){
			++size_;
			up->is_a_word = true;
			return true;
		}
		char ch = word[0];
		auto res = up->map_childs.find(ch);
		if (res != up->map_childs.end()){
			return _insert(word.substr(1), res->second);
		}else{
			auto is_a_word = (word.size() == 1 ? true : false);
			auto node = make_node(ch, is_a_word);
			up->map_childs[ch] = std::move(node);
			return _insert(word.substr(1), up->map_childs[ch]);
		}
	}
void XAP_Dialog_Insert_Symbol::_onInsertButton()
{
	// get the character to be inserted
	UT_UCSChar c = getInsertedSymbol();
	// get the font of the symbol to be inserted
	gchar * symfont = (gchar *) getInsertedFont();
	// do the actual insert
	_insert(c, const_cast<const gchar*>(symfont));
}
Esempio n. 21
0
	void bst<type>::insert(const type& x) {
		if (empty()) {
			np tmp = new nt();
			_root = tmp;
			_root->_data = x;
			_root->height = 0;
			return;
		}
		_insert(_root, x);
	}
Esempio n. 22
0
	Node* _insert(Node*& node, Node* parent, const Key& key)
	{
		if (node)
		{
			if (node == _end)
			{
				node = new Node(key);
				
				node->right = _end;
				node->parent = parent;
				_end->parent = node;
				
			}
			
			if (key < node->key)
			{
				Node* child = _insert(node->left, node, key);
				
				if (node == _begin) _begin = child;
				
				node->resize();
				
				return child;
			}
			
			else if (key > node->key)
			{
				Node* child = _insert(node->left, node, key);
				
				node->resize();
				
				return child;
			}
		}
		
		else
		{
			node = new Node(key);
			node->parent = parent;
		}
		
		return node;
	}
Esempio n. 23
0
	static void _remove_overlaps(void *addr_start, void *addr_end, bool entire_regions) {
		while(true) {
			int i = _find_overlap(addr_start, addr_end);
			if(i == -1) {
				return;
			}
			void *found_start = region[i].addr_start;
			void *found_end = region[i].addr_end;
			_remove(i);
			if(!entire_regions) {
				if (found_start < addr_start) {
					_insert(found_start, ((UINT8 *)addr_start) - 1);
				}
				if (found_end > addr_end) {
					_insert(((UINT8 *)addr_end) + 1, found_end);
				}
			}
		}
	}
Esempio n. 24
0
template<class T1, class T2> void DS::map<T1, T2>::_insert(node* current, node* prv, T1 key, T2 value) {
	if(current == null){
		current = new node();
		current->value = value, current->key = key, current->parent = prv;
		if(prv != null) (prv->key < key ? prv->right = current : prv->left = current);
		else root = current;
		return;
	}
	_insert(current->key < key ? current->right : current->left, current, key, value);
}
Esempio n. 25
0
void AVLTree::_insert(AVLNode*& node, Node*& data)
{
    if (node == nullptr)
    {
        node = new AVLNode(data);
    }
    else
    {
        if (data->getWord() > node->data->getWord())
        {
            _insert(node->right, data);
            if (height(node->right) - height(node->left) == 2)
            {
                if (data->getWord() > node->right->data->getWord())
                {
                    rotateWithRightChild(node);
                }
                else
                {
                    doubleWithRightChild(node);
                }
            }
        }
        else
        {
            _insert(node->left, data);
            if (height(node->left) - height(node->right) == 2)
            {
                if (data->getWord() < node->left->data->getWord())
                {
                    rotateWithLeftChild(node);
                }
                else
                {
                    doubleWithLeftChild(node);
                }
            }
        }

    }
    node->height = (max(height(node->left), height(node->right)) + 1);
}
Esempio n. 26
0
        /**
         * Semantics for insert are ContinueOnError - to match mongod semantics :
         * 1) Error is thrown immediately for corrupt objects
         * 2) Error is thrown only for UserExceptions during the insert process, if last obj had error that's thrown
         */
        void _insert( Request& r , DbMessage& d, ChunkManagerPtr manager ){

            vector<BSONObj> insertsRemaining;
            while ( d.moreJSObjs() ){
                insertsRemaining.push_back( d.nextJsObj() );
            }

            map<ChunkPtr, vector<BSONObj> > insertsForChunks; // Map for bulk inserts to diff chunks

            _insert( r, d, manager, insertsRemaining, insertsForChunks );
        }
Esempio n. 27
0
/*	==================== _insert ====================
	This function uses recursion to insert the new data 
	into a leaf node in the BST tree.
	   Pre    Application has called BST_Insert, which  
	          passes root and data pointer
	   Post   Data have been inserted
	   Return pointer to [potentially] new root
*/
static BSTNODE* _insert (HEADER* listHeader, BSTNODE* root, BSTNODE* newPackage)
{
	if(!root)
		return newPackage;

	// Locate null subtree for insertion 
	if (comparePackage(root->ptrPackage, newPackage->ptrPackage) > 0)
	{
		root->left = _insert(listHeader, root->left, newPackage);
	    return root; 
	}     // new < node
	else
	{ // new data >= root data 		  
	    root->right = _insert(listHeader, root->right, newPackage);
	    return root;
	} // else new data >= root data 


	return root;
}// _insert
Esempio n. 28
0
void AVLTree::insert(Node* data)
{
    if(root == nullptr)
    {
      root = new AVLNode(data);
    }
    else
    {
        if (data->getWord() > root->data->getWord())
        {
            _insert(root->right, data);
            if (height(root->right) - height(root->left) == 2)
            {
                if (data->getWord() > root->right->data->getWord())
                {
                   rotateWithRightChild(root);
                }
                else
                {
                    doubleWithRightChild(root);
                }
            }
        }
        else
        {
            _insert(root->left, data);
            if (height(root->left) - height(root->right) == 2)
            {
                if (data->getWord() < root->left->data->getWord())
                {
                    rotateWithLeftChild(root);
                }
                else
                {
                    doubleWithLeftChild(root);
                }
                }
        }
    }
    root->height = max(height(root->left), height(root->right)) + 1;
}
Esempio n. 29
0
File: misc.c Progetto: kode54/cog-1
extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line){
  bytes+=HEAD_ALIGN;
  if(ptr){
    ptr-=HEAD_ALIGN;
    _ripremove(ptr);
    ptr=realloc(ptr,bytes);
  }else{
    ptr=malloc(bytes);
    memset(ptr,0,bytes);
  }
  return _insert(ptr,bytes,file,line);
}
int insert (const char *string, size_t strlen, int32_t ip4_address) {
  // Skip strings of length 0
  if (strlen == 0)
    return 0;

  /* Edge case: root is null */
  if (root == NULL) {
    root = new_leaf (string, strlen, ip4_address);
    return 1;
  }
  return _insert (string, strlen, ip4_address, root, NULL, NULL);
}