Create BitSet class - Node.js Data Structure

Node.js examples for Data Structure:Set

Description

Create BitSet class

Demo Code

function BitSet() {
  this.data = [];/*from  w w  w  .j a  v  a2  s .  c  o  m*/
  return this;
}

BitSet.prototype.add = function(value) {
  this.data[value] = true;
};

BitSet.prototype.or = function(set) {
  var bits = this;
  Object.keys(set.data).map( function(alt) { bits.add(alt); });
};

BitSet.prototype.remove = function(value) {
  delete this.data[value];
};

BitSet.prototype.contains = function(value) {
  return this.data[value] === true;
};

BitSet.prototype.values = function() {
  return Object.keys(this.data);
};

BitSet.prototype.minValue = function() {
  return Math.min.apply(null, this.values());
};

BitSet.prototype.hashString = function() {
  return this.values().toString();
};

BitSet.prototype.equals = function(other) {
  if(!(other instanceof BitSet)) {
    return false;
  }
  return this.hashString()===other.hashString();
};

Object.defineProperty(BitSet.prototype, "length", {
  get : function() {
    return this.values().length;
  }
});

BitSet.prototype.toString = function() {
  return "{" + this.values().join(", ") + "}";
};

Related Tutorials