Blame libarchive/archive_rb.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * This code is derived from software contributed to The NetBSD Foundation
Packit Service 1d0348
 * by Matt Thomas <matt@3am-software.com>.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
Packit Service 1d0348
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Packit Service 1d0348
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit Service 1d0348
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
Packit Service 1d0348
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit Service 1d0348
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit Service 1d0348
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit Service 1d0348
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit Service 1d0348
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit Service 1d0348
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit Service 1d0348
 * POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Based on: NetBSD: rb.c,v 1.6 2010/04/30 13:58:09 joerg Exp
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
Packit Service 1d0348
#include <stddef.h>
Packit Service 1d0348
Packit Service 1d0348
#include "archive_rb.h"
Packit Service 1d0348
Packit Service 1d0348
/* Keep in sync with archive_rb.h */
Packit Service 1d0348
#define	RB_DIR_LEFT		0
Packit Service 1d0348
#define	RB_DIR_RIGHT		1
Packit Service 1d0348
#define	RB_DIR_OTHER		1
Packit Service 1d0348
#define	rb_left			rb_nodes[RB_DIR_LEFT]
Packit Service 1d0348
#define	rb_right		rb_nodes[RB_DIR_RIGHT]
Packit Service 1d0348
Packit Service 1d0348
#define	RB_FLAG_POSITION	0x2
Packit Service 1d0348
#define	RB_FLAG_RED		0x1
Packit Service 1d0348
#define	RB_FLAG_MASK		(RB_FLAG_POSITION|RB_FLAG_RED)
Packit Service 1d0348
#define	RB_FATHER(rb) \
Packit Service 1d0348
    ((struct archive_rb_node *)((rb)->rb_info & ~RB_FLAG_MASK))
Packit Service 1d0348
#define	RB_SET_FATHER(rb, father) \
Packit Service 1d0348
    ((void)((rb)->rb_info = (uintptr_t)(father)|((rb)->rb_info & RB_FLAG_MASK)))
Packit Service 1d0348
Packit Service 1d0348
#define	RB_SENTINEL_P(rb)	((rb) == NULL)
Packit Service 1d0348
#define	RB_LEFT_SENTINEL_P(rb)	RB_SENTINEL_P((rb)->rb_left)
Packit Service 1d0348
#define	RB_RIGHT_SENTINEL_P(rb)	RB_SENTINEL_P((rb)->rb_right)
Packit Service 1d0348
#define	RB_FATHER_SENTINEL_P(rb) RB_SENTINEL_P(RB_FATHER((rb)))
Packit Service 1d0348
#define	RB_CHILDLESS_P(rb) \
Packit Service 1d0348
    (RB_SENTINEL_P(rb) || (RB_LEFT_SENTINEL_P(rb) && RB_RIGHT_SENTINEL_P(rb)))
Packit Service 1d0348
#define	RB_TWOCHILDREN_P(rb) \
Packit Service 1d0348
    (!RB_SENTINEL_P(rb) && !RB_LEFT_SENTINEL_P(rb) && !RB_RIGHT_SENTINEL_P(rb))
Packit Service 1d0348
Packit Service 1d0348
#define	RB_POSITION(rb)	\
Packit Service 1d0348
    (((rb)->rb_info & RB_FLAG_POSITION) ? RB_DIR_RIGHT : RB_DIR_LEFT)
Packit Service 1d0348
#define	RB_RIGHT_P(rb)		(RB_POSITION(rb) == RB_DIR_RIGHT)
Packit Service 1d0348
#define	RB_LEFT_P(rb)		(RB_POSITION(rb) == RB_DIR_LEFT)
Packit Service 1d0348
#define	RB_RED_P(rb) 		(!RB_SENTINEL_P(rb) && ((rb)->rb_info & RB_FLAG_RED) != 0)
Packit Service 1d0348
#define	RB_BLACK_P(rb) 		(RB_SENTINEL_P(rb) || ((rb)->rb_info & RB_FLAG_RED) == 0)
Packit Service 1d0348
#define	RB_MARK_RED(rb) 	((void)((rb)->rb_info |= RB_FLAG_RED))
Packit Service 1d0348
#define	RB_MARK_BLACK(rb) 	((void)((rb)->rb_info &= ~RB_FLAG_RED))
Packit Service 1d0348
#define	RB_INVERT_COLOR(rb) 	((void)((rb)->rb_info ^= RB_FLAG_RED))
Packit Service 1d0348
#define	RB_ROOT_P(rbt, rb)	((rbt)->rbt_root == (rb))
Packit Service 1d0348
#define	RB_SET_POSITION(rb, position) \
Packit Service 1d0348
    ((void)((position) ? ((rb)->rb_info |= RB_FLAG_POSITION) : \
Packit Service 1d0348
    ((rb)->rb_info &= ~RB_FLAG_POSITION)))
