Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/generics/lib.generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3847,6 +3847,93 @@ class Set<V = any> {
}
}

// The value of a string index signature, `{ [key: string]: T }`, and of an object literal with a key known
// only at run time (`{ [m]: v }`): its entries, by key, in the order they were added. The compiler reads
// `d[k]`, `d["name"]` and `d.name` as `get` (T | undefined: undefined for a key it does not hold) and
// writes them with `set`; outside this class they are always entries, never these members (@dictionary).
// Under -mm=own it owns its values: a value written in moves in, one read borrows, one overwritten or
// deleted is dropped. The keys are its own copies.
@dictionary
class ObjectDictionary<T = any> {
private index: Map<string, int>;
private _keys: string[];
private _values: T[];

constructor() {
this.index = new Map<string, int>();
this._keys = [];
this._values = [];
}

get size() {
return this._keys.length;
}

get(key: string): T | undefined {
const i = this.index.get(key);
if (i === undefined) {
return undefined;
}

return this._values[i];
}

// writing undefined deletes the entry
set(key: string, value: T | undefined) {
if (value === undefined) {
this.delete(key);
return;
}

const i = this.index.get(key);
if (i === undefined) {
// a copy of the key: the caller's stays the caller's
this.index.set("" + key, this._values.length);
this._keys.push("" + key);
this._values.push(value);
return;
}

this._values[i] = value;
}

[key: string]: T | undefined;

has(key: string): boolean {
return this.index.has(key);
}

delete(key: string): boolean {
const i = this.index.get(key);
if (i === undefined) {
return false;
}

this.index.delete(key);
this._keys.splice(i, 1);
this._values.splice(i, 1);
for (let j = i; j < this._keys.length; j++) {
this.index.set(this._keys[j], j);
}

return true;
}

keys(): string[] {
return this._keys.slice(0);
}
}

// `{ ...source }` where the object made is a dictionary: source's entries copied into target, in order (the
// compiler calls it for a spread of a dictionary). A function of its own, not a member: it is made only for
// the types it is used with, so under -mm=own a dictionary of objects, which can't be copied, fails only
// where one is spread.
function objectDictionaryAssign<T>(target: ObjectDictionary<T>, source: ObjectDictionary<T>) {
for (const k in source) {
target[k] = source[k];
}
}

namespace __Iterator
{
// example how to restrict to iterators only
Expand Down
40 changes: 40 additions & 0 deletions tests/object_dictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// not own: until a released compiler has the fix for a parameter moved on paths that exclude each other (set)
// ObjectDictionary<T>: the value of a string index signature, `{ [key: string]: T }`. Its entries by keys
// known at run time: written, overwritten, deleted by writing undefined, and an absent key reads undefined.
class P {
constructor(public n: string) {}
}

const d = new ObjectDictionary<number>();
for (let i = 0; i < 20; i++) {
d["k" + (i % 5)] = i;
}

assert(d["k0"] == 15);
assert(d["k4"] == 19);
assert(d["none"] === undefined);

d["k1"] = undefined;
assert(d["k1"] === undefined);
assert(d["k2"] == 17);

// objects: under -mm=own the dictionary owns them, and a read borrows (so it is read in a function: a global
// can't hold a borrow)
function objects() {
const o = new ObjectDictionary<P>();
for (let i = 0; i < 100; i++) {
const key = "p" + (i % 10);
o[key] = new P("v" + i);
if (i % 3 == 0) {
o[key] = undefined;
}
}

const p = o["p8"];
assert(p !== undefined && p.n == "v98");
assert(o["p9"] === undefined);
}

objects();

console.log("ALL DONE");
Loading