1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*!
* lunr.Set
* Copyright (C) @YEAR Oliver Nightingale
*/
/**
* A lunr set.
*
* @constructor
*/
lunr.Set = function (elements) {
this.elements = Object.create(null)
if (elements) {
this.length = elements.length
for (var i = 0; i < this.length; i++) {
this.elements[elements[i]] = true
}
} else {
this.length = 0
}
}
/**
* A complete set that contains all elements.
*
* @static
* @readonly
* @type {lunr.Set}
*/
lunr.Set.complete = {
intersect: function (other) {
return other
},
union: function (other) {
return other
},
contains: function () {
return true
}
}
/**
* An empty set that contains no elements.
*
* @static
* @readonly
* @type {lunr.Set}
*/
lunr.Set.empty = {
intersect: function () {
return this
},
union: function (other) {
return other
},
contains: function () {
return false
}
}
/**
* Returns true if this set contains the specified object.
*
* @param {object} object - Object whose presence in this set is to be tested.
* @returns {boolean} - True if this set contains the specified object.
*/
lunr.Set.prototype.contains = function (object) {
return !!this.elements[object]
}
/**
* Returns a new set containing only the elements that are present in both
* this set and the specified set.
*
* @param {lunr.Set} other - set to intersect with this set.
* @returns {lunr.Set} a new set that is the intersection of this and the specified set.
*/
lunr.Set.prototype.intersect = function (other) {
var a, b, elements, intersection = []
if (other === lunr.Set.complete) {
return this
}
if (other === lunr.Set.empty) {
return other
}
if (this.length < other.length) {
a = this
b = other
} else {
a = other
b = this
}
elements = Object.keys(a.elements)
for (var i = 0; i < elements.length; i++) {
var element = elements[i]
if (element in b.elements) {
intersection.push(element)
}
}
return new lunr.Set (intersection)
}
/**
* Returns a new set combining the elements of this and the specified set.
*
* @param {lunr.Set} other - set to union with this set.
* @return {lunr.Set} a new set that is the union of this and the specified set.
*/
lunr.Set.prototype.union = function (other) {
if (other === lunr.Set.complete) {
return lunr.Set.complete
}
if (other === lunr.Set.empty) {
return this
}
return new lunr.Set(Object.keys(this.elements).concat(Object.keys(other.elements)))
}
|