"use strict"
var __createBinding =
  (this && this.__createBinding) ||
  (Object.create
    ? function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        Object.defineProperty(o, k2, {
          enumerable: true,
          get: function () {
            return m[k]
          }
        })
      }
    : function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        o[k2] = m[k]
      })
var __setModuleDefault =
  (this && this.__setModuleDefault) ||
  (Object.create
    ? function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v })
      }
    : function (o, v) {
        o["default"] = v
      })
var __importStar =
  (this && this.__importStar) ||
  function (mod) {
    if (mod && mod.__esModule) return mod
    var result = {}
    if (mod != null)
      for (var k in mod)
        if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
          __createBinding(result, mod, k)
    __setModuleDefault(result, mod)
    return result
  }
Object.defineProperty(exports, "__esModule", { value: true })
exports.union =
  exports.union_ =
  exports.toggle_ =
  exports.toggle =
  exports.reduce =
  exports.reduce_ =
  exports.difference =
  exports.difference_ =
  exports.endMutation =
  exports.beginMutation =
  exports.partition_ =
  exports.partition =
  exports.filter_ =
  exports.filter =
  exports.isSubset_ =
  exports.isSubset =
  exports.chain_ =
  exports.chain =
  exports.every_ =
  exports.every =
  exports.equal =
  exports.size =
  exports.some_ =
  exports.some =
  exports.map =
  exports.map_ =
  exports.intersection =
  exports.intersection_ =
  exports.mutate_ =
  exports.forEach_ =
  exports.has_ =
  exports.values =
  exports.remove =
  exports.remove_ =
  exports.add =
  exports.add_ =
  exports.make =
  exports.HashSet =
    void 0
// tracing: off
require("../../../Operator")
const Function_1 = require("../../../Function")
const I = __importStar(require("../../../Iterable"))
const St = __importStar(require("../../../Structural"))
const HM = __importStar(require("../HashMap/core"))
class HashSet {
  constructor(keyMap) {
    this.keyMap = keyMap
  }
  [Symbol.iterator]() {
    return HM.keys(this.keyMap)
  }
  get [St.hashSym]() {
    return St.hashIterator(this[Symbol.iterator]())
  }
  [St.equalsSym](that) {
    return (
      that instanceof HashSet &&
      that.keyMap.size === this.keyMap.size &&
      I.corresponds(this, that, St.equals)
    )
  }
}
exports.HashSet = HashSet
function make() {
  return new HashSet(HM.make())
}
exports.make = make
function add_(set, v) {
  return set.keyMap.editable
    ? (HM.set_(set.keyMap, v, true), set)
    : new HashSet(HM.set_(set.keyMap, v, undefined))
}
exports.add_ = add_
function add(v) {
  return (set) => add_(set, v)
}
exports.add = add
function remove_(set, v) {
  return set.keyMap.editable
    ? (HM.remove_(set.keyMap, v), set)
    : new HashSet(HM.remove_(set.keyMap, v))
}
exports.remove_ = remove_
function remove(v) {
  return (set) => remove_(set, v)
}
exports.remove = remove
function values(set) {
  return HM.keys(set.keyMap)
}
exports.values = values
function has_(set, v) {
  return HM.has_(set.keyMap, v)
}
exports.has_ = has_
/**
 * Apply f to each element
 */
function forEach_(map, f) {
  HM.forEachWithIndex_(map.keyMap, (k) => {
    f(k)
  })
}
exports.forEach_ = forEach_
/**
 * Mutate `set` within the context of `f`.
 */
function mutate_(set, transient) {
  const s = beginMutation(set)
  transient(s)
  return endMutation(s)
}
exports.mutate_ = mutate_
/**
 * The set of elements which are in both the first and second set,
 *
 * the hash and equal of the 2 sets has to be the same
 */
function intersection_(l, r) {
  const x = make()
  return mutate_(x, (y) => {
    for (const k of r) {
      if (has_(l, k)) {
        add_(y, k)
      }
    }
  })
}
exports.intersection_ = intersection_
/**
 * The set of elements which are in both the first and second set
 *
 * @dataFirst intersection_
 */
function intersection(r) {
  return (l) => intersection_(l, r)
}
exports.intersection = intersection
/**
 * Projects a Set through a function
 */
function map_(set, f) {
  const r = make()
  mutate_(r, (r) => {
    forEach_(set, (e) => {
      const v = f(e)
      if (!has_(r, v)) {
        add_(r, v)
      }
    })
  })
  return r
}
exports.map_ = map_
/**
 * Projects a Set through a function
 *
 * @dataFirst map_
 */
function map(f) {
  return (set) => map_(set, f)
}
exports.map = map
/**
 * true if one or more elements match predicate
 *
 * @dataFirst some_
 */
function some(predicate) {
  return (set) => some_(set, predicate)
}
exports.some = some
/**
 * true if one or more elements match predicate
 */
