Created
September 24, 2014 01:16
-
-
Save qcom/7de04bbc19433c5b4c8a to your computer and use it in GitHub Desktop.
ordered vector implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package data_structures; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
import data_structures.*; | |
public class OrderedVector<E> implements Iterable<E> { | |
private E[] arr; | |
private int size; | |
private void checkBounds(int i) { | |
if (i > size - 1) throw new IndexOutOfBoundsException(); | |
} | |
private int binarySearch(E[] arr, E obj, int lo, int hi, boolean findPlace) { | |
if (lo > hi) return findPlace ? lo : -1; | |
int mid = (lo + hi) >> 1; | |
int comp = arr[mid] == null ? -1 : ((Comparable<E>) obj).compareTo(arr[mid]); | |
if (comp == 0) return mid; | |
return comp < 0 ? binarySearch(arr, obj, lo, mid - 1, findPlace) : binarySearch(arr, obj, mid + 1, hi, findPlace); | |
} | |
private int binSearchFindPlace(E obj) { | |
return binarySearch(arr, obj, 0, arr.length - 1, true); | |
} | |
private int binSearch(E obj) { | |
return binarySearch(arr, obj, 0, arr.length - 1, false); | |
} | |
private void removeFromArr(int index) { | |
E[] newArr = (E[]) new Object[arr.length]; | |
boolean set = false; | |
for (int i = 0; i < arr.length; i++) { | |
if (i == index) set = true; | |
else newArr[set ? i - 1 : i] = arr[i]; | |
} | |
arr = newArr; | |
if ((((float) --size) / ((float) arr.length)) < .25) shrinkArr(); | |
} | |
private void shrinkArr() { | |
copyArr(arr.length >> 1); | |
} | |
private void expandArr() { | |
copyArr(arr.length << 1); | |
} | |
private void copyArr(int size) { | |
E[] newArr = (E[]) new Object[size]; | |
for (int i = 0; i < arr.length; i++) newArr[i] = arr[i]; | |
arr = newArr; | |
} | |
public OrderedVector() { | |
clear(); | |
} | |
public void insert(E obj) { | |
if (size + 1 == arr.length) expandArr(); | |
int position = binSearchFindPlace(obj); | |
E[] newArr = (E[]) new Object[arr.length]; | |
boolean set = false; | |
for (int i = 0; i < arr.length; i++) { | |
if (i == position) { | |
newArr[i] = obj; | |
newArr[i + 1] = arr[i++]; | |
set = true; | |
} else { | |
newArr[i] = arr[set ? i - 1 : i]; | |
} | |
} | |
arr = newArr; | |
size++; | |
} | |
public E remove(int index) { | |
checkBounds(index); | |
E found = arr[index]; | |
removeFromArr(index); | |
return found; | |
} | |
public E remove(E obj) { | |
int position = binSearch(obj); | |
if (position == -1) return null; | |
removeFromArr(position); | |
return obj; | |
} | |
public E get(int index) { | |
checkBounds(index); | |
return arr[index]; | |
} | |
public E get(E obj) { | |
int position = binSearch(obj); | |
if (position == -1) return null; | |
return arr[position]; | |
} | |
public boolean contains(E obj) { | |
int position = binSearch(obj); | |
return position != -1; | |
} | |
public void clear() { | |
arr = (E[]) new Object[100]; | |
size = 0; | |
} | |
public boolean isEmpty() { | |
return size == 0; | |
} | |
public int size() { | |
return size; | |
} | |
public Iterator iterator() { | |
return new Iterator<E>() { | |
private int iteratedOver = 0; | |
public boolean hasNext() { | |
return iteratedOver < size; | |
} | |
public E next() { | |
return arr[iteratedOver++]; | |
} | |
public void remove() { throw new UnsupportedOperationException(); } | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment