Changes indices to update only necesary data

This commit is contained in:
alonso.torres 2021-05-03 10:32:32 +02:00 committed by Andrey Antukh
parent 308fd8d4b0
commit 285a0d5f47
11 changed files with 382 additions and 219 deletions

View file

@ -32,12 +32,16 @@
"use strict";
goog.provide("app.util.quadtree");
goog.require("cljs.core");
goog.scope(function() {
const self = app.util.quadtree;
const eq = cljs.core._EQ_;
const contains = cljs.core.contains_QMARK_;
class Node {
constructor(bounds, data) {
constructor(id, bounds, data) {
this.id = id;
this.bounds = bounds;
this.data = data;
}
@ -51,8 +55,8 @@ goog.scope(function() {
this.level = level || 0;
this.bounds = bounds;
this.objects = [];
this.indexes = [];
this.objects = [];
this.indexes = [];
}
split() {
@ -183,14 +187,18 @@ goog.scope(function() {
this.objects = [];
this.indexes = [];
}
getObjects() {
return this.objects;
}
}
self.create = function(rect) {
return new Quadtree(rect, 10, 4, 0);
};
self.insert = function(index, bounds, data) {
const node = new Node(bounds, data);
self.insert = function(index, id, bounds, data) {
const node = new Node(id, bounds, data);
index.insert(node);
return index;
};
@ -210,4 +218,29 @@ goog.scope(function() {
}
};
self.remove = function(index, id) {
const result = self.create(index.bounds);
for (let node of index.objects) {
if (!eq(id, node.id)) {
self.insert(result, node.id, node.bounds, node.data);
}
}
return result;
}
// FIXME: Inefficient to recreate the index. Needs to be improved
self.remove_all = function(index, ids) {
const result = self.create(index.bounds);
for (let node of self.search(index, index.bounds)) {
if (!contains(ids, node.id)) {
self.insert(result, node.id, node.bounds, node.data);
}
}
return result;
}
});

View file

@ -13,7 +13,7 @@
"use strict";
goog.provide("app.util.range_tree");
goog.require("cljs.core")
goog.require("cljs.core");
goog.scope(function() {
const eq = cljs.core._EQ_;
@ -92,7 +92,7 @@ goog.scope(function() {
}
isEmpty() {
return this.root === null;
return !this.root;
}
toString() {
@ -116,7 +116,7 @@ goog.scope(function() {
// Insert recursively in the tree
function recInsert (branch, value, data) {
if (branch === null) {
if (!branch) {
const ret = new Node(value, data);
ret.color = Color.RED;
return ret;
@ -144,7 +144,7 @@ goog.scope(function() {
// Search for the min node
function searchMin(branch) {
if (branch.left === null) {
if (!branch.left) {
return branch;
} else {
return searchMin(branch.left);
@ -153,7 +153,7 @@ goog.scope(function() {
// Remove the lefmost node of the current branch
function recRemoveMin(branch) {
if (branch.left === null) {
if (!branch.left) {
return null;
}
@ -167,7 +167,7 @@ goog.scope(function() {
// Remove the data element for the value given
// this will not remove the node, we have to remove the empty node afterwards
function recRemoveData(branch, value, data) {
if (branch === null) {
if (!branch) {
// Not found
return branch;
} else if (branch.value === value) {
@ -193,7 +193,7 @@ goog.scope(function() {
if (isRed(branch.left)) {
branch = rotateRight(branch);
}
if (value === branch.value && branch.right === null) {
if (value === branch.value && !branch.right) {
return null;
}
if (!isRed(branch.right) && !isRed(branch.right.left)) {
@ -214,7 +214,7 @@ goog.scope(function() {
// Retrieve all the data related to value
function recGet(branch, value) {
if (branch === null) {
if (!branch) {
return null;
} else if (branch.value === value) {
return branch.data;
@ -226,7 +226,7 @@ goog.scope(function() {
}
function recUpdate(branch, value, oldData, newData) {
if (branch === null) {
if (!branch) {
return branch;
} else if (branch.value === value) {
branch.data = branch.data.map((it) => (eq(it, oldData)) ? newData : it);
@ -239,7 +239,7 @@ goog.scope(function() {
}
function recRangeQuery(branch, fromValue, toValue, result) {
if (branch === null) {
if (!branch) {
return result;
}
if (fromValue < branch.value) {
@ -329,7 +329,7 @@ goog.scope(function() {
// This will return the string representation. We don't care about internal structure
// only the data
function recToString(branch, result) {
if (branch === null) {
if (!branch) {
return;
}