Exemple #1
0
int similar(char p[], char q[], short condition)
{
    TREEPTR node1, node2;
    double p_mean, q_mean,
	   p_deviation, q_deviation,
	   difference;

    node1=get_leaf(p);
    node2=get_leaf(q);
    p_mean = node1->mean;
    q_mean = node2->mean;

    p_deviation = get_deviation(node1->mean,node1->sqr_mean);
    q_deviation = get_deviation(node2->mean,node2->sqr_mean);

    /* This is to avoid very little regions not to get merged because */
    /* a null standar daviation */
    if (p_deviation==0) p_deviation=0.5;
    if (q_deviation==0) q_deviation=0.5;

    difference = p_mean-q_mean;

    return ( (abs(difference)<=condition*p_deviation) ||
	     (abs(difference)<=condition*q_deviation) );
}
Exemple #2
0
/* ***********************************************************************
   this function traverses the trie rooted at head following the string s. 
   Returns the leaf "corresponding" to the string s
   *********************************************************************** */
static node *find_companion(node *head, UChar *s)
{
  UChar c;
  node *p;
  int t;

  Stack_size = 0;                // init stack
  while(head->skip >= 0) {
    Stack[Stack_size++] = head;
    t = head->skip;
    if(s+t>=Upper_text_limit)    // s[t] does not exist: mismatch 
      return get_leaf(head);
    c = s[t]; p = head->down;
  repeat:
    if(c==p->key) {              // found branch corresponding to c
      head = p;
      continue;
    }
    else if(c<p->key)            // no branch corresponding to c: mismatch
      return get_leaf(head);
    if((p=(p->right))==NULL)     // no other branches: mismatch
      return get_leaf(head);
    goto repeat;                 // look at next branch
  }
  Stack[Stack_size++] = head;
  return head;
}
	RStarTreeNodePtr RStarTreeNode::find_small_leaf(
		const Uint32 minimum_load, RStarTreeNodePtrStack &path_buffer)
	{
		RStarTreeNodePtr found;
		Uint32 i;

		if (get_leaf())
		{
			if (get_count() < minimum_load)
			{
				return this;
			}
			else
			{
				return 0;
			}
		}

		path_buffer.push(this);

		for (i = 0; i < get_count(); i++)
		{
			found = get_node(i)->find_small_leaf(minimum_load,
				path_buffer);

			if (found)
			{
				return found;
			}
		}

		path_buffer.pop();

		return 0;
	}
	bool RStarTreeNode::insert(const BoundedObjectPtr element)
	{
		Uint32 i;

		if (get_leaf())
		{
			return add_element(element);
		}
		else
		{
			for (i = 0; i < get_count(); i++)
			{
				if (get_element_bounding_box(i).contains(
					element->get_bounding_box()))
				{
					if (get_node(i)->insert(element))
					{
						return true;
					}
				}
			}

			return false;
		}
	}
	RStarTreeNodePtr RStarTreeNode::choose_sub_tree(
		const BoundingBox &bounding_box, const Uint32 insertion_level,
		RStarTreeNodePtrStack &path_buffer)
	{
		RStarTreeNodePtr node;

		if (get_leaf())
		{
			return this;
		}
		else
		{
			if (insertion_level == get_level())
			{
				return this;
			}

			path_buffer.push(this);

			if (get_level() == 1)
			{
				node = find_least_overlap(bounding_box);
			}
			else
			{
				node = find_least_enlargement(bounding_box);
			}

			assert(node);

			return node->choose_sub_tree(bounding_box,
				insertion_level, path_buffer);
		}
	}
	/**
	 * Remove a value from the tree.
	 *
	 * The tree no longer owns the value.
	 *
	 * @param key The key for which the value will be removed.
	 *
	 * @return The value if it is in the tree, nullptr otherwise.
	 */
	value_type
	remove(key_type key)
	{
		nvobj::persistent_ptr<entry> parent = nullptr;
		auto leaf = get_leaf(key, &parent);

		if (leaf == nullptr)
			return nullptr;

		auto ret = leaf->value;

		auto pop = nvobj::pool_by_vptr(this);
		nvobj::transaction::exec_tx(pop, [&] {
			if (parent == nullptr) {
				leaf->key = 0;
				leaf->value = nullptr;
			} else {
				auto n = parent->inode;
				*parent = *(
					n->entries[parent->inode->entries[0]
							   ->key == leaf->key]);

				/* cleanup entries and the unnecessary node */
				nvobj::delete_persistent<entry>(n->entries[0]);
				nvobj::delete_persistent<entry>(n->entries[1]);
				nvobj::delete_persistent<node>(n);
			}
		});

		return ret;
	}
	/**
	 * Return the value from the tree for the given key.
	 *
	 * @param key The key for which the value will be returned.
	 *
	 * @return The value if it is in the tree, nullptr otherwise.
	 */
	value_type
	get(key_type key)
	{
		auto ret = get_leaf(key, nullptr);

		return ret ? ret->value : nullptr;
	}
	RStarTreeNodePtr RStarTreeNode::find_least_enlargement(
		const BoundingBox &bounding_box) const
	{
		float enlargement, min_enlargement, v;
		Uint32 i, index;

		assert(!get_leaf());
		assert(get_count() > 0);

		min_enlargement = std::numeric_limits<float>::max();

		index = std::numeric_limits<Uint32>::max();

		for (i = 0; i < get_count(); i++)
		{
			v = get_element_bounding_box(i).get_volume();
			enlargement = enclose(bounding_box,
				get_element_bounding_box(i)).get_volume() - v;

			if (enlargement < min_enlargement)
			{
				if (enlargement <= 0.0f)
				{
					return get_node(i);
				}
				min_enlargement = enlargement;
				index = i;
			}
		}

		return get_node(index);
	}
