some more refactoring

This commit is contained in:
2021-02-27 13:47:37 +00:00
parent 1d2395e00d
commit 71297c4e5a
7 changed files with 23 additions and 35 deletions

View File

@ -1,5 +1,7 @@
package com.minres.scviewer.database;
import java.util.Arrays;
public class EventEntry implements Comparable<EventEntry>{
public long timestamp; // unsigned
public IEvent[] events = null;
@ -25,4 +27,15 @@ public class EventEntry implements Comparable<EventEntry>{
public String toString() {
return String.format("e.%d@%d", events.length,timestamp);
}
public void append(IEvent value) {
if(events.length==0)
events = new IEvent[] {value};
else {
int idx = events.length;
events = Arrays.copyOf(events, idx+1);
events[idx]=value;
}
}
}

View File

@ -1,6 +1,7 @@
package com.minres.scviewer.database;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -85,24 +86,23 @@ public class EventList implements IEventList {
}
@Override
public IEvent[] put(long key, IEvent[] value) {
public void put(long key, IEvent value) {
if(unmodifiable) throw new UnsupportedOperationException();
EventEntry e = new EventEntry(key, value);
if(store.size()==0 || store.get(store.size()-1).timestamp < key) {
store.add(e);
store.add(new EventEntry(key, new IEvent[] {value}));
} else {
int index = Collections.binarySearch(store, new EventEntry(key));
// < 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.set(index, e);
store.get(index).append(value);
}
}
end=store.size();
return e.events;
}
@Override

View File

@ -12,7 +12,7 @@ public interface IEventList extends Iterable<EventEntry> {
IEvent[] get(long key);
IEvent[] put(long key, IEvent[] value);
void put(long key, IEvent value);
long firstKey();