File: java.util.BitSet

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (141 lines) | stat: -rw-r--r-- 2,697 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// java.util.BitSet -- an implementation of the Java Language Spec sec 21.2
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.

package java.util;

public final class BitSet implements Cloneable {

	private long[] data;

	public BitSet()
	{
		data=new long[4];
	}

	public BitSet(int cap)
	{
		data=new long[(cap+63)>>6];
	}

	public synchronized String toString()
	{
		StringBuffer out=new StringBuffer();
		out.append('{');
		boolean first=true;
		for (int i=0; i<size(); i++) {
			if (get(i)) {
				if (first)
					first=false;
				else
					out.append(", ");
				out.append(i);
			}
		}
		out.append('}');
		return out.toString();
	}

	public synchronized boolean equals(Object o)
	{
		try {
			BitSet bs=(BitSet) o;
			for (int i=data.length; i<bs.data.length; i++) {
				if (bs.data[i]!=0) return false;
			}
			for (int i=bs.data.length; i<data.length; i++) {
				if (data[i]!=0) return false;
			}
			for (int i=0; i<bs.data.length && i<data.length; i++) {
				if (data[i]!=bs.data[i]) return false;
			}
			return true;
		} catch (ClassCastException e) {
			return false;
		} catch (NullPointerException e) {
			return false;
		}
	}

	public synchronized int hashCode()
	{
		long a=1234;
		for (int i=data.length-1; i>=0; i--) {
			a^=data[i]*(i+1);
		}
		return (int) ((a>>32) ^ a);
	}

	public synchronized Object clone()
	{
		BitSet bs=new BitSet(size());
		for (int i=data.length-1; i>=0; i--) {
			bs.data[i]=data[i];
		}
		return bs;
	}

	public synchronized boolean get(int index)
	{
		if (index >= size())
			return false;
		return (data[index>>6] & (1L << index)) != 0;
	}

	private synchronized void ensureSize(int index)
	{
		if (index < size())
			return;
		int newSize=data.length*2;
		if (newSize<((index+63)>>6))
			newSize=(index+63)>>6;
		long[] ll=new long[newSize];
		for (int i=0; i<data.length; i++) {
			ll[i]=data[i];
		}
		data=ll;
	}

	public synchronized void set(int index)
	{
		ensureSize(index);
		data[index>>6] |= (1L << index);
	}

	public synchronized void clear(int index)
	{
		ensureSize(index);
		data[index>>6] &= ~(1L << index);
	}

	public synchronized void and(BitSet bs)
	{
		for (int i=0; i<data.length && i<bs.data.length; i++) {
			data.length &= bs.data.length;
		}
		for (int i=bs.data.length; i<data.length; i++) {
			data[i]=0;
		}
	}

	public synchronized void or(BitSet bs)
	{
		ensureSize(bs.data.length);
		for (int i=0; i<bs.data.length; i++) {
			data.length |= bs.data.length;
		}
	}

	public synchronized void xor(BitSet bs)
	{
		ensureSize(bs.data.length);
		for (int i=0; i<bs.data.length; i++) {
			data.length ^= bs.data.length;
		}
	}

	public synchronized int size()
	{
		return data.length << 6;
	}

}