package com.google.common.collect; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.SortedLists; import com.google.common.primitives.Ints; import java.io.Serializable; import java.lang.Comparable; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; /* loaded from: classes2.dex */ public final class ImmutableRangeSet extends AbstractRangeSet implements Serializable { private transient ImmutableRangeSet complement; private final transient ImmutableList> ranges; private static final ImmutableRangeSet> EMPTY = new ImmutableRangeSet<>(ImmutableList.of()); private static final ImmutableRangeSet> ALL = new ImmutableRangeSet<>(ImmutableList.of(Range.all())); @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final /* bridge */ /* synthetic */ void clear() { super.clear(); } /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final /* bridge */ /* synthetic */ boolean contains(Comparable comparable) { return super.contains(comparable); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final /* bridge */ /* synthetic */ boolean enclosesAll(RangeSet rangeSet) { return super.enclosesAll(rangeSet); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final /* bridge */ /* synthetic */ boolean enclosesAll(Iterable iterable) { return super.enclosesAll(iterable); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final /* bridge */ /* synthetic */ boolean equals(Object obj) { return super.equals(obj); } public static ImmutableRangeSet of(Range range) { Preconditions.checkNotNull(range); if (range.isEmpty()) { return of(); } if (range.equals(Range.all())) { return all(); } return new ImmutableRangeSet<>(ImmutableList.of(range)); } public static ImmutableRangeSet copyOf(RangeSet rangeSet) { Preconditions.checkNotNull(rangeSet); if (rangeSet.isEmpty()) { return of(); } if (rangeSet.encloses(Range.all())) { return all(); } if (rangeSet instanceof ImmutableRangeSet) { ImmutableRangeSet immutableRangeSet = (ImmutableRangeSet) rangeSet; if (!immutableRangeSet.isPartialView()) { return immutableRangeSet; } } return new ImmutableRangeSet<>(ImmutableList.copyOf((Collection) rangeSet.asRanges())); } public static > ImmutableRangeSet copyOf(Iterable> iterable) { return new Builder().addAll(iterable).build(); } public static > ImmutableRangeSet unionOf(Iterable> iterable) { return copyOf(TreeRangeSet.create(iterable)); } ImmutableRangeSet(ImmutableList> immutableList) { this.ranges = immutableList; } private ImmutableRangeSet(ImmutableList> immutableList, ImmutableRangeSet immutableRangeSet) { this.ranges = immutableList; this.complement = immutableRangeSet; } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final boolean intersects(Range range) { int binarySearch = SortedLists.binarySearch(this.ranges, Range.lowerBoundFn(), range.lowerBound, Ordering.natural(), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_HIGHER); if (binarySearch < this.ranges.size() && this.ranges.get(binarySearch).isConnected(range) && !this.ranges.get(binarySearch).intersection(range).isEmpty()) { return true; } if (binarySearch > 0) { int i = binarySearch - 1; if (this.ranges.get(i).isConnected(range) && !this.ranges.get(i).intersection(range).isEmpty()) { return true; } } return false; } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final boolean encloses(Range range) { int binarySearch = SortedLists.binarySearch(this.ranges, Range.lowerBoundFn(), range.lowerBound, Ordering.natural(), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_LOWER); return binarySearch != -1 && this.ranges.get(binarySearch).encloses(range); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final Range rangeContaining(C c) { int binarySearch = SortedLists.binarySearch(this.ranges, Range.lowerBoundFn(), Cut.belowValue(c), Ordering.natural(), SortedLists.KeyPresentBehavior.ANY_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_LOWER); if (binarySearch == -1) { return null; } Range range = this.ranges.get(binarySearch); if (range.contains(c)) { return range; } return null; } @Override // com.google.common.collect.RangeSet public final Range span() { if (this.ranges.isEmpty()) { throw new NoSuchElementException(); } return Range.create(this.ranges.get(0).lowerBound, this.ranges.get(r1.size() - 1).upperBound); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet public final boolean isEmpty() { return this.ranges.isEmpty(); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet @Deprecated public final void add(Range range) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet @Deprecated public final void addAll(RangeSet rangeSet) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet @Deprecated public final void addAll(Iterable> iterable) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet @Deprecated public final void remove(Range range) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet @Deprecated public final void removeAll(RangeSet rangeSet) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.AbstractRangeSet, com.google.common.collect.RangeSet @Deprecated public final void removeAll(Iterable> iterable) { throw new UnsupportedOperationException(); } @Override // com.google.common.collect.RangeSet public final ImmutableSet> asRanges() { if (this.ranges.isEmpty()) { return ImmutableSet.of(); } return new RegularImmutableSortedSet(this.ranges, Range.rangeLexOrdering()); } @Override // com.google.common.collect.RangeSet public final ImmutableSet> asDescendingSetOfRanges() { if (this.ranges.isEmpty()) { return ImmutableSet.of(); } return new RegularImmutableSortedSet(this.ranges.reverse(), Range.rangeLexOrdering().reverse()); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public final class ComplementRanges extends ImmutableList> { private final boolean positiveBoundedAbove; private final boolean positiveBoundedBelow; private final int size; final ImmutableRangeSet this$0; /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableCollection public final boolean isPartialView() { return true; } /* JADX WARN: Multi-variable type inference failed */ ComplementRanges(ImmutableRangeSet immutableRangeSet) { this.this$0 = immutableRangeSet; boolean hasLowerBound = ((Range) immutableRangeSet.ranges.get(0)).hasLowerBound(); this.positiveBoundedBelow = hasLowerBound; boolean hasUpperBound = ((Range) Iterables.getLast(immutableRangeSet.ranges)).hasUpperBound(); this.positiveBoundedAbove = hasUpperBound; int size = immutableRangeSet.ranges.size(); size = hasLowerBound ? size : size - 1; this.size = hasUpperBound ? size + 1 : size; } /* JADX WARN: Multi-variable type inference failed */ @Override // java.util.List public final Range get(int i) { Cut cut; Cut cut2; Preconditions.checkElementIndex(i, this.size); if (!this.positiveBoundedBelow) { cut = ((Range) this.this$0.ranges.get(i)).upperBound; } else { cut = i == 0 ? Cut.belowAll() : ((Range) this.this$0.ranges.get(i - 1)).upperBound; } if (!this.positiveBoundedAbove || i != this.size - 1) { cut2 = ((Range) this.this$0.ranges.get(i + (!this.positiveBoundedBelow ? 1 : 0))).lowerBound; } else { cut2 = Cut.aboveAll(); } return Range.create(cut, cut2); } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public final int size() { return this.size; } } @Override // com.google.common.collect.RangeSet public final ImmutableRangeSet complement() { ImmutableRangeSet immutableRangeSet = this.complement; if (immutableRangeSet != null) { return immutableRangeSet; } if (this.ranges.isEmpty()) { ImmutableRangeSet all = all(); this.complement = all; return all; } if (this.ranges.size() == 1 && this.ranges.get(0).equals(Range.all())) { ImmutableRangeSet of = of(); this.complement = of; return of; } ImmutableRangeSet immutableRangeSet2 = new ImmutableRangeSet<>(new ComplementRanges(this), this); this.complement = immutableRangeSet2; return immutableRangeSet2; } public final ImmutableRangeSet union(RangeSet rangeSet) { return unionOf(Iterables.concat(asRanges(), rangeSet.asRanges())); } public final ImmutableRangeSet intersection(RangeSet rangeSet) { TreeRangeSet create = TreeRangeSet.create(this); create.removeAll(rangeSet.complement()); return copyOf(create); } public final ImmutableRangeSet difference(RangeSet rangeSet) { TreeRangeSet create = TreeRangeSet.create(this); create.removeAll(rangeSet); return copyOf(create); } private ImmutableList> intersectRanges(Range range) { int size; if (this.ranges.isEmpty() || range.isEmpty()) { return ImmutableList.of(); } if (range.encloses(span())) { return this.ranges; } int binarySearch = range.hasLowerBound() ? SortedLists.binarySearch(this.ranges, (Function>) Range.upperBoundFn(), range.lowerBound, SortedLists.KeyPresentBehavior.FIRST_AFTER, SortedLists.KeyAbsentBehavior.NEXT_HIGHER) : 0; if (range.hasUpperBound()) { size = SortedLists.binarySearch(this.ranges, (Function>) Range.lowerBoundFn(), range.upperBound, SortedLists.KeyPresentBehavior.FIRST_PRESENT, SortedLists.KeyAbsentBehavior.NEXT_HIGHER); } else { size = this.ranges.size(); } int i = size - binarySearch; if (i == 0) { return ImmutableList.of(); } return (ImmutableList>) new ImmutableList>(this, i, binarySearch, range) { // from class: com.google.common.collect.ImmutableRangeSet.1 final ImmutableRangeSet this$0; final int val$fromIndex; final int val$length; final Range val$range; /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableCollection public boolean isPartialView() { return true; } { this.this$0 = this; this.val$length = i; this.val$fromIndex = binarySearch; this.val$range = range; } /* JADX WARN: Multi-variable type inference failed */ @Override // java.util.List public Range get(int i2) { Preconditions.checkElementIndex(i2, this.val$length); return (i2 == 0 || i2 == this.val$length + (-1)) ? ((Range) this.this$0.ranges.get(i2 + this.val$fromIndex)).intersection(this.val$range) : (Range) this.this$0.ranges.get(i2 + this.val$fromIndex); } @Override // java.util.AbstractCollection, java.util.Collection, java.util.List public int size() { return this.val$length; } }; } @Override // com.google.common.collect.RangeSet public final ImmutableRangeSet subRangeSet(Range range) { if (!isEmpty()) { Range span = span(); if (range.encloses(span)) { return this; } if (range.isConnected(span)) { return new ImmutableRangeSet<>(intersectRanges(range)); } } return of(); } public final ImmutableSortedSet asSet(DiscreteDomain discreteDomain) { Preconditions.checkNotNull(discreteDomain); if (isEmpty()) { return ImmutableSortedSet.of(); } Range canonical = span().canonical(discreteDomain); if (!canonical.hasLowerBound()) { throw new IllegalArgumentException("Neither the DiscreteDomain nor this range set are bounded below"); } if (!canonical.hasUpperBound()) { try { discreteDomain.maxValue(); } catch (NoSuchElementException unused) { throw new IllegalArgumentException("Neither the DiscreteDomain nor this range set are bounded above"); } } return new AsSet(this, discreteDomain); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public final class AsSet extends ImmutableSortedSet { private final DiscreteDomain domain; private transient Integer size; final ImmutableRangeSet this$0; @Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet public final /* bridge */ /* synthetic */ Iterator descendingIterator() { return descendingIterator(); } @Override // com.google.common.collect.ImmutableSortedSet, com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable public final /* bridge */ /* synthetic */ Iterator iterator() { return iterator(); } /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ AsSet(ImmutableRangeSet immutableRangeSet, DiscreteDomain discreteDomain) { super(Ordering.natural()); this.this$0 = immutableRangeSet; this.domain = discreteDomain; } @Override // java.util.AbstractCollection, java.util.Collection, java.util.Set public final int size() { Integer num = this.size; if (num == null) { Iterator it = this.this$0.ranges.iterator(); long j = 0; while (it.hasNext()) { j += ContiguousSet.create((Range) it.next(), this.domain).size(); if (j >= 2147483647L) { break; } } num = Integer.valueOf(Ints.saturatedCast(j)); this.size = num; } return num.intValue(); } @Override // com.google.common.collect.ImmutableSortedSet, com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set, java.util.NavigableSet, com.google.common.collect.SortedIterable public final UnmodifiableIterator iterator() { return new AbstractIterator(this) { // from class: com.google.common.collect.ImmutableRangeSet.AsSet.1 Iterator elemItr = Iterators.emptyIterator(); final Iterator> rangeItr; final AsSet this$1; { this.this$1 = this; this.rangeItr = this.this$0.ranges.iterator(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.collect.AbstractIterator public C computeNext() { while (!this.elemItr.hasNext()) { if (this.rangeItr.hasNext()) { this.elemItr = ContiguousSet.create(this.rangeItr.next(), this.this$1.domain).iterator(); } else { return (C) endOfData(); } } return this.elemItr.next(); } }; } @Override // com.google.common.collect.ImmutableSortedSet, java.util.NavigableSet public final UnmodifiableIterator descendingIterator() { return new AbstractIterator(this) { // from class: com.google.common.collect.ImmutableRangeSet.AsSet.2 Iterator elemItr = Iterators.emptyIterator(); final Iterator> rangeItr; final AsSet this$1; { this.this$1 = this; this.rangeItr = this.this$0.ranges.reverse().iterator(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.collect.AbstractIterator public C computeNext() { while (!this.elemItr.hasNext()) { if (this.rangeItr.hasNext()) { this.elemItr = ContiguousSet.create(this.rangeItr.next(), this.this$1.domain).descendingIterator(); } else { return (C) endOfData(); } } return this.elemItr.next(); } }; } final ImmutableSortedSet subSet(Range range) { return this.this$0.subRangeSet((Range) range).asSet(this.domain); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableSortedSet public final ImmutableSortedSet headSetImpl(C c, boolean z) { return subSet(Range.upTo(c, BoundType.forBoolean(z))); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableSortedSet public final ImmutableSortedSet subSetImpl(C c, boolean z, C c2, boolean z2) { if (!z && !z2 && Range.compareOrThrow(c, c2) == 0) { return ImmutableSortedSet.of(); } return subSet(Range.range(c, BoundType.forBoolean(z), c2, BoundType.forBoolean(z2))); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableSortedSet public final ImmutableSortedSet tailSetImpl(C c, boolean z) { return subSet(Range.downTo(c, BoundType.forBoolean(z))); } @Override // com.google.common.collect.ImmutableCollection, java.util.AbstractCollection, java.util.Collection, java.util.Set public final boolean contains(Object obj) { if (obj == null) { return false; } try { return this.this$0.contains((Comparable) obj); } catch (ClassCastException unused) { return false; } } /* JADX INFO: Access modifiers changed from: package-private */ /* JADX WARN: Multi-variable type inference failed */ @Override // com.google.common.collect.ImmutableSortedSet public final int indexOf(Object obj) { if (!contains(obj)) { return -1; } Comparable comparable = (Comparable) obj; Iterator it = this.this$0.ranges.iterator(); long j = 0; while (it.hasNext()) { if (((Range) it.next()).contains(comparable)) { return Ints.saturatedCast(j + ContiguousSet.create(r3, this.domain).indexOf(comparable)); } j += ContiguousSet.create(r3, this.domain).size(); } throw new AssertionError("impossible"); } @Override // com.google.common.collect.ImmutableSortedSet final ImmutableSortedSet createDescendingSet() { return new DescendingImmutableSortedSet(this); } /* JADX INFO: Access modifiers changed from: package-private */ @Override // com.google.common.collect.ImmutableCollection public final boolean isPartialView() { return this.this$0.ranges.isPartialView(); } @Override // java.util.AbstractCollection public final String toString() { return this.this$0.ranges.toString(); } @Override // com.google.common.collect.ImmutableSortedSet, com.google.common.collect.ImmutableSet, com.google.common.collect.ImmutableCollection final Object writeReplace() { return new AsSetSerializedForm(this.this$0.ranges, this.domain); } } /* loaded from: classes2.dex */ static class AsSetSerializedForm implements Serializable { private final DiscreteDomain domain; private final ImmutableList> ranges; AsSetSerializedForm(ImmutableList> immutableList, DiscreteDomain discreteDomain) { this.ranges = immutableList; this.domain = discreteDomain; } Object readResolve() { return new ImmutableRangeSet(this.ranges).asSet(this.domain); } } final boolean isPartialView() { return this.ranges.isPartialView(); } public static > Builder builder() { return new Builder<>(); } /* loaded from: classes2.dex */ public static class Builder> { private final List> ranges = Lists.newArrayList(); public Builder add(Range range) { Preconditions.checkArgument(!range.isEmpty(), "range must not be empty, but was %s", range); this.ranges.add(range); return this; } public Builder addAll(RangeSet rangeSet) { return addAll(rangeSet.asRanges()); } public Builder addAll(Iterable> iterable) { Iterator> it = iterable.iterator(); while (it.hasNext()) { add(it.next()); } return this; } Builder combine(Builder builder) { addAll(builder.ranges); return this; } public ImmutableRangeSet build() { ImmutableList.Builder builder = new ImmutableList.Builder(this.ranges.size()); Collections.sort(this.ranges, Range.rangeLexOrdering()); PeekingIterator peekingIterator = Iterators.peekingIterator(this.ranges.iterator()); while (peekingIterator.hasNext()) { Range range = (Range) peekingIterator.next(); while (peekingIterator.hasNext()) { Range range2 = (Range) peekingIterator.peek(); if (range.isConnected(range2)) { Preconditions.checkArgument(range.intersection(range2).isEmpty(), "Overlapping ranges not permitted but found %s overlapping %s", range, range2); range = range.span((Range) peekingIterator.next()); } } builder.add((ImmutableList.Builder) range); } ImmutableList build = builder.build(); if (build.isEmpty()) { return ImmutableRangeSet.of(); } if (build.size() == 1 && ((Range) Iterables.getOnlyElement(build)).equals(Range.all())) { return ImmutableRangeSet.all(); } return new ImmutableRangeSet<>(build); } } /* loaded from: classes2.dex */ static final class SerializedForm implements Serializable { private final ImmutableList> ranges; SerializedForm(ImmutableList> immutableList) { this.ranges = immutableList; } final Object readResolve() { if (this.ranges.isEmpty()) { return ImmutableRangeSet.of(); } if (this.ranges.equals(ImmutableList.of(Range.all()))) { return ImmutableRangeSet.all(); } return new ImmutableRangeSet(this.ranges); } } final Object writeReplace() { return new SerializedForm(this.ranges); } public static ImmutableRangeSet of() { return EMPTY; } static ImmutableRangeSet all() { return ALL; } }