Packit Service 1d0348
#define	RB_ZERO_PROPERTIES(rb)	((void)((rb)->rb_info &= ~RB_FLAG_MASK))
Packit Service 1d0348
#define	RB_COPY_PROPERTIES(dst, src) \
Packit Service 1d0348
    ((void)((dst)->rb_info ^= ((dst)->rb_info ^ (src)->rb_info) & RB_FLAG_MASK))
Packit Service 1d0348
#define RB_SWAP_PROPERTIES(a, b) do { \
Packit Service 1d0348
    uintptr_t xorinfo = ((a)->rb_info ^ (b)->rb_info) & RB_FLAG_MASK; \
Packit Service 1d0348
    (a)->rb_info ^= xorinfo; \
Packit Service 1d0348
    (b)->rb_info ^= xorinfo; \
Packit Service 1d0348
  } while (/*CONSTCOND*/ 0)
Packit Service 1d0348
Packit Service 1d0348
static void __archive_rb_tree_insert_rebalance(struct archive_rb_tree *,
Packit Service 1d0348
    struct archive_rb_node *);
Packit Service 1d0348
static void __archive_rb_tree_removal_rebalance(struct archive_rb_tree *,
Packit Service 1d0348
    struct archive_rb_node *, unsigned int);
Packit Service 1d0348
Packit Service 1d0348
#define	RB_SENTINEL_NODE	NULL
Packit Service 1d0348
Packit Service 1d0348
#define T	1
Packit Service 1d0348
#define	F	0
Packit Service 1d0348
Packit Service 1d0348
void
Packit Service 1d0348
__archive_rb_tree_init(struct archive_rb_tree *rbt,
Packit Service 1d0348
    const struct archive_rb_tree_ops *ops)
