Browse Source

add generic array long type length

Boyoung- 9 years ago
parent
commit
39e52d8f85
4 changed files with 250 additions and 1 deletions
  1. 44 0
      src/Util/Array64.java
  2. 183 0
      src/oram/Tree.java
  3. 17 0
      test/Util/TestArray64.java
  4. 6 1
      test/misc/HelloWorld.java

+ 44 - 0
src/Util/Array64.java

@@ -0,0 +1,44 @@
+package Util;
+
+public class Array64<T> {
+	private final int CHUNK_SIZE = 1024 * 1024 * 1024;
+
+	private long size;
+	private T[][] data;
+
+	@SuppressWarnings("unchecked")
+	public Array64(long s) {
+		size = (s > 0) ? s : 0;
+		int chunks = (int) (size / CHUNK_SIZE);
+		int remainder = (int) (size % CHUNK_SIZE);
+		data = (T[][]) new Object[chunks + (remainder == 0 ? 0 : 1)][];
+		for (int i = 0; i < chunks; i++)
+			data[i] = (T[]) new Object[CHUNK_SIZE];
+		if (remainder != 0)
+			data[chunks] = (T[]) new Object[remainder];
+	}
+
+	public long size() {
+		return size;
+	}
+
+	// WARNING: shallow copy!
+	public T get(long index) {
+		if (index < 0 || index >= size)
+			throw new ArrayIndexOutOfBoundsException("" + index);
+		int chunk = (int) (index / CHUNK_SIZE);
+		int offset = (int) (index % CHUNK_SIZE);
+		return data[chunk][offset];
+		// return SerializationUtils.clone(data[chunk][offset]);
+	}
+
+	// WARNING: shallow copy!
+	public void set(long index, T item) {
+		if (index < 0 || index >= size)
+			throw new ArrayIndexOutOfBoundsException("" + index);
+		int chunk = (int) (index / CHUNK_SIZE);
+		int offset = (int) (index % CHUNK_SIZE);
+		data[chunk][offset] = item;
+		// data[chunk][offset] = SerializationUtils.clone(item);
+	}
+}

+ 183 - 0
src/oram/Tree.java

