Example #1
0
int
erase_node(struct macrotable *oldnode, struct macrotable *node,
		   const char *killname, struct macrotable *mtop)
{
	if (!node)
		return 0;
	else if (strcmp(killname, node->name) < 0)
		return erase_node(node, node->left, killname, mtop);
	else if (strcmp(killname, node->name))
		return erase_node(node, node->right, killname, mtop);
	else {
		if (node == oldnode->left) {
			oldnode->left = node->left;
			if (node->right)
				grow_macro_tree(mtop, node->right);
			if (node->name)
				free(node->name);
			if (node->definition)
				free(node->definition);
			free((void *) node);
			return 1;
		} else {
			oldnode->right = node->right;
			if (node->left)
				grow_macro_tree(mtop, node->left);
			if (node->name)
				free(node->name);
			if (node->definition)
				free(node->definition);
			free((void *) node);
			return 1;
		}
	}
}
Example #2
0
int main(){
    List list;
    List list2;
    int i;
    
    init_list( &list );
    printf( "Empty: %d\n", empty_list( list ) );
    for( i = 1; i <= 5; i++ ){
        append_node( &i, sizeof( int ), list );
        printf( "Length: %d | ", length( list ) );
        print_list( list );
    }
    for( i = 6; i <= 10; i++ ){
        insert_front( &i, sizeof( int ), list );
        printf( "Length: %d | ", length( list ) );
        print_list( list );
    }
    printf( "Empty: %d\n", empty_list( list ) );
    printf( "First: %d\n", get_int( list_op_first( list ) -> data ) );
    printf( "Last:  %d\n", get_int( list_op_last( list ) -> data ) );
    printf( "Second to last: %d\n", get_int( list_op_prev( list_op_last( list ), list ) -> data ) );
    printf( "Second element: %d\n", get_int( get_elem_at( 1, list ) -> data ) );
    
    place_after( &i, sizeof( int ), get_elem_at( 1, list ), list );
    print_list( list );
    i++;
    insert_at( &i, sizeof( int ), 2, list );
    print_list( list );
    i++;
    insert_at( &i, sizeof( int ), 0, list );
    print_list( list );
    
    init_list( &list2 );
    for( i = 100; i > 95; i-- ){
        insert_front( &i, sizeof( int ), list2 );
    }
    append_list( list, &list2 );
    free( list2 );
    print_list( list );
    
    printf( "Erasing 5th element...\n" );
    erase_at( 4, list );
    print_list( list );
    printf( "Erasing 1st element...\n" );
    erase_at( 0, list );
    print_list( list );
    printf( "Erasing last element...\n" );
    erase_node( list_op_last( list ), list );
    print_list( list );
    printf( "Erasing 3rd element...\n" );
    erase_node( get_elem_at( 2, list ), list );
    print_list( list );
    
    clear_list( &list );
    
    return 0;
}
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::reverse_iterator
PB_DS_CLASS_C_DEC::
erase(reverse_iterator it)
{
  _GLIBCXX_DEBUG_ONLY(assert_valid());
  if (it.m_p_nd == base_type::m_p_head)
    return it;

  reverse_iterator ret_it = it;
  ++ret_it;
  erase_node(it.m_p_nd);
  _GLIBCXX_DEBUG_ONLY(assert_valid());
  return ret_it;
}
      /**
       * deletes the passed node and all decedents if available
       */
      void erase_node(Node *n) noexcept
      {
        if (n == nullptr) {
          return;
        }
        if (n->next.load()) {
          // delete all possible next Nodes in the list
          erase_node(n->next.load());
          n->next = nullptr;
        }
        // Create a temporary node on the stack
        Node stackNode;

        // Move the allocator to the temporary node
        stackNode = std::move(*n);
        block allocatedBlock(n, stackNode.allocatedThisSize);

        stackNode.allocator.deallocate(allocatedBlock);
      }
Example #5
0
 bool erase_node(Iterator begin, Iterator end, node_type &cur, bool &result) 
 {
     if (begin == end) //当到达字符串结尾:递归的终止条件
     { 
         result = (cur.freq > 0);   //如果当前节点的频率>0,则当前节点可以作为终止字符,那么结果为真
         if(cur.freq)
             cur.freq --;            //如果当前节点为终止字符,词频减一
         return cur.freq == 0 && cur.node == 0;    //若该节点为树叶,那么通知其父节点删除它
     }
     //当无法匹配当前字符时,将结果设为假并返回假,即通知其父节点不要删除它
     if (cur.child[index[*begin]] == 0) return result = false; 
     //判断是否应该删除该子节点
     else if (erase_node((++begin)--, end, *(cur.child[index[*begin]]), result)) 
     { 
         delete cur.child[index[*begin]]; //删除该子节点
         cur.child[index[*begin]] = 0; //子节点数减一
         //若当前节点为树叶,那么通知其父节点删除它
         if (--cur.node == 0 && cur.freq == 0) return true; 
     }
     return false; //其他情况都返回假
 }
      /**
       * Sends the request to the first allocator, if it cannot fulfill the request
       * then the next Allocator is created and so on
       */
      block allocate(size_t n) noexcept
      {
        if (n == 0) {
          return{};
        }

        block result = allocate_no_grow(n);
        if (result) {
          return result;
        }

        // no node at all there
        if (root_.load() == nullptr) {
          auto firstNode = create_node();
          Node *nullNode = nullptr;
          // test if in the meantime someone else has created a node
          if (!root_.compare_exchange_weak(nullNode, firstNode)) {
            erase_node(firstNode);
          }

          result = allocate_no_grow(n);
          if (result) {
            return result;
          }
        }

        // a new node must be appended
        auto newNode = create_node();
        Node *nullNode = nullptr;
        auto p = root_.load();
        do {
          p = root_;
          while (p->next.load() != nullptr) {
            p = p->next;
          }
        } while (!p->next.compare_exchange_weak(nullNode, newNode));

        result = allocate_no_grow(n);
        return result;
      }
Example #7
0
int
kill_macro(const char *macroname, dbref player, struct macrotable **mtop)
{
	if (!(*mtop)) {
		return (0);
	} else if (!string_compare(macroname, (*mtop)->name)) {
		struct macrotable *macrotemp = (*mtop);
		int whichway = ((*mtop)->left) ? 1 : 0;

		*mtop = whichway ? (*mtop)->left : (*mtop)->right;
		if ((*mtop) && (whichway ? macrotemp->right : macrotemp->left))
			grow_macro_tree((*mtop), whichway ? macrotemp->right : macrotemp->left);
		if (macrotemp->name)
			free(macrotemp->name);
		if (macrotemp->definition)
			free(macrotemp->definition);
		free((void *) macrotemp);
		return (1);
	} else if (erase_node((*mtop), (*mtop), macroname, (*mtop)))
		return (1);
	else
		return (0);
}
 void shrink() noexcept
 {
   erase_node(root_.load());
 }
Example #9
0
 bool erase(Iterator begin, Iterator end) 
 {
     bool result; //用于存放搜索结果
     erase_node(begin, end, root, result);
     return result;
 }