stream_data: Convert BinaryDict to an ES6 class.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-07-22 17:42:29 -07:00 committed by Tim Abbott
parent 30e4b51731
commit 0ad8da139e
1 changed files with 42 additions and 41 deletions

View File

@ -3,7 +3,7 @@ const LazySet = require("./lazy_set").LazySet;
const settings_config = require("./settings_config"); const settings_config = require("./settings_config");
const util = require("./util"); const util = require("./util");
const BinaryDict = function (pred) { class BinaryDict {
/* /*
A dictionary that keeps track of which objects had the predicate A dictionary that keeps track of which objects had the predicate
return true or false for efficient lookups and iteration. return true or false for efficient lookups and iteration.
@ -19,66 +19,67 @@ const BinaryDict = function (pred) {
- autocomplete stream in compose - autocomplete stream in compose
*/ */
const self = {}; trues = new FoldDict();
self.trues = new FoldDict(); falses = new FoldDict();
self.falses = new FoldDict();
self.true_values = function () { constructor(pred) {
return self.trues.values(); this.pred = pred;
}; }
self.num_true_items = function () { true_values() {
return self.trues.size; return this.trues.values();
}; }
self.false_values = function () { num_true_items() {
return self.falses.values(); return this.trues.size;
}; }
self.values = function* () { false_values() {
for (const value of self.trues.values()) { return this.falses.values();
}
*values() {
for (const value of this.trues.values()) {
yield value; yield value;
} }
for (const value of self.falses.values()) { for (const value of this.falses.values()) {
yield value; yield value;
} }
}; }
self.get = function (k) { get(k) {
const res = self.trues.get(k); const res = this.trues.get(k);
if (res !== undefined) { if (res !== undefined) {
return res; return res;
} }
return self.falses.get(k); return this.falses.get(k);
}; }
self.set = function (k, v) { set(k, v) {
if (pred(v)) { if (this.pred(v)) {
self.set_true(k, v); this.set_true(k, v);
} else { } else {
self.set_false(k, v); this.set_false(k, v);
} }
}; }
self.set_true = function (k, v) { set_true(k, v) {
self.falses.delete(k); this.falses.delete(k);
self.trues.set(k, v); this.trues.set(k, v);
}; }
self.set_false = function (k, v) { set_false(k, v) {
self.trues.delete(k); this.trues.delete(k);
self.falses.set(k, v); this.falses.set(k, v);
}; }
self.delete = function (k) { delete(k) {
self.trues.delete(k); this.trues.delete(k);
self.falses.delete(k); this.falses.delete(k);
}; }
}
return self;
};
// The stream_info variable maps stream names to stream properties objects // The stream_info variable maps stream names to stream properties objects
// Call clear_subscriptions() to initialize it. // Call clear_subscriptions() to initialize it.