2018-05-11 08:10:01 +03:00
// Copyright (C) 2018 Red Hat, Inc. All rights reserved.
//
// This file is part of LVM2.
//
// This copyrighted material is made available to anyone wishing to use,
// modify, copy, or redistribute it subject to the terms and conditions
// of the GNU Lesser General Public License v.2.1.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# ifndef BASE_DATA_STRUCT_RADIX_TREE_H
# define BASE_DATA_STRUCT_RADIX_TREE_H
# include <stdbool.h>
# include <stdint.h>
2018-06-20 12:04:59 +03:00
# include <stdio.h>
2018-05-11 08:10:01 +03:00
//----------------------------------------------------------------
struct radix_tree ;
union radix_value {
void * ptr ;
uint64_t n ;
} ;
typedef void ( * radix_value_dtr ) ( void * context , union radix_value v ) ;
2018-05-29 13:03:10 +03:00
// dtr will be called on any deleted entries. dtr may be NULL.
struct radix_tree * radix_tree_create ( radix_value_dtr dtr , void * dtr_context ) ;
void radix_tree_destroy ( struct radix_tree * rt ) ;
2018-05-11 08:10:01 +03:00
unsigned radix_tree_size ( struct radix_tree * rt ) ;
2024-05-31 22:46:19 +03:00
bool radix_tree_insert ( struct radix_tree * rt , const void * key , size_t keylen , union radix_value v ) ;
bool radix_tree_remove ( struct radix_tree * rt , const void * key , size_t keylen ) ;
2018-05-29 15:25:59 +03:00
// Returns the number of values removed
2024-05-31 22:46:19 +03:00
unsigned radix_tree_remove_prefix ( struct radix_tree * rt , const void * prefix , size_t prefix_len ) ;
2018-05-29 15:25:59 +03:00
2024-05-31 22:46:19 +03:00
bool radix_tree_lookup ( struct radix_tree * rt , const void * key , size_t keylen ,
union radix_value * result ) ;
2018-05-11 08:10:01 +03:00
2018-05-29 19:58:58 +03:00
// The radix tree stores entries in lexicographical order. Which means
// we can iterate entries, in order. Or iterate entries with a particular
// prefix.
struct radix_tree_iterator {
2024-05-31 17:21:24 +03:00
// Returns false if the iteration should end.
2018-05-29 19:58:58 +03:00
bool ( * visit ) ( struct radix_tree_iterator * it ,
2024-05-31 22:46:19 +03:00
const void * key , size_t keylen , union radix_value v ) ;
2018-05-29 19:58:58 +03:00
} ;
2024-05-31 22:46:19 +03:00
void radix_tree_iterate ( struct radix_tree * rt , const void * key , size_t keylen ,
2024-05-31 17:21:24 +03:00
struct radix_tree_iterator * it ) ;
2018-05-29 19:58:58 +03:00
2024-06-02 15:11:37 +03:00
// Alternative traversing radix_tree.
// Builds whole set all radix_tree nr_values values.
// After use, free(values).
bool radix_tree_values ( struct radix_tree * rt , const void * key , size_t keylen ,
union radix_value * * values , unsigned * nr_values ) ;
2018-06-19 12:19:06 +03:00
// Checks that some constraints on the shape of the tree are
// being held. For debug only.
bool radix_tree_is_well_formed ( struct radix_tree * rt ) ;
2018-06-20 12:04:59 +03:00
void radix_tree_dump ( struct radix_tree * rt , FILE * out ) ;
2018-06-19 12:19:06 +03:00
2024-06-01 20:30:33 +03:00
// Shortcut for ptr value return
// Note: if value would be NULL, it's same result for not/found case.
static inline void * radix_tree_lookup_ptr ( struct radix_tree * rt , const void * key , size_t keylen )
{
union radix_value v ;
return radix_tree_lookup ( rt , key , keylen , & v ) ? v . ptr : NULL ;
}
static inline bool radix_tree_insert_ptr ( struct radix_tree * rt , const void * key , size_t keylen , void * ptr )
{
union radix_value v = { . ptr = ptr } ;
return radix_tree_insert ( rt , key , keylen , v ) ;
}
2018-05-11 08:10:01 +03:00
//----------------------------------------------------------------
# endif