JSFiddle - React, Tailwind, and code Playground

by Hans PUFAL

JavaScript

function Set (len) {
    this.bits = new Array (Math.floor ((len + 49) / 50));
    for (var i = this.bits.length; i--;)
      this.bits[i] = 0;
  };

  Set.prototype.full = function () {
    for (var i = this.bits.length; i--;)
      if (this.bits[i] !== Math.pow (2, 51) - 1)
        return false;
    return true;
  };

  Set.prototype.check = function (n) {
    var m = n % 50; 
    return Math.floor (this.bits [(n - m) / 50] / Math.pow (2, m)) === 1;
  };

  Set.prototype.add = function (n) {
    if (!this.check (n)) {
      var m = n % 50; 
      this.bits [(n - m) / 50] += Math.pow (2, m);
      return true;
    }
    return false;
  };

  Set.prototype.remove = function (n) {
    if (this.check (n)) {
      var m = n % 50; 
      this.bits [(n - m) / 50] -= Math.pow (2, m);
      return true;
    }
    return false;
  };

  Set.prototype.toString = function () {
    for (var s = [], i = this.bits.length; i--;)
      s.push ((Array (51).join ('0') + this.bits[i].toString (2)).slice (-50));
    return s.join ('').replace (/^0+/, '') || '0';
  }
      
  b = new Set (150);
  console.log ('' + b, b.check (4), b.check (3), b.check(4));
  b.add (4);
  console.log ('' + b, b.check (4), b.check (3), b.check(4));
  b.add (3);
  console.log ('' + b, b.check (4), b.check (3), b.check(4));