/**
   * Skip a specified number of entries and return the resulting position.
   *
   * @param startPosition the current position
   * @param entriesToSkip the numbers of entries to skip
   * @return the new position
   */
  protected synchronized Position skipEntries(Position startPosition, int entriesToSkip) {
    log.debug("[{}] Skipping {} entries from position {}", va(name, entriesToSkip, startPosition));
    long ledgerId = startPosition.getLedgerId();
    entriesToSkip += startPosition.getEntryId();

    while (entriesToSkip > 0) {
      if (currentLedger != null && ledgerId == currentLedger.getId()) {
        checkArgument(entriesToSkip <= (currentLedger.getLastAddConfirmed() + 1));
        return new Position(ledgerId, entriesToSkip);
      } else {
        LedgerStat ledger = ledgers.get(ledgerId);
        if (ledger == null) {
          checkArgument(!ledgers.isEmpty());
          ledgerId = ledgers.ceilingKey(ledgerId);
          continue;
        }

        if (entriesToSkip < ledger.getEntriesCount()) {
          return new Position(ledgerId, entriesToSkip);
        } else {
          // Move to next ledger
          entriesToSkip -= ledger.getEntriesCount();
          ledgerId = ledgers.ceilingKey(ledgerId + 1);
        }
      }
    }

    return new Position(ledgerId, 0);
  }
Esempio n. 2
0
  @Override
  public BigInteger locate(T data) throws HashException {
    BigInteger hashLocation = function.hash(data);
    BigInteger node = entryMap.ceilingKey(hashLocation);

    /* Wraparound edge case */
    if (node == null) {
      node = entryMap.ceilingKey(BigInteger.ZERO);
    }

    return node;
  }
  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    pr = new PrintWriter(new OutputStreamWriter(System.out));
    // br = new BufferedReader(new FileReader("in.txt"));
    // pr = new PrintWriter(new FileWriter("out.txt"));

    int n = readInt();
    int k = readInt();
    int[] boy = new int[n];
    TreeMap<Integer, Integer> girl = new TreeMap<Integer, Integer>();
    for (int i = 0; i < n; i++) {
      boy[i] = readInt();
    }
    for (int i = 0; i < n; i++) {
      int in = readInt();
      if (!girl.containsKey(in)) girl.put(in, 0);
      girl.put(in, girl.get(in) + 1);
    }
    Arrays.sort(boy);
    int ans = 0;
    for (int i = 0; i < n; i++) {
      Integer next = girl.ceilingKey(boy[i] - k);
      if (next != null && Math.abs(next - boy[i]) <= k) {
        ans++;
        girl.put(next, girl.get(next) - 1);
        if (girl.get(next) == 0) girl.remove(next);
      }
    }
    System.out.println(ans);

    pr.close();
  }
  private void internalReadFromLedger(LedgerHandle ledger, OpReadEntry opReadEntry) {
    // Perform the read
    long firstEntry = opReadEntry.readPosition.getEntryId();

    if (firstEntry > ledger.getLastAddConfirmed()) {
      log.debug(
          "[{}] No more messages to read from ledger={} lastEntry={} readEntry={}",
          va(name, ledger.getId(), ledger.getLastAddConfirmed(), firstEntry));

      if (ledger.getId() != currentLedger.getId()) {
        // Cursor was placed past the end of one ledger, move it to the
        // beginning of the next ledger
        Long nextLedgerId = ledgers.ceilingKey(ledger.getId() + 1);
        opReadEntry.nextReadPosition = new Position(nextLedgerId, 0);
      }

      opReadEntry.emptyResponse();
      return;
    }

    long lastEntry = min(firstEntry + opReadEntry.count - 1, ledger.getLastAddConfirmed());

    long expectedEntries = lastEntry - firstEntry + 1;
    opReadEntry.entries = Lists.newArrayListWithExpectedSize((int) expectedEntries);

    log.debug(
        "[{}] Reading entries from ledger {} - first={} last={}",
        va(name, ledger.getId(), firstEntry, lastEntry));
    ledger.asyncReadEntries(firstEntry, lastEntry, this, opReadEntry);
  }
Esempio n. 5
0
  HeadPosition getSeparator(int nodeNum) {
    if (nodeNum >= stack.size()) {
      return null;
    }
    TreeShapedStack<Tree> stack = this.stack;
    for (int i = 0; i < nodeNum; ++i) {
      stack = stack.pop();
    }
    Tree node = stack.peek();
    int head = ShiftReduceUtils.headIndex(node);
    if (separators.get(head) != null) {
      return HeadPosition.HEAD;
    }
    int left = ShiftReduceUtils.leftIndex(node);
    Integer nextLeft = separators.floorKey(head);
    boolean hasLeft = (nextLeft != null && nextLeft >= left);

    int right = ShiftReduceUtils.rightIndex(node);
    Integer nextRight = separators.ceilingKey(head);
    boolean hasRight = (nextRight != null && nextRight <= right);

    if (hasLeft && hasRight) {
      return HeadPosition.BOTH;
    } else if (hasLeft) {
      return HeadPosition.LEFT;
    } else if (hasRight) {
      return HeadPosition.RIGHT;
    } else {
      return HeadPosition.NONE;
    }
  }
