Java Code Examples for com.intellij.util.ObjectUtil#binarySearch()

The following examples show how to use com.intellij.util.ObjectUtil#binarySearch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilePointerPartNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
private int binarySearchChildByName(@Nonnull CharSequence name) {
  return ObjectUtil.binarySearch(0, children.length, i -> {
    FilePointerPartNode child = children[i];
    CharSequence childName = child.getName();
    return StringUtil.compare(childName, name, !SystemInfo.isFileSystemCaseSensitive);
  });
}
 
Example 2
Source File: FoldRegionsTree.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
FoldRegion fetchOutermost(int offset) {
  if (!isFoldingEnabled()) return null;
  CachedData cachedData = ensureAvailableData();

  final int[] starts = cachedData.topStartOffsets;
  final int[] ends = cachedData.topEndOffsets;
  if (starts == null || ends == null) {
    return null;
  }

  int i = ObjectUtil.binarySearch(0, ends.length, mid -> ends[mid] < offset ? -1 : starts[mid] > offset ? 1 : 0);
  return i < 0 ? null : cachedData.topLevelRegions[i];
}
 
Example 3
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int hostToInjectedUnescaped(DocumentWindow window, int hostOffset) {
  Place shreds = ((DocumentWindowImpl)window).getShreds();
  Segment hostRangeMarker = shreds.get(0).getHostRangeMarker();
  if (hostRangeMarker == null || hostOffset < hostRangeMarker.getStartOffset()) {
    return shreds.get(0).getPrefix().length();
  }
  StringBuilder chars = new StringBuilder();
  int unescaped = 0;
  for (int i = 0; i < shreds.size(); i++, chars.setLength(0)) {
    PsiLanguageInjectionHost.Shred shred = shreds.get(i);
    int prefixLength = shred.getPrefix().length();
    int suffixLength = shred.getSuffix().length();
    PsiLanguageInjectionHost host = shred.getHost();
    TextRange rangeInsideHost = shred.getRangeInsideHost();
    LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = host == null ? null : host.createLiteralTextEscaper();
    unescaped += prefixLength;
    Segment currentRange = shred.getHostRangeMarker();
    if (currentRange == null) continue;
    Segment nextRange = i == shreds.size() - 1 ? null : shreds.get(i + 1).getHostRangeMarker();
    if (nextRange == null || hostOffset < nextRange.getStartOffset()) {
      hostOffset = Math.min(hostOffset, currentRange.getEndOffset());
      int inHost = hostOffset - currentRange.getStartOffset();
      if (escaper != null && escaper.decode(rangeInsideHost, chars)) {
        int found = ObjectUtil.binarySearch(0, inHost, index -> Comparing.compare(escaper.getOffsetInHost(index, TextRange.create(0, host.getTextLength())), inHost));
        return unescaped + (found >= 0 ? found : -found - 1);
      }
      return unescaped + inHost;
    }
    if (escaper != null && escaper.decode(rangeInsideHost, chars)) {
      unescaped += chars.length();
    }
    else {
      unescaped += currentRange.getEndOffset() - currentRange.getStartOffset();
    }
    unescaped += suffixLength;
  }
  return unescaped - shreds.get(shreds.size() - 1).getSuffix().length();
}
 
Example 4
Source File: TreeUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static <T extends TreeNode> int indexedBinarySearch(@Nonnull T parent, @Nonnull T key, @Nonnull Comparator<? super T> comparator) {
  return ObjectUtil.binarySearch(0, parent.getChildCount(), mid -> comparator.compare((T)parent.getChildAt(mid), key));
}
 
Example 5
Source File: VirtualDirectoryImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private int findIndex(@Nonnull int[] ids, @Nonnull CharSequence name, boolean caseSensitive) {
  return ObjectUtil.binarySearch(0, ids.length, mid -> compareNames(mySegment.vfsData.getNameByFileId(ids[mid]), name, caseSensitive));
}
 
Example 6
Source File: IntToIntBtree.java    From consulo with Apache License 2.0 4 votes vote down vote up
private int search(final int value) {
  if (isIndexLeaf() && isHashedLeaf()) {
    return hashIndex(value);
  }
  return ObjectUtil.binarySearch(0, getChildrenCount(), mid -> Integer.compare(keyAt(mid), value));
}