use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project phtree by tzaeschke.
the class NodeTreeV11 method addEntry.
@SuppressWarnings("unchecked")
public static <T> T addEntry(NtNode<T> root, long hcPos, long[] kdKey, Object value, Node phNode) {
NtNode<T> currentNode = root;
while (true) {
long localHcPos = NtNode.pos2LocalPos(hcPos, currentNode.getPostLen());
int pin = currentNode.getPosition(localHcPos, NtNode.MAX_DIM);
if (pin < 0) {
// insert
currentNode.localAddEntryPIN(pin, localHcPos, hcPos, kdKey, value);
incCounter(phNode);
return null;
}
Object localVal = currentNode.getValueByPIN(pin);
boolean isSubNode = localVal instanceof NtNode;
long postInFix;
int conflictingLevels;
NtNode<T> sub = null;
if (isSubNode) {
sub = (NtNode<T>) localVal;
// check infix if infixLen > 0
if (currentNode.getPostLen() - sub.getPostLen() > 1) {
postInFix = currentNode.localReadInfix(pin, localHcPos);
conflictingLevels = NtNode.getConflictingLevels(hcPos, postInFix, currentNode.getPostLen(), sub.getPostLen());
} else {
postInFix = 0;
conflictingLevels = 0;
}
} else {
postInFix = currentNode.localReadPostfix(pin, localHcPos);
// long mask = ~((-1L) << (currentNode.getPostLen()*NtNode.MAX_DIM));
// conflictingLevels = NtNode.getMaxConflictingLevelsWithMask(hcPos, postInFix, mask);
// TODO
conflictingLevels = NtNode.getConflictingLevels(hcPos, postInFix, currentNode.getPostLen());
}
if (conflictingLevels != 0) {
int newPostLen = conflictingLevels - 1;
NtNode<T> newNode = NtNode.createNode(newPostLen, kdKey.length);
currentNode.localReplaceEntryWithSub(pin, localHcPos, hcPos, newNode);
long localHcInSubOfNewEntry = NtNode.pos2LocalPos(hcPos, newPostLen);
long localHcInSubOfPrevEntry = NtNode.pos2LocalPos(postInFix, newPostLen);
// TODO assume pin=0 for first entry?
newNode.localAddEntry(localHcInSubOfNewEntry, hcPos, kdKey, value);
long[] localKdKey = new long[kdKey.length];
currentNode.readKdKeyPIN(pin, localKdKey);
newNode.localAddEntry(localHcInSubOfPrevEntry, postInFix, localKdKey, localVal);
incCounter(phNode);
return null;
}
if (isSubNode) {
// traverse subNode
currentNode = sub;
} else {
// external postfix is not checked
if (phNode == null) {
return (T) currentNode.localReplaceEntry(pin, kdKey, value);
} else {
if (localVal instanceof Node) {
Node subNode = (Node) localVal;
// TODO
// TODO
// TODO
// if (hasSubInfix(offs, dims)) {
long mask = phNode.calcInfixMask(subNode.getPostLen());
return (T) insertSplitPH(kdKey, value, localVal, pin, mask, currentNode, phNode);
// }
// return v;
} else {
if (phNode.getPostLen() > 0) {
long mask = phNode.calcPostfixMask();
return (T) insertSplitPH(kdKey, value, localVal, pin, mask, currentNode, phNode);
}
// perfect match -> replace value
currentNode.localReplaceValue(pin, value);
return (T) value;
}
}
}
}
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project phtree by tzaeschke.
the class TestBST16compute method testEmpty.
@Test
public void testEmpty() {
Node ht = create();
checkEmpty(ht);
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project phtree by tzaeschke.
the class TestBST16compute method runTest.
private void runTest(List<BSTEntry> list, String prefix) {
Node ht = create();
// populate
long l11 = System.currentTimeMillis();
for (BSTEntry i : list) {
// if (i%1000 == 0)
// System.out.println("ins=" + i);
// ht.bstPut((Integer)i.getValue(), i);
ht.bstCompute((int) i.getValue(), i.getKdKey(), tree, true, (longs, o) -> {
assertSame(i.getKdKey(), longs);
assertNull(o);
// TODO clone 'i'?
return i.getValue();
});
// Check
BSTEntry be = ht.bstGet((Integer) i.getValue());
assertEquals((int) i.getValue(), (int) be.getValue());
}
long l12 = System.currentTimeMillis();
assertEquals(list.size(), ht.getEntryCount());
println(ht.getStats().toString());
// lookup
long l21 = System.currentTimeMillis();
for (BSTEntry i : list) {
BSTEntry e = ht.bstGet((Integer) i.getValue());
// assertNotNull("i=" + i, e);
int x = (int) e.getValue();
assertEquals(i.getValue(), x);
}
long l22 = System.currentTimeMillis();
// iterate
long l51 = System.currentTimeMillis();
BSTIteratorAll iter = ht.iterator();
long prev = -1;
while (iter.hasNextEntry()) {
long current = iter.nextEntry().getKey();
assertEquals(prev + 1, current);
prev = current;
}
assertEquals(prev, list.size() - 1);
long l52 = System.currentTimeMillis();
long l61 = System.currentTimeMillis();
BSTIteratorMask iterMask = new BSTIteratorMask().reset(ht.getRoot(), 0, 0xFFFFFFFFFFFEL, ht.getEntryCount());
prev = -2;
while (iterMask.hasNextEntry()) {
long current = iterMask.nextEntry().getKey();
assertEquals(prev + 2, current);
prev = current;
}
assertEquals(prev, ((list.size() - 1) & 0xFFFFFFFE));
long l62 = System.currentTimeMillis();
// replace some
long l31 = System.currentTimeMillis();
for (BSTEntry i : list) {
// ht.bstPut((Integer)i.getValue(), new BSTEntry(i.getKdKey(), -(Integer)i.getValue()));
ht.bstCompute((Integer) i.getValue(), i.getKdKey(), tree, true, (longs, o) -> -(Integer) i.getValue());
}
long l32 = System.currentTimeMillis();
assertEquals(list.size(), ht.getEntryCount());
// remove some
long l41 = System.currentTimeMillis();
boolean[] found = new boolean[1];
for (BSTEntry i : list) {
found[0] = false;
ht.bstCompute((Integer) i.getValue(), i.getKdKey(), tree, true, (longs, o) -> {
found[0] = true;
return null;
});
assertTrue(found[0]);
}
long l42 = System.currentTimeMillis();
assertEquals(0, ht.getEntryCount());
println(prefix + "Load: " + (l12 - l11));
println(prefix + "Get: " + (l22 - l21));
println(prefix + "Iter: " + (l52 - l51));
println(prefix + "IterM:" + (l62 - l61));
println(prefix + "Load: " + (l32 - l31));
println(prefix + "Rem : " + (l42 - l41));
println();
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project phtree by tzaeschke.
the class TestBST16compute method testEmptyAfterLoad.
@Test
public void testEmptyAfterLoad() {
Node ht = create();
for (int r = 0; r < 10; r++) {
for (int i = 0; i < 100000; i++) {
BSTEntry e = ht.bstGetOrCreate(i, tree);
e.set(i, new long[] { i }, i);
}
for (int i = 0; i < 100000; i++) {
BSTEntry e = ht.bstRemove(i, new long[] { i }, null, tree);
assertEquals(i, (int) e.getValue());
}
checkEmpty(ht);
}
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Node in project openhab-addons by openhab.
the class HomieChildMapTests method testArrayToSubtopicCreateAndRemove.
@Test
public void testArrayToSubtopicCreateAndRemove() {
AddedAction addedAction = spy(new AddedAction());
// Assign "abc,def" to the
subject.apply(new String[] { "abc", "def" }, addedAction, this::createNode, this::removedNode);
assertThat(future.isDone(), is(true));
assertThat(subject.get("abc").nodeID, is("abc"));
assertThat(subject.get("def").nodeID, is("def"));
verify(addedAction, times(2)).apply(any());
Node soonToBeRemoved = subject.get("def");
subject.apply(new String[] { "abc" }, addedAction, this::createNode, this::removedNode);
verify(callback).nodeRemoved(eq(soonToBeRemoved));
}
Aggregations