Example #1
0
void SkipList<T>::insert(const T & newValue) {
	int level = randomLevel();
	Node<T> *node = this->head;
	Node<T> *insertNode = new Node<T>(newValue, level);

	while ((level - 1) >= 0) {
		while (node->next[level] != NULL && node->next[level]->data < newValue) {
			node = node->next[level];
		}
		insertNode->next[level] = node->next[level];
		node->next[level] = insertNode;
	}
}
/*
    Function: insert();
    Use: void insert(searchKey, newValue);

    It searches the skip list for elements
    with seachKey, if there is an element
    with that key its value is reassigned to the 
    newValue, otherwise it creates and splices
    a new node, of random level.
*/
void Skip_list::insert(int searchKey, std::string newValue) {
    // reassign if node exists 
    Skip_Node* x = nullptr;
    x = find(searchKey);
    if (x) {
        x->value = newValue;
        return;
    }

    // vector of pointers that needs to be updated to account for the new node
    std::vector<Skip_Node*> update(head->forward);
    unsigned int currentMaximum = nodeLevel(head->forward);
    x = head;

    // search the list 
    for (unsigned int i = currentMaximum; i-- > 0;) {

        while (x->forward[i] != nullptr && x->forward[i]->key < searchKey) {

            x = x->forward[i];
        }
        update[i] = x;
    }
    x = x->forward[0];

    // create new node
    int newNodeLevel = 1;
    if (x->key != searchKey) {

        newNodeLevel = randomLevel();
        int currentLevel = nodeLevel(update);

        if (newNodeLevel > currentLevel) {

            for (int i = currentLevel + 1; i < newNodeLevel; ++i) {

                update[i] = head;
            }   
        }
        x = makeNode(searchKey, newValue, newNodeLevel);
    }

    // connect pointers of predecessors and new node to successors
    for (int i = 0; i < newNodeLevel; ++i) {

        x->forward[i] = update[i]->forward[i];
        update[i]->forward[i] = x;
    }
}
Example #3
0
/*
step1:查找到在每层待插入位置,跟新update数组
step2:需要随机产生一个层数
step3:从高层至下插入,与普通链表的插入完全相同。
*/
bool insert(skip_list *sl, keyType key, valueType val)
{
  Node *update[MAX_L];
  Node *q=NULL,*p=sl->head;//q,p初始化
  int i=sl->level-1;
  /******************step1*******************/
  //从最高层往下查找需要插入的位置,并更新update
  //即把降层节点指针保存到update数组
  for( ; i>=0; --i)
  {
    while((q=p->next[i])&& q->key<key)
    p=q;
    update[i]=p;
  }
  if(q && q->key == key)//key已经存在的情况下
  {
    q->value = val;
    return true;
  }
  /******************step2*******************/
  //产生一个随机层数level
  int level = randomLevel();
  //如果新生成的层数比跳表的层数大
  if(level>sl->level)
  {
    //在update数组中将新添加的层指向header
    for(i=sl->level; i<level; ++i)
    {
      update[i]=sl->head;
    }
    sl->level=level;
  }
  //printf("%d\n", sizeof(Node)+level*sizeof(Node*));
  /******************step3*******************/
  //新建一个待插入节点,一层一层插入
  q=create_node(level, key, val);
  if(!q)
  return false;

  //逐层更新节点的指针,和普通链表插入一样
  for(i=level-1; i>=0; --i)
  {
    q->next[i]=update[i]->next[i];
    update[i]->next[i]=q;
  }
  return true;
}
Example #4
0
void SkipList<T>::insert(const T & x)
{
	int level = randomLevel();
	Node<T> *newnode = new Node<T>(x, level), *node = head;

	while (--level >= 0) {
		while (node->next[level] != NULL && node->next[level]->data < x)
			node = node->next[level];
		if (node->next[level] != NULL && node->next[level]->data == x) {
			std::cout << "DEBUG: Insert failed - node already exists\n";
			return;
		}
		newnode->next[level] = node->next[level];
		node->next[level] = newnode;
	}
	std::cout << "DEBUG: Inserted value " << x << " at level " << newnode->height << std::endl;
}
Example #5
0
// insert a node key into the skiplist, return false if key already exists
// otherwise insert key and return true. 
bool SkipList::insert( ELEMENT_TYPE info ){
    // pointer array to keep track of the next nodes at the insertion point
    SkipNode ** lookBack = new SkipNode * [_maxHeight+1];
    SkipNode* cur = _head;
    
    int curInfo;
    // find where new node goes
    for ( int h = _curHeight; h >= 1; h-- ){  
        curInfo = cur->nextNodes[h]->info;
        while ( curInfo < info ){
            cur = cur->nextNodes[h];
            curInfo = cur->nextNodes[h]->info;
        }
        lookBack[h] = cur; // keep track of previous node
    }
    cur = cur->nextNodes[1]; // move to next node at level 1
    curInfo = cur->info;
    
    int lvl = 0;
    // If dup, return false
    if ( curInfo == info ){
        delete [] lookBack; 
        return false;
    }
    else{ // Perform an insert
        lvl = randomLevel();
        // prepare insertion point
        // check if node has higher level that the current height
        if ( lvl > _curHeight ){
            for ( int i = _curHeight + 1; i <= lvl; i++ )
                lookBack[i] = _head;
            _curHeight = lvl;
        }
        // Insert new element
        cur = new SkipNode(info, lvl);
        for ( int i = 1; i <= lvl; i++ ){
            cur->nextNodes[i] = lookBack[i]->nextNodes[i];
            lookBack[i]->nextNodes[i] = cur;
        }
        delete [] lookBack;
        return true;
    }
}
Example #6
0
//插入节点  
bool insert(skiplist *sl,int key,int value)  
{  
    nodeStructure *update[MAX_LEVEL];  
    nodeStructure *p, *q = NULL;  
    p=sl->header;  
    int k=sl->level;  
    //从最高层往下查找需要插入的位置  
    //填充update  
    for(int i=k-1; i >= 0; i--){  
        while((q=p->forward[i])&&(q->key<key))  
        {  
            p=q;  
        }  
        update[i]=p;  
    }  
    //不能插入相同的key  
    if(q&&q->key==key)  
    {  
        return false;  
    }  
    
    //产生一个随机层数K  
    //新建一个待插入节点q  
    //一层一层插入  
    k=randomLevel();  
    //更新跳表的level  
    if(k>(sl->level))  
    {  
        for(int i=sl->level; i < k; i++){  
            update[i] = sl->header;  
        }  
        sl->level=k;  
    }  
    
    q=createNode(k,key,value);  
    //逐层更新节点的指针,和普通列表插入一样  
    for(int i=0;i<k;i++)  
    {  
        q->forward[i]=update[i]->forward[i];  
        update[i]->forward[i]=q;  
    }  
    return true;  
}  
Example #7
0
void insert(Skiplist* list, int key, int value)
{
	Node* tmp;
	int i;
	int level;
	Node* update[MAX_LEVEL-1];

	tmp = list->header;
	/* On parcourt la liste à partir du plus haut niveau et on descend quand on trouve 
	une clé supérieur ou égal à la clé recherchée)*/
	for(i = (list->level)-1; i>=0; i--)
	{
		while(tmp->forward[i] != NULL && tmp->forward[i]->key < key)
		{
			tmp = tmp->forward[i];
		}
		update[i] = tmp;
	}
	tmp = tmp->forward[0];
	if(tmp != NULL && tmp->key == key)
	{
		tmp->val = value;
	}
	else {
		level = randomLevel(PROBABILITY);
		if( level > list->level)
		{
			for( i = (list->level); i <level; i++)
			{
				update[i] = list->header;
			}
			list->level = level;
		}
		tmp = makeNode(level, key, value);
		for(i = 0; i<level; i++)
		{
			tmp->forward[i] = update[i]->forward[i];
			update[i]->forward[i] = tmp;
		}
	}
}
Example #8
0
boolean insert(list ls, KeyType k, ValueType v)
{
	int n = ls->current_max_level;
	node p, q;
	node * update = (node*)malloc(sizeof(node)* MaxSkipListLevel);
	p = ls->head;

	do{
		q = p->forward[n];
		while (q != &Nil && q->key < k){
			p = q;
			q = p->forward[n];
		}
			
		update[n] = p;
	} while (--n >= 0);

	if (q && q->key == k){
		free(update);
		return FALSE;
	}

	n = randomLevel();
	if (n > ls->current_max_level){
		n = ++(ls->current_max_level);
		update[n] = ls->head;
	}

	q = createNode(n, k, v);

	do{
		p = update[n];
		q->forward[n] = p->forward[n];
		p->forward[n] = q;
	} while (--n >= 0);

	free(update);
	return TRUE;
}
Example #9
0
int insert(skiplist *slist, int k, int value){
	node *update[MAX_LEVEL];
	node *currentNode;
	node *nextNode = NULL;
	int level = slist->level;
	int i;
	
	currentNode = slist->header;	// top
	for (i = level - 1; i >= 0; i--){
		while ((nextNode = currentNode->next[i]) && (nextNode->key < key)){
			currentNode = nextNode;
		}
		update[i] = currentNode;	// 最大的小于key的节点(保证有序)
	}
	
	// 已存在该值,插入失败
	if (nextNode && nextNode->key == key){
		return 0;
	}
	
	level = randomLevel();
	
	if (level > (slist->level)){
		for (i = slist->level; i < level; i++){
			update[i] = slist->header;	// 全部指向top,即指向左边第一个元素
		}
		slist->level = level;
	}
	
	q = createNode(level, key, value);
	for (i = 0; i < level; i++){
		// 插入,普通链表插入完全相同(for循环只是说明,逐层完成插入)
		nextNode->next[i] = update[i]->next[i];
		update[i]->next[i] = nextNode;
	}
	
	// 输出0,失败,输出1,成功
	return 1;
}
Example #10
0
skiplist_node *skiplist_insert(skiplist_node *slnode, skiplist *l, keyType key,
			       valueType value, u64 randseed)
{
	int k;
	skiplist_node *update[MaxNumberOfLevels];
	skiplist_node *p, *q;

	k = l->level;
	p = slnode;
	do {
		/* In this implementation, we allow multiple entries of the
		 * same key, but place all entries after the previous ones. */
		while (q = p->next[k], q->key <= key)
			p = q;
		update[k] = p;
	} while (--k >= 0);

	k = randomLevel(randseed);
	if (k > l->level) {
		k = ++l->level;
		update[k] = slnode;
	}

	/* We assume that the memory allocation always succeeds */
	q = newNode;
	q->level = k;
	q->key = key;
	q->value = value;
	do {
		p = update[k];
		q->next[k] = p->next[k];
		p->next[k] = q;
		q->prev[k] = p;
		q->next[k]->prev[k] = q;
	} while (--k >= 0);
	return q;
}
Example #11
0
void skipList::insert (int key, std::string v) {
  //std::cout << "Inserting Key: " << key << " with value: " << v << std::endl;
  int actualLevel = getLevel(head->_next);
  std::vector<Node*> to_update(head->_next);

  Node* x = head;

  //for (int i = actualLevel; i > 0; i--) {
    //while( x->_next[i] != nullptr && x->_next[i]->key < key) {
      //x = x->_next[i];
    //}
    //to_update[i] = x;
  //}
  //x = x->_next[0];

  //if (x->_next[0] == nullptr || x->_next[0]->key != key) {
    ////Create node
    //int newLevel = randomLevel();
    //int height = getLevel(to_update);
    //if ( newLevel > height ) {
      //for( int i = height+1; i <= newLevel; ++i) {
        //to_update[i] = head;
      //}
    //}
    //x = new Node(key,v,newLevel);
    //for ( int i = 0; i < newLevel; ++i) {
      //x->_next[i] = to_update[i]->_next[i];
      //to_update[i]->_next[i] = x;
    //}
  //} else {
    //x->_next[0]->value = v;
  //}


  int l = actualLevel;
  while ( l>=0 ){
    if (x->_next[l] == nullptr || key <= x->_next[l]->key){
      to_update[l] = x;
      --l;
    }
    else {
      x = x->_next[l];
    }
  }

  if ( x->_next[0] == nullptr || x->_next[0]->key != key) {
   int level = randomLevel();
   int height = getLevel(to_update);
   if ( level > height ) {
     for (unsigned int i = height; i < level - 1; ++i) {
      to_update[i] = head;
     }
   }
   x = new Node(key, v, level);

    for (unsigned i = 0; i < level; ++i) {
      x->_next[i] = to_update[i]->_next[i];
      to_update[i]->_next[i] = x;
    }
  }
  else {
    x->_next[0]->value = v;
  }

  return;
}