sorted_set.js is added only to make elasticlunr.js compatible with lunr-languages.
if elasticlunr.js support different languages by default, this will make elasticlunr.js
much bigger that not good for browser usage.
lunr.SortedSets are used to maintain an array of uniq values in a sorted
order.
lunr.SortedSet = function () {
this.length = 0
this.elements = []
}
Option name | Type | Description |
---|---|---|
serialisedData | Array | The serialised set to load. |
Loads a previously serialised sorted set.
lunr.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.
lunr.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.
lunr.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. |
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.
lunr.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.
lunr.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. |
Returns the index at which a given element can be found in the
sorted set, or -1 if it is not present.
lunr.SortedSet.prototype.indexOf = function (elem) {
var start = 0,
end = this.elements.length,
sectionLength = end - start,
pivot = start + Math.floor(sectionLength / 2),
pivotElem = this.elements[pivot]
while (sectionLength > 1) {
if (pivotElem === elem) return pivot
if (pivotElem < elem) start = pivot
if (pivotElem > elem) end = pivot
sectionLength = end - start
pivot = start + Math.floor(sectionLength / 2)
pivotElem = this.elements[pivot]
}
if (pivotElem === elem) return pivot
return -1
}
Option name | Type | Description |
---|---|---|
elem | Object | The elem to find the position for in the set |
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.
lunr.SortedSet.prototype.locationFor = function (elem) {
var start = 0,
end = this.elements.length,
sectionLength = end - start,
pivot = start + Math.floor(sectionLength / 2),
pivotElem = this.elements[pivot]
while (sectionLength > 1) {
if (pivotElem < elem) start = pivot
if (pivotElem > elem) end = pivot
sectionLength = end - start
pivot = start + Math.floor(sectionLength / 2)
pivotElem = this.elements[pivot]
}
if (pivotElem > elem) return pivot
if (pivotElem < elem) return pivot + 1
}
Option name | Type | Description |
---|---|---|
otherSet | lunr.SortedSet | The set to intersect with this set. |
Creates a new lunr.SortedSet that contains the elements in the intersection
of this set and the passed set.
lunr.SortedSet.prototype.intersect = function (otherSet) {
var intersectSet = new lunr.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
lunr.SortedSet.prototype.clone = function () {
var clone = new lunr.SortedSet
clone.elements = this.toArray()
clone.length = clone.elements.length
return clone
}
Option name | Type | Description |
---|---|---|
otherSet | lunr.SortedSet | The set to union with this set. |
Creates a new lunr.SortedSet that contains the elements in the union
of this set and the passed set.
lunr.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()
for(var i = 0, shortSetElements = shortSet.toArray(); i < shortSetElements.length; i++){
unionSet.add(shortSetElements[i])
}
return unionSet
}
Returns a representation of the sorted set ready for serialisation.
lunr.SortedSet.prototype.toJSON = function () {
return this.toArray()
}