View Javadoc
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  
11  package org.eclipse.jgit.internal.storage.pack;
12  
13  class IntSet {
14  	private int[] set;
15  
16  	private int cnt;
17  
18  	IntSet() {
19  		set = new int[64];
20  	}
21  
22  	boolean add(int key) {
23  		int high = cnt;
24  		int low = 0;
25  
26  		if (high == 0) {
27  			set[0] = key;
28  			cnt = 1;
29  			return true;
30  		}
31  
32  		do {
33  			int p = (low + high) >>> 1;
34  			if (key < set[p])
35  				high = p;
36  			else if (key == set[p])
37  				return false;
38  			else
39  				low = p + 1;
40  		} while (low < high);
41  
42  		if (cnt == set.length) {
43  			int[] n = new int[set.length * 2];
44  			System.arraycopy(set, 0, n, 0, cnt);
45  			set = n;
46  		}
47  
48  		if (low < cnt)
49  			System.arraycopy(set, low, set, low + 1, cnt - low);
50  		set[low] = key;
51  		cnt++;
52  		return true;
53  	}
54  }