|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | + |
| 4 | +public class RList<T> { |
| 5 | + final int DEFAULT_SIZE = 5; |
| 6 | + final int multiplier = 2; |
| 7 | + private int internalSize = DEFAULT_SIZE; |
| 8 | + T[] list; |
| 9 | + int size; |
| 10 | + public RList() { |
| 11 | + this.list = (T[]) new Object[DEFAULT_SIZE]; // Hack |
| 12 | + size = 0; |
| 13 | + } |
| 14 | + public RList(int init_size) { |
| 15 | + this.list = (T[]) new Object[init_size]; // Hack |
| 16 | + size = 0; |
| 17 | + } |
| 18 | + public RList(RList<T> r) { |
| 19 | + this.list = r.list; |
| 20 | + size = r.size; |
| 21 | + } |
| 22 | + public void recreateArray(int size) { |
| 23 | + internalSize = size; |
| 24 | + T[] temp = (T[]) new Object[size]; // Hack |
| 25 | + for(int i = 0; i < this.list.length; i ++) { |
| 26 | + temp[i] = this.list[i]; |
| 27 | + } |
| 28 | + this.list = temp; |
| 29 | + |
| 30 | + } |
| 31 | + public void add(T item) { |
| 32 | + this.size ++; |
| 33 | + if(this.size > this.list.length) { |
| 34 | + System.out.println("Extending Array"); |
| 35 | + this.recreateArray(internalSize * multiplier); |
| 36 | + } |
| 37 | + } |
| 38 | + public T get(int i) throws ArrayIndexOutOfBoundsException{ |
| 39 | + if(i < size) { |
| 40 | + return this.list[i]; |
| 41 | + }else { |
| 42 | + throw new ArrayIndexOutOfBoundsException(i+" is out of bounds sry. "); |
| 43 | + } |
| 44 | + } |
| 45 | + // Release must be private |
| 46 | + public void printStats() { |
| 47 | + System.out.println("Internal Size: "+internalSize); |
| 48 | + } |
| 49 | + public void insert(int index, T item) { |
| 50 | + |
| 51 | + } |
| 52 | + public void shift(int start, int end, int newIndex) { |
| 53 | + if(end + newIndex > internalSize) { |
| 54 | + this.recreateArray(internalSize * multiplier); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + assert start < end; |
| 59 | + T[] newArr = this.list.clone(); |
| 60 | + for(int i = start; i < end; i++) { |
| 61 | + |
| 62 | + } |
| 63 | + } |
| 64 | + public static void main(String[] args) { |
| 65 | + long t1 = System.currentTimeMillis(); |
| 66 | + RList<Integer> l = new RList<Integer>(); |
| 67 | + for(int i = 0; i < 99999; i ++) { |
| 68 | + l.add(i); |
| 69 | + |
| 70 | + } |
| 71 | + long t2 = System.currentTimeMillis(); |
| 72 | + System.out.println(l.size + " RList took "+(t2-t1)+"ms "); |
| 73 | + l.printStats(); |
| 74 | + t1 = System.currentTimeMillis(); |
| 75 | + List<Integer> arraylist = new ArrayList<Integer>(); |
| 76 | + for(int i = 0; i < 99999; i ++) { |
| 77 | + arraylist.add(i); |
| 78 | + } |
| 79 | + t2 = System.currentTimeMillis(); |
| 80 | + System.out.println("ArrayList took "+(t2-t1)+"ms \n\n\n"); |
| 81 | + //System.out.println(l.list.length); |
| 82 | + System.out.println("Performing get benchmark"); |
| 83 | + t1 = System.currentTimeMillis(); |
| 84 | + for(int i = 0; i < 99999; i++) { |
| 85 | + l.get(i); |
| 86 | + } |
| 87 | + t2 = System.currentTimeMillis(); |
| 88 | + System.out.println("RList took "+(t2-t1)+"ms "); |
| 89 | + t1 = System.currentTimeMillis(); |
| 90 | + for(int i = 0; i < 99999; i++) { |
| 91 | + arraylist.get(i); |
| 92 | + } |
| 93 | + t2 = System.currentTimeMillis(); |
| 94 | + System.out.println("ArrayList took "+(t2-t1)+"ms "); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | +} |
0 commit comments