ngtcp2_range.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. void ngtcp2_range_init(ngtcp2_range *r, uint64_t begin, uint64_t end) {
  28. r->begin = begin;
  29. r->end = end;
  30. }
  31. ngtcp2_range ngtcp2_range_intersect(const ngtcp2_range *a,
  32. const ngtcp2_range *b) {
  33. ngtcp2_range r = {0, 0};
  34. uint64_t begin = ngtcp2_max_uint64(a->begin, b->begin);
  35. uint64_t end = ngtcp2_min_uint64(a->end, b->end);
  36. if (begin < end) {
  37. ngtcp2_range_init(&r, begin, end);
  38. }
  39. return r;
  40. }
  41. uint64_t ngtcp2_range_len(const ngtcp2_range *r) { return r->end - r->begin; }
  42. int ngtcp2_range_eq(const ngtcp2_range *a, const ngtcp2_range *b) {
  43. return a->begin == b->begin && a->end == b->end;
  44. }
  45. void ngtcp2_range_cut(ngtcp2_range *left, ngtcp2_range *right,
  46. const ngtcp2_range *a, const ngtcp2_range *b) {
  47. /* Assume that b is included in a */
  48. left->begin = a->begin;
  49. left->end = b->begin;
  50. right->begin = b->end;
  51. right->end = a->end;
  52. }
  53. int ngtcp2_range_not_after(const ngtcp2_range *a, const ngtcp2_range *b) {
  54. return a->end <= b->end;
  55. }