@@ -0,0 +1,183 @@
+package oram;
+
+import Util.Array64;
+
+public class Tree {
+	private int treeIndex;
+	private int w;
+	private int stashSize;
+	private int nBits;
+	private int lBits;
+	private int aBits;
+	private int nBytes;
+	private int lBytes;
+	private int aBytes;
+	private int tupleBytes;
+	private long numBuckets;
+	private int d;
+
+	private Array64<Bucket> buckets;
+
+	public Tree(int i, Metadata md) {
+		treeIndex = i;
+		w = md.getW();
+		stashSize = md.getStashSizeOfTree(i);
+		nBits = md.getNBitsOfTree(i);
+		lBits = md.getLBitsOfTree(i);
+		aBits = md.getABitsOfTree(i);
+		nBytes = md.getNBytesOfTree(i);
+		lBytes = md.getLBytesOfTree(i);
+		aBytes = md.getABytesOfTree(i);
+		tupleBytes = md.getTupleBytesOfTree(i);
+		numBuckets = md.getNumBucketsOfTree(i);
+		d = lBits + 1;
+
+		buckets = new Array64<Bucket>(numBuckets);
+
+	}
+
+	/*
+	private byte[] getByteBucket(long bucketNum) {
+		if (bucketNum < 0 || bucketNum >= ForestMetadata.getNumBuckets(index))
+			try {
+				throw new TreeException("Bucket number error");
+			} catch (TreeException e) {
+				e.printStackTrace();
+			}
+
+		int bucketBytes = ForestMetadata.getBucketBytes(index);
+		long start = ForestMetadata.getTreeOffset(index) + bucketNum * bucketBytes;
+		return Forest.getForestData(start, bucketBytes);
+	}
+
+	public Bucket getBucket(long bucketNum) {
+		return new Bucket(index, getByteBucket(bucketNum));
+	}
+
+	private void setByteBucket(byte[] bucket, long bucketNum) {
+		if (bucketNum < 0 || bucketNum >= ForestMetadata.getNumBuckets(index))
+			try {
+				throw new TreeException("Bucket number error");
+			} catch (TreeException e) {
+				e.printStackTrace();
+			}
+
+		int bucketBytes = ForestMetadata.getBucketBytes(index);
+		if (bucket.length != bucketBytes)
+			try {
+				throw new TreeException("Bucket length error");
+			} catch (TreeException e) {
+				e.printStackTrace();
+			}
+
+		long start = ForestMetadata.getTreeOffset(index) + bucketNum * bucketBytes;
+		Forest.setForestData(start, bucket);
+	}
+
+	public void setBucket(Bucket bucket, long bucketNum) {
+		setByteBucket(bucket.toByteArray(), bucketNum);
+	}
+
+	private List<Long> getBucketIndicesOnPath(long L) {
+		List<Long> indices = new ArrayList<Long>();
+		if (index == 0) {
+			indices.add(0L);
+			return indices;
+		}
+
+		if (L < 0 || L >= ForestMetadata.getNumLeaves(index))
+			try {
+				throw new TreeException("L=" + L + ": Invalid path");
+			} catch (TreeException e1) {
+				e1.printStackTrace();
+			}
+
+		int e = ForestMetadata.getLeafExpansion();
+		int lBits = ForestMetadata.getLBits(index);
+
+		for (int i = 0; i < lBits; i++) {
+			long bucketIndex = (L >> (lBits - i)) + (long) Math.pow(2, i) - 1;
+			indices.add(bucketIndex);
+		}
+
+		long bucketIndex = ForestMetadata.getNumLeaves(index) - 1 + L * e;
+		for (int i = 0; i < e; i++)
+			indices.add(bucketIndex + i);
+
+		return indices;
+	}
+
+	public Bucket[] getBucketsOnPath(BigInteger L) {
+		if (L == null && index != 0) {
+			try {
+				throw new TreeException("L is null");
+			} catch (TreeException e) {
+				e.printStackTrace();
+			}
+			return null;
+		} else if (L == null && index == 0)
+			return getBucketsOnPath(0);
+		else
+			return getBucketsOnPath(L.longValue());
+	}
+
+	public Bucket[] getBucketsOnPath(long L) {
+		List<Long> indices = getBucketIndicesOnPath(L);
+		Bucket[] buckets = new Bucket[indices.size()];
+		for (int i = 0; i < indices.size(); i++)
+			buckets[i] = getBucket(indices.get(i));
+		return buckets;
+	}
+
+	public Bucket[] getBucketsOnPath(String L) throws TreeException, BucketException {
+		if (L == null)
+			throw new TreeException("L is null");
+		if (L.equals("") && index != 0)
+			throw new TreeException("Invalid L");
+
+		if (L.equals(""))
+			return getBucketsOnPath(0);
+		return getBucketsOnPath(new BigInteger(L, 2).longValue());
+	}
+
+	public void setBucketsOnPath(Bucket[] buckets, BigInteger L) {
+		if (L == null && index != 0)
+			try {
+				throw new TreeException("L is null");
+			} catch (TreeException e) {
+				e.printStackTrace();
+			}
+		else if (L == null && index == 0) {
+			setBucketsOnPath(buckets, 0);
+			return;
+		} else
+			setBucketsOnPath(buckets, L.longValue());
+	}
+
+	public void setBucketsOnPath(Bucket[] buckets, long L) {
+		List<Long> indices = getBucketIndicesOnPath(L);
+		if (indices.size() != buckets.length)
+			try {
+				throw new TreeException("Number of buckets is not correct");
+			} catch (TreeException e) {
+				e.printStackTrace();
+			}
+
+		for (int i = 0; i < indices.size(); i++)
+			setBucket(buckets[i], indices.get(i));
+	}
+
+	public void setBucketsOnPath(Bucket[] buckets, String L) throws TreeException {
+		if (L == null)
+			throw new TreeException("L is null");
+		if (L.equals("") && index != 0)
+			throw new TreeException("Invalid L");
+
+		if (L.equals("")) {
+			setBucketsOnPath(buckets, 0);
+			return;
+		}
+		setBucketsOnPath(buckets, new BigInteger(L, 2).longValue());
+	}
+*/
+}

+ 17 - 0
test/Util/TestArray64.java

@@ -0,0 +1,17 @@
+package Util;
+
+public class TestArray64 {
+
+	public static void main(String[] args) {
+		long size = (long) Math.pow(2, 20);
+		Array64<Integer> arr = new Array64<Integer>(size);
+		long pos = (long) Math.pow(2, 19);
+
+		Integer a = 1;
+		arr.set(pos, a);
+		Integer b = arr.get(pos);
+		a++;
+		System.out.println(a + " " + b);
+	}
+
+}

+ 6 - 1
test/misc/HelloWorld.java

@@ -7,9 +7,14 @@ public class HelloWorld {
 	public static void main(String[] args) {
 		System.out.println("HelloWorld!");
 
-		byte[] tmp = new byte[0];
+		byte[] tmp = new byte[3];
 		BigInteger bi = new BigInteger(1, tmp);
 		System.out.println(bi.toByteArray().length);
+
+		// System.out.println(tmp[3]);
+
+		// System.out.println(Arrays.copyOfRange(tmp, 5, 6).length);
+		throw new ArrayIndexOutOfBoundsException("" + 11);
 	}
 
 }