1848b8605Smrg/*
2848b8605Smrg * Copyright 2013 Marek Olšák <maraeo@gmail.com>
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
8848b8605Smrg * license, and/or sell copies of the Software, and to permit persons to whom
9848b8605Smrg * the Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18848b8605Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19848b8605Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20848b8605Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21848b8605Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22848b8605Smrg
23848b8605Smrg/**
24848b8605Smrg * @file
25848b8605Smrg * 1D integer range, capable of the union and intersection operations.
26848b8605Smrg *
27848b8605Smrg * It only maintains a single interval which is extended when the union is
28848b8605Smrg * done. This implementation is partially thread-safe (readers are not
29848b8605Smrg * protected by a lock).
30848b8605Smrg *
31848b8605Smrg * @author Marek Olšák
32848b8605Smrg */
33848b8605Smrg
34848b8605Smrg#ifndef U_RANGE_H
35848b8605Smrg#define U_RANGE_H
36848b8605Smrg
37848b8605Smrg#include "os/os_thread.h"
38848b8605Smrg
39848b8605Smrg#include "util/u_math.h"
40848b8605Smrg
41848b8605Smrgstruct util_range {
42848b8605Smrg   unsigned start; /* inclusive */
43848b8605Smrg   unsigned end; /* exclusive */
44848b8605Smrg
45848b8605Smrg   /* for the range to be consistent with multiple contexts: */
46b8e80941Smrg   mtx_t write_mutex;
47848b8605Smrg};
48848b8605Smrg
49848b8605Smrg
50b8e80941Smrgstatic inline void
51848b8605Smrgutil_range_set_empty(struct util_range *range)
52848b8605Smrg{
53848b8605Smrg   range->start = ~0;
54848b8605Smrg   range->end = 0;
55848b8605Smrg}
56848b8605Smrg
57848b8605Smrg/* This is like a union of two sets. */
58b8e80941Smrgstatic inline void
59848b8605Smrgutil_range_add(struct util_range *range, unsigned start, unsigned end)
60848b8605Smrg{
61848b8605Smrg   if (start < range->start || end > range->end) {
62b8e80941Smrg      mtx_lock(&range->write_mutex);
63848b8605Smrg      range->start = MIN2(start, range->start);
64848b8605Smrg      range->end = MAX2(end, range->end);
65b8e80941Smrg      mtx_unlock(&range->write_mutex);
66848b8605Smrg   }
67848b8605Smrg}
68848b8605Smrg
69b8e80941Smrgstatic inline boolean
70b8e80941Smrgutil_ranges_intersect(const struct util_range *range,
71b8e80941Smrg                      unsigned start, unsigned end)
72848b8605Smrg{
73848b8605Smrg   return MAX2(start, range->start) < MIN2(end, range->end);
74848b8605Smrg}
75848b8605Smrg
76848b8605Smrg
77848b8605Smrg/* Init/deinit */
78848b8605Smrg
79b8e80941Smrgstatic inline void
80848b8605Smrgutil_range_init(struct util_range *range)
81848b8605Smrg{
82b8e80941Smrg   (void) mtx_init(&range->write_mutex, mtx_plain);
83848b8605Smrg   util_range_set_empty(range);
84848b8605Smrg}
85848b8605Smrg
86b8e80941Smrgstatic inline void
87848b8605Smrgutil_range_destroy(struct util_range *range)
88848b8605Smrg{
89b8e80941Smrg   mtx_destroy(&range->write_mutex);
90848b8605Smrg}
91848b8605Smrg
92848b8605Smrg#endif
93