IntSet.java

  1. /*
  2.  * Copyright (C) 2011, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.internal.storage.pack;

  11. class IntSet {
  12.     private int[] set;

  13.     private int cnt;

  14.     IntSet() {
  15.         set = new int[64];
  16.     }

  17.     boolean add(int key) {
  18.         int high = cnt;
  19.         int low = 0;

  20.         if (high == 0) {
  21.             set[0] = key;
  22.             cnt = 1;
  23.             return true;
  24.         }

  25.         do {
  26.             int p = (low + high) >>> 1;
  27.             if (key < set[p])
  28.                 high = p;
  29.             else if (key == set[p])
  30.                 return false;
  31.             else
  32.                 low = p + 1;
  33.         } while (low < high);

  34.         if (cnt == set.length) {
  35.             int[] n = new int[set.length * 2];
  36.             System.arraycopy(set, 0, n, 0, cnt);
  37.             set = n;
  38.         }

  39.         if (low < cnt)
  40.             System.arraycopy(set, low, set, low + 1, cnt - low);
  41.         set[low] = key;
  42.         cnt++;
  43.         return true;
  44.     }
  45. }