1 /*
2 * ngtcp2
3 *
4 * Copyright (c) 2017 ngtcp2 contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "ngtcp2_range.h"
26 #include "ngtcp2_macro.h"
27
ngtcp2_range_init(ngtcp2_range * r,uint64_t begin,uint64_t end)28 void ngtcp2_range_init(ngtcp2_range *r, uint64_t begin, uint64_t end) {
29 r->begin = begin;
30 r->end = end;
31 }
32
ngtcp2_range_intersect(const ngtcp2_range * a,const ngtcp2_range * b)33 ngtcp2_range ngtcp2_range_intersect(const ngtcp2_range *a,
34 const ngtcp2_range *b) {
35 ngtcp2_range r = {0, 0};
36 uint64_t begin = ngtcp2_max(a->begin, b->begin);
37 uint64_t end = ngtcp2_min(a->end, b->end);
38 if (begin < end) {
39 ngtcp2_range_init(&r, begin, end);
40 }
41 return r;
42 }
43
ngtcp2_range_len(const ngtcp2_range * r)44 uint64_t ngtcp2_range_len(const ngtcp2_range *r) { return r->end - r->begin; }
45
ngtcp2_range_eq(const ngtcp2_range * a,const ngtcp2_range * b)46 int ngtcp2_range_eq(const ngtcp2_range *a, const ngtcp2_range *b) {
47 return a->begin == b->begin && a->end == b->end;
48 }
49
ngtcp2_range_cut(ngtcp2_range * left,ngtcp2_range * right,const ngtcp2_range * a,const ngtcp2_range * b)50 void ngtcp2_range_cut(ngtcp2_range *left, ngtcp2_range *right,
51 const ngtcp2_range *a, const ngtcp2_range *b) {
52 /* Assume that b is included in a */
53 left->begin = a->begin;
54 left->end = b->begin;
55 right->begin = b->end;
56 right->end = a->end;
57 }
58
ngtcp2_range_not_after(const ngtcp2_range * a,const ngtcp2_range * b)59 int ngtcp2_range_not_after(const ngtcp2_range *a, const ngtcp2_range *b) {
60 return a->end <= b->end;
61 }
62