Packit Service 1d0348
{
Packit Service 1d0348
	rbt->rbt_ops = ops;
Packit Service 1d0348
	*((struct archive_rb_node **)&rbt->rbt_root) = RB_SENTINEL_NODE;
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
struct archive_rb_node *
Packit Service 1d0348
__archive_rb_tree_find_node(struct archive_rb_tree *rbt, const void *key)
Packit Service 1d0348
{
Packit Service 1d0348
	archive_rbto_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key;
Packit Service 1d0348
	struct archive_rb_node *parent = rbt->rbt_root;
Packit Service 1d0348
Packit Service 1d0348
	while (!RB_SENTINEL_P(parent)) {
Packit Service 1d0348
		const signed int diff = (*compare_key)(parent, key);
Packit Service 1d0348
		if (diff == 0)
Packit Service 1d0348
			return parent;
Packit Service 1d0348
		parent = parent->rb_nodes[diff > 0];
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return NULL;
Packit Service 1d0348
}
Packit Service 1d0348
 
Packit Service 1d0348
struct archive_rb_node *
Packit Service 1d0348
__archive_rb_tree_find_node_geq(struct archive_rb_tree *rbt, const void *key)
Packit Service 1d0348
{
Packit Service 1d0348
	archive_rbto_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key;
Packit Service 1d0348
	struct archive_rb_node *parent = rbt->rbt_root;
Packit Service 1d0348
	struct archive_rb_node *last = NULL;
Packit Service 1d0348
Packit Service 1d0348
	while (!RB_SENTINEL_P(parent)) {
Packit Service 1d0348
		const signed int diff = (*compare_key)(parent, key);
Packit Service 1d0348
		if (diff == 0)
Packit Service 1d0348
			return parent;
Packit Service 1d0348
		if (diff < 0)
Packit Service 1d0348
			last = parent;
Packit Service 1d0348
		parent = parent->rb_nodes[diff > 0];
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return last;
Packit Service 1d0348
}
Packit Service 1d0348
 
Packit Service 1d0348
struct archive_rb_node *
Packit Service 1d0348
__archive_rb_tree_find_node_leq(struct archive_rb_tree *rbt, const void *key)
Packit Service 1d0348
{
Packit Service 1d0348
	archive_rbto_compare_key_fn compare_key = rbt->rbt_ops->rbto_compare_key;
Packit Service 1d0348
	struct archive_rb_node *parent = rbt->rbt_root;
Packit Service 1d0348
	struct archive_rb_node *last = NULL;
Packit Service 1d0348
Packit Service 1d0348
	while (!RB_SENTINEL_P(parent)) {
Packit Service 1d0348
		const signed int diff = (*compare_key)(parent, key);
Packit Service 1d0348
		if (diff == 0)
Packit Service 1d0348
			return parent;
Packit Service 1d0348
		if (diff > 0)
Packit Service 1d0348
			last = parent;
Packit Service 1d0348
		parent = parent->rb_nodes[diff > 0];
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return last;
Packit Service 1d0348
}
Packit Service 1d0348

Packit Service 1d0348
int
Packit Service 1d0348
__archive_rb_tree_insert_node(struct archive_rb_tree *rbt,
Packit Service 1d0348
    struct archive_rb_node *self)
Packit Service 1d0348
{
Packit Service 1d0348
	archive_rbto_compare_nodes_fn compare_nodes = rbt->rbt_ops->rbto_compare_nodes;
Packit Service 1d0348
	struct archive_rb_node *parent, *tmp;
Packit Service 1d0348
	unsigned int position;
Packit Service 1d0348
	int rebalance;
Packit Service 1d0348
Packit Service 1d0348
	tmp = rbt->rbt_root;
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * This is a hack.  Because rbt->rbt_root is just a
Packit Service 1d0348
	 * struct archive_rb_node *, just like rb_node->rb_nodes[RB_DIR_LEFT],
Packit Service 1d0348
	 * we can use this fact to avoid a lot of tests for root and know
Packit Service 1d0348
	 * that even at root, updating
Packit Service 1d0348
	 * RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will
Packit Service 1d0348
	 * update rbt->rbt_root.
Packit Service 1d0348
	 */
Packit Service 1d0348
	parent = (struct archive_rb_node *)(void *)&rbt->rbt_root;
Packit Service 1d0348
	position = RB_DIR_LEFT;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Find out where to place this new leaf.
Packit Service 1d0348
	 */
Packit Service 1d0348
	while (!RB_SENTINEL_P(tmp)) {
Packit Service 1d0348
		const signed int diff = (*compare_nodes)(tmp, self);
Packit Service 1d0348
		if (diff == 0) {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * Node already exists; don't insert.
Packit Service 1d0348
			 */
Packit Service 1d0348
			return F;
Packit Service 1d0348
		}
Packit Service 1d0348
		parent = tmp;
Packit Service 1d0348
		position = (diff > 0);
Packit Service 1d0348
		tmp = parent->rb_nodes[position];
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Initialize the node and insert as a leaf into the tree.
Packit Service 1d0348
	 */
Packit Service 1d0348
	RB_SET_FATHER(self, parent);
Packit Service 1d0348
	RB_SET_POSITION(self, position);
Packit Service 1d0348
	if (parent == (struct archive_rb_node *)(void *)&rbt->rbt_root) {
Packit Service 1d0348
		RB_MARK_BLACK(self);		/* root is always black */
Packit Service 1d0348
		rebalance = F;
Packit Service 1d0348
	} else {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * All new nodes are colored red.  We only need to rebalance
Packit Service 1d0348
		 * if our parent is also red.
Packit Service 1d0348
		 */
Packit Service 1d0348
		RB_MARK_RED(self);
Packit Service 1d0348
		rebalance = RB_RED_P(parent);
Packit Service 1d0348
	}
Packit Service 1d0348
	self->rb_left = parent->rb_nodes[position];
Packit Service 1d0348
	self->rb_right = parent->rb_nodes[position];
Packit Service 1d0348
	parent->rb_nodes[position] = self;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Rebalance tree after insertion
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (rebalance)
Packit Service 1d0348
		__archive_rb_tree_insert_rebalance(rbt, self);
Packit Service 1d0348
Packit Service 1d0348
	return T;
Packit Service 1d0348
}
Packit Service 1d0348

Packit Service 1d0348
/*
Packit Service 1d0348
 * Swap the location and colors of 'self' and its child @ which.  The child
Packit Service 1d0348
 * can not be a sentinel node.  This is our rotation function.  However,
Packit Service 1d0348
 * since it preserves coloring, it great simplifies both insertion and
Packit Service 1d0348
 * removal since rotation almost always involves the exchanging of colors
Packit Service 1d0348
 * as a separate step.
Packit Service 1d0348
 */
Packit Service 1d0348
/*ARGSUSED*/
Packit Service 1d0348
static void
Packit Service 1d0348
__archive_rb_tree_reparent_nodes(
Packit Service 1d0348
    struct archive_rb_node *old_father, const unsigned int which)
Packit Service 1d0348
{
Packit Service 1d0348
	const unsigned int other = which ^ RB_DIR_OTHER;
Packit Service 1d0348
	struct archive_rb_node * const grandpa = RB_FATHER(old_father);
Packit Service 1d0348
	struct archive_rb_node * const old_child = old_father->rb_nodes[which];
Packit Service 1d0348
	struct archive_rb_node * const new_father = old_child;
Packit Service 1d0348
	struct archive_rb_node * const new_child = old_father;
Packit Service 1d0348
Packit Service 1d0348
	if (new_father == NULL)
Packit Service 1d0348
		return;
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Exchange descendant linkages.
Packit Service 1d0348
	 */
Packit Service 1d0348
	grandpa->rb_nodes[RB_POSITION(old_father)] = new_father;
Packit Service 1d0348
	new_child->rb_nodes[which] = old_child->rb_nodes[other];
Packit Service 1d0348
	new_father->rb_nodes[other] = new_child;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Update ancestor linkages
Packit Service 1d0348
	 */
Packit Service 1d0348
	RB_SET_FATHER(new_father, grandpa);
Packit Service 1d0348
	RB_SET_FATHER(new_child, new_father);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Exchange properties between new_father and new_child.  The only
Packit Service 1d0348
	 * change is that new_child's position is now on the other side.
Packit Service 1d0348
	 */
Packit Service 1d0348
	RB_SWAP_PROPERTIES(new_father, new_child);
Packit Service 1d0348
	RB_SET_POSITION(new_child, other);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Make sure to reparent the new child to ourself.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (!RB_SENTINEL_P(new_child->rb_nodes[which])) {
Packit Service 1d0348
		RB_SET_FATHER(new_child->rb_nodes[which], new_child);
Packit Service 1d0348
		RB_SET_POSITION(new_child->rb_nodes[which], which);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
}
Packit Service 1d0348

Packit Service 1d0348
static void
Packit Service 1d0348
__archive_rb_tree_insert_rebalance(struct archive_rb_tree *rbt,
Packit Service 1d0348
    struct archive_rb_node *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_rb_node * father = RB_FATHER(self);
Packit Service 1d0348
	struct archive_rb_node * grandpa;
Packit Service 1d0348
	struct archive_rb_node * uncle;
Packit Service 1d0348
	unsigned int which;
Packit Service 1d0348
	unsigned int other;
Packit Service 1d0348
Packit Service 1d0348
	for (;;) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * We are red and our parent is red, therefore we must have a
Packit Service 1d0348
		 * grandfather and he must be black.
Packit Service 1d0348
		 */
Packit Service 1d0348
		grandpa = RB_FATHER(father);
Packit Service 1d0348
		which = (father == grandpa->rb_right);
Packit Service 1d0348
		other = which ^ RB_DIR_OTHER;
Packit Service 1d0348
		uncle = grandpa->rb_nodes[other];
Packit Service 1d0348
Packit Service 1d0348
		if (RB_BLACK_P(uncle))
Packit Service 1d0348
			break;
Packit Service 1d0348
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Case 1: our uncle is red
Packit Service 1d0348
		 *   Simply invert the colors of our parent and
Packit Service 1d0348
		 *   uncle and make our grandparent red.  And
Packit Service 1d0348
		 *   then solve the problem up at his level.
Packit Service 1d0348
		 */
Packit Service 1d0348
		RB_MARK_BLACK(uncle);
Packit Service 1d0348
		RB_MARK_BLACK(father);
Packit Service 1d0348
		if (RB_ROOT_P(rbt, grandpa)) {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * If our grandpa is root, don't bother
Packit Service 1d0348
			 * setting him to red, just return.
Packit Service 1d0348
			 */
Packit Service 1d0348
			return;
Packit Service 1d0348
		}
Packit Service 1d0348
		RB_MARK_RED(grandpa);
Packit Service 1d0348
		self = grandpa;
Packit Service 1d0348
		father = RB_FATHER(self);
Packit Service 1d0348
		if (RB_BLACK_P(father)) {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * If our great-grandpa is black, we're done.
Packit Service 1d0348
			 */
Packit Service 1d0348
			return;
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Case 2&3: our uncle is black.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (self == father->rb_nodes[other]) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Case 2: we are on the same side as our uncle
Packit Service 1d0348
		 *   Swap ourselves with our parent so this case
Packit Service 1d0348
		 *   becomes case 3.  Basically our parent becomes our
Packit Service 1d0348
		 *   child.
Packit Service 1d0348
		 */
Packit Service 1d0348
		__archive_rb_tree_reparent_nodes(father, other);
Packit Service 1d0348
	}
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Case 3: we are opposite a child of a black uncle.
Packit Service 1d0348
	 *   Swap our parent and grandparent.  Since our grandfather
Packit Service 1d0348
	 *   is black, our father will become black and our new sibling
Packit Service 1d0348
	 *   (former grandparent) will become red.
Packit Service 1d0348
	 */
Packit Service 1d0348
	__archive_rb_tree_reparent_nodes(grandpa, which);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Final step: Set the root to black.
Packit Service 1d0348
	 */
Packit Service 1d0348
	RB_MARK_BLACK(rbt->rbt_root);
Packit Service 1d0348
}
Packit Service 1d0348

Packit Service 1d0348
static void
Packit Service 1d0348
__archive_rb_tree_prune_node(struct archive_rb_tree *rbt,
Packit Service 1d0348
    struct archive_rb_node *self, int rebalance)
Packit Service 1d0348
{
Packit Service 1d0348
	const unsigned int which = RB_POSITION(self);
Packit Service 1d0348
	struct archive_rb_node *father = RB_FATHER(self);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Since we are childless, we know that self->rb_left is pointing
Packit Service 1d0348
	 * to the sentinel node.
Packit Service 1d0348
	 */
Packit Service 1d0348
	father->rb_nodes[which] = self->rb_left;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Rebalance if requested.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (rebalance)
Packit Service 1d0348
		__archive_rb_tree_removal_rebalance(rbt, father, which);
Packit Service 1d0348
}
Packit Service 1d0348

Packit Service 1d0348
/*
Packit Service 1d0348
 * When deleting an interior node
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
__archive_rb_tree_swap_prune_and_rebalance(struct archive_rb_tree *rbt,
Packit Service 1d0348
    struct archive_rb_node *self, struct archive_rb_node *standin)
Packit Service 1d0348
{
Packit Service 1d0348
	const unsigned int standin_which = RB_POSITION(standin);
Packit Service 1d0348
	unsigned int standin_other = standin_which ^ RB_DIR_OTHER;
Packit Service 1d0348
	struct archive_rb_node *standin_son;
Packit Service 1d0348
	struct archive_rb_node *standin_father = RB_FATHER(standin);
Packit Service 1d0348
	int rebalance = RB_BLACK_P(standin);
Packit Service 1d0348
Packit Service 1d0348
	if (standin_father == self) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * As a child of self, any children would be opposite of
Packit Service 1d0348
		 * our parent.
Packit Service 1d0348
		 */
Packit Service 1d0348
		standin_son = standin->rb_nodes[standin_which];
Packit Service 1d0348
	} else {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Since we aren't a child of self, any children would be
Packit Service 1d0348
		 * on the same side as our parent.
Packit Service 1d0348
		 */
Packit Service 1d0348
		standin_son = standin->rb_nodes[standin_other];
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (RB_RED_P(standin_son)) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * We know we have a red child so if we flip it to black
Packit Service 1d0348
		 * we don't have to rebalance.
Packit Service 1d0348
		 */
Packit Service 1d0348
		RB_MARK_BLACK(standin_son);
Packit Service 1d0348
		rebalance = F;
Packit Service 1d0348
Packit Service 1d0348
		if (standin_father != self) {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * Change the son's parentage to point to his grandpa.
Packit Service 1d0348
			 */
Packit Service 1d0348
			RB_SET_FATHER(standin_son, standin_father);
Packit Service 1d0348
			RB_SET_POSITION(standin_son, standin_which);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (standin_father == self) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * If we are about to delete the standin's father, then when
Packit Service 1d0348
		 * we call rebalance, we need to use ourselves as our father.
Packit Service 1d0348
		 * Otherwise remember our original father.  Also, since we are
Packit Service 1d0348
		 * our standin's father we only need to reparent the standin's
Packit Service 1d0348
		 * brother.
Packit Service 1d0348
		 *
Packit Service 1d0348
		 * |    R      -->     S    |
Packit Service 1d0348
		 * |  Q   S    -->   Q   T  |
Packit Service 1d0348
		 * |        t  -->          |
Packit Service 1d0348
		 *
Packit Service 1d0348
		 * Have our son/standin adopt his brother as his new son.
Packit Service 1d0348
		 */
Packit Service 1d0348
		standin_father = standin;
Packit Service 1d0348
	} else {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * |    R          -->    S       .  |
Packit Service 1d0348
		 * |   / \  |   T  -->   / \  |  /   |
Packit Service 1d0348
		 * |  ..... | S    -->  ..... | T    |
Packit Service 1d0348
		 *
Packit Service 1d0348
		 * Sever standin's connection to his father.
Packit Service 1d0348
		 */
Packit Service 1d0348
		standin_father->rb_nodes[standin_which] = standin_son;
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Adopt the far son.
Packit Service 1d0348
		 */
Packit Service 1d0348
		standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
Packit Service 1d0348
		RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Use standin_other because we need to preserve standin_which
Packit Service 1d0348
		 * for the removal_rebalance.
Packit Service 1d0348
		 */
Packit Service 1d0348
		standin_other = standin_which;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Move the only remaining son to our standin.  If our standin is our
Packit Service 1d0348
	 * son, this will be the only son needed to be moved.
Packit Service 1d0348
	 */
Packit Service 1d0348
	standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
Packit Service 1d0348
	RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Now copy the result of self to standin and then replace
Packit Service 1d0348
	 * self with standin in the tree.
Packit Service 1d0348
	 */
Packit Service 1d0348
	RB_COPY_PROPERTIES(standin, self);
Packit Service 1d0348
	RB_SET_FATHER(standin, RB_FATHER(self));
Packit Service 1d0348
	RB_FATHER(standin)->rb_nodes[RB_POSITION(standin)] = standin;
Packit Service 1d0348
Packit Service 1d0348
	if (rebalance)
Packit Service 1d0348
		__archive_rb_tree_removal_rebalance(rbt, standin_father, standin_which);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * We could do this by doing
Packit Service 1d0348
 *	__archive_rb_tree_node_swap(rbt, self, which);
Packit Service 1d0348
 *	__archive_rb_tree_prune_node(rbt, self, F);
Packit Service 1d0348
 *
Packit Service 1d0348
 * But it's more efficient to just evaluate and recolor the child.
Packit Service 1d0348
 */
Packit Service 1d0348
static void
Packit Service 1d0348
__archive_rb_tree_prune_blackred_branch(
Packit Service 1d0348
    struct archive_rb_node *self, unsigned int which)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_rb_node *father = RB_FATHER(self);
Packit Service 1d0348
	struct archive_rb_node *son = self->rb_nodes[which];
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Remove ourselves from the tree and give our former child our
Packit Service 1d0348
	 * properties (position, color, root).
Packit Service 1d0348
	 */
Packit Service 1d0348
	RB_COPY_PROPERTIES(son, self);
Packit Service 1d0348
	father->rb_nodes[RB_POSITION(son)] = son;
Packit Service 1d0348
	RB_SET_FATHER(son, father);
Packit Service 1d0348
}
Packit Service 1d0348
/*
Packit Service 1d0348
 *
Packit Service 1d0348
 */
Packit Service 1d0348
void
Packit Service 1d0348
__archive_rb_tree_remove_node(struct archive_rb_tree *rbt,
Packit Service 1d0348
    struct archive_rb_node *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_rb_node *standin;
Packit Service 1d0348
	unsigned int which;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * In the following diagrams, we (the node to be removed) are S.  Red
Packit Service 1d0348
	 * nodes are lowercase.  T could be either red or black.
Packit Service 1d0348
	 *
Packit Service 1d0348
	 * Remember the major axiom of the red-black tree: the number of
Packit Service 1d0348
	 * black nodes from the root to each leaf is constant across all
Packit Service 1d0348
	 * leaves, only the number of red nodes varies.
Packit Service 1d0348
	 *
Packit Service 1d0348
	 * Thus removing a red leaf doesn't require any other changes to a
Packit Service 1d0348
	 * red-black tree.  So if we must remove a node, attempt to rearrange
Packit Service 1d0348
	 * the tree so we can remove a red node.
Packit Service 1d0348
	 *
Packit Service 1d0348
	 * The simplest case is a childless red node or a childless root node:
Packit Service 1d0348
	 *
Packit Service 1d0348
	 * |    T  -->    T  |    or    |  R  -->  *  |
Packit Service 1d0348
	 * |  s    -->  *    |
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (RB_CHILDLESS_P(self)) {
Packit Service 1d0348
		const int rebalance = RB_BLACK_P(self) && !RB_ROOT_P(rbt, self);
Packit Service 1d0348
		__archive_rb_tree_prune_node(rbt, self, rebalance);
Packit Service 1d0348
		return;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (!RB_TWOCHILDREN_P(self)) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * The next simplest case is the node we are deleting is
Packit Service 1d0348
		 * black and has one red child.
Packit Service 1d0348
		 *
Packit Service 1d0348
		 * |      T  -->      T  -->      T  |
Packit Service 1d0348
		 * |    S    -->  R      -->  R      |
Packit Service 1d0348
		 * |  r      -->    s    -->    *    |
Packit Service 1d0348
		 */
Packit Service 1d0348
		which = RB_LEFT_SENTINEL_P(self) ? RB_DIR_RIGHT : RB_DIR_LEFT;
Packit Service 1d0348
		__archive_rb_tree_prune_blackred_branch(self, which);
Packit Service 1d0348
		return;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * We invert these because we prefer to remove from the inside of
Packit Service 1d0348
	 * the tree.
Packit Service 1d0348
	 */
Packit Service 1d0348
	which = RB_POSITION(self) ^ RB_DIR_OTHER;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Let's find the node closes to us opposite of our parent
Packit Service 1d0348
	 * Now swap it with ourself, "prune" it, and rebalance, if needed.
Packit Service 1d0348
	 */
Packit Service 1d0348
	standin = __archive_rb_tree_iterate(rbt, self, which);
Packit Service 1d0348
	__archive_rb_tree_swap_prune_and_rebalance(rbt, self, standin);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
__archive_rb_tree_removal_rebalance(struct archive_rb_tree *rbt,
Packit Service 1d0348
    struct archive_rb_node *parent, unsigned int which)
Packit Service 1d0348
{
Packit Service 1d0348
Packit Service 1d0348
	while (RB_BLACK_P(parent->rb_nodes[which])) {
Packit Service 1d0348
		unsigned int other = which ^ RB_DIR_OTHER;
Packit Service 1d0348
		struct archive_rb_node *brother = parent->rb_nodes[other];
Packit Service 1d0348
Packit Service 1d0348
		if (brother == NULL)
Packit Service 1d0348
			return;/* The tree may be broken. */
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * For cases 1, 2a, and 2b, our brother's children must
Packit Service 1d0348
		 * be black and our father must be black
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (RB_BLACK_P(parent)
Packit Service 1d0348
		    && RB_BLACK_P(brother->rb_left)
Packit Service 1d0348
		    && RB_BLACK_P(brother->rb_right)) {
Packit Service 1d0348
			if (RB_RED_P(brother)) {
Packit Service 1d0348
				/*
Packit Service 1d0348
				 * Case 1: Our brother is red, swap its
Packit Service 1d0348
				 * position (and colors) with our parent. 
Packit Service 1d0348
				 * This should now be case 2b (unless C or E
Packit Service 1d0348
				 * has a red child which is case 3; thus no
Packit Service 1d0348
				 * explicit branch to case 2b).
Packit Service 1d0348
				 *
Packit Service 1d0348
				 *    B         ->        D
Packit Service 1d0348
				 *  A     d     ->    b     E
Packit Service 1d0348
				 *      C   E   ->  A   C
Packit Service 1d0348
				 */
Packit Service 1d0348
				__archive_rb_tree_reparent_nodes(parent, other);
Packit Service 1d0348
				brother = parent->rb_nodes[other];
Packit Service 1d0348
				if (brother == NULL)
Packit Service 1d0348
					return;/* The tree may be broken. */
Packit Service 1d0348
			} else {
Packit Service 1d0348
				/*
Packit Service 1d0348
				 * Both our parent and brother are black.
Packit Service 1d0348
				 * Change our brother to red, advance up rank
Packit Service 1d0348
				 * and go through the loop again.
Packit Service 1d0348
				 *
Packit Service 1d0348
				 *    B         ->   *B
Packit Service 1d0348
				 * *A     D     ->  A     d
Packit Service 1d0348
				 *      C   E   ->      C   E
Packit Service 1d0348
				 */
Packit Service 1d0348
				RB_MARK_RED(brother);
Packit Service 1d0348
				if (RB_ROOT_P(rbt, parent))
Packit Service 1d0348
					return;	/* root == parent == black */
Packit Service 1d0348
				which = RB_POSITION(parent);
Packit Service 1d0348
				parent = RB_FATHER(parent);
Packit Service 1d0348
				continue;
Packit Service 1d0348
			}
Packit Service 1d0348
		}
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Avoid an else here so that case 2a above can hit either
Packit Service 1d0348
		 * case 2b, 3, or 4.
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (RB_RED_P(parent)
Packit Service 1d0348
		    && RB_BLACK_P(brother)
Packit Service 1d0348
		    && RB_BLACK_P(brother->rb_left)
Packit Service 1d0348
		    && RB_BLACK_P(brother->rb_right)) {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * We are black, our father is red, our brother and
Packit Service 1d0348
			 * both nephews are black.  Simply invert/exchange the
Packit Service 1d0348
			 * colors of our father and brother (to black and red
Packit Service 1d0348
			 * respectively).
Packit Service 1d0348
			 *
Packit Service 1d0348
			 *	|    f        -->    F        |
Packit Service 1d0348
			 *	|  *     B    -->  *     b    |
Packit Service 1d0348
			 *	|      N   N  -->      N   N  |
Packit Service 1d0348
			 */
Packit Service 1d0348
			RB_MARK_BLACK(parent);
Packit Service 1d0348
			RB_MARK_RED(brother);
Packit Service 1d0348
			break;		/* We're done! */
Packit Service 1d0348
		} else {
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * Our brother must be black and have at least one
Packit Service 1d0348
			 * red child (it may have two).
Packit Service 1d0348
			 */
Packit Service 1d0348
			if (RB_BLACK_P(brother->rb_nodes[other])) {
Packit Service 1d0348
				/*
Packit Service 1d0348
				 * Case 3: our brother is black, our near
Packit Service 1d0348
				 * nephew is red, and our far nephew is black.
Packit Service 1d0348
				 * Swap our brother with our near nephew.  
Packit Service 1d0348
				 * This result in a tree that matches case 4.
Packit Service 1d0348
				 * (Our father could be red or black).
Packit Service 1d0348
				 *
Packit Service 1d0348
				 *	|    F      -->    F      |
Packit Service 1d0348
				 *	|  x     B  -->  x   B    |
Packit Service 1d0348
				 *	|      n    -->        n  |
Packit Service 1d0348
				 */
Packit Service 1d0348
				__archive_rb_tree_reparent_nodes(brother, which);
Packit Service 1d0348
				brother = parent->rb_nodes[other];
Packit Service 1d0348
			}
Packit Service 1d0348
			/*
Packit Service 1d0348
			 * Case 4: our brother is black and our far nephew
Packit Service 1d0348
			 * is red.  Swap our father and brother locations and
Packit Service 1d0348
			 * change our far nephew to black.  (these can be
Packit Service 1d0348
			 * done in either order so we change the color first).
Packit Service 1d0348
			 * The result is a valid red-black tree and is a
Packit Service 1d0348
			 * terminal case.  (again we don't care about the
Packit Service 1d0348
			 * father's color)
Packit Service 1d0348
			 *
Packit Service 1d0348
			 * If the father is red, we will get a red-black-black
Packit Service 1d0348
			 * tree:
Packit Service 1d0348
			 *	|  f      ->  f      -->    b    |
Packit Service 1d0348
			 *	|    B    ->    B    -->  F   N  |
Packit Service 1d0348
			 *	|      n  ->      N  -->         |
Packit Service 1d0348
			 *
Packit Service 1d0348
			 * If the father is black, we will get an all black
Packit Service 1d0348
			 * tree:
Packit Service 1d0348
			 *	|  F      ->  F      -->    B    |
Packit Service 1d0348
			 *	|    B    ->    B    -->  F   N  |
Packit Service 1d0348
			 *	|      n  ->      N  -->         |
Packit Service 1d0348
			 *
Packit Service 1d0348
			 * If we had two red nephews, then after the swap,
Packit Service 1d0348
			 * our former father would have a red grandson. 
Packit Service 1d0348
			 */
Packit Service 1d0348
			if (brother->rb_nodes[other] == NULL)
Packit Service 1d0348
				return;/* The tree may be broken. */
Packit Service 1d0348
			RB_MARK_BLACK(brother->rb_nodes[other]);
Packit Service 1d0348
			__archive_rb_tree_reparent_nodes(parent, other);
Packit Service 1d0348
			break;		/* We're done! */
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
struct archive_rb_node *
Packit Service 1d0348
__archive_rb_tree_iterate(struct archive_rb_tree *rbt,
Packit Service 1d0348
    struct archive_rb_node *self, const unsigned int direction)
Packit Service 1d0348
{
Packit Service 1d0348
	const unsigned int other = direction ^ RB_DIR_OTHER;
Packit Service 1d0348
Packit Service 1d0348
	if (self == NULL) {
Packit Service 1d0348
		self = rbt->rbt_root;
Packit Service 1d0348
		if (RB_SENTINEL_P(self))
Packit Service 1d0348
			return NULL;
Packit Service 1d0348
		while (!RB_SENTINEL_P(self->rb_nodes[direction]))
Packit Service 1d0348
			self = self->rb_nodes[direction];
Packit Service 1d0348
		return self;
Packit Service 1d0348
	}
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * We can't go any further in this direction.  We proceed up in the
Packit Service 1d0348
	 * opposite direction until our parent is in direction we want to go.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (RB_SENTINEL_P(self->rb_nodes[direction])) {
Packit Service 1d0348
		while (!RB_ROOT_P(rbt, self)) {
Packit Service 1d0348
			if (other == (unsigned int)RB_POSITION(self))
Packit Service 1d0348
				return RB_FATHER(self);
Packit Service 1d0348
			self = RB_FATHER(self);
Packit Service 1d0348
		}
Packit Service 1d0348
		return NULL;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Advance down one in current direction and go down as far as possible
Packit Service 1d0348
	 * in the opposite direction.
Packit Service 1d0348
	 */
Packit Service 1d0348
	self = self->rb_nodes[direction];
Packit Service 1d0348
	while (!RB_SENTINEL_P(self->rb_nodes[other]))
Packit Service 1d0348
		self = self->rb_nodes[other];
Packit Service 1d0348
	return self;
Packit Service 1d0348
}