This repository was archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRList.java
More file actions
98 lines (89 loc) · 2.49 KB
/
RList.java
File metadata and controls
98 lines (89 loc) · 2.49 KB
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
import java.util.ArrayList;
import java.util.List;
public class RList<T> {
final int DEFAULT_SIZE = 5;
final int multiplier = 2;
private int internalSize = DEFAULT_SIZE;
T[] list;
int size;
public RList() {
this.list = (T[]) new Object[DEFAULT_SIZE]; // Hack
size = 0;
}
public RList(int init_size) {
this.list = (T[]) new Object[init_size]; // Hack
size = 0;
}
public RList(RList<T> r) {
this.list = r.list;
size = r.size;
}
public void recreateArray(int size) {
internalSize = size;
T[] temp = (T[]) new Object[size]; // Hack
for(int i = 0; i < this.list.length; i ++) {
temp[i] = this.list[i];
}
this.list = temp;
}
public void add(T item) {
this.size ++;
if(this.size > this.list.length) {
System.out.println("Extending Array");
this.recreateArray(internalSize * multiplier);
}
}
public T get(int i) throws ArrayIndexOutOfBoundsException{
if(i < size) {
return this.list[i];
}else {
throw new ArrayIndexOutOfBoundsException(i+" is out of bounds sry. ");
}
}
// Release must be private
public void printStats() {
System.out.println("Internal Size: "+internalSize);
}
public void insert(int index, T item) {
}
public void shift(int start, int end, int newIndex) {
if(end + newIndex > internalSize) {
this.recreateArray(internalSize * multiplier);
}
assert start < end;
T[] newArr = this.list.clone();
for(int i = start; i < end; i++) {
}
}
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
RList<Integer> l = new RList<Integer>();
for(int i = 0; i < 99999; i ++) {
l.add(i);
}
long t2 = System.currentTimeMillis();
System.out.println(l.size + " RList took "+(t2-t1)+"ms ");
l.printStats();
t1 = System.currentTimeMillis();
List<Integer> arraylist = new ArrayList<Integer>();
for(int i = 0; i < 99999; i ++) {
arraylist.add(i);
}
t2 = System.currentTimeMillis();
System.out.println("ArrayList took "+(t2-t1)+"ms \n\n\n");
//System.out.println(l.list.length);
System.out.println("Performing get benchmark");
t1 = System.currentTimeMillis();
for(int i = 0; i < 99999; i++) {
l.get(i);
}
t2 = System.currentTimeMillis();
System.out.println("RList took "+(t2-t1)+"ms ");
t1 = System.currentTimeMillis();
for(int i = 0; i < 99999; i++) {
arraylist.get(i);
}
t2 = System.currentTimeMillis();
System.out.println("ArrayList took "+(t2-t1)+"ms ");
}
}