/usr/include/ncbi-vdb/klib/pbstree.h is in libncbi-vdb-dev 2.8.1+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
*/
#ifndef _h_klib_pbstree_
#define _h_klib_pbstree_
#ifndef _h_klib_extern_
#include <klib/extern.h>
#endif
#ifndef _h_klib_defs_
#include <klib/defs.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*--------------------------------------------------------------------------
* forwards
*/
struct BSTree;
/*--------------------------------------------------------------------------
* PBSTNode
* identifies a node within persisted binary search tree
*
* a BSTree will contain BSTNodes, which themselves are intrusive but
* irrelevant internal tree links plus an externally defined data
* structure, having both key and value, and supporting multiple
* keys per node. the internal links permit navigation from node
* to node that is not possible with the PBSTNode.
*
* a PBSTree does not contain structured nodes, but stores linkage
* and ordering information separately from the externally defined
* data, and rather than using pointers, assigns integer ids to the
* nodes themselves. navigation by pointers would require allocations,
* which are unnecessary and expensive when traversing a read-only
* persisted image. navigation is therefore intrusive on an externally
* allocated node structure.
*/
typedef struct PBSTNode PBSTNode;
struct PBSTNode
{
struct
{
const void *addr;
size_t size;
} data;
const void *internal;
uint32_t id;
};
/* PBSTNodeNext
* updates the structure
* returns next 1-based node id or 0 for NULL
*/
KLIB_EXTERN uint32_t CC PBSTNodeNext ( PBSTNode *self );
/* PBSTNodePrev
* updates the structure
* returns prev 1-based node id or 0 for NULL
*/
KLIB_EXTERN uint32_t CC PBSTNodePrev ( PBSTNode *self );
/* PBSTNodeFindNext
* find next element satisfying criteria
* fills out "n" if found
* returns 1-based node id or 0 for NULL
*/
KLIB_EXTERN uint32_t CC PBSTNodeFindNext ( PBSTNode *self,
bool ( CC * f ) ( const PBSTNode *n ) );
/* PBSTNodeFindPrev
* find previous element satisfying criteria
* fills out "n" if found
* returns 1-based node id or 0 for NULL
*/
KLIB_EXTERN uint32_t CC PBSTNodeFindPrev ( PBSTNode *self,
bool ( CC * f ) ( const PBSTNode *n ) );
/*--------------------------------------------------------------------------
* PBSTree
* a flattened binary search tree
*
* mimics read-only behavior of a BSTree
*/
typedef struct PBSTree PBSTree;
/* PBSTreeMake
* make a PBSTree structure
*
* "mem" [ IN ] - constant memory image of persisted tree
* with a lifetime exceeding that of the PBSTree itself
*
* "byteswap" [ IN ] - if true, the persisted image needs
* to be read with byteswapping
*/
KLIB_EXTERN rc_t CC PBSTreeMake ( PBSTree **pt, const void *addr, size_t size, bool byteswap );
/* PBSTreeCount
* returns number of elements in tree
* not included within the BSTree interface itself, but
* was included here due to the fact that it is constant
*
* return value:
* integer value >= 0
*/
KLIB_EXTERN uint32_t CC PBSTreeCount ( const PBSTree *self );
/* PBSTreeDepth
* returns number of layers in tree
*
* return value:
* integer value >= 0
*/
KLIB_EXTERN uint32_t CC PBSTreeDepth ( const PBSTree *self );
/* PBSTreeSize
* returns the size in bytes
* of the PBSTree image
*/
KLIB_EXTERN size_t CC PBSTreeSize ( const PBSTree *self );
/* PBSTreeGetNode
* gets a PBSTNode from an id
*
* "node" [ OUT ] - return parameter for node
*
* "id" [ IN ] - a 1-based integer node id
*
* return values:
* EINVAL => an invalid parameter was passed
* ENOENT => id out of range
*/
KLIB_EXTERN rc_t CC PBSTreeGetNode ( const PBSTree *self, PBSTNode *node, uint32_t id );
/* PBSTreeFind
* find an object within tree
*
* "rtn" [ OUT ] - return parameter for node if found. its value
* is undefined unless the function returns success.
*
* "item" [ IN ] - item to be matched against a node. will be
* supplied as the first parameter to the comparison function.
*
* "cmp" [ IN ] - function that evaluates "item" against each internal
* node for relative position, returning 0 for match, negative when
* "item" is to left of node, and positive otherwise.
*
* return value:
* 0 => not found
* 1..n => internal id of node, also recorded within "rtn"
*/
KLIB_EXTERN uint32_t CC PBSTreeFind ( const PBSTree *self, PBSTNode *rtn,
const void *item, int ( CC * cmp ) ( const void *item, const PBSTNode *n , void * data), void * data );
/* PBSTreeForEach
* executes a function on each tree element
*
* "reverse" [ IN ] - if true, traverse from last to first element,
* if false, traverse in the normal forward direction.
*
* "f" [ IN ] and "data" [ IN ] - callback function for evaluating each
* node within the tree. the passed out node structure is itself fully
* modifiable.
*/
KLIB_EXTERN void CC PBSTreeForEach ( const PBSTree *self, bool reverse,
void ( CC * f ) ( PBSTNode *n, void *data ), void *data );
/* PBSTreeDoUntil
* executes a function on each element
* until the function returns true
*
* "reverse" [ IN ] - if true, traverse from last to first element,
* if false, traverse in the normal forward direction.
*
* "f" [ IN ] and "data" [ IN ] - callback function for evaluating each
* node within the tree. the passed out node structure is itself fully
* modifiable. the function returns "true" to halt iteration.
*
* return values:
* the last value returned by "f" or false if never invoked
*/
KLIB_EXTERN bool CC PBSTreeDoUntil ( const PBSTree *self, bool reverse,
bool ( CC * f ) ( PBSTNode *n, void *data ), void *data );
/* PBSTreeWhack
* whacks PBSTree object
* the constant memory image used to create the PBSTree may now be released
*/
KLIB_EXTERN void CC PBSTreeWhack ( PBSTree *self );
/*--------------------------------------------------------------------------
* persistence functions
*/
/* PTWriteFunc
* a generic streaming function
*/
typedef rc_t ( CC * PTWriteFunc )
( void *param, const void *buffer, size_t bytes, size_t *num_writ );
/* PTAuxFunc
* a function to measure or write auxiliary node data
* where "node" is a BSTNode or TTNode.
*
* when "write" is NULL, the number of bytes that would
* be written is returned in "num_writ".
*/
typedef rc_t ( CC * PTAuxFunc )
( void *param, const void *node, size_t *num_writ,
PTWriteFunc write, void *write_param );
/*--------------------------------------------------------------------------
* BSTree
*/
/* BSTreePersist
* write a binary search tree to some storage location
*
* the tree is persisted by making between one and three passes
* over its nodes, see description of "write" parameter.
*
* the first pass examines internal tree structure and invokes
* a user-supplied function to determine overall size.
*
* the second pass persists the internal structure in a packed
* format, using the user-supplied generic "write" function.
*
* the third pass invokes another user-supplied function to write
* auxiliary node data to output.
*
* "num_writ" [ OUT, NULL OKAY ] - returns parameter for the number
* of bytes written as a result of persisting the tree. this will
* be the actual bytes written regardless of return status.
*
* "write" [ IN, NULL OKAY ] and "write_param" [ IN ] - a generic
* output streaming function used for all operations. if NULL, then
* the function will exit after its first pass with the number of
* bytes required in "num_writ".
*
* "aux" [ IN ] and "aux_param" [ IN ] - a specialized function for
* streaming auxiliary node data to output using the supplied "write"
* function. it is invoked during the first pass with a NULL write
* function for gathering size data, and during the third pass with
* a non-NULL write function.
*/
KLIB_EXTERN rc_t CC BSTreePersist ( struct BSTree const *self, size_t *num_writ,
PTWriteFunc write, void *write_param, PTAuxFunc aux, void *aux_param );
#ifdef __cplusplus
}
#endif
#endif /* _h_klib_pbstree_ */
|