set.js 2.4 KB

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