Exemple #9
0
static void
test_unicode(void **state) {
	char * filepath = "快乐/财富/财富";
	char * leaf = get_leaf(filepath) ;
	char * subpath = get_next_sub_path(filepath);
        assert_string_equal(leaf, "财富");
        assert_string_equal(subpath, "快乐");
}
			/**
			 * @brief Get node.
			 *
			 * Returns the pointer of the node at the given
			 * position.
			 * @param index The index of the node.
			 * @return The pointer of the node.
			 */
			inline RStarTreeNodeSharedPtr get_node(
				const Uint32 index) const noexcept
			{
				assert(!get_leaf());

				return boost::dynamic_pointer_cast<
					RStarTreeNode>(get_element(index));
			}
			/**
			 * @brief Deletes an element.
			 *
			 * Deletes an element from the node.
			 * @param element The pointer of the element to delete.
			 */
			inline void delete_element(
				const BoundedObjectSharedPtr &element)
			{
				assert(get_leaf());

				remove_element(get_index(element));

				assert(find_element(element) == -1);
			}
			/**
			 * @brief Deletes a sub node.
			 *
			 * Deletes a sub node from the node.
			 * @param sub_node The pointer of the sub node to
			 * delete.
			 */
			inline void delete_sub_node(
				const RStarTreeNodeSharedPtr &sub_node)
			{
				assert(!get_leaf());

				remove_element(get_index(sub_node));

				assert(find_element(sub_node) == -1);
			}
