Blame libarchive/archive_rb.h

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.h,v 1.13 2009/08/16 10:57:01 yamt Exp
Packit Service 1d0348
 */
Packit Service 1d0348
#ifndef ARCHIVE_RB_H_
Packit Service 1d0348
#define	ARCHIVE_RB_H_
Packit Service 1d0348
Packit Service 1d0348
struct archive_rb_node {
Packit Service 1d0348
	struct archive_rb_node *rb_nodes[2];
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * rb_info contains the two flags and the parent back pointer.
Packit Service 1d0348
	 * We put the two flags in the low two bits since we know that
Packit Service 1d0348
	 * rb_node will have an alignment of 4 or 8 bytes.
Packit Service 1d0348
	 */
Packit Service 1d0348
	uintptr_t rb_info;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
#define	ARCHIVE_RB_DIR_LEFT		0
Packit Service 1d0348
#define	ARCHIVE_RB_DIR_RIGHT		1
Packit Service 1d0348
Packit Service 1d0348
#define ARCHIVE_RB_TREE_MIN(T) \
Packit Service 1d0348
    __archive_rb_tree_iterate((T), NULL, ARCHIVE_RB_DIR_LEFT)
Packit Service 1d0348
#define ARCHIVE_RB_TREE_MAX(T) \
Packit Service 1d0348
    __archive_rb_tree_iterate((T), NULL, ARCHIVE_RB_DIR_RIGHT)
Packit Service 1d0348
#define ARCHIVE_RB_TREE_FOREACH(N, T) \
Packit Service 1d0348
    for ((N) = ARCHIVE_RB_TREE_MIN(T); (N); \
Packit Service 1d0348
	(N) = __archive_rb_tree_iterate((T), (N), ARCHIVE_RB_DIR_RIGHT))
Packit Service 1d0348
#define ARCHIVE_RB_TREE_FOREACH_REVERSE(N, T) \
Packit Service 1d0348
    for ((N) = ARCHIVE_RB_TREE_MAX(T); (N); \
Packit Service 1d0348
	(N) = __archive_rb_tree_iterate((T), (N), ARCHIVE_RB_DIR_LEFT))
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * archive_rbto_compare_nodes_fn:
Packit Service 1d0348
 *	return a positive value if the first node < the second node.
Packit Service 1d0348
 *	return a negative value if the first node > the second node.
Packit Service 1d0348
 *	return 0 if they are considered same.
Packit Service 1d0348
 *
Packit Service 1d0348
 * archive_rbto_compare_key_fn:
Packit Service 1d0348
 *	return a positive value if the node < the key.
Packit Service 1d0348
 *	return a negative value if the node > the key.
Packit Service 1d0348
 *	return 0 if they are considered same.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
typedef signed int (*const archive_rbto_compare_nodes_fn)(const struct archive_rb_node *,
Packit Service 1d0348
    const struct archive_rb_node *);
Packit Service 1d0348
typedef signed int (*const archive_rbto_compare_key_fn)(const struct archive_rb_node *,
Packit Service 1d0348
    const void *);
Packit Service 1d0348
Packit Service 1d0348
struct archive_rb_tree_ops {
Packit Service 1d0348
	archive_rbto_compare_nodes_fn rbto_compare_nodes;
Packit Service 1d0348
	archive_rbto_compare_key_fn rbto_compare_key;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
struct archive_rb_tree {
Packit Service 1d0348
	struct archive_rb_node *rbt_root;
Packit Service 1d0348
	const struct archive_rb_tree_ops *rbt_ops;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
void	__archive_rb_tree_init(struct archive_rb_tree *,
Packit Service 1d0348
    const struct archive_rb_tree_ops *);
Packit Service 1d0348
int	__archive_rb_tree_insert_node(struct archive_rb_tree *,
Packit Service 1d0348
    struct archive_rb_node *);
Packit Service 1d0348
struct archive_rb_node	*
Packit Service 1d0348
	__archive_rb_tree_find_node(struct archive_rb_tree *, const void *);
Packit Service 1d0348
struct archive_rb_node	*
Packit Service 1d0348
	__archive_rb_tree_find_node_geq(struct archive_rb_tree *, const void *);
Packit Service 1d0348
struct archive_rb_node	*
Packit Service 1d0348
	__archive_rb_tree_find_node_leq(struct archive_rb_tree *, const void *);
Packit Service 1d0348
void	__archive_rb_tree_remove_node(struct archive_rb_tree *, struct archive_rb_node *);
Packit Service 1d0348
struct archive_rb_node *
Packit Service 1d0348
	__archive_rb_tree_iterate(struct archive_rb_tree *,
Packit Service 1d0348
	struct archive_rb_node *, const unsigned int);
Packit Service 1d0348
Packit Service 1d0348
#endif	/* ARCHIVE_RB_H_*/