<commit_before>'use strict';
var gbv = require('geojson-bounding-volume');
var utils = require('./utils');
var MemStore = require('./memmap');
var uuid = require('node-uuid');
var MIN = 3;
var MAX = 9;

function RTree(store) {
  this.store = store || new MemStore();
  this.root = false;
}

function insert(self, id, bbox, nodeID) {
  var store = self.store;
  if (!nodeID) {
    if (!self.root) {
      return store.put('root', {
        bbox: bbox,
        leaf: true,
        children: [{
          id: id,
          bbox: bbox
        }]
      }).then(function (resp){
        self.root = 'root';
        return resp;
      });
    } else {
      return insert(self, id, bbox, self.root);
    }
  }
  return choosePath(self, bbox, nodeID).then(function (path) {
    var leaf = path[path.length - 1];
    leaf.children.push({
      id: id,
      bbox: bbox
    });
    leaf.bbox = utils.enlarge(bbox, leaf.bbox);
    if (leaf.children.length < MAX) {
      return updatePath(self.store, path);
    } else {
      return splitLeaf(self, path);
    }
  });
}
// much of this from rbush
function splitLeaf(self, path) {
  var node = path[path.length - 1];
  chooseAxis(node.children);
  var splitIndex = chooseIndex(node.children);
}
function chooseAxis(children) {
  var indices = children[0].bbox[0].length;
  var i = 0;
  var minMargin = allDistMargin(children, 0);
  var mindex = 0;
  var margin;
  while (++i < indices) {
    margin = allDistMargin(children, i);
    if (margin < minMargin) {
      mindex = i;
      minMargin = margin;
    }
  }
  if (mindex !== (i - 1)) {
    children.sort(function (a, b) {
      return a[0][mindex] - b[0][mindex];
    });
  }
}
function chooseIndex(children) {
  var len = children.length;

  var i = MIN;
  var leftBBox = children.slice(0, i).reduce(utils.enlarger);
  var rightBBox = children.slice(len - i, len).reduce(utils.enlarger);<commit_msg>propigate splits<commit_after>'use strict';
var gbv = require('geojson-bounding-volume');
var utils = require('./utils');
var MemStore = require('./memmap');
var uuid = require('node-uuid');
var MIN = 3;
var MAX = 9;

function RTree(store) {
  this.store = store || new MemStore();
  this.root = false;
}

function insert(self, id, bbox, nodeID) {
  var store = self.store;
  if (!nodeID) {
    if (!self.root) {
      return store.put('root', {
        bbox: bbox,
        leaf: true,
        children: [{
          id: id,
          bbox: bbox
        }]
      }).then(function (resp){
        self.root = 'root';
        return resp;
      });
    } else {
      return insert(self, id, bbox, self.root);
    }
  }
  return choosePath(self, bbox, nodeID).then(function (path) {
    var leaf = path[path.length - 1];
    leaf.children.push({
      id: id,
      bbox: bbox
    });
    leaf.bbox = utils.enlarge(bbox, leaf.bbox);
    if (leaf.children.length >= MAX) {
      splitLeaf(self, path);
    }
    return updatePath(self.store, path);
  });
}
// much of this from rbush
function splitLeaf(self, path) {
  var i = path.length;
  var newNode, splitedNode;
  while (path[--i].children.length < MAX && i) {
    newNode = splitNode(path[i]);
    path[i - 1].push(newNode);
    path.push(newNode);
  }
  if (i === 0 && path[0].children.length >= MAX) {
    newNode = {
      id: uuid.v4(),
      bbox: path[0].bbox,
      children: path[0].children
    }
    path.push(newNode);
    splitedNode = splitNode(newNode);
    path.push(splitedNode);
    path[0].children = [newNode, splitedNode];
  }
}
function splitNode(node) {
  chooseAxis(node.children);
  var index = chooseIndex(node.children);
  var newNode = {
    id: uuid.v4()
  };
  if ('leaf' in node) }
    newNode.leaf = true;
  }
  newNode.children = node.children.slice(0, index);
  node.children = node.children.slice(node.children.length - index, node.children.length);
  newNode.bbox = newNode.children.reduce(utils.enlarger);
  node.bbox = node.children.reduce(utils.enlarger);
}
function chooseAxis(children) {
  var indices = children[0].bbox[0].length;
  var i = 0;
  var minMargin = allDistMargin(children, 0);
  var mindex = 0;
  var margin;
  while (++i < indices) {
    margin = allDistMargin(children, i);
    if (margin < minMargin) {
      mindex = i;
      minMargin = margin;
    }
  }
  if (mindex !== (i - 1)) {
    children.sort(function (a, b) {
      return a[0][mindex] - b[0][mindex];
    });
  }
}
function chooseIndex(children) {
  var len = children.length;

  var i = MIN;
  var leftBBox = children.slice(0, i).reduce(utils.enlarger);
  var rightBBox = children.slice(len - i, len).reduce(utils.enlarger);
{'fraction_non_alphanumeric': 0.15897212543554007, 'fraction_numerical': 0.007404181184668989, 'mean_word_length': 2.716019417475728, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '4386434', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1624, 'n_words': 377}