# Path: cle/utils.py
# def key_bisect_find(lst, item, lo=0, hi=None, keyfunc=lambda x: x):
#     if lo < 0:
#         raise ValueError('lo must be non-negative')
#     if hi is None:
#         hi = len(lst)
#     while lo < hi:
#         mid = (lo + hi) // 2
#         if keyfunc(lst[mid]) <= keyfunc(item):
#             lo = mid + 1
#         else:
#             hi = mid
#     return lo
# 
# def key_bisect_insort_left(lst, item, lo=0, hi=None, keyfunc=lambda x: x):
#     if lo < 0:
#         raise ValueError('lo must be non-negative')
#     if hi is None:
#         hi = len(lst)
#     while lo < hi:
#         mid = (lo + hi) // 2
#         if keyfunc(lst[mid]) < keyfunc(item):
#             lo = mid + 1
#         else:
#             hi = mid
#     lst.insert(lo, item)


from typing import List, Optional, TYPE_CHECKING
    from .. import Region
from ..utils import key_bisect_find, key_bisect_insort_left
        from .elf.regions import ELFSection
        # update self._sorted_list
        self._sorted_list = self._make_sorted(self._list)

    def __len__(self) -> int:
        return len(self._list)

    def __repr__(self):
        return "<Regions: %s>" % repr(self._list)

    def _rebase(self, delta):
        """
        Does regions rebasing to other base address.
        Modifies state of each internal object, so the list reference doesn't need to be updated,
        the same is also valid for sorted list as operation preserves the ordering.

        :param delta: Delta offset between an old and a new image bases
        :type delta: int
        """
        for x in self._list:
            x._rebase(delta)

    def append(self, region: 'Region'):
        """
        Append a new Region instance into the list.

        :param Region region: The region to append.
        """
        self._list.append(region)

        if self._is_region_mapped(region):

# Path: cle/utils.py
# def key_bisect_find(lst, item, lo=0, hi=None, keyfunc=lambda x: x):
#     if lo < 0:
#         raise ValueError('lo must be non-negative')
#     if hi is None:
#         hi = len(lst)
#     while lo < hi:
#         mid = (lo + hi) // 2
#         if keyfunc(lst[mid]) <= keyfunc(item):
#             lo = mid + 1
#         else:
#             hi = mid
#     return lo
# 
# def key_bisect_insort_left(lst, item, lo=0, hi=None, keyfunc=lambda x: x):
#     if lo < 0:
#         raise ValueError('lo must be non-negative')
#     if hi is None:
#         hi = len(lst)
#     while lo < hi:
#         mid = (lo + hi) // 2
#         if keyfunc(lst[mid]) < keyfunc(item):
#             lo = mid + 1
#         else:
#             hi = mid
#     lst.insert(lo, item)


# Path: cle/backends/regions.py
from typing import List, Optional, TYPE_CHECKING
    from .. import Region
from ..utils import key_bisect_find, key_bisect_insort_left
        from .elf.regions import ELFSection

        # update self._sorted_list
        self._sorted_list = self._make_sorted(self._list)

    def __len__(self) -> int:
        return len(self._list)

    def __repr__(self):
        return "<Regions: %s>" % repr(self._list)

    def _rebase(self, delta):
        """
        Does regions rebasing to other base address.
        Modifies state of each internal object, so the list reference doesn't need to be updated,
        the same is also valid for sorted list as operation preserves the ordering.

        :param delta: Delta offset between an old and a new image bases
        :type delta: int
        """
        for x in self._list:
            x._rebase(delta)

    def append(self, region: 'Region'):
        """
        Append a new Region instance into the list.

        :param Region region: The region to append.
        """
        self._list.append(region)

        if self._is_region_mapped(region):

            key_bisect_insort_left(self._sorted_list, region, keyfunc=lambda r: r.vaddr)