Esempio n. 6
0
  /**
   * Given a token that may or may not be exactly equal to one of the endpoint's tokens, returns the
   * token of the endpoint that is responsible for storing this token.
   *
   * @throws NoSuchElementException if there are no known tokens
   */
  BigInteger getResponsibleToken(BigInteger token) {
    if (tokenToEndPointMap_.isEmpty()) {
      throw new NoSuchElementException("No tokens in ring!");
    }

    // find smallest key >= token
    BigInteger ctoken = tokenToEndPointMap_.ceilingKey(token);

    if (ctoken != null) return ctoken;

    return tokenToEndPointMap_.firstKey();
  }
Esempio n. 7
0
 /**
  * Find the IP address of the node that has the specified file
  *
  * @param FileName name of the file that has to be found
  * @return null if no nodes in the system, else return IP address of the node.
  */
 public InetAddress getLocation(String FileName) {
   if (NodeList.isEmpty()) {
     return null;
   }
   Hasher myHasher = new Hasher();
   int fileID = myHasher.Hash(FileName);
   Integer NodeID = NodeList.ceilingKey(fileID);
   if (NodeID == null) {
     NodeID = NodeList.firstKey();
   }
   return NodeList.get(NodeID);
 }
Esempio n. 8
0
  /**
   * Finds closest vertical key in raster to specified vert value
   *
   * @param dVertValue Vertical value
   * @return Closest vertical value
   */
  private double findNearestVertKey() {

    // dFloor = floor value
    // dCeiling = ceiling value

    double dFloor;
    double dCeiling;

    if (dVert <= mapVert.firstKey()) {
      return mapVert.firstKey();
    } else if (dVert >= mapVert.lastKey()) {
      return mapVert.lastKey();
    } else {
      dFloor = mapVert.floorKey(dVert);
      dCeiling = mapVert.ceilingKey(dVert);
      if (dVert - dFloor < dCeiling - dVert) {
        return dFloor;
      } else {
        return dCeiling;
      }
    }
  }
Esempio n. 9
0
  /**
   * Finds closest time key in raster to specified time value
   *
   * @param timValue Time value
   * @return Closest time value
   */
  private LocalDate findNearestTimeKey() {

    // timFloor = floor value
    // timCeiling = ceiling value

    LocalDate timFloor;
    LocalDate timCeiling;

    if (!tim1.isAfter(mapDate.firstKey())) {
      return mapDate.firstKey();
    } else if (!tim1.isBefore(mapDate.lastKey())) {
      return mapDate.lastKey();
    } else {
      timFloor = mapDate.floorKey(tim1);
      timCeiling = mapDate.ceilingKey(tim1);
      if (Days.daysBetween(timFloor, tim1).getDays()
          < Days.daysBetween(tim1, timCeiling).getDays()) {
        return timFloor;
      } else {
        return timCeiling;
      }
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.apache.bookkeeper.client.AsyncCallback.ReadCallback#readComplete(int,
   * org.apache.bookkeeper.client.LedgerHandle, java.util.Enumeration,
   * java.lang.Object)
   */
  @Override
  public void readComplete(
      int rc, LedgerHandle lh, Enumeration<LedgerEntry> entriesEnum, Object ctx) {
    OpReadEntry opReadEntry = (OpReadEntry) ctx;

    if (rc != BKException.Code.OK) {
      log.warn(
          "[{}] read failed from ledger {} at position:{}",
          va(name, lh.getId(), opReadEntry.readPosition));
      opReadEntry.failed(new ManagedLedgerException(BKException.create(rc)));
      return;
    }

    List<Entry> entries = opReadEntry.entries;
    while (entriesEnum.hasMoreElements()) entries.add(new EntryImpl(entriesEnum.nextElement()));

    long lastEntry = entries.get(entries.size() - 1).getPosition().getEntryId();

    // Get the "next read position", we need to advance the position taking
    // care of ledgers boundaries
    Position nextReadPosition;
    if (lastEntry < lh.getLastAddConfirmed()) {
      nextReadPosition = new Position(lh.getId(), lastEntry + 1);
    } else {
      // Move to next ledger
      Long nextLedgerId = ledgers.ceilingKey(lh.getId() + 1);
      if (nextLedgerId == null) {
        // We are already in the last ledger
        nextReadPosition = new Position(lh.getId(), lastEntry + 1);
      } else {
        nextReadPosition = new Position(nextLedgerId, 0);
      }
    }

    opReadEntry.nextReadPosition = nextReadPosition;
    opReadEntry.succeeded();
  }
Esempio n. 11
0
 public K ceilingKey(K key) {
   return realMap.ceilingKey(key);
 }
Esempio n. 12
0
 /**
  * Returns the smallest key in the symbol table greater than or equal to <tt>key</tt>.
  *
  * @return the smallest key in the symbol table greater than or equal to <tt>key</tt>
  * @param key the key
  * @throws NoSuchElementException if there is no such key
  * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
  */
 public Key ceiling(Key key) {
   if (key == null) throw new NullPointerException("called ceiling() with null key");
   Key k = st.ceilingKey(key);
   if (k == null) throw new NoSuchElementException("all keys are less than " + key);
   return k;
 }