001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: public class OidLongArray {
007: public static final int BytesPerLong = 8;
008: public static final int BitsPerLong = BytesPerLong * 8;
009:
010: private long key;
011: private long[] ary;
012:
013: public OidLongArray(int size, long key) {
014: ary = new long[size];
015: this .key = key;
016: }
017:
018: public OidLongArray(byte[] key, byte[] value) {
019: // ary null is used as an indicator for end of processing
020: if (key == null || value == null) {
021: ary = null;
022: return;
023: }
024:
025: this .key = Conversion.bytes2Long(key);
026: ary = new long[value.length / BytesPerLong];
027: for (int i = 0; i < ary.length; ++i) {
028: ary[i] = Conversion.bytes2Long(value, i * BytesPerLong);
029: }
030: }
031:
032: private long bit(int bitIndex) {
033: Assert.assertTrue("Bit index out of range", bitIndex >= 0);
034: Assert.assertTrue("Bit index out of range",
035: bitIndex < BitsPerLong);
036: return 1L << bitIndex;
037: }
038:
039: public void setKey(long key) {
040: this .key = key;
041: }
042:
043: public long getKey() {
044: return (this .key);
045: }
046:
047: public long[] getArray() {
048: return (this .ary);
049: }
050:
051: public byte[] keyToBytes() {
052: return Conversion.long2Bytes(key);
053: }
054:
055: public byte[] arrayToBytes() {
056: byte[] data = new byte[length() * BytesPerLong];
057: for (int i = 0; i < length(); ++i) {
058: Conversion.writeLong(ary[i], data, i * BytesPerLong);
059: }
060: return (data);
061: }
062:
063: public void copyOut(OidLongArray dest, int offset) {
064: for (int i = 0; i < dest.length(); ++i) {
065: dest.set(i, ary[offset + i]);
066: }
067: }
068:
069: public void applyIn(OidLongArray src, int offset) {
070: for (int i = 0; i < src.length(); ++i) {
071: ary[i + offset] |= src.get(i);
072: }
073: }
074:
075: public boolean isZero() {
076: for (int i = 0; i < length(); ++i) {
077: if (ary[i] != 0)
078: return (false);
079: }
080: return (true);
081: }
082:
083: // use null array as an indicator of end of record
084: public boolean isEnded() {
085: return (ary == null);
086: }
087:
088: public long get(int index) {
089: return ary[index];
090: }
091:
092: public void set(int index, long val) {
093: ary[index] = val;
094: }
095:
096: public int length() {
097: return ary.length;
098: }
099:
100: public long setBit(int bit) {
101: int byteIndex = bit / BitsPerLong;
102: int bitIndex = bit % BitsPerLong;
103: ary[byteIndex] |= bit(bitIndex);
104: return (ary[byteIndex]);
105: }
106:
107: public long clrBit(int bit) {
108: int byteIndex = bit / BitsPerLong;
109: int bitIndex = bit % BitsPerLong;
110: ary[byteIndex] &= ~bit(bitIndex);
111: return (ary[byteIndex]);
112: }
113:
114: public boolean isSet(int bit) {
115: int byteIndex = bit / BitsPerLong;
116: int bitIndex = bit % BitsPerLong;
117: return ((ary[byteIndex] & bit(bitIndex)) != 0);
118: }
119:
120: public int totalBits() {
121: return ary.length * BitsPerLong;
122: }
123:
124: }
|