function some_(set, predicate) {
  let found = false
  for (const e of set) {
    found = predicate(e)
    if (found) {
      break
    }
  }
  return found
}
exports.some_ = some_
/**
 * Calculate the number of keys pairs in a set
 */
function size(set) {
  return HM.size(set.keyMap)
}
exports.size = size
/**
 * Creates an equal for a set
 */
function equal() {
  return {
    equals: (x, y) => {
      if (y === x) {
        return true
      }
      if (size(x) !== size(y)) {
        return false
      }
      let eq = true
      for (const vx of x) {
        if (!has_(y, vx)) {
          eq = false
          break
        }
      }
      return eq
    }
  }
}
exports.equal = equal
/**
 * true if all elements match predicate
 *
 * @dataFirst every_
 */
function every(predicate) {
  return (set) => every_(set, predicate)
}
exports.every = every
/**
 * true if all elements match predicate
 */
function every_(set, predicate) {
  return (0, Function_1.not)(some((0, Function_1.not)(predicate)))(set)
}
exports.every_ = every_
/**
 * Map + Flatten
 *
 * @dataFirst chain_
 */
function chain(f) {
  return (set) => chain_(set, f)
}
exports.chain = chain
/**
 * Map + Flatten
 */
function chain_(set, f) {
  const r = make()
  mutate_(r, (r) => {
    forEach_(set, (e) => {
      for (const a of f(e)) {
        if (!has_(r, a)) {
          add_(r, a)
        }
      }
    })
  })
  return r
}
exports.chain_ = chain_
/**
 * `true` if and only if every element in the first set is an element of the second set,
 *
 * the hash and equal of the 2 sets has to be the same
 *
 * @dataFirst isSubset_
 */
function isSubset(y) {
  return (x) => isSubset_(y, x)
}
exports.isSubset = isSubset
/**
 * `true` if and only if every element in the first set is an element of the second set,
 *
 * the hash and equal of the 2 sets has to be the same
 */
function isSubset_(x, y) {
  return every_(x, (a) => has_(y, a))
}
exports.isSubset_ = isSubset_
function filter(predicate) {
  return (set) => filter_(set, predicate)
}
exports.filter = filter
function filter_(set, predicate) {
  const r = make()
  return mutate_(r, (r) => {
    const values_ = values(set)
    let e
    while (!(e = values_.next()).done) {
      const value = e.value
      if (predicate(value)) {
        add_(r, value)
      }
    }
    return r
  })
}
exports.filter_ = filter_
function partition(predicate) {
  return (set) => partition_(set, predicate)
}
exports.partition = partition
function partition_(set, predicate) {
  const values_ = values(set)
  let e
  const right = beginMutation(make())
  const left = beginMutation(make())
  while (!(e = values_.next()).done) {
    const value = e.value
    if (predicate(value)) {
      add_(right, value)
    } else {
      add_(left, value)
    }
  }
  return { left: endMutation(left), right: endMutation(right) }
}
exports.partition_ = partition_
/**
 * Mark `set` as mutable.
 */
function beginMutation(set) {
  return new HashSet(HM.beginMutation(set.keyMap))
}
exports.beginMutation = beginMutation
/**
 * Mark `set` as immutable.
 */
function endMutation(set) {
  set.keyMap.editable = false
  return set
}
exports.endMutation = endMutation
/**
 * Form the set difference (`x` - `y`)
 */
function difference_(x, y) {
  return mutate_(x, (s) => {
    for (const k of y) {
      remove_(s, k)
    }
  })
}
exports.difference_ = difference_
/**
 * Form the set difference (`x` - `y`)
 *
 * @dataFirst difference_
 */
function difference(y) {
  return (x) => difference_(x, y)
}
exports.difference = difference
/**
 * Reduce a state over the map entries
 */
function reduce_(set, z, f) {
  return HM.reduceWithIndex_(set.keyMap, z, (z, v) => f(z, v))
}
exports.reduce_ = reduce_
/**
 * Reduce a state over the map entries
 *
 * @dataFirst reduce_
 */
function reduce(z, f) {
  return (set) => reduce_(set, z, f)
}
exports.reduce = reduce
/**
 * If element is present remove it, if not add it
 *
 * @dataFirst toggle_
 */
function toggle(a) {
  return (set) => toggle_(set, a)
}
exports.toggle = toggle
/**
 * If element is present remove it, if not add it
 */
function toggle_(set, a) {
  return (has_(set, a) ? remove : add)(a)(set)
}
exports.toggle_ = toggle_
/**
 * Form the union of two sets,
 *
 * the hash and equal of the 2 sets has to be the same
 */
function union_(l, r) {
  const x = make()
  return mutate_(x, (x) => {
    forEach_(l, (a) => {
      add_(x, a)
    })
    for (const a of r) {
      add_(x, a)
    }
  })
}
exports.union_ = union_
/**
 * Form the union of two sets,
 *
 * the hash and equal of the 2 sets has to be the same
 *
 * @dataFirst union_
 */
function union(y) {
  return (x) => union_(x, y)
}
exports.union = union
//# sourceMappingURL=index.js.map
