Home | History | Annotate | Line # | Download | only in linux
interval_tree.h revision 1.8.4.2
      1  1.8.4.2  christos /*	$NetBSD: interval_tree.h,v 1.8.4.2 2019/06/10 22:08:31 christos Exp $	*/
      2  1.8.4.2  christos 
      3  1.8.4.2  christos /*-
      4  1.8.4.2  christos  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.8.4.2  christos  * All rights reserved.
      6  1.8.4.2  christos  *
      7  1.8.4.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8.4.2  christos  * by Taylor R. Campbell.
      9  1.8.4.2  christos  *
     10  1.8.4.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.8.4.2  christos  * modification, are permitted provided that the following conditions
     12  1.8.4.2  christos  * are met:
     13  1.8.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.8.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.8.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.8.4.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.8.4.2  christos  *
     19  1.8.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.8.4.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.8.4.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.8.4.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.8.4.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.8.4.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.8.4.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.8.4.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.8.4.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.8.4.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.8.4.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.8.4.2  christos  */
     31  1.8.4.2  christos 
     32  1.8.4.2  christos #ifndef	_LINUX_INTERVAL_TREE_H_
     33  1.8.4.2  christos #define	_LINUX_INTERVAL_TREE_H_
     34  1.8.4.2  christos 
     35  1.8.4.2  christos #include <sys/rbtree.h>
     36  1.8.4.2  christos 
     37  1.8.4.2  christos struct rb_root {
     38  1.8.4.2  christos 	struct rb_tree	rbr_tree;
     39  1.8.4.2  christos };
     40  1.8.4.2  christos 
     41  1.8.4.2  christos static inline bool
     42  1.8.4.2  christos RB_EMPTY_ROOT(struct rb_root *root)
     43  1.8.4.2  christos {
     44  1.8.4.2  christos 
     45  1.8.4.2  christos 	return RB_TREE_MIN(&root->rbr_tree) == NULL;
     46  1.8.4.2  christos }
     47  1.8.4.2  christos 
     48  1.8.4.2  christos struct interval_tree_node {
     49  1.8.4.2  christos 	struct rb_node	itn_node;
     50  1.8.4.2  christos 	unsigned long	start;	/* inclusive */
     51  1.8.4.2  christos 	unsigned long	last;	/* inclusive */
     52  1.8.4.2  christos };
     53  1.8.4.2  christos 
     54  1.8.4.2  christos static inline int
     55  1.8.4.2  christos interval_tree_compare_nodes(void *cookie, const void *va, const void *vb)
     56  1.8.4.2  christos {
     57  1.8.4.2  christos 	const struct interval_tree_node *na = va;
     58  1.8.4.2  christos 	const struct interval_tree_node *nb = vb;
     59  1.8.4.2  christos 
     60  1.8.4.2  christos 	if (na->start < nb->start)
     61  1.8.4.2  christos 		return -1;
     62  1.8.4.2  christos 	if (na->start > nb->start)
     63  1.8.4.2  christos 		return +1;
     64  1.8.4.2  christos 	if (na->last < nb->last)
     65  1.8.4.2  christos 		return -1;
     66  1.8.4.2  christos 	if (na->last > nb->last)
     67  1.8.4.2  christos 		return +1;
     68  1.8.4.2  christos 	return 0;
     69  1.8.4.2  christos }
     70  1.8.4.2  christos 
     71  1.8.4.2  christos static inline int
     72  1.8.4.2  christos interval_tree_compare_key(void *cookie, const void *vn, const void *vk)
     73  1.8.4.2  christos {
     74  1.8.4.2  christos 	const struct interval_tree_node *n = vn;
     75  1.8.4.2  christos 	const unsigned long *k = vk;
     76  1.8.4.2  christos 
     77  1.8.4.2  christos 	if (n->start < *k)
     78  1.8.4.2  christos 		return -1;
     79  1.8.4.2  christos 	if (*k < n->start)
     80  1.8.4.2  christos 		return +1;
     81  1.8.4.2  christos 	return 0;
     82  1.8.4.2  christos }
     83  1.8.4.2  christos 
     84  1.8.4.2  christos static const rb_tree_ops_t interval_tree_ops = {
     85  1.8.4.2  christos 	.rbto_compare_nodes = interval_tree_compare_nodes,
     86  1.8.4.2  christos 	.rbto_compare_key = interval_tree_compare_key,
     87  1.8.4.2  christos 	.rbto_node_offset = offsetof(struct interval_tree_node, itn_node),
     88  1.8.4.2  christos };
     89  1.8.4.2  christos 
     90  1.8.4.2  christos static inline void
     91  1.8.4.2  christos interval_tree_init(struct rb_root *root)
     92  1.8.4.2  christos {
     93  1.8.4.2  christos 
     94  1.8.4.2  christos 	rb_tree_init(&root->rbr_tree, &interval_tree_ops);
     95  1.8.4.2  christos }
     96  1.8.4.2  christos 
     97  1.8.4.2  christos static inline void
     98  1.8.4.2  christos interval_tree_insert(struct interval_tree_node *node, struct rb_root *root)
     99  1.8.4.2  christos {
    100  1.8.4.2  christos 	struct interval_tree_node *collision __diagused;
    101  1.8.4.2  christos 
    102  1.8.4.2  christos 	collision = rb_tree_insert_node(&root->rbr_tree, node);
    103  1.8.4.2  christos 	KASSERT(collision == node);
    104  1.8.4.2  christos }
    105  1.8.4.2  christos 
    106  1.8.4.2  christos static inline void
    107  1.8.4.2  christos interval_tree_remove(struct interval_tree_node *node, struct rb_root *root)
    108  1.8.4.2  christos {
    109  1.8.4.2  christos 
    110  1.8.4.2  christos 	rb_tree_remove_node(&root->rbr_tree, node);
    111  1.8.4.2  christos }
    112  1.8.4.2  christos 
    113  1.8.4.2  christos static inline struct interval_tree_node *
    114  1.8.4.2  christos interval_tree_iter_first(struct rb_root *root, unsigned long start,
    115  1.8.4.2  christos     unsigned long last)
    116  1.8.4.2  christos {
    117  1.8.4.2  christos 	struct interval_tree_node *node;
    118  1.8.4.2  christos 
    119  1.8.4.2  christos 	node = rb_tree_find_node_geq(&root->rbr_tree, &start);
    120  1.8.4.2  christos 	if (node == NULL)
    121  1.8.4.2  christos 		return NULL;
    122  1.8.4.2  christos 	if (last < node->start)
    123  1.8.4.2  christos 		return NULL;
    124  1.8.4.2  christos 	KASSERT(node->start <= last && node->last >= start);
    125  1.8.4.2  christos 
    126  1.8.4.2  christos 	return node;
    127  1.8.4.2  christos }
    128  1.8.4.2  christos 
    129  1.8.4.2  christos /*
    130  1.8.4.2  christos  * XXX Linux's interval_tree_iter_next doesn't take the root as an
    131  1.8.4.2  christos  * argument, which makes this difficult.  So we'll just patch those
    132  1.8.4.2  christos  * uses.
    133  1.8.4.2  christos  */
    134  1.8.4.2  christos static inline struct interval_tree_node *
    135  1.8.4.2  christos interval_tree_iter_next(struct rb_root *root, struct interval_tree_node *node,
    136  1.8.4.2  christos     unsigned long start, unsigned long last)
    137  1.8.4.2  christos {
    138  1.8.4.2  christos 	struct interval_tree_node *next;
    139  1.8.4.2  christos 
    140  1.8.4.2  christos 	KASSERT(node != NULL);
    141  1.8.4.2  christos 	next = rb_tree_iterate(&root->rbr_tree, node, RB_DIR_RIGHT);
    142  1.8.4.2  christos 	if (next == NULL)
    143  1.8.4.2  christos 		return NULL;
    144  1.8.4.2  christos 	if (last < next->start)
    145  1.8.4.2  christos 		return NULL;
    146  1.8.4.2  christos 	KASSERT(next->start <= last && next->last >= start);
    147  1.8.4.2  christos 
    148  1.8.4.2  christos 	return next;
    149  1.8.4.2  christos }
    150  1.8.4.2  christos 
    151  1.8.4.2  christos /*
    152  1.8.4.2  christos  * XXX This is not actually postorder, but I can't fathom why you would
    153  1.8.4.2  christos  * want postorder for an ordered tree; different insertion orders lead
    154  1.8.4.2  christos  * to different traversal orders.
    155  1.8.4.2  christos  */
    156  1.8.4.2  christos #define	rbtree_postorder_for_each_entry_safe(NODE, TMP, ROOT, FIELD)	      \
    157  1.8.4.2  christos 	for ((NODE) = RB_TREE_MIN(&(ROOT)->rbr_tree);			      \
    158  1.8.4.2  christos 		((NODE) != NULL &&					      \
    159  1.8.4.2  christos 		    ((TMP) = rb_tree_iterate(&(ROOT)->rbr_tree, (NODE),	      \
    160  1.8.4.2  christos 			RB_DIR_RIGHT)));				      \
    161  1.8.4.2  christos 		(NODE) = (TMP))
    162  1.8.4.2  christos 
    163  1.8.4.2  christos #endif	/* _LINUX_INTERVAL_TREE_H_ */
    164