Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change content to getDefault

  • Loading branch information...
commit de6513c7a3d1d1a517209428c1ca206eea41acc5 1 parent 73a2d65
Kris Kowal authored
46 README.md
View
@@ -6,7 +6,7 @@ This package contains JavaScript implementations of common data
structures with idiomatic iterfaces, including extensions for Array and
Object.
-- **List(values, equals, content)**
+- **List(values, equals, getDefault)**
An ordered collection of values with fast insertion and deletion and
forward and backward traversal, backed by a cyclic doubly linked
@@ -18,7 +18,7 @@ Object.
the list prototype and can be overridden by inheritors. Each node
has `prev` and `next` properties.
-- **Set(values, equals, hash, content)**
+- **Set(values, equals, hash, getDefault)**
A collection of unique values. The set can be iterated in the order
of insertion. With a good hash function for the stored values,
@@ -27,18 +27,18 @@ Object.
functions can be overridden to provide alternate definitions of
"unique". `Set` is backed by `FastSet` and `List`.
-- **Map(map, equals, hash, content)**
+- **Map(map, equals, hash, getDefault)**
A collection of key and value items with unique keys. Keys may be
objects. The collection iterates in the order of insertion. `Map`
is backed by `Set`.
-- **MultiMap(map, content, equals, hash)**
+- **MultiMap(map, getDefault, equals, hash)**
A collection of keys mapped to collections of values. The default
- `content` collection is an `Array`, but it can be a `List` or any
+ `getDefault` collection is an `Array`, but it can be a `List` or any
other array-like object. `MultiMap` inherits `Map` but overrides
- the `content` constructor.
+ the `getDefault(key)` provider.
- **WeakMap()**
@@ -51,18 +51,18 @@ Object.
not necessarily leak memory, but cannot collect certain reference
graphs. This WeakMap shim was implemented by Mark Miller of Google.
-- **SortedSet(values, equals, compare, content)**
+- **SortedSet(values, equals, compare, getDefault)**
A collection of unique values stored in stored order, backed by a
splay tree. The `equals` and `compare` functions can be overridden
to provide alternate definitions of "unique".
-- **SortedMap(map, equals, compare, content)**
+- **SortedMap(map, equals, compare, getDefault)**
A collection of key value pairs stored in sorted order. `SortedMap`
is backed by `SortedSet` and the `GenericMap` mixin.
-- **LruSet(values, maxLength, equals, hash, content)**
+- **LruSet(values, maxLength, equals, hash, getDefault)**
A cache with the Least-Recently-Used strategy for truncating its
content when it’s length exceeds `maxLength`. `LruSet` is backed by
@@ -70,16 +70,16 @@ Object.
Both getting and setting a value constitute usage, but checking
whether the set has a value and iterating values do not.
-- **LruMap(map, maxLength, equals, hash, content)**
+- **LruMap(map, maxLength, equals, hash, getDefault)**
A cache of items backed by an `LruSet`.
-- **SortedArray(values, equals, compare, content)**
+- **SortedArray(values, equals, compare, getDefault)**
A collection of values stored in a stable sorted order, backed by an
array.
-- **SortedArraySet(values, equals, compare, content)**
+- **SortedArraySet(values, equals, compare, getDefault)**
A collection of unique values stored in sorted order, backed by a
plain array. If the given values are an actual array, the sorted
@@ -87,12 +87,12 @@ Object.
sorted array set performs better than a sorted set when it has
roughly less than 100 values.
-- **SortedArrayMap(values, equals, compare, content)**
+- **SortedArrayMap(values, equals, compare, getDefault)**
A collection of key value pairs stored in sorted order, backed by a
sorted array set.
-- **FastSet(values, equals, hash, content)**
+- **FastSet(values, equals, hash, getDefault)**
A collection of unique values stored like a hash table. The
underlying storage is a `Dict` that maps hashes to lists of values
@@ -100,13 +100,13 @@ Object.
`hash` functions can be overridden to provide alternate definitions
of "unique".
-- **FastMap(map, equals, hash, content)**
+- **FastMap(map, equals, hash, getDefault)**
A collection of key and value items with unique keys, backed by a
set. Keys may be objects. `FastMap` is backed by `FastSet` and the
`GenericMap` mixin.
-- **Dict(values, content)**
+- **Dict(values, getDefault)**
A collection of string to value mappings backed by a plain
JavaScript object. The keys are mangled to prevent collisions with
@@ -178,12 +178,12 @@ same string for any given object.
[Unique Label]: (http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps#unique_labeler)
-- **content(key or value)**
+- **getDefault(key or value)**
- The default `content` function is `Function.noop`, which returns
- `undefined`. The content function is used when you `get` a
- nonexistant value from any collection. The `content` function
- becomes a member of the collection object, so `content` is called
+ The default `getDefault` function is `Function.noop`, which returns
+ `undefined`. The fallback function is used when you `get` a
+ nonexistant value from any collection. The `getDefault` function
+ becomes a member of the collection object, so `getDefault` is called
with the collection as `this`, so you can also use it to guarantee
that default values in a collection are retained, as in `MultiMap`.
@@ -225,13 +225,13 @@ SortedArrayMap, FastSet, FastMap, Dict)
- **get(key or index)**
The value for a key. If a Map or SortedMap lacks a key, returns
- `content(key)`.
+ `getDefault(key)`.
(Array+, Map, SortedMap, SortedArrayMap, WeakMap, Object+)
**get(value)**
- Gets the equivalent value, or falls back to `content(value)`.
+ Gets the equivalent value, or falls back to `getDefault(value)`.
(List, Set, SortedSet, LruSet, SortedArray, SortedArraySet, FastSet)
12 dict.js
View
@@ -8,12 +8,12 @@ var PropertyChanges = require("./listen/property-changes");
// Burgled from https://github.com/domenic/dict
module.exports = Dict;
-function Dict(values, content) {
+function Dict(values, getDefault) {
if (!(this instanceof Dict)) {
- return new Dict(values, content);
+ return new Dict(values, getDefault);
}
- content = content || Function.noop;
- this.content = content;
+ getDefault = getDefault || Function.noop;
+ this.getDefault = getDefault;
this.store = {};
this.length = 0;
this.addEach(values);
@@ -32,7 +32,7 @@ Object.addEach(Dict.prototype, GenericMap.prototype);
Object.addEach(Dict.prototype, PropertyChanges.prototype);
Dict.prototype.constructClone = function (values) {
- return new this.constructor(values, this.mangle, this.content);
+ return new this.constructor(values, this.mangle, this.getDefault);
};
Dict.prototype.assertString = function (key) {
@@ -49,7 +49,7 @@ Dict.prototype.get = function (key, defaultValue) {
} else if (arguments.length > 1) {
return defaultValue;
} else {
- return this.content();
+ return this.getDefault(key);
}
};
8 fast-map.js
View
@@ -8,16 +8,16 @@ var PropertyChanges = require("./listen/property-changes");
module.exports = FastMap;
-function FastMap(values, equals, hash, content) {
+function FastMap(values, equals, hash, getDefault) {
if (!(this instanceof FastMap)) {
return new FastMap(values, equals, hash);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentHash = hash;
- this.content = content;
+ this.getDefault = getDefault;
this.store = new Set(
undefined,
function keysEqual(a, b) {
@@ -40,7 +40,7 @@ FastMap.prototype.constructClone = function (values) {
values,
this.contentEquals,
this.contentHash,
- this.content
+ this.getDefault
);
};
10 fast-set.js
View
@@ -12,16 +12,16 @@ var object_has = Object.prototype.hasOwnProperty;
module.exports = FastSet;
-function FastSet(values, equals, hash, content) {
+function FastSet(values, equals, hash, getDefault) {
if (!(this instanceof FastSet)) {
return new FastSet(values, equals, hash);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentHash = hash;
- this.content = content;
+ this.getDefault = getDefault;
this.buckets = new this.Buckets(null, this.Bucket);
this.length = 0;
this.addEach(values);
@@ -39,7 +39,7 @@ FastSet.prototype.constructClone = function (values) {
values,
this.contentEquals,
this.contentHash,
- this.content
+ this.getDefault
);
};
@@ -54,7 +54,7 @@ FastSet.prototype.get = function (value) {
if (buckets.has(hash)) {
return buckets.get(hash).get(value);
} else {
- return this.content(value);
+ return this.getDefault(value);
}
};
2  generic-map.js
View
@@ -45,7 +45,7 @@ GenericMap.prototype.get = function (key, defaultValue) {
} else if (arguments.length > 1) {
return defaultValue;
} else {
- return this.content(key);
+ return this.getDefault(key);
}
};
10 list.js
View
@@ -7,15 +7,15 @@ var GenericCollection = require("./generic-collection");
var GenericOrder = require("./generic-order");
var PropertyChanges = require("./listen/property-changes");
-function List(values, equals, content) {
+function List(values, equals, getDefault) {
if (!(this instanceof List)) {
- return new List(values, equals, content);
+ return new List(values, equals, getDefault);
}
var head = this.head = new this.Node();
head.next = head;
head.prev = head;
this.contentEquals = equals || Object.equals;
- this.content = content || Function.noop;
+ this.getDefault = getDefault || Function.noop;
this.length = 0;
this.addEach(values);
}
@@ -25,7 +25,7 @@ Object.addEach(List.prototype, GenericOrder.prototype);
Object.addEach(List.prototype, PropertyChanges.prototype);
List.prototype.constructClone = function (values) {
- return new this.constructor(values, this.contentEquals, this.content);
+ return new this.constructor(values, this.contentEquals, this.getDefault);
};
List.prototype.find = function (value, equals) {
@@ -61,7 +61,7 @@ List.prototype.get = function (value, equals) {
if (found) {
return found.value;
}
- return this.content();
+ return this.getDefault(value);
};
// LIFO (delete removes the most recently added equivalent value)
8 lru-map.js
View
@@ -8,16 +8,16 @@ var PropertyChanges = require("./listen/property-changes");
module.exports = LruMap;
-function LruMap(values, maxLength, equals, hash, content) {
+function LruMap(values, maxLength, equals, hash, getDefault) {
if (!(this instanceof LruMap)) {
return new LruMap(values, maxLength, equals, hash);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentHash = hash;
- this.content = content;
+ this.getDefault = getDefault;
this.store = new LruSet(
undefined,
maxLength,
@@ -42,7 +42,7 @@ LruMap.prototype.constructClone = function (values) {
this.maxLength,
this.contentEquals,
this.contentHash,
- this.content
+ this.getDefault
);
};
10 lru-set.js
View
@@ -8,18 +8,18 @@ var PropertyChanges = require("./listen/property-changes");
module.exports = LruSet;
-function LruSet(values, maxLength, equals, hash, content) {
+function LruSet(values, maxLength, equals, hash, getDefault) {
if (!(this instanceof LruSet)) {
return new LruSet(values, maxLength, equals, hash);
}
maxLength = maxLength || Infinity;
equals = equals || Object.equals;
hash = hash || Object.hash;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.store = new Set(undefined, equals, hash);
this.contentEquals = equals;
this.contentHash = hash;
- this.content = content;
+ this.getDefault = getDefault;
this.maxLength = maxLength;
this.length = 0;
this.addEach(values);
@@ -35,7 +35,7 @@ LruSet.prototype.constructClone = function (values) {
this.maxLength,
this.contentEquals,
this.contentHash,
- this.content
+ this.getDefault
);
};
@@ -49,7 +49,7 @@ LruSet.prototype.get = function (value) {
this.store["delete"](value);
this.store.add(value);
} else {
- value = this.content();
+ value = this.getDefault(value);
}
return value;
};
8 map.js
View
@@ -8,16 +8,16 @@ var PropertyChanges = require("./listen/property-changes");
module.exports = Map;
-function Map(values, equals, hash, content) {
+function Map(values, equals, hash, getDefault) {
if (!(this instanceof Map)) {
return new Map(values, equals, hash);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentHash = hash;
- this.content = content;
+ this.getDefault = getDefault;
this.store = new Set(
undefined,
function keysEqual(a, b) {
@@ -40,7 +40,7 @@ Map.prototype.constructClone = function (values) {
values,
this.contentEquals,
this.contentHash,
- this.content
+ this.getDefault
);
};
2  multi-map.js
View
@@ -8,7 +8,7 @@ function MultiMap(values, bucket, equals, hash) {
return new MultiMap(values, bucket, equals, hash);
}
this.bucket = bucket || this.bucket;
- Map.call(this, values, equals, hash, function (key) {
+ Map.call(this, values, equals, hash, function getDefault(key) {
var bucket = this.bucket();
Map.prototype.set.call(this, key, bucket);
return bucket;
10 set.js
View
@@ -10,16 +10,16 @@ var RangeChanges = require("./listen/range-changes");
module.exports = Set;
-function Set(values, equals, hash, content) {
+function Set(values, equals, hash, getDefault) {
if (!(this instanceof Set)) {
return new Set(values, equals, hash);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentHash = hash;
- this.content = content;
+ this.getDefault = getDefault;
// a list of values in insertion order, used for all operations that depend
// on iterating in insertion order
this.order = new this.Order(undefined, equals);
@@ -47,7 +47,7 @@ Set.prototype.Order = List;
Set.prototype.Store = FastSet;
Set.prototype.constructClone = function (values) {
- return new this.constructor(values, this.contentEquals, this.contentHash, this.content);
+ return new this.constructor(values, this.contentEquals, this.contentHash, this.getDefault);
};
Set.prototype.has = function (value) {
@@ -61,7 +61,7 @@ Set.prototype.get = function (value) {
if (node) {
return node.value;
} else {
- return this.content(value);
+ return this.getDefault(value);
}
};
10 sorted-array-map.js
View
@@ -8,16 +8,16 @@ var PropertyChanges = require("./listen/property-changes");
module.exports = SortedArrayMap;
-function SortedArrayMap(values, equals, compare, content) {
+function SortedArrayMap(values, equals, compare, getDefault) {
if (!(this instanceof SortedArrayMap)) {
- return new SortedArrayMap(values, equals, compare, content);
+ return new SortedArrayMap(values, equals, compare, getDefault);
}
equals = equals || Object.equals;
compare = compare || Object.compare;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentCompare = compare;
- this.content = content;
+ this.getDefault = getDefault;
this.store = new SortedArraySet(
null,
function keysEqual(a, b) {
@@ -40,7 +40,7 @@ SortedArrayMap.prototype.constructClone = function (values) {
values,
this.contentEquals,
this.contentCompare,
- this.content
+ this.getDefault
);
};
6 sorted-array-set.js
View
@@ -7,11 +7,11 @@ var SortedArray = require("./sorted-array");
var GenericSet = require("./generic-set");
var PropertyChanges = require("./listen/property-changes");
-function SortedArraySet(values, equals, compare, content) {
+function SortedArraySet(values, equals, compare, getDefault) {
if (!(this instanceof SortedArraySet)) {
- return new SortedArraySet(values, equals, compare, content);
+ return new SortedArraySet(values, equals, compare, getDefault);
}
- SortedArray.call(this, values, equals, compare, content);
+ SortedArray.call(this, values, equals, compare, getDefault);
}
SortedArraySet.prototype = Object.create(SortedArray.prototype);
10 sorted-array.js
View
@@ -7,9 +7,9 @@ var GenericCollection = require("./generic-collection");
var PropertyChanges = require("./listen/property-changes");
var RangeChanges = require("./listen/range-changes");
-function SortedArray(values, equals, compare, content) {
+function SortedArray(values, equals, compare, getDefault) {
if (!(this instanceof SortedArray)) {
- return new SortedArray(values, equals, compare, content);
+ return new SortedArray(values, equals, compare, getDefault);
}
if (Array.isArray(values)) {
this.array = values;
@@ -19,7 +19,7 @@ function SortedArray(values, equals, compare, content) {
}
this.contentEquals = equals || Object.equals;
this.contentCompare = compare || Object.compare;
- this.content = content || Function.noop;
+ this.getDefault = getDefault || Function.noop;
this.length = 0;
this.addEach(values);
@@ -96,7 +96,7 @@ SortedArray.prototype.constructClone = function (values) {
values,
this.contentEquals,
this.contentCompare,
- this.content
+ this.getDefault
);
};
@@ -110,7 +110,7 @@ SortedArray.prototype.get = function (value) {
if (index !== -1) {
return this.array[index];
} else {
- return this.content(value);
+ return this.getDefault(value);
}
};
10 sorted-map.js
View
@@ -8,16 +8,16 @@ var PropertyChanges = require("./listen/property-changes");
module.exports = SortedMap;
-function SortedMap(values, equals, compare, content) {
+function SortedMap(values, equals, compare, getDefault) {
if (!(this instanceof SortedMap)) {
- return new SortedMap(values, equals, compare, content);
+ return new SortedMap(values, equals, compare, getDefault);
}
equals = equals || Object.equals;
compare = compare || Object.compare;
- content = content || Function.noop;
+ getDefault = getDefault || Function.noop;
this.contentEquals = equals;
this.contentCompare = compare;
- this.content = content;
+ this.getDefault = getDefault;
this.store = new SortedSet(
null,
function keysEqual(a, b) {
@@ -41,7 +41,7 @@ SortedMap.prototype.constructClone = function (values) {
values,
this.contentEquals,
this.contentCompare,
- this.content
+ this.getDefault
);
};
10 sorted-set.js
View
@@ -9,13 +9,13 @@ var PropertyChanges = require("./listen/property-changes");
var RangeChanges = require("./listen/range-changes");
var TreeLog = require("./tree-log");
-function SortedSet(values, equals, compare, content) {
+function SortedSet(values, equals, compare, getDefault) {
if (!(this instanceof SortedSet)) {
- return new SortedSet(values, equals, compare, content);
+ return new SortedSet(values, equals, compare, getDefault);
}
this.contentEquals = equals || Object.equals;
this.contentCompare = compare || Object.compare;
- this.content = content || Function.noop;
+ this.getDefault = getDefault || Function.noop;
this.root = null;
this.length = 0;
this.addEach(values);
@@ -31,7 +31,7 @@ SortedSet.prototype.constructClone = function (values) {
values,
this.contentEquals,
this.contentCompare,
- this.content
+ this.getDefault
);
};
@@ -51,7 +51,7 @@ SortedSet.prototype.get = function (value) {
return this.root.value;
}
}
- return this.content(value);
+ return this.getDefault(value);
};
SortedSet.prototype.add = function (value) {
Please sign in to comment.
Something went wrong with that request. Please try again.