Exemple #13
0
int fs_ptree_remove(fs_ptree *pt, fs_rid pk, fs_rid pair[2], fs_rid_set *models)
{
    if (!pt) {
        fs_error(LOG_ERR, "tried to remove from to NULL ptree");

        return 1;
    }

    nodeid lid = get_leaf(pt, pk);
    if (!lid) {
        /* the leaf doesn't exist, so it doesn't need to be deleted */
    
        return 0;
    }
    leaf *lref = LEAF_REF(pt, lid);
    if (!lref->block) {
        fs_error(LOG_ERR, "block for leaf %x not found", lid);

        return 1;
    }

    int removed = 0;
    fs_row_id newblock = fs_ptable_remove_pair(pt->table, lref->block, pair, &removed, models);
    if (lref->block != newblock) {
        lref->block = newblock;
    }
    if (removed) {
        lref->length -= removed;
        pt->header->count -= removed;
        if (lref->length == 0) {
            collapse_by_pk(pt, pk);
#if 0
            node *nref = NODE_REF(pt, parent);
            int collapsed = 0;
            for (int i=0; i<FS_PTREE_BRANCHES; i++) {
                if (nref->branch[i] == lid) {
                    nref->branch[i] = FS_PTREE_NULL_NODE;
                    fs_ptree_free_leaf(pt, lid);
                    collapsed = 1;
                    collapse_by_pk(pt, pk);
                    break;
                }
            }
            if (!collapsed) {
                fs_error(LOG_INFO, "leaf %s/%x is empty, should be removed", pt->filename, lid);
            }
#endif
        }

        return 0;
    }

    return 1;
}
	void RStarTreeNode::intersect_node(const Frustum &frustum,
		const PlaneMask in_mask, BoundedObjectPtrVector &visitor) const
	{
		Uint32 i;
		PlaneMask out_mask;

		assert(get_leaf());

		for (i = 0; i < get_count(); i++)
		{
			if (frustum.intersect(get_element_bounding_box(i), in_mask,
				out_mask) != it_outside)
			{
				visitor.push_back(get_element(i));
			}
		}
	}
	RStarTreeNodePtr RStarTreeNode::find_least_overlap(
		const BoundingBox &bounding_box) const
	{
		BoundingBox tmp_bounding_box;
		float overlap, min_overlap;
		Uint32 i, j, index;

		assert(!get_leaf());
		assert(get_count() > 0);

		min_overlap = std::numeric_limits<float>::max();

		index = std::numeric_limits<Uint32>::max();

		for (i = 0; i < get_count(); i++)
		{
			tmp_bounding_box = enclose(bounding_box,
				get_element_bounding_box(i));

			overlap = 0.0f;

			for (j = 0; j < get_count(); j++)
			{
				if (i != j)
				{
					overlap += tmp_bounding_box.overlap(
						get_element_bounding_box(j));
				}
			}

			if (overlap < min_overlap)
			{
				if (overlap <= 0.0f)
				{
					return get_node(i);
				}

				min_overlap = overlap;
				index = i;
			}
		}

		assert(index < get_count());

		return get_node(index);
	}
	void RStarTreeNode::add_node(BoundedObjectPtrVector &visitor) const
	{
		Uint32 i;

		if (get_leaf())
		{
			for (i = 0; i < get_count(); i++)
			{
				visitor.push_back(get_element(i));
			}
		}
		else
		{
			for (i = 0; i < get_count(); i++)
			{
				get_node(i)->add_node(visitor);
			}
		}
	}
	Uint32 RStarTreeNode::get_sub_nodes_count() const throw()
	{
		Uint32 i, result;

		if (get_leaf())
		{
			return 0;
		}
		else
		{
			result = get_count();

			for (i = 0; i < get_count(); i++)
			{
				result += get_node(i)->get_sub_nodes_count();
			}

			return result;
		}
	}
	Uint32 RStarTreeNode::get_item_count() const
	{
		Uint32 i, count;

		if (get_leaf())
		{
			return get_count();
		}
		else
		{
			count = 0;

			for (i = 0; i < get_count(); i++)
			{
				count += get_node(i)->get_item_count();
			}

			return count;
		}
	}
Exemple #19
0
fs_ptree_it *fs_ptree_search(fs_ptree *pt, fs_rid pk, fs_rid pair[2])
{
    if (!pt) {
        fs_error(LOG_ERR, "tried to search NULL ptree");

        return NULL;
    }

    nodeid lid = get_leaf(pt, pk);
    if (!lid) {
        return NULL;
    }
    fs_ptree_it *it = calloc(1, sizeof(fs_ptree_it));
    it->pt = pt;
    it->leaf = LEAF_REF(pt, lid);
    it->length = it->leaf->length;
    it->block = it->leaf->block;
    it->pair[0] = pair[0];
    it->pair[1] = pair[1];

    return it;
}
	void RStarTreeNode::intersect_tree(const Frustum &frustum,
		const PlaneMask mask, BoundedObjectPtrVector &visitor) const
	{
		Uint32 i;
		PlaneMask out_mask;
		IntersectionType result;

		if (get_leaf())
		{
			intersect_node(frustum, mask, visitor);
		}
		else
		{
			for (i = 0; i < get_count(); i++)
			{
				result = frustum.intersect(get_element_bounding_box(i),
					mask, out_mask);
				switch (result)
				{
					case it_inside:
					{
						get_node(i)->add_node(visitor);
						break;
					}
					case it_intersect:
					{
						get_node(i)->intersect_tree(
							frustum, out_mask,
							visitor);
						break;
					}
					case it_outside:
					{
						break;
					}
				}
			}
		}
	}
	RStarTreeNodePtr RStarTreeNode::find_leaf(
		const BoundedObjectPtr element,
		RStarTreeNodePtrStack &path_buffer)
	{
		RStarTreeNodePtr found;
		Uint32 i;

		if (get_leaf())
		{
			if (find_element(element) != -1)
			{
				return this;
			}
			else
			{
				return 0;
			}
		}

		path_buffer.push(this);

		for (i = 0; i < get_count(); i++)
		{
			if (get_element_bounding_box(i).contains(
				element->get_bounding_box()))
			{
				found = get_node(i)->find_leaf(element, path_buffer);

				if (found != 0)
				{
					return found;
				}
			}
		}

		path_buffer.pop();

		return 0;
	}
	bool RStarTreeNode::check_nodes() const
	{
		Uint32 i;

		for (i = 0; i < get_count(); i++)
		{
			if (!get_bounding_box().contains(get_element_bounding_box(i)))
			{
				return false;
			}

			if (!get_leaf())
			{
				if (!get_node(i)->check_nodes())
				{
					return false;
				}
			}
		}

		return true;
	}
