Class TreeRangeSet<C extends Comparable<? super C>>

java.lang.Object
cloud.opencode.base.collections.specialized.TreeRangeSet<C>
Type Parameters:
C - the type of range endpoints | 范围端点类型
All Implemented Interfaces:
RangeSet<C>, Serializable

public final class TreeRangeSet<C extends Comparable<? super C>> extends Object implements RangeSet<C>, Serializable
TreeRangeSet - Tree-based Range Set Implementation TreeRangeSet - 基于树的范围集合实现

A range set implementation using a tree structure for efficient range operations.

使用树结构进行高效范围操作的范围集合实现。

Features | 主要功能:

  • Automatic range coalescing - 自动范围合并
  • O(log n) operations - O(log n) 操作
  • Efficient range queries - 高效范围查询

Usage Examples | 使用示例:

TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
rangeSet.add(Range.closed(1, 10));
rangeSet.add(Range.closed(5, 15));  // coalesces to [1, 15]

boolean contains = rangeSet.contains(5);  // true
Range<Integer> range = rangeSet.rangeContaining(5);  // [1, 15]

Performance | 性能特性:

  • add: O(log n) - add: O(log n)
  • remove: O(log n) - remove: O(log n)
  • contains: O(log n) - contains: O(log n)
  • rangeContaining: O(log n) - rangeContaining: O(log n)

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: No (nulls not allowed) - 空值安全: 否(不允许空值)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static <C extends Comparable<? super C>> TreeRangeSet<C> create()
      Create an empty TreeRangeSet. 创建空 TreeRangeSet。
      Type Parameters:
      C - endpoint type | 端点类型
      Returns:
      new empty TreeRangeSet | 新空 TreeRangeSet
    • create

      public static <C extends Comparable<? super C>> TreeRangeSet<C> create(Iterable<Range<C>> ranges)
      Create a TreeRangeSet with the given ranges. 使用给定范围创建 TreeRangeSet。
      Type Parameters:
      C - endpoint type | 端点类型
      Parameters:
      ranges - the ranges | 范围
      Returns:
      new TreeRangeSet | 新 TreeRangeSet
    • contains

      public boolean contains(C value)
      Description copied from interface: RangeSet
      Check if the value is contained in any range. 检查值是否包含在任何范围中。
      Specified by:
      contains in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      value - the value | 值
      Returns:
      true if contained | 如果包含则返回 true
    • rangeContaining

      public Range<C> rangeContaining(C value)
      Description copied from interface: RangeSet
      Return the range containing the value, or null. 返回包含该值的范围,如果不存在则返回 null。
      Specified by:
      rangeContaining in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      value - the value | 值
      Returns:
      the containing range, or null | 包含的范围,或 null
    • encloses

      public boolean encloses(Range<C> otherRange)
      Description copied from interface: RangeSet
      Check if this range set encloses the given range. 检查此范围集合是否完全包含给定范围。
      Specified by:
      encloses in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      otherRange - the range to check | 要检查的范围
      Returns:
      true if enclosed | 如果完全包含则返回 true
    • enclosesAll

      public boolean enclosesAll(RangeSet<C> other)
      Description copied from interface: RangeSet
      Check if this range set encloses all ranges in the other set. 检查此范围集合是否完全包含另一个集合中的所有范围。
      Specified by:
      enclosesAll in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      other - the other range set | 另一个范围集合
      Returns:
      true if all enclosed | 如果全部包含则返回 true
    • intersects

      public boolean intersects(Range<C> otherRange)
      Description copied from interface: RangeSet
      Check if this range set intersects the given range. 检查此范围集合是否与给定范围相交。
      Specified by:
      intersects in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      otherRange - the range to check | 要检查的范围
      Returns:
      true if intersects | 如果相交则返回 true
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RangeSet
      Check if this range set is empty. 检查此范围集合是否为空。
      Specified by:
      isEmpty in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      true if empty | 如果为空则返回 true
    • asRanges

      public Set<Range<C>> asRanges()
      Description copied from interface: RangeSet
      Return all ranges as a set. 以集合形式返回所有范围。
      Specified by:
      asRanges in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the set of ranges | 范围集合
    • asDescendingSetOfRanges

      public Set<Range<C>> asDescendingSetOfRanges()
      Description copied from interface: RangeSet
      Return all ranges as a descending set. 以降序集合形式返回所有范围。
      Specified by:
      asDescendingSetOfRanges in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the descending set of ranges | 降序范围集合
    • complement

      public RangeSet<C> complement()
      Description copied from interface: RangeSet
      Return the complement of this range set. 返回此范围集合的补集。
      Specified by:
      complement in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the complement | 补集
    • subRangeSet

      public RangeSet<C> subRangeSet(Range<C> view)
      Description copied from interface: RangeSet
      Return a sub range set within the given range. 返回给定范围内的子范围集合。
      Specified by:
      subRangeSet in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      view - the viewing range | 视图范围
      Returns:
      the sub range set | 子范围集合
    • span

      public Range<C> span()
      Description copied from interface: RangeSet
      Return the minimal range that encloses all ranges. 返回包含所有范围的最小范围。
      Specified by:
      span in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the span | 跨度
    • add

      public void add(Range<C> range)
      Description copied from interface: RangeSet
      Add a range to this set. 向此集合添加范围。
      Specified by:
      add in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      range - the range to add | 要添加的范围
    • remove

      public void remove(Range<C> range)
      Description copied from interface: RangeSet
      Remove a range from this set. 从此集合移除范围。
      Specified by:
      remove in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      range - the range to remove | 要移除的范围
    • addAll

      public void addAll(RangeSet<C> other)
      Description copied from interface: RangeSet
      Add all ranges from another range set. 从另一个范围集合添加所有范围。
      Specified by:
      addAll in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      other - the other range set | 另一个范围集合
    • removeAll

      public void removeAll(RangeSet<C> other)
      Description copied from interface: RangeSet
      Remove all ranges in another range set. 移除另一个范围集合中的所有范围。
      Specified by:
      removeAll in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      other - the other range set | 另一个范围集合
    • clear

      public void clear()
      Description copied from interface: RangeSet
      Clear all ranges. 清除所有范围。
      Specified by:
      clear in interface RangeSet<C extends Comparable<? super C>>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object