elasticlunr.SortedSets are used to maintain an array of uniq values in a sorted
order.
elasticlunr.SortedSet = function () {
this.length = 0
this.elements = []
}
Option name | Type | Description |
---|---|---|
serialisedData | Array | The serialised set to load. |
return | elasticlunr.SortedSet |
Loads a previously serialised sorted set.
elasticlunr.SortedSet.load = function (serialisedData) {
var set = new this
set.elements = serialisedData
set.length = serialisedData.length
return set
}
Option name | Type | Description |
---|---|---|
The | Object | objects to add to this set. |
Inserts new items into the set in the correct position to maintain the
order.
elasticlunr.SortedSet.prototype.add = function () {
var i, element
for (i = 0; i < arguments.length; i++) {
element = arguments[i]
if (~this.indexOf(element)) continue
this.elements.splice(this.locationFor(element), 0, element)
}
this.length = this.elements.length
}
Converts this sorted set into an array.
elasticlunr.SortedSet.prototype.toArray = function () {
return this.elements.slice()
}
Option name | Type | Description |
---|---|---|
fn | Function | The function that is called on each element of the set. |
ctx | Object | An optional object that can be used as the context for the function fn. |
return | Array |
Creates a new array with the results of calling a provided function on every
element in this sorted set.
Delegates to Array.prototype.map and has the same signature.
elasticlunr.SortedSet.prototype.map = function (fn, ctx) {
return this.elements.map(fn, ctx)
}
Option name | Type | Description |
---|---|---|
fn | Function | The function that is called on each element of the set. |
ctx | Object | An optional object that can be used as the context |
Executes a provided function once per sorted set element.
Delegates to Array.prototype.forEach and has the same signature.
elasticlunr.SortedSet.prototype.forEach = function (fn, ctx) {
return this.elements.forEach(fn, ctx)
}
Option name | Type | Description |
---|---|---|
elem | Object | The object to locate in the sorted set. |
return | Number |
Returns the index at which a given element can be found in the
sorted set, or -1 if it is not present.
elasticlunr.SortedSet.prototype.indexOf = function (elem) {
var start = 0,
end = this.elements.length - 1;
while (end >= start) {
var pivot = start + Math.floor((end - start + 1) / 2);
var pivotElem = this.elements[pivot];
if (pivotElem === elem) return pivot;
if (pivotElem < elem) start = pivot + 1;
else end = pivot - 1;
}
return -1;
}
Option name | Type | Description |
---|---|---|
elem | Object | The elem to find the position for in the set |
return | Number |
Returns the position within the sorted set that an element should be
inserted at to maintain the current order of the set.
This function assumes that the element to search for does not already exist
in the sorted set.
elasticlunr.SortedSet.prototype.locationFor = function (elem) {
var start = 0,
end = this.elements.length - 1;
while (end >= start) {
var pivot = start + Math.floor((end - start + 1) / 2);
var pivotElem = this.elements[pivot];
if (pivotElem === elem) return pivot;
if (pivotElem < elem) start = pivot + 1;
else end = pivot - 1;
}
if (pivotElem > elem) return pivot;
if (pivotElem < elem) return pivot + 1;
}
Option name | Type | Description |
---|---|---|
otherSet | elasticlunr.SortedSet | The set to intersect with this set. |
return | elasticlunr.SortedSet |
Creates a new elasticlunr.SortedSet that contains the elements in the intersection
of this set and the passed set.
elasticlunr.SortedSet.prototype.intersect = function (otherSet) {
var intersectSet = new elasticlunr.SortedSet,
i = 0, j = 0,
a_len = this.length, b_len = otherSet.length,
a = this.elements, b = otherSet.elements
while (true) {
if (i > a_len - 1 || j > b_len - 1) break
if (a[i] === b[j]) {
intersectSet.add(a[i])
i++, j++
continue
}
if (a[i] < b[j]) {
i++
continue
}
if (a[i] > b[j]) {
j++
continue
}
};
return intersectSet
}
Makes a copy of this set
elasticlunr.SortedSet.prototype.clone = function () {
var clone = new elasticlunr.SortedSet
clone.elements = this.toArray()
clone.length = clone.elements.length
return clone
}
Option name | Type | Description |
---|---|---|
otherSet | elasticlunr.SortedSet | The set to union with this set. |
return | elasticlunr.SortedSet |
Creates a new elasticlunr.SortedSet that contains the elements in the union
of this set and the passed set.
elasticlunr.SortedSet.prototype.union = function (otherSet) {
var longSet, shortSet, unionSet
if (this.length >= otherSet.length) {
longSet = this, shortSet = otherSet
} else {
longSet = otherSet, shortSet = this
}
unionSet = longSet.clone()
unionSet.add.apply(unionSet, shortSet.toArray())
return unionSet
}
Returns a representation of the sorted set ready for serialisation.
elasticlunr.SortedSet.prototype.toJSON = function () {
return this.toArray()
}