<commit_before>(function () {
  'use strict';

  var emptyBucket = require('./bucket');

  function arrayCopy(array) {
    var copy = [];
    for (var i = 0; i < array.length; i++) {
      copy.push(array[i]);
    }
    return copy;
  }

  function arraysEqual(a, b) {
    if (!a || !b) {
      return false;
    }
    if (a.byteLength !== b.byteLength) {
      return false;
    }
    a = new Int8Array(a);
    b = new Int8Array(b);
    for (var i = 0; i < a.length; i++) {
      if (a[i] !== b[i]) {
        return false;
      }
    }
    return true;
  }

  function intFromDigestedBytes(digested) {
    return new DataView(digested).getInt32(0);
  }

  function bucketsOfSize(size) {
    var buckets = [];
    for (var i = 0; i < size; i++) {
      buckets.push(emptyBucket);
    }
    return buckets;
  }

  function ibf(size, digest, spread) {
    return ibfFromBuckets(bucketsOfSize(size), digest, spread);
  }

  function ibfFromBuckets(buckets, digest, spread) {

    function toJSON() {
      var json = [];
      buckets.forEach(function (bucket) {
        json.push(bucket.toJSON());
      });
      return json;
    }

    function modify(variation, content) {
      var bucketId;
      var bucketsCopy = arrayCopy(buckets, buckets.length);

      var digested = digest(content);

      var inview = new Int8Array(content);
      var copy = new ArrayBuffer(inview.length + 1);
      var outview = new Int8Array(copy);
      for (var j = 0; j < inview.length; j++) {
        outview[j] = inview[j];
      }

      for (var i = 0; i < spread; i++) {
        outview[outview.length - 1] = i;

        bucketId = intFromDigestedBytes(digest(copy)) % buckets.length;
        if (bucketId < 0) {
          bucketId += buckets.length;
        }
        bucketsCopy[bucketId] = bucketsCopy[bucketId].modify(variation, content, digested);
      }

      return ibfFromBuckets(bucketsCopy, digest, spread);
    }

    function addItem(content) {
      return modify(1, content);
    }

    function asDifference() {<commit_msg>Efficient array buffer copy when computing buckets to store an item in<commit_after>(function () {
  'use strict';

  var emptyBucket = require('./bucket');

  function arrayCopy(array) {
    var copy = [];
    for (var i = 0; i < array.length; i++) {
      copy.push(array[i]);
    }
    return copy;
  }

  function arraysEqual(a, b) {
    if (!a || !b) {
      return false;
    }
    if (a.byteLength !== b.byteLength) {
      return false;
    }
    a = new Int8Array(a);
    b = new Int8Array(b);
    for (var i = 0; i < a.length; i++) {
      if (a[i] !== b[i]) {
        return false;
      }
    }
    return true;
  }

  function intFromDigestedBytes(digested) {
    return new DataView(digested).getInt32(0);
  }

  function bucketsOfSize(size) {
    var buckets = [];
    for (var i = 0; i < size; i++) {
      buckets.push(emptyBucket);
    }
    return buckets;
  }

  function ibf(size, digest, spread) {
    return ibfFromBuckets(bucketsOfSize(size), digest, spread);
  }

  function ibfFromBuckets(buckets, digest, spread) {

    function toJSON() {
      var json = [];
      buckets.forEach(function (bucket) {
        json.push(bucket.toJSON());
      });
      return json;
    }

    function modify(variation, content) {
      var bucketId;
      var bucketsCopy = arrayCopy(buckets, buckets.length);

      var digested = digest(content);

      var copy = new Int8Array(content.byteLength + 1);
      copy.set(new Int8Array(content));
      for (var i = 0; i < spread; i++) {
        copy[copy.length - 1] = i;

        bucketId = intFromDigestedBytes(digest(copy.buffer)) % buckets.length;
        if (bucketId < 0) {
          bucketId += buckets.length;
        }
        bucketsCopy[bucketId] = bucketsCopy[bucketId].modify(variation, content, digested);
      }

      return ibfFromBuckets(bucketsCopy, digest, spread);
    }

    function addItem(content) {
      return modify(1, content);
    }

    function asDifference() {
{'fraction_non_alphanumeric': 0.1368421052631579, 'fraction_numerical': 0.007769423558897243, 'mean_word_length': 2.3879456706281834, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1652095', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1289, 'n_words': 316}