Rizin
unix-like reverse engineering framework and cli tools
rz_intervaltree.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2019 thestr4ng3r <info@florianmaerkl.de>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #ifndef RZ_INTERVALTREE_H
5 #define RZ_INTERVALTREE_H
6 
7 #include "rz_rbtree.h"
8 #include "../rz_types.h"
9 
10 /*
11  * RzIntervalTree is a special RBTree (augmented red-black tree)
12  * that holds its entries, each associated with a interval,
13  * ordered by the start of the interval.
14  *
15  * It allows efficient lookup for intersections with a given interval or value.
16  * This is achieved by, at each node, saving the maximum value of the node
17  * and all of its children.
18  *
19  * It can hold multiple entries with the same start or end.
20  * For multiple entries with the same start, the ordering is undefined.
21  */
22 
23 typedef struct rz_interval_node_t {
25  ut64 start; // inclusive, key of the node
26  ut64 end; // may be inclusive or exclusive, this is only determined by how they are queried
27  ut64 max_end; // augmented value, maximum end of this node and all of its children
28  void *data;
30 
31 typedef void (*RzIntervalNodeFree)(void *data);
32 
33 typedef struct rz_interval_tree_t {
37 
40 
41 // return false if the insertion failed.
43 
44 // Removes a given node from the tree. The node will be freed.
45 // If free is true, the data in the node is freed as well.
46 // false if the removal failed
47 // Complexity is O(log(n) + m) if there are m nodes with the same start as the given node.
49 
50 // Change start/end of a given node.
51 // It is more efficient if only the end changed.
52 // The RzIntervalNode pointer is INVALID after this operation!
53 // Complexity is O(log(n) + m) if there are m nodes with the same start as the given node.
54 RZ_API bool rz_interval_tree_resize(RzIntervalTree *tree, RzIntervalNode *node, ut64 new_start, ut64 new_end);
55 
56 // Returns an iterator that starts at the leftmost node that has the given start
57 // Iterating over it will yield all nodes with given start, then all with a higher one.
59 
60 // Returns a node that starts at exactly start or NULL
62 
63 // Returns a node that starts at exactly start and contains data or NULL
65 
66 // Same as rz_interval_tree_node_at, but directly returns the contained value or NULL
67 static inline void *rz_interval_tree_at(RzIntervalTree *tree, ut64 start) {
69  return node ? node->data : NULL;
70 }
71 
72 typedef bool (*RzIntervalIterCb)(RzIntervalNode *node, void *user);
73 
74 // Call cb for all entries starting at exactly start
76 
77 // Call cb for all entries whose intervals contain value
78 // end_inclusive if true, all start/end values are considered inclusive/inclusive, else inclusive/exclusive
79 RZ_API bool rz_interval_tree_all_in(RzIntervalTree *tree, ut64 value, bool end_inclusive, RzIntervalIterCb cb, void *user);
80 
81 // Call cb for all entries whose intervals intersect the given interval (might not contain it completely)
82 // end_inclusive if true, all start/end values are considered inclusive/inclusive, else inclusive/exclusive
83 RZ_API bool rz_interval_tree_all_intersect(RzIntervalTree *tree, ut64 start, ut64 end, bool end_inclusive, RzIntervalIterCb cb, void *user);
84 
86 
88  return rz_rbtree_iter_get(it, RzIntervalNode, node);
89 }
90 
91 static inline bool rz_interval_tree_empty(RzIntervalTree *tree) {
92  return tree->root == NULL;
93 }
94 
95 #define rz_interval_tree_foreach(tree, it, dat) \
96  for ((it) = rz_rbtree_first(&(tree)->root->node); rz_rbtree_iter_has(&it) && (dat = rz_interval_tree_iter_get(&it)->data); rz_rbtree_iter_next(&(it)))
97 
98 #define rz_interval_tree_foreach_prev(tree, it, dat) \
99  for ((it) = rz_rbtree_last(&(tree)->root->node); rz_rbtree_iter_has(&it) && (dat = rz_rbtree_iter_get(&it, RzIntervalNode, node)->data); rz_rbtree_iter_prev(&(it)))
100 
101 #endif // RZ_INTERVALTREE_H
static int value
Definition: cmd_api.c:93
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API bool rz_interval_tree_all_intersect(RzIntervalTree *tree, ut64 start, ut64 end, bool end_inclusive, RzIntervalIterCb cb, void *user)
Definition: intervaltree.c:291
static void * rz_interval_tree_at(RzIntervalTree *tree, ut64 start)
RZ_API bool rz_interval_tree_delete(RzIntervalTree *tree, RzIntervalNode *node, bool free)
Definition: intervaltree.c:139
RBIter RzIntervalTreeIter
static bool rz_interval_tree_empty(RzIntervalTree *tree)
RZ_API RzIntervalNode * rz_interval_tree_node_at(RzIntervalTree *tree, ut64 start)
Definition: intervaltree.c:169
RZ_API bool rz_interval_tree_all_in(RzIntervalTree *tree, ut64 value, bool end_inclusive, RzIntervalIterCb cb, void *user)
Definition: intervaltree.c:262
RZ_API RzIntervalNode * rz_interval_tree_node_at_data(RzIntervalTree *tree, ut64 start, void *data)
Definition: intervaltree.c:206
RZ_API bool rz_interval_tree_resize(RzIntervalTree *tree, RzIntervalNode *node, ut64 new_start, ut64 new_end)
Definition: intervaltree.c:147
RZ_API bool rz_interval_tree_all_at(RzIntervalTree *tree, ut64 start, RzIntervalIterCb cb, void *user)
Definition: intervaltree.c:221
RZ_API void rz_interval_tree_init(RzIntervalTree *tree, RzIntervalNodeFree free)
Definition: intervaltree.c:101
struct rz_interval_tree_t RzIntervalTree
void(* RzIntervalNodeFree)(void *data)
static RzIntervalNode * rz_interval_tree_iter_get(RzIntervalTreeIter *it)
RZ_API bool rz_interval_tree_insert(RzIntervalTree *tree, ut64 start, ut64 end, void *data)
Definition: intervaltree.c:121
RZ_API RBIter rz_interval_tree_first_at(RzIntervalTree *tree, ut64 start)
Definition: intervaltree.c:183
struct rz_interval_node_t RzIntervalNode
RZ_API void rz_interval_tree_fini(RzIntervalTree *tree)
Definition: intervaltree.c:114
bool(* RzIntervalIterCb)(RzIntervalNode *node, void *user)
#define rz_rbtree_iter_get(it, struc, rb)
Definition: rz_rbtree.h:85
RzIntervalNodeFree free
RzIntervalNode * root
#define bool
Definition: sysdefs.h:146
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const char * cb[]
Definition: z80_tab.h:176