2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
2013-12-16 03:32:02 +01:00
|
|
|
#ifndef VPX_MEM_MEMORY_MANAGER_INCLUDE_CAVL_IF_H_
|
|
|
|
#define VPX_MEM_MEMORY_MANAGER_INCLUDE_CAVL_IF_H_
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
/* Abstract AVL Tree Generic C Package.
|
|
|
|
** Interface generation header file.
|
|
|
|
**
|
|
|
|
** This code is in the public domain. See cavl_tree.html for interface
|
|
|
|
** documentation.
|
|
|
|
**
|
|
|
|
** Version: 1.5 Author: Walt Karas
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This header contains the definition of CHAR_BIT (number of bits in a
|
|
|
|
** char). */
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#undef L_
|
|
|
|
#undef L_EST_LONG_BIT
|
|
|
|
#undef L_SIZE
|
|
|
|
#undef L_SC
|
|
|
|
#undef L_LONG_BIT
|
|
|
|
#undef L_BIT_ARR_DEFN
|
|
|
|
|
|
|
|
#ifndef AVL_SEARCH_TYPE_DEFINED_
|
|
|
|
#define AVL_SEARCH_TYPE_DEFINED_
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
typedef enum {
|
|
|
|
AVL_EQUAL = 1,
|
|
|
|
AVL_LESS = 2,
|
|
|
|
AVL_GREATER = 4,
|
|
|
|
AVL_LESS_EQUAL = AVL_EQUAL | AVL_LESS,
|
|
|
|
AVL_GREATER_EQUAL = AVL_EQUAL | AVL_GREATER
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
avl_search_type;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AVL_UNIQUE
|
|
|
|
|
|
|
|
#define L_ AVL_UNIQUE
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define L_(X) X
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Determine storage class for function prototypes. */
|
|
|
|
#ifdef AVL_PRIVATE
|
|
|
|
|
|
|
|
#define L_SC static
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define L_SC extern
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AVL_SIZE
|
|
|
|
|
|
|
|
#define L_SIZE AVL_SIZE
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define L_SIZE unsigned long
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
typedef struct {
|
2010-05-18 17:58:33 +02:00
|
|
|
#ifdef AVL_INSIDE_STRUCT
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
AVL_INSIDE_STRUCT
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
AVL_HANDLE root;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
L_(avl);
|
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
|
|
|
L_SC void L_(init)(L_(avl) *tree);
|
|
|
|
|
|
|
|
L_SC int L_(is_empty)(L_(avl) *tree);
|
|
|
|
|
|
|
|
L_SC AVL_HANDLE L_(insert)(L_(avl) *tree, AVL_HANDLE h);
|
|
|
|
|
|
|
|
L_SC AVL_HANDLE L_(search)(L_(avl) *tree, AVL_KEY k, avl_search_type st);
|
|
|
|
|
|
|
|
L_SC AVL_HANDLE L_(search_least)(L_(avl) *tree);
|
|
|
|
|
|
|
|
L_SC AVL_HANDLE L_(search_greatest)(L_(avl) *tree);
|
|
|
|
|
|
|
|
L_SC AVL_HANDLE L_(remove)(L_(avl) *tree, AVL_KEY k);
|
|
|
|
|
|
|
|
L_SC AVL_HANDLE L_(subst)(L_(avl) *tree, AVL_HANDLE new_node);
|
|
|
|
|
|
|
|
#ifdef AVL_BUILD_ITER_TYPE
|
|
|
|
|
|
|
|
L_SC int L_(build)(
|
2012-07-14 00:21:29 +02:00
|
|
|
L_(avl) *tree, AVL_BUILD_ITER_TYPE p, L_SIZE num_nodes);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ANSI C/ISO C++ require that a long have at least 32 bits. Set
|
|
|
|
** L_EST_LONG_BIT to be the greatest multiple of 8 in the range
|
|
|
|
** 32 - 64 (inclusive) that is less than or equal to the number of
|
|
|
|
** bits in a long.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if (((LONG_MAX >> 31) >> 7) == 0)
|
|
|
|
|
|
|
|
#define L_EST_LONG_BIT 32
|
|
|
|
|
|
|
|
#elif (((LONG_MAX >> 31) >> 15) == 0)
|
|
|
|
|
|
|
|
#define L_EST_LONG_BIT 40
|
|
|
|
|
|
|
|
#elif (((LONG_MAX >> 31) >> 23) == 0)
|
|
|
|
|
|
|
|
#define L_EST_LONG_BIT 48
|
|
|
|
|
|
|
|
#elif (((LONG_MAX >> 31) >> 31) == 0)
|
|
|
|
|
|
|
|
#define L_EST_LONG_BIT 56
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define L_EST_LONG_BIT 64
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Number of bits in a long. */
|
|
|
|
#define L_LONG_BIT (sizeof(long) * CHAR_BIT)
|
|
|
|
|
|
|
|
/* The macro L_BIT_ARR_DEFN defines a bit array whose index is a (0-based)
|
|
|
|
** node depth. The definition depends on whether the maximum depth is more
|
|
|
|
** or less than the number of bits in a single long.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if ((AVL_MAX_DEPTH) > L_EST_LONG_BIT)
|
|
|
|
|
|
|
|
/* Maximum depth may be more than number of bits in a long. */
|
|
|
|
|
|
|
|
#define L_BIT_ARR_DEFN(NAME) \
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned long NAME[((AVL_MAX_DEPTH) + L_LONG_BIT - 1) / L_LONG_BIT];
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* Maximum depth is definitely less than number of bits in a long. */
|
|
|
|
|
|
|
|
#define L_BIT_ARR_DEFN(NAME) unsigned long NAME;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Iterator structure. */
|
2012-07-14 00:21:29 +02:00
|
|
|
typedef struct {
|
|
|
|
/* Tree being iterated over. */
|
|
|
|
L_(avl) *tree_;
|
|
|
|
|
|
|
|
/* Records a path into the tree. If bit n is true, indicates
|
|
|
|
** take greater branch from the nth node in the path, otherwise
|
|
|
|
** take the less branch. bit 0 gives branch from root, and
|
|
|
|
** so on. */
|
|
|
|
L_BIT_ARR_DEFN(branch)
|
|
|
|
|
|
|
|
/* Zero-based depth of path into tree. */
|
|
|
|
unsigned depth;
|
|
|
|
|
|
|
|
/* Handles of nodes in path from root to current node (returned by *). */
|
|
|
|
AVL_HANDLE path_h[(AVL_MAX_DEPTH) - 1];
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
L_(iter);
|
|
|
|
|
|
|
|
/* Iterator function prototypes. */
|
|
|
|
|
|
|
|
L_SC void L_(start_iter)(
|
2012-07-14 00:21:29 +02:00
|
|
|
L_(avl) *tree, L_(iter) *iter, AVL_KEY k, avl_search_type st);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
L_SC void L_(start_iter_least)(L_(avl) *tree, L_(iter) *iter);
|
|
|
|
|
|
|
|
L_SC void L_(start_iter_greatest)(L_(avl) *tree, L_(iter) *iter);
|
|
|
|
|
|
|
|
L_SC AVL_HANDLE L_(get_iter)(L_(iter) *iter);
|
|
|
|
|
|
|
|
L_SC void L_(incr_iter)(L_(iter) *iter);
|
|
|
|
|
|
|
|
L_SC void L_(decr_iter)(L_(iter) *iter);
|
|
|
|
|
|
|
|
L_SC void L_(init_iter)(L_(iter) *iter);
|
|
|
|
|
|
|
|
#define AVL_IMPL_INIT 1
|
|
|
|
#define AVL_IMPL_IS_EMPTY (1 << 1)
|
|
|
|
#define AVL_IMPL_INSERT (1 << 2)
|
|
|
|
#define AVL_IMPL_SEARCH (1 << 3)
|
|
|
|
#define AVL_IMPL_SEARCH_LEAST (1 << 4)
|
|
|
|
#define AVL_IMPL_SEARCH_GREATEST (1 << 5)
|
|
|
|
#define AVL_IMPL_REMOVE (1 << 6)
|
|
|
|
#define AVL_IMPL_BUILD (1 << 7)
|
|
|
|
#define AVL_IMPL_START_ITER (1 << 8)
|
|
|
|
#define AVL_IMPL_START_ITER_LEAST (1 << 9)
|
|
|
|
#define AVL_IMPL_START_ITER_GREATEST (1 << 10)
|
|
|
|
#define AVL_IMPL_GET_ITER (1 << 11)
|
|
|
|
#define AVL_IMPL_INCR_ITER (1 << 12)
|
|
|
|
#define AVL_IMPL_DECR_ITER (1 << 13)
|
|
|
|
#define AVL_IMPL_INIT_ITER (1 << 14)
|
|
|
|
#define AVL_IMPL_SUBST (1 << 15)
|
|
|
|
|
|
|
|
#define AVL_IMPL_ALL (~0)
|
|
|
|
|
|
|
|
#undef L_
|
|
|
|
#undef L_EST_LONG_BIT
|
|
|
|
#undef L_SIZE
|
|
|
|
#undef L_SC
|
|
|
|
#undef L_LONG_BIT
|
|
|
|
#undef L_BIT_ARR_DEFN
|
2013-12-16 03:32:02 +01:00
|
|
|
|
|
|
|
#endif // VPX_MEM_MEMORY_MANAGER_INCLUDE_CAVL_IF_H_
|