Exemple #23
0
int main(int argc, char *argv[])
{
    char cpath[NAME_SIZ];
    char str[NAME_SIZ] = ".";
    char *cptr;
    f_node *ptr, *root;
    int cnt = 0;

    bzero(cpath, sizeof(char)*NAME_SIZ);
    getcwd(cpath, NAME_SIZ);
    while (1)
    {
	printf("get node(%d)\n", cnt);
	if ((root = get_node(cpath, ".", 1, 1)) == NULL)
	{
	    exit(1);
	}
	cnt++;
    }
    ptr = root;
    //    show_node(ptr, 0);

    cptr = strtok(str, "/");
    while (cptr != NULL) 
    {
	ptr = ptr->child;
 	printf("%s ", cptr);
	ptr = get_leaf(ptr, cptr);
	if (ptr == NULL) 
	{
	    perror("not find!");
	    break;
	}
	cptr = strtok(NULL, "/");
    }
  
    printf("\nfind!\n");
    return 0;
}
Exemple #24
0
int internal_get(struct DB *db, Block block, struct DBT *key)
{
	int i = 0;
	struct DBT key1, value1;
	while (i < get_n(block)) {
		get_data(block, i, &key1, &value1);
		if (cmp(key, &key1) <= 0)
			break;
		i++;
	}

	if (i < get_n(block) && !cmp(key, &key1))
		return i;

	if (get_leaf(block))
		return -1;
	else {
		int id = get_c(block, i);
		db->block_read(db, block, id);
		return internal_get(db, block, key);
	}

	return -1;
}
Exemple #25
0
void analize_tree(TREEPTR root, short condition, short reg, int level)
{
    char    neigh[10],path[10];
    PATHPTR path_list=NULL,
	    neigh_list=NULL,
	    breath_h=NULL,
	    breath_q=NULL;
    short   length, merged, end;
    TREEPTR node,neigh_node;
    MERGEPTR node1, node2;


    initialize_label(path,0);
    node=root;

    end=False;
    while (!end) {
	node=get_leaf(path);

	/*Compute merge operations*/
	if (node->region!=NULL) {    /*A leaf has been found*/
	    /*Merge its neighbours if condition is satisfied*/
	    if (node->included!=yes) {
		/*Add a new element to the list of merged regions*/
		add_new_merged_region(&merged_region_list);
		/*Insert the region in the list of merged regions*/
		insert_leaf_list(&merged_region_list, node);
		node->included=yes;
		/*Actualize the pointer to the merged region which includes it*/
		node->pmerge = merged_region_list;
	    }
	    /* Obtain the list of neighbours of the node referenced by path */
	    get_neighbours(&neigh_list,root,path);
	    while (neigh_list!=NULL) {
		initialize_label(neigh,0);
		/* Get and delete the first element in the list of neighbours */
		delete_first(&neigh_list,neigh);
		neigh_node=get_leaf(neigh);
		/* Get the pointers to the candidate regions */
		node1=*get_location(node);
		node2=*get_location(neigh_node);
		if ( node1!=node2 ) {
		    /* The neighbour region doesn't belong to a merged region yet*/
		    if (neigh_node->included!=yes) {
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Insert the neighbour in the same list than path */
			    /* All the regions that are homogeneous belong to */
			    /* the same list */ 
			    insert_leaf_list(get_location(node),neigh_node);
			    neigh_node->included=yes;
			    /* Set pmerge field to point to the list */
			    /* where it has been inserted */
			    neigh_node->pmerge=*get_location(node);
			}
		    } else {  /* Each region belongs to a different merged region */
			/* Check whether both regions are homogeneous or not */ 
			if (similar(path,neigh,condition)) {
			    /* Check whether both merged regions are homogeneous */
			    if (abs( node1->mean - node2->mean ) < condition)
				/* Merge both lists of merged regions */
				merge_lists(&node1,&node2);
			}
		    }
		}
	    }
	} else {
	    length = strlen(path);
	    /* Insert new nodes to be analised using the breath first method */
	    path[length]='0'; insert_path(&breath_h,&breath_q,path);
	    path[length]='1'; insert_path(&breath_h,&breath_q,path);
	    path[length]='2'; insert_path(&breath_h,&breath_q,path);
	    path[length]='3'; insert_path(&breath_h,&breath_q,path);
	}
	/* Take a node from the list to analize it if there are nodes left */
	if (breath_h==NULL) end=True;
	    else delete_first(&breath_h,path);
    }
}
uint32_t huffman_decompress(FILE *fp, frequency_t *_frequency, uint32_t num_samples, char** codes) {
	uint32_t count;						/* count the number of frequency-sample */
	size_t reading;						/* fread control return */
	size_t i, j = 0;					/* loop indexes */
	uint8_t data;						/* data sample */
	char target[MAX_HUFF_CODE];			/* target code to search in the table */
	unsigned char read_byte;			/* bytes read from file */
	tree_t *huffman_tree = NULL;		/* one huffman tree */
	uint8_t bits = 0;					/* number of bits of the last byte */
	uint32_t header_counter;			/* count the number of bytes in the huffman header */
	char *buffer = NULL;
	char *sample = NULL;

	huffman_tree = (tree_t*) malloc(sizeof(tree_t));
	tree_create(&huffman_tree);

	/* reading the huffman header */
	reading = fread(&bits, sizeof(uint8_t), 1,fp);
	if (reading != 1) {
		TRACE("[ERROR] Fail to read file -- number of bits of the last character\n");
		return 0;
	}

	reading = fread(&count, sizeof(uint32_t), 1, fp);
	if (reading != 1) {
		TRACE("[ERROR] Fail to read file -- frequency counter\n");
		return 0;
	}

	for (i = 0; i < count; i++) {
		data = 0;
		fread(&data, sizeof(uint8_t), 1, fp);
		fread(&_frequency[data], sizeof(frequency_t), 1, fp);
	}
	/* huffman header has been read */

	/*huffman_table(table, count);*/

	huffman_tree->root = huffman(_frequency, MAX_SAMPLE);

	/*generate_table(huffman_tree->root, table, _frequency);*/

	/* decode every byte read from data sector in input file */
	memset(target, '\0', MAX_HUFF_CODE);
	while(j < num_samples) {
		/* read file until find a huffman code */
		fread(&read_byte, sizeof(unsigned char), 1, fp);
		for (i = 0; i < 8 && j < num_samples; i++) {
			strncat(target, (read_byte >> (7-i)) & 0x01 ? "1" : "0", sizeof(char));
			if (get_leaf(huffman_tree->root, target) != NULL) {	/* found the huffman code? */
				/*codes[j] = (char*) malloc((strlen(target) + 1) * sizeof(char));*/
				memset(codes[j], '\0', (strlen(target) + 1) * sizeof(char));
				strncpy(codes[j], target, strlen(target) * sizeof(char));
				memset(target, '\0', MAX_HUFF_CODE * sizeof(char));
				j++;
			}
		}
	}

	buffer = (char*) malloc(MAX_CODE * sizeof(char));
 	   	   	   	   	   	   	   	   	   	   /* data              frequency             bits               count            num_samples*/
	header_counter = count * (sizeof(uint8_t) + sizeof(frequency_t)) + sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint32_t);
	for (i = 0; i < num_samples; i++) {
		sample = get_leaf(huffman_tree->root, codes[i]);
		if (sample != NULL) {
			memset(buffer, '\0', (strlen(sample) - 1) * sizeof(char));
			strncpy(buffer, (sample+1), (strlen(sample) - 2) * sizeof(char));
			data_sample[i] = (uint8_t) string_to_int(buffer);
		}
	}
	free(huffman_tree);

	return header_counter;
}