Java Code Examples for com.google.common.collect.ImmutableRangeMap#Builder

The following examples show how to use com.google.common.collect.ImmutableRangeMap#Builder . 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: CapabilityMultiplexer.java    From besu with Apache License 2.0 5 votes vote down vote up
private ImmutableRangeMap<Integer, Capability> calculateAgreedCapabilities(
    final List<Capability> a, final List<Capability> b) {
  final List<Capability> caps = new ArrayList<>(a);
  caps.sort(CAPABILITY_COMPARATOR);
  caps.retainAll(b);

  final ImmutableRangeMap.Builder<Integer, Capability> builder = ImmutableRangeMap.builder();
  // Reserve some messages for WireProtocol
  int offset = WIRE_PROTOCOL_MESSAGE_SPACE;
  String prevProtocol = null;
  for (final Iterator<Capability> itr = caps.iterator(); itr.hasNext(); ) {
    final Capability cap = itr.next();
    final String curProtocol = cap.getName();
    if (curProtocol.equalsIgnoreCase(prevProtocol)) {
      // A later version of this protocol is already being used, so ignore this version
      continue;
    }
    prevProtocol = curProtocol;
    final SubProtocol subProtocol = subProtocols.get(cap.getName());
    final int messageSpace = subProtocol == null ? 0 : subProtocol.messageSpace(cap.getVersion());
    if (messageSpace > 0) {
      builder.put(Range.closedOpen(offset, offset + messageSpace), cap);
    }
    offset += messageSpace;
  }

  return builder.build();
}
 
Example 2
Source File: Renewable.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
MaterialData chooseShuffledMaterial() {
  ImmutableRangeMap.Builder<Double, MaterialData> weightsBuilder = ImmutableRangeMap.builder();
  double sum = 0d;
  for (MaterialData material : shuffleableMaterialDeficit.materials()) {
    double weight = shuffleableMaterialDeficit.get(material);
    if (weight > 0) {
      weightsBuilder.put(Range.closedOpen(sum, sum + weight), material);
      sum += weight;
    }
  }
  RangeMap<Double, MaterialData> weights = weightsBuilder.build();
  return weights.get(match.getRandom().nextDouble() * sum);
}
 
Example 3
Source File: Renewable.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
MaterialData chooseShuffledMaterial() {
    ImmutableRangeMap.Builder<Double, MaterialData> weightsBuilder = ImmutableRangeMap.builder();
    double sum = 0d;
    for(MaterialData material : shuffleableMaterialDeficit.materials()) {
        double weight = shuffleableMaterialDeficit.get(material);
        if(weight > 0) {
            weightsBuilder.put(Range.closedOpen(sum, sum + weight), material);
            sum += weight;
        }
    }
    RangeMap<Double, MaterialData> weights = weightsBuilder.build();
    return weights.get(match.getRandom().nextDouble() * sum);
}
 
Example 4
Source File: TestAffinityCalculator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildRangeMap() {
  BlockLocation[] blocks = buildBlockLocations(new String[4], 256*1024*1024);
  long tA = System.nanoTime();
  ImmutableRangeMap.Builder<Long, BlockLocation> blockMapBuilder = new ImmutableRangeMap.Builder<>();
  for (BlockLocation block : blocks) {
    long start = block.getOffset();
    long end = start + block.getLength();
    Range<Long> range = Range.closedOpen(start, end);
    blockMapBuilder = blockMapBuilder.put(range, block);
  }
  ImmutableRangeMap<Long,BlockLocation> map = blockMapBuilder.build();
  long tB = System.nanoTime();
  System.out.println(String.format("Took %f ms to build range map", (tB - tA) / 1e6));
}
 
Example 5
Source File: LineMap.java    From turbine with Apache License 2.0 5 votes vote down vote up
public static LineMap create(String source) {
  int last = 0;
  int line = 1;
  ImmutableRangeMap.Builder<Integer, Integer> builder = ImmutableRangeMap.builder();
  for (int idx = 0; idx < source.length(); idx++) {
    char ch = source.charAt(idx);
    switch (ch) {
        // handle CR line endings
      case '\r':
        // ...and CRLF
        if (idx + 1 < source.length() && source.charAt(idx + 1) == '\n') {
          idx++;
        }
        // falls through
      case '\n':
        builder.put(Range.closedOpen(last, idx + 1), line++);
        last = idx + 1;
        break;
      default:
        break;
    }
  }
  // no trailing newline
  if (last < source.length()) {
    builder.put(Range.closedOpen(last, source.length()), line++);
  }
  return new LineMap(source, builder.build());
}