1854c3394SAlexey Lapshin //===- AddressRanges.cpp ----------------------------------------*- C++ -*-===//
2854c3394SAlexey Lapshin //
3854c3394SAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4854c3394SAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
5854c3394SAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6854c3394SAlexey Lapshin //
7854c3394SAlexey Lapshin //===----------------------------------------------------------------------===//
8854c3394SAlexey Lapshin
9854c3394SAlexey Lapshin #include "llvm/ADT/AddressRanges.h"
10854c3394SAlexey Lapshin #include "llvm/ADT/STLExtras.h"
11854c3394SAlexey Lapshin #include <inttypes.h>
12854c3394SAlexey Lapshin
13854c3394SAlexey Lapshin using namespace llvm;
14854c3394SAlexey Lapshin
15*8bb4451aSAlexey Lapshin AddressRanges::Collection::const_iterator
insert(AddressRange Range)16*8bb4451aSAlexey Lapshin AddressRanges::insert(AddressRange Range) {
17854c3394SAlexey Lapshin if (Range.size() == 0)
18*8bb4451aSAlexey Lapshin return Ranges.end();
19854c3394SAlexey Lapshin
20854c3394SAlexey Lapshin auto It = llvm::upper_bound(Ranges, Range);
21854c3394SAlexey Lapshin auto It2 = It;
22*8bb4451aSAlexey Lapshin while (It2 != Ranges.end() && It2->start() <= Range.end())
23854c3394SAlexey Lapshin ++It2;
24854c3394SAlexey Lapshin if (It != It2) {
25*8bb4451aSAlexey Lapshin Range = {Range.start(), std::max(Range.end(), std::prev(It2)->end())};
26854c3394SAlexey Lapshin It = Ranges.erase(It, It2);
27854c3394SAlexey Lapshin }
28*8bb4451aSAlexey Lapshin if (It != Ranges.begin() && Range.start() <= std::prev(It)->end()) {
29*8bb4451aSAlexey Lapshin --It;
30*8bb4451aSAlexey Lapshin *It = {It->start(), std::max(It->end(), Range.end())};
31*8bb4451aSAlexey Lapshin return It;
32854c3394SAlexey Lapshin }
33854c3394SAlexey Lapshin
34*8bb4451aSAlexey Lapshin return Ranges.insert(It, Range);
35*8bb4451aSAlexey Lapshin }
36*8bb4451aSAlexey Lapshin
37*8bb4451aSAlexey Lapshin AddressRanges::Collection::const_iterator
find(uint64_t Addr) const38*8bb4451aSAlexey Lapshin AddressRanges::find(uint64_t Addr) const {
39854c3394SAlexey Lapshin auto It = std::partition_point(
40854c3394SAlexey Lapshin Ranges.begin(), Ranges.end(),
41854c3394SAlexey Lapshin [=](const AddressRange &R) { return R.start() <= Addr; });
42*8bb4451aSAlexey Lapshin
43*8bb4451aSAlexey Lapshin if (It == Ranges.begin())
44*8bb4451aSAlexey Lapshin return Ranges.end();
45*8bb4451aSAlexey Lapshin
46*8bb4451aSAlexey Lapshin --It;
47*8bb4451aSAlexey Lapshin if (Addr >= It->end())
48*8bb4451aSAlexey Lapshin return Ranges.end();
49*8bb4451aSAlexey Lapshin
50*8bb4451aSAlexey Lapshin return It;
51854c3394SAlexey Lapshin }
52854c3394SAlexey Lapshin
53*8bb4451aSAlexey Lapshin AddressRanges::Collection::const_iterator
find(AddressRange Range) const54*8bb4451aSAlexey Lapshin AddressRanges::find(AddressRange Range) const {
55854c3394SAlexey Lapshin if (Range.size() == 0)
56*8bb4451aSAlexey Lapshin return Ranges.end();
57*8bb4451aSAlexey Lapshin
58854c3394SAlexey Lapshin auto It = std::partition_point(
59854c3394SAlexey Lapshin Ranges.begin(), Ranges.end(),
60854c3394SAlexey Lapshin [=](const AddressRange &R) { return R.start() <= Range.start(); });
61d2a4d6bfSAlexey Lapshin
62*8bb4451aSAlexey Lapshin if (It == Ranges.begin())
63*8bb4451aSAlexey Lapshin return Ranges.end();
64*8bb4451aSAlexey Lapshin
65*8bb4451aSAlexey Lapshin --It;
66*8bb4451aSAlexey Lapshin if (Range.end() > It->end())
67*8bb4451aSAlexey Lapshin return Ranges.end();
68*8bb4451aSAlexey Lapshin
69*8bb4451aSAlexey Lapshin return It;
70854c3394SAlexey Lapshin }
71