Removed ShiftingMap (project for another day)
Removed Shifting Set Removed Shifting Entry Added Shifting List inheriting most of it's functionality from the Shifting Set Optimized load balancing algorithm to ensure that array isn't unnecessarily resized Optimized load balancing algorithm by simplifying load calculation Added new List Iterator in addition to regular Iterator
This commit is contained in:
parent
d7d49e8ee2
commit
d7660799be
@ -1,35 +0,0 @@
|
||||
package com.tofvesson.collections;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static com.tofvesson.collections.ShiftingSet.empty;
|
||||
|
||||
public class ShiftingEntry<K, V> implements Map.Entry<K, V>{
|
||||
|
||||
private final ShiftingSet<K> keys;
|
||||
private final ShiftingSet<V> values;
|
||||
int pos;
|
||||
|
||||
public ShiftingEntry(ShiftingSet<K> keys, ShiftingSet<V> values, int pos){
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getKey() {
|
||||
return keys.entries[pos]==empty?null:(K)keys.entries[pos];
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
return values.entries[pos]==empty?null:(V)values.entries[pos];
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue(V value) {
|
||||
V v = getValue();
|
||||
if(keys.entries[pos]!=null) values.entries[pos] = value;
|
||||
return v;
|
||||
}
|
||||
}
|
354
src/com/tofvesson/collections/ShiftingList.java
Normal file
354
src/com/tofvesson/collections/ShiftingList.java
Normal file
@ -0,0 +1,354 @@
|
||||
package com.tofvesson.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ShiftingList<E> implements List<E> {
|
||||
/**
|
||||
* Holder for entries. Dur to reasons, the array holds objects.
|
||||
*/
|
||||
Object[] entries;
|
||||
|
||||
/**
|
||||
* Populated entries.
|
||||
*/
|
||||
int pop = 0;
|
||||
|
||||
/**
|
||||
* Maximum size of set.
|
||||
*/
|
||||
final int maxSize;
|
||||
|
||||
/**
|
||||
* Load factor used when calculating how to resize array.
|
||||
*/
|
||||
double load;
|
||||
|
||||
/**
|
||||
* Internal reference to reserved, unpopulated entries.
|
||||
*/
|
||||
static final Empty empty = new Empty();
|
||||
|
||||
public ShiftingList(int maxSize, double load){
|
||||
this.maxSize = maxSize<1?20:maxSize;
|
||||
this.load = load>0.99||load<0.1?0.75:load;
|
||||
entries = new Object[1];
|
||||
}
|
||||
|
||||
public ShiftingList(int maxSize){
|
||||
this(maxSize, 0.75);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return pop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return pop==0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
if(o==empty) return false;
|
||||
for(Object o1 : entries) if(o.equals(o1)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
Object[] o = new Object[pop];
|
||||
System.arraycopy(entries, 0, o, 0, pop);
|
||||
for(int i = 0; i<o.length; ++i) if(o[i]==empty) o[i] = null;
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
for(int i = 0; i<Math.min(pop, a.length); ++i) a[i] = (T) entries[i];
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
preparePopulate(1);
|
||||
entries[0] = e!=null?e:empty;
|
||||
pop=pop!=maxSize?pop+1:pop;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(entries[i]==o){
|
||||
entries[i]=null;
|
||||
if(pop<entries.length*load) {
|
||||
shift();
|
||||
adaptLoad(-1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
boolean b = true;
|
||||
for(Object o : c) b &= contains(o);
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
ArrayList<? extends E> l = new ArrayList<>(c);
|
||||
preparePopulate(c.size());
|
||||
for(int i = 0; i<Math.min(entries.length, c.size()); ++i) entries[i] = l.get(i);
|
||||
pop=Math.min(pop+c.size(), maxSize);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends E> c) {
|
||||
if(index>=entries.length || index<0 || c.size()==0) return false;
|
||||
ArrayList<Object> l = new ArrayList<>(c);
|
||||
for(int i = 0; i<l.size(); ++i) if(l.get(i)==null) l.set(i, empty);
|
||||
if(pop+l.size()>maxSize) for(int i = pop+l.size()-maxSize; i<l.size(); ++i) l.remove(l.size());
|
||||
pop = pop==maxSize?pop:pop+l.size();
|
||||
if(pop==entries.length) adaptLoad();
|
||||
if(index==entries.length-1){
|
||||
entries[index] = l.get(0);
|
||||
return true;
|
||||
}
|
||||
if(l.size()+index<entries.length)
|
||||
System.arraycopy(entries, index, entries, index+1, entries.length-(index+l.size()));
|
||||
for(int i = index; i<Math.min(index+l.size(), entries.length); ++i) entries[i] = l.get(i);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
int removed = 0;
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(!c.contains(entries[i])) {
|
||||
entries[i] = null;
|
||||
++removed;
|
||||
}
|
||||
if(removed==0) return false;
|
||||
shift();
|
||||
if((pop-=removed)<entries.length*load) adaptLoad();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
int removed = 0;
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(c.contains(entries[i])) {
|
||||
entries[i] = null;
|
||||
++removed;
|
||||
}
|
||||
if(removed==0) return false;
|
||||
shift();
|
||||
if((pop-=removed)<entries.length*load) adaptLoad();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
pop = 0;
|
||||
entries = new Object[1];
|
||||
}
|
||||
|
||||
public int indexOf(Object o){
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(entries[i].equals(o)) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts array size according to currently populated entries.
|
||||
* Meant to be used when populated entries variable has already been updated.
|
||||
* Same as invoking {@link #adaptLoad(int)} with parameter 0.
|
||||
*/
|
||||
protected void adaptLoad(){
|
||||
adaptLoad(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes size of array to account for new entries.
|
||||
* @param accountFor The amount new elements to be accounted for.
|
||||
*/
|
||||
protected void adaptLoad(int accountFor){
|
||||
if(pop+accountFor<=0){
|
||||
entries = new Object[1];
|
||||
return;
|
||||
}
|
||||
Object[] o = new Object[(int) Math.max(1, Math.min((pop+accountFor)/load, maxSize))]; // Load adaptation algorithm capping at maxSize or 0
|
||||
System.arraycopy(entries, 0, o, 0, Math.min(o.length, entries.length)); // Move as many entries as possible
|
||||
}
|
||||
|
||||
/**
|
||||
* Shift all elements towards the start of the arrays.
|
||||
*/
|
||||
protected void shift(){
|
||||
for(int i = 0; i<entries.length; ++i)
|
||||
if(entries[i]==null && i!=pop-1)
|
||||
for(int j = i; j<entries.length; ++j)
|
||||
if(entries[j]!=null){
|
||||
entries[i] = entries[j];
|
||||
entries[i] = null;
|
||||
break;
|
||||
}else if(j+1==entries.length) return; // Found all populated entries
|
||||
}
|
||||
|
||||
protected void preparePopulate(int accountFor){
|
||||
if(accountFor>entries.length) adaptLoad(accountFor); // If new elements exceed limit, adapt load
|
||||
if(accountFor>entries.length) return; // No need to delete elements if new values exceed limit
|
||||
System.arraycopy(entries, 0, entries, accountFor, entries.length-accountFor); // Shift array elements to account for new elements
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int i){
|
||||
if(i>pop) return null;
|
||||
return entries[i]==empty?null:(E)entries[i];
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
if(index > pop) return null;
|
||||
E e = get(index);
|
||||
entries[index] = element;
|
||||
return e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
if(index>=entries.length || index<0) return;
|
||||
Object o = element==null?empty:element;
|
||||
pop = pop==maxSize?pop:pop+1;
|
||||
if(pop==entries.length) adaptLoad();
|
||||
if(index==entries.length-1){
|
||||
entries[index] = o;
|
||||
return;
|
||||
}
|
||||
System.arraycopy(entries, index, entries, index+1, entries.length-(index+1));
|
||||
entries[index] = o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
if(index>pop || index<0) return null;
|
||||
E e = entries[index]==empty?null:(E)entries[index];
|
||||
entries[index] = null;
|
||||
shift();
|
||||
if(--pop<entries.length*load) adaptLoad();
|
||||
return e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
for(int i = pop; i>0; --i)
|
||||
if(o.equals(entries[i]))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator(){
|
||||
return new Iterator<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator() {
|
||||
return new ListIterator<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator(int index) {
|
||||
if(index<0) throw new RuntimeException("Invalid starting point for iterator defined: "+index);
|
||||
return new ListIterator<>(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> subList(int fromIndex, int toIndex) {
|
||||
if(fromIndex<0 || fromIndex>=toIndex || fromIndex>pop || toIndex>pop) return new ArrayList<>();
|
||||
ShiftingList<E> l = new ShiftingList<>(maxSize);
|
||||
for(int i = toIndex-1; i>fromIndex; --i) l.add(entries[i]==empty?null:(E)entries[i]);
|
||||
return l;
|
||||
}
|
||||
|
||||
public class Iterator<V> implements java.util.Iterator<V>{
|
||||
|
||||
int counter = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return counter<ShiftingList.this.pop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V next() {
|
||||
return entries[counter++]==empty?null:(V)entries[counter];
|
||||
}
|
||||
}
|
||||
|
||||
public class ListIterator<V> implements java.util.ListIterator<V>{
|
||||
|
||||
protected int counter = 0;
|
||||
protected boolean opNxt = false;
|
||||
private Object pEl = null;
|
||||
|
||||
public ListIterator(){}
|
||||
public ListIterator(int start){ counter = start; }
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return counter<ShiftingList.this.pop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V next() {
|
||||
opNxt = true;
|
||||
return (V)(pEl=entries[counter++]==empty?null:entries[counter-1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return counter>0&&ShiftingList.this.pop!=0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V previous() {
|
||||
opNxt = false;
|
||||
return (V)(pEl=entries[--counter]==empty?null:entries[counter]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return counter+1<pop?counter+1:pop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return counter!=0?counter-1:0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
ShiftingList.this.remove(counter-(opNxt?0:1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(V v) {
|
||||
if(pEl==entries[counter-(opNxt?0:1)]) entries[counter-(opNxt?0:1)] = v==null?empty:v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(V v) {
|
||||
ShiftingList.this.add(counter, (E)v);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
package com.tofvesson.collections;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static com.tofvesson.collections.ShiftingSet.empty;
|
||||
|
||||
public class ShiftingMap<K, V> implements Map<K, V> {
|
||||
|
||||
protected final ShiftingSet<K> keys;
|
||||
protected final ShiftingSet<V> values;
|
||||
protected final ShiftingSet<Entry<K, V>> entries;
|
||||
|
||||
public ShiftingMap(int maxSize, double load){
|
||||
keys = new ShiftingSet<>(maxSize, load);
|
||||
values = new ShiftingSet<>(maxSize, load);
|
||||
entries = new ShiftingSet<>(maxSize, load);
|
||||
}
|
||||
|
||||
public ShiftingMap(int maxSize){
|
||||
this(maxSize, 0.75);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return keys.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return keys.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return keys.contains(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return values.contains(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
int i = keys.indexOf(key);
|
||||
return i!=-1?(V)values.entries[i]:null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) {
|
||||
V v=null;
|
||||
int i = keys.indexOf(key);
|
||||
if(i!=-1){
|
||||
v = values.entries[i]!= empty?(V)values.entries[i]:null;
|
||||
values.entries[i] = value!=null?value:empty;
|
||||
}else{
|
||||
for(Entry e : entries) ++((ShiftingEntry)e).pos;
|
||||
entries.add(new ShiftingEntry<>(keys, values, 0));
|
||||
keys.add(key);
|
||||
values.add(value);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(Object key) {
|
||||
V v = get(key);
|
||||
if(keys.contains(key)){
|
||||
values.entries[keys.indexOf(key)] = null;
|
||||
values.shift();
|
||||
values.adaptLoad(-1);
|
||||
keys.remove(key);
|
||||
entries.entries[entries.size()-1] = null;
|
||||
entries.adaptLoad(-1);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends K, ? extends V> m) {
|
||||
keys.stream().filter(m::containsKey).forEach(k -> {
|
||||
put(k, m.get(k));
|
||||
m.remove(k);
|
||||
});
|
||||
if(m.size()<entries.maxSize) for(Entry e : entries) ((ShiftingEntry)e).pos += m.size();
|
||||
for(int i = 0; i<m.size(); ++i) entries.add(new ShiftingEntry<>(keys, values, m.size()-i));
|
||||
keys.addAll(m.keySet());
|
||||
values.addAll(m.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
entries.clear();
|
||||
keys.clear();
|
||||
values.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
return keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return entries;
|
||||
}
|
||||
}
|
@ -1,217 +0,0 @@
|
||||
package com.tofvesson.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
public class ShiftingSet<E> implements Set<E> {
|
||||
|
||||
/**
|
||||
* Holder for entries. Dur to reasons, the array holds objects.
|
||||
*/
|
||||
Object[] entries;
|
||||
|
||||
/**
|
||||
* Populated entries.
|
||||
*/
|
||||
int pop = 0;
|
||||
|
||||
/**
|
||||
* Maximum size of set.
|
||||
*/
|
||||
final int maxSize;
|
||||
|
||||
/**
|
||||
* Load factor used when calculating how to resize array.
|
||||
*/
|
||||
double load;
|
||||
|
||||
static final Empty empty = new Empty();
|
||||
|
||||
public ShiftingSet(int maxSize, double load){
|
||||
this.maxSize = maxSize>1?20:maxSize;
|
||||
this.load = load>1||load<0.1?0.75:load;
|
||||
entries = new Object[1];
|
||||
}
|
||||
|
||||
public ShiftingSet(int maxSize){
|
||||
this(maxSize, 0.75);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return pop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return pop==0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
if(o==empty) return false;
|
||||
for(Object o1 : entries) if(o.equals(o1)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
Object[] o = new Object[pop];
|
||||
System.arraycopy(entries, 0, o, 0, pop);
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
for(int i = 0; i<Math.min(pop, a.length); ++i) a[i] = (T) entries[i];
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
preparePopulate(1);
|
||||
entries[0] = e!=null?e:empty;
|
||||
pop=pop!=maxSize?pop+1:pop;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(entries[i]==o){
|
||||
entries[i]=null;
|
||||
shift();
|
||||
adaptLoad(-1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
boolean b = true;
|
||||
for(Object o : c) b &= contains(o);
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
ArrayList<? extends E> l = new ArrayList<>(c);
|
||||
preparePopulate(c.size());
|
||||
for(int i = 0; i<Math.min(entries.length, c.size()); ++i) entries[i] = l.get(i);
|
||||
pop=Math.min(pop+c.size(), maxSize);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
int removed = 0;
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(!c.contains(entries[i])) {
|
||||
entries[i] = null;
|
||||
++removed;
|
||||
}
|
||||
if(removed==0) return false;
|
||||
shift();
|
||||
adaptLoad(-removed);
|
||||
pop -= removed;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
int removed = 0;
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(c.contains(entries[i])) {
|
||||
entries[i] = null;
|
||||
++removed;
|
||||
}
|
||||
if(removed==0) return false;
|
||||
shift();
|
||||
adaptLoad(-removed);
|
||||
pop -= removed;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
pop = 0;
|
||||
entries = new Object[1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if(!(o instanceof ShiftingSet) || ((ShiftingSet) o).pop!=pop ||
|
||||
((ShiftingSet) o).load!=load || ((ShiftingSet) o).maxSize!=maxSize) return false; // Check for matching parameters
|
||||
for(int i = 0; i<pop; ++i) if(entries[i]!=((ShiftingSet) o).entries[i]) return false; // Check for matching data
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hc = 0;
|
||||
for(int i = 0; i<pop; ++i) hc+=entries[i]!=null?entries[i].hashCode():0;
|
||||
return hc;
|
||||
}
|
||||
|
||||
public int indexOf(Object o){
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(entries[i].equals(o)) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes size of array to account for new entries.
|
||||
* @param accountFor The amount new elements to be accounted for.
|
||||
*/
|
||||
protected void adaptLoad(int accountFor){
|
||||
if(accountFor==0) throw new RuntimeException("Invalid load adaptation value specified!");
|
||||
if(pop+accountFor<=0){
|
||||
entries = new Object[1];
|
||||
return;
|
||||
}
|
||||
Object[] o = new Object[(int) Math.max(1, Math.min(100/(100/(pop+accountFor)*load), maxSize))]; // Load adaptation algorithm capping at maxSize or 0
|
||||
System.arraycopy(entries, 0, o, 0, Math.min(o.length, entries.length)); // Move as many entries as possible
|
||||
}
|
||||
|
||||
/**
|
||||
* Shift all elements towards the start of the arrays.
|
||||
*/
|
||||
protected void shift(){
|
||||
for(int i = 0; i<pop; ++i)
|
||||
if(entries[i]==null && i!=pop-1)
|
||||
for(int j = i; j<pop; ++j)
|
||||
if(entries[j]!=null){
|
||||
entries[i] = entries[j];
|
||||
entries[i] = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected void preparePopulate(int accountFor){
|
||||
if(accountFor>entries.length) adaptLoad(accountFor); // If new elements exceed limit, adapt load
|
||||
if(accountFor>entries.length) return; // If the expected new load still exceeds the limit, no need to delete elements
|
||||
System.arraycopy(entries, 0, entries, accountFor, entries.length-accountFor); // Shift array elements to account for new elements
|
||||
}
|
||||
|
||||
public class Iterator<E> implements java.util.Iterator<E>{
|
||||
|
||||
int counter = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return counter<ShiftingSet.this.pop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
return (E) entries[counter++];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user