Fix event list range iteration and navigation
This commit is contained in:
1 parent
f4908e45ba
commit
de66bb2580
2 files changed
+82
-128
No files matched your search
+47
-128
@@ -3,193 +3,112 @@ package com.minres.scviewer.database;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
/** Sorted timestamp buckets. Range views are read-only and bounded by their parent. */
|
||||||
public class EventList implements IEventList {
|
public class EventList implements IEventList {
|
||||||
|
private final ArrayList<EventEntry> store;
|
||||||
ArrayList<EventEntry> store = new ArrayList<>();
|
private final int start;
|
||||||
int start = 0;
|
private int end;
|
||||||
int end = store.size();
|
private final boolean unmodifiable;
|
||||||
boolean unmodifiable = false;
|
|
||||||
|
|
||||||
public class Iterator implements java.util.Iterator<EventEntry> {
|
|
||||||
|
|
||||||
EventList list;
|
|
||||||
|
|
||||||
private int pos;
|
|
||||||
|
|
||||||
public Iterator(EventList list) {
|
|
||||||
this.list=list;
|
|
||||||
this.pos=-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasNext() {
|
|
||||||
return pos<(list.end-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EventEntry next() {
|
|
||||||
if(hasNext()) {
|
|
||||||
pos++;
|
|
||||||
return list.store.get(pos);
|
|
||||||
} else
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void remove() {
|
|
||||||
list.store.remove(pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public EventList() {
|
public EventList() {
|
||||||
|
this(new ArrayList<>(), 0, 0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private EventList(ArrayList<EventEntry> store , int start, int end) {
|
private EventList(ArrayList<EventEntry> store, int start, int end, boolean unmodifiable) {
|
||||||
this.store = store;
|
this.store = store;
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.end = end;
|
this.end = end;
|
||||||
this.unmodifiable=true;
|
this.unmodifiable = unmodifiable;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Index of the first timestamp >= key (or > key when exclusive). */
|
||||||
|
private int bound(long key, boolean exclusive) {
|
||||||
|
int low = start, high = end;
|
||||||
|
while (low < high) {
|
||||||
|
int mid = (low + high) >>> 1;
|
||||||
|
int cmp = Long.compareUnsigned(store.get(mid).timestamp, key);
|
||||||
|
if (cmp < 0 || (exclusive && cmp == 0)) low = mid + 1;
|
||||||
|
else high = mid;
|
||||||
|
}
|
||||||
|
return low;
|
||||||
|
}
|
||||||
|
|
||||||
|
private EventEntry entry(int index) {
|
||||||
|
return index < start || index >= end ? null : store.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IEventList subMap(long from, boolean includeFrom, long to) {
|
public IEventList subMap(long from, boolean includeFrom, long to) {
|
||||||
//the index of the first element greater than the key, or list.size()
|
if (Long.compareUnsigned(from, to) > 0) throw new IllegalArgumentException("Reversed time range");
|
||||||
int beginIndex = Collections.binarySearch(store, new EventEntry(from));
|
int first = bound(from, !includeFrom);
|
||||||
int startIndex = beginIndex < 0? -(beginIndex + 1): beginIndex;
|
return new EventList(store, first, Math.max(first, bound(to, true)), true);
|
||||||
int endIndex = Collections.binarySearch(store, new EventEntry(to));
|
|
||||||
endIndex = endIndex < 0? -(endIndex + 1): endIndex+1;
|
|
||||||
if(beginIndex>0 && !includeFrom)
|
|
||||||
return new EventList(store, startIndex+1, endIndex);
|
|
||||||
else
|
|
||||||
return new EventList(store, startIndex, endIndex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() { return end - start; }
|
||||||
return end-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsKey(long key) {
|
public boolean containsKey(long key) {
|
||||||
int index = Collections.binarySearch(store, new EventEntry(key));
|
EventEntry e = entry(bound(key, false));
|
||||||
return index>=0 && index>=start && index<end;
|
return e != null && e.timestamp == key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IEvent[] get(long key) {
|
public IEvent[] get(long key) {
|
||||||
int index = Collections.binarySearch(store, new EventEntry(key));
|
EventEntry e = entry(bound(key, false));
|
||||||
if(index<0) return null;
|
return e != null && e.timestamp == key ? e.events : null;
|
||||||
return index>=start && index<end? store.get(index).events: null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(long key, IEvent value) {
|
public void put(long key, IEvent value) {
|
||||||
if (unmodifiable) throw new UnsupportedOperationException();
|
if (unmodifiable) throw new UnsupportedOperationException();
|
||||||
if(store.size()==0 || store.get(store.size()-1).timestamp < key) {
|
int index = store.isEmpty() || Long.compareUnsigned(store.get(end - 1).timestamp, key) < 0
|
||||||
store.add(new EventEntry(key, new IEvent[] {value}));
|
? end : bound(key, false);
|
||||||
} else {
|
if (index < end && store.get(index).timestamp == key) store.get(index).append(value);
|
||||||
int index = Collections.binarySearch(store, new EventEntry(key));
|
else store.add(index, new EventEntry(key, new IEvent[] {value}));
|
||||||
// < 0 if element is not in the list, see Collections.binarySearch
|
|
||||||
if (index < 0) {
|
|
||||||
EventEntry e = new EventEntry(key, new IEvent[] {value});
|
|
||||||
index = -(index + 1);
|
|
||||||
store.add(index, e);
|
|
||||||
} else {
|
|
||||||
// Insertion index is index of existing element, to add new element behind it increase index
|
|
||||||
store.get(index).append(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end = store.size();
|
end = store.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<EventEntry> entrySet() {
|
public Collection<EventEntry> entrySet() {
|
||||||
if(start != 0 || end != store.size())
|
|
||||||
return Collections.unmodifiableList(store.subList(start, end));
|
return Collections.unmodifiableList(store.subList(start, end));
|
||||||
else
|
|
||||||
return Collections.unmodifiableList(store);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() { return start == end; }
|
||||||
return start==end || store.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long firstKey() {
|
public long firstKey() {
|
||||||
|
if (isEmpty()) throw new NoSuchElementException();
|
||||||
return store.get(start).timestamp;
|
return store.get(start).timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long lastKey() {
|
public long lastKey() {
|
||||||
|
if (isEmpty()) throw new NoSuchElementException();
|
||||||
return store.get(end - 1).timestamp;
|
return store.get(end - 1).timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Navigable map functions
|
|
||||||
@Override
|
@Override
|
||||||
public EventEntry firstEntry() {
|
public EventEntry firstEntry() { return entry(start); }
|
||||||
return store.get(start);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventEntry lastEntry() {
|
public EventEntry lastEntry() { return entry(end - 1); }
|
||||||
return store.get(end-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventEntry floorEntry(long key) {
|
public EventEntry floorEntry(long key) { return entry(bound(key, true) - 1); }
|
||||||
int index = Collections.binarySearch(store, new EventEntry(key));
|
|
||||||
if(index==-1) return null;
|
|
||||||
// < 0 if element is not in the list, see Collections.binarySearch
|
|
||||||
if (index < 0) {
|
|
||||||
index = -(index + 2);
|
|
||||||
}
|
|
||||||
if(index>=end)
|
|
||||||
return store.get(end-1);
|
|
||||||
return index<start? null: store.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventEntry ceilingEntry(long key) {
|
public EventEntry ceilingEntry(long key) { return entry(bound(key, false)); }
|
||||||
int index = Collections.binarySearch(store, new EventEntry(key));
|
|
||||||
// < 0 if element is not in the list, see Collections.binarySearch
|
|
||||||
if (index < 0)
|
|
||||||
index = -(index + 1);
|
|
||||||
if(index<start)
|
|
||||||
return store.get(start);
|
|
||||||
return index>=end? null: store.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventEntry lowerEntry(long key) {
|
public EventEntry lowerEntry(long key) { return entry(bound(key, false) - 1); }
|
||||||
int index = Collections.binarySearch(store, new EventEntry(key));
|
|
||||||
// < 0 if element is not in the list, see Collections.binarySearch
|
|
||||||
if (index < 0)
|
|
||||||
index = -(index + 1);
|
|
||||||
index--;
|
|
||||||
if(index>=end)
|
|
||||||
return store.get(end-1);
|
|
||||||
return index>=end || index<start ?null:store.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EventEntry higherEntry(long key) {
|
public EventEntry higherEntry(long key) { return entry(bound(key, true)); }
|
||||||
int index = Collections.binarySearch(store, new EventEntry(key));
|
|
||||||
// < 0 if element is not in the list, see Collections.binarySearch
|
|
||||||
if (index < 0)
|
|
||||||
index = -(index + 1);
|
|
||||||
else
|
|
||||||
index++;
|
|
||||||
if(index<start)
|
|
||||||
return store.get(start);
|
|
||||||
return index>=end || index<start ?null:store.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public java.util.Iterator<EventEntry> iterator() {
|
public java.util.Iterator<EventEntry> iterator() { return entrySet().iterator(); }
|
||||||
return new Iterator(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+35
@@ -194,4 +194,39 @@ public class EventListTest {
|
|||||||
assertNotNull(populatedList.get(5));
|
assertNotNull(populatedList.get(5));
|
||||||
assertNull(populatedList.get(7));
|
assertNull(populatedList.get(7));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRangeIterationAndBounds() {
|
||||||
|
IEventList list = createList(new int[] {2, 5, 8, 11});
|
||||||
|
IEventList view = list.subMap(5, true, 8);
|
||||||
|
java.util.List<Long> times = new java.util.ArrayList<>();
|
||||||
|
for (com.minres.scviewer.database.EventEntry e : view) times.add(e.timestamp);
|
||||||
|
assertArrayEquals(new Long[] {5L, 8L}, times.toArray(new Long[0]));
|
||||||
|
assertArrayEquals(new Long[] {5L}, getTimestamps(list.subMap(2, false, 5)));
|
||||||
|
assertArrayEquals(new Long[] {5L, 8L}, getTimestamps(view.subMap(2, true, 11)));
|
||||||
|
assertTrue(list.subMap(2, false, 2).isEmpty());
|
||||||
|
java.util.Iterator<?> iterator = view.iterator();
|
||||||
|
iterator.next();
|
||||||
|
try { iterator.remove(); org.junit.Assert.fail("Read-only iterator"); }
|
||||||
|
catch (UnsupportedOperationException expected) { }
|
||||||
|
assertEquals(4, list.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmptyNavigationAndUnsignedOrder() {
|
||||||
|
IEventList empty = new EventList();
|
||||||
|
assertNull(empty.firstEntry());
|
||||||
|
assertNull(empty.lastEntry());
|
||||||
|
assertNull(empty.floorEntry(5));
|
||||||
|
assertNull(empty.ceilingEntry(5));
|
||||||
|
assertNull(empty.lowerEntry(5));
|
||||||
|
assertNull(empty.higherEntry(5));
|
||||||
|
EventList list = new EventList();
|
||||||
|
list.put(Long.MIN_VALUE, null);
|
||||||
|
list.put(Long.MAX_VALUE, null);
|
||||||
|
assertEquals(Long.MAX_VALUE, list.firstKey());
|
||||||
|
assertNotNull(list.get(Long.MIN_VALUE));
|
||||||
|
assertNotNull(list.get(Long.MAX_VALUE));
|
||||||
|
assertNull(list.subMap(0, true, 1).floorEntry(20));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user