123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- lunr.SortedSet = function () {
- this.length = 0
- this.elements = []
- }
- lunr.SortedSet.load = function (serialisedData) {
- var set = new this
- set.elements = serialisedData
- set.length = serialisedData.length
- return set
- }
- /**
- * Inserts new items into the set in the correct position to maintain the
- * order.
- *
- * @param {Object} The objects to add to this set.
- * @memberOf SortedSet
- */
- 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
- }
- lunr.SortedSet.prototype.toArray = function () {
- return this.elements.slice()
- }
- lunr.SortedSet.prototype.map = function (fn, ctx) {
- return this.elements.map(fn, ctx)
- }
- lunr.SortedSet.prototype.forEach = function (fn, ctx) {
- return this.elements.forEach(fn, ctx)
- }
- 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
- }
- 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
- }
- 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
- }
- lunr.SortedSet.prototype.clone = function () {
- var clone = new lunr.SortedSet
- clone.elements = this.toArray()
- clone.length = clone.elements.length
- return clone
- }
- 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
- }
- lunr.SortedSet.prototype.toJSON = function () {
- return this.toArray()
- }
|