예제 #1
0
파일: BTree.cpp 프로젝트: tomahawkpl/Jello
void BTree::addToNode(BTreeNode *node, BTreeElement *child) {
	if (!node->addChild(child->getMinId(), child)) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Node is full");
		BTreeNode *newNode = new BTreeNode(this, nodeCapacity);
		int oldMinId = node->getMinId();
		node->split(newNode);

		BTreeNode *addTo;
		if (node->getMinId() == -1 || child->getMinId() > node->getMinId())
			addTo = node;
		else
			addTo = newNode;

		if (!addTo->addChild(child->getMinId(), child)) {
		//	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Node add FAILED!");
			return;
		}

		if (node->getParent() == NULL) {
		//	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "This node's parent is null");
			BTreeNode *parent = new BTreeNode(this, nodeCapacity);
			parent->addChild(node->getMinId(), node);
			root = parent;
		}
		addToNode(node->getParent(), newNode);
	}
	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Added new node to parent");
}
예제 #2
0
/**
 * Adds new element to the subtree.
 *
 * @param [in,out] tree AVL tree.
 * @param [in,out] node Root of the subtree to add to.
 * @param [in] key The key of the element.
 * @param [in,out] value The value of the element.
 *
 * @retval ADDED The node succesfully added.
 * @retval EXISTS The node is already exists in the AVL tree.
 */
static NodeAddResult addToNode(AVL_Tree *tree, AVL_Node *node, const void *key, void *value)
{
    int tmp = tree->keyComparer(key, node->key);
    AVL_Node **nodeToAddTo = 0;

    // Find the key in the tree recursively, and add it to a leaf node.
    if (tmp < 0)
    {
        if (node->left)
        {
            return addToNode(tree, node->left, key, value);
        }
        else
        {
            nodeToAddTo = &node->left;
        }
    }
    else if (tmp > 0)
    {
        if (node->right)
        {
           return addToNode(tree, node->right, key, value);
        }
        else
        {
            nodeToAddTo = &node->right;
        }
    }
    else
    {
        return EXISTS;
    }
    *nodeToAddTo = allocateNode();
    (*nodeToAddTo)->parent = node;
    (*nodeToAddTo)->key = key;
    (*nodeToAddTo)->value = value;
    // Reballance the leaf node.
    reballance(tree, *nodeToAddTo);

    return ADDED;
}
예제 #3
0
void AVL_add(AVL_Tree *tree, const void *key, void *value)
{
    if (!tree->rootNode)
    {
        // First element.
        tree->rootNode = allocateNode();
        tree->rootNode->key = key;
        tree->rootNode->value = value;
    }
    else
    {
        // Not the first element.
        addToNode(tree, tree->rootNode, key, value);
    }
}
예제 #4
0
파일: BTree.cpp 프로젝트: tomahawkpl/Jello
void BTree::add(int id, RecordInfo *record) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "== BTree add id: %d", id);
	if (root == NULL) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "new leaf as root: %d", leafCapacity);
		root = new BTreeLeaf(this, leafCapacity);
	}

	BTreeElement *e = root;

	while (e->type == BTreeElement::ELEMENT_NODE) {
		e = ((BTreeNode*)e)->getSubNodeFor(id);
		if (e == NULL)
			return;
	}

	BTreeLeaf *leaf = (BTreeLeaf*)e;

	if (!leaf->add(id, record)) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Not enough space in leaf");
		BTreeLeaf *newLeaf = new BTreeLeaf(this, leafCapacity);

		int oldMinId = leaf->getMinId();
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Splitting");
		leaf->split(newLeaf);
		BTreeLeaf *addTo;
		if (leaf->getMinId() == -1 || id > leaf->getMinId())
			addTo = leaf;
		else
			addTo = newLeaf;

		if (!addTo->add(id, record)) {
			//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Leaf add FAILED!");
			return;
		}

		if (leaf->getParent() == NULL) {
			//__android_log_print(ANDROID_LOG_INFO, "Jello",  "This leaf's parent is NULL, creating new node");
			BTreeNode *node = new BTreeNode(this, nodeCapacity);
			node->addChild(leaf->getMinId(), leaf);
			root = node;
		}

		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Adding new node to parent");
		addToNode(leaf->getParent(), newLeaf);
	}

}
예제 #5
0
파일: BTree.cpp 프로젝트: tomahawkpl/Jello
void BTree::update(int id, RecordInfo *record) {
	//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Update: %d", id);
	BTreeElement *e = root;

	if (e == NULL)
		return;

	while (e->type == BTreeElement::ELEMENT_NODE) {
		e = ((BTreeNode*)e)->getSubNodeFor(id);
		if (e == NULL)
			return;
	}

	BTreeLeaf *leaf = (BTreeLeaf*)e;

	if (!leaf->update(id, record)) {
		//__android_log_print(ANDROID_LOG_INFO, "Jello",  "update failed, splitting node");
		BTreeLeaf *newLeaf = new BTreeLeaf(this, leafCapacity);
		int oldMinId = leaf->getMinId();
		leaf->split(newLeaf);
		if (id < leaf->getMinId()) {
			if (!newLeaf->update(id, record)) {
				//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Leaf add FAILED!");
				return;
			}
		} else {
			if (!leaf->update(id, record)) {
				//__android_log_print(ANDROID_LOG_INFO, "Jello",  "Leaf add FAILED!");
				return;
			}
		}

		if (leaf->getParent() == NULL) {
			BTreeNode *node = new BTreeNode(this, nodeCapacity);
			node->addChild(leaf->getMinId(), leaf);
		}

		addToNode(leaf->getParent(), newLeaf);
	} else
		mergeNode(leaf);

}