<commit_before>var namespace = require("./namespace");

var document = global.document,
    CustomEvent = global.CustomEvent,
    Map = global.Map,
    slice = [].slice,
    filteredEvents = {mouseenter: "mouseover", mouseleave: "mouseout"},
    requoteRe = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;

// Initialize polyfills as necessary.
if (document) {
  for (var type in filteredEvents) { // mouseenter, mouseleave
    if ("on" + type in document) {
      delete filteredEvents[type];
    }
  }

  if (!CustomEvent) {
    CustomEvent = function(type, params) {
      var event = document.createEvent("CustomEvent");
      if (params) event.initCustomEvent(type, params.bubbles, params.cancelable, params.detail);
      else event.initCustomEvent(type, false, false, undefined);
      return event;
    };
    CustomEvent.prototype = global.Event.prototype;
  }

  if (!Map) {
    Map = function() {};
    Map.prototype = {
      set: function(key, value) { this["$" + key] = value; return this; },
      get: function(key) { return this["$" + key]; },
      has: function(key) { return "$" + key in this; }
    };
  }
}

// For a flat selection, root = NodeList or [Node, …].
// For a one-level nested selection, root = [NodeList, NodeList, …].
// For a two-level nested selection, root = [[NodeList, …], …] etc.
function Selection(root, depth) {
  root._parent = null;
  this._root = root;
  this._depth = depth;
}

Selection.prototype = {

  // The selector may either be a selector string (e.g., ".foo")
  // or a function that optionally returns the node to select.
  select: function(selector) {
    var depth = this._depth,
        stack = new Array(depth * 2);

    selector = selectorOf(selector);

    function visit(nodes, depth) {
      var i = -1,
          n = nodes.length,
          update,
          node,
          subnode,
          subnodes = new Array(n);

      if (--depth) {
        var stack0 = depth * 2,
            stack1 = stack0 + 1;
        while (++i < n) {
          node = nodes[i];
          stack[stack0] = node._parent.__data__, stack[stack1] = i;
          subnodes[i] = visit(node, depth);
        }
      }

      else {
        update = nodes._update;
        while (++i < n) {
          if (node = nodes[i]) {
            stack[0] = node.__data__, stack[1] = i;
            if (subnode = selector.apply(node, stack)) {
              if ("__data__" in node) subnode.__data__ = node.__data__;
              if (update) update[i] = subnode;
              subnodes[i] = subnode;
            }
          }
        }
      }

      subnodes._parent = nodes._parent;
      return subnodes;
    }

    return new Selection(visit(this._root, depth), depth);
  },

  // The selector may either be a selector string (e.g., ".foo")
  // or a function that optionally returns an array of nodes to select.
  selectAll: function(selector) {
    var depth = this._depth,
        stack = new Array(depth * 2);

    selector = selectorAllOf(selector);

    function visit(nodes, depth) {
      var i = -1,
          n = nodes.length,
          node,
          subnode,
          subnodes = new Array(n);

      if (--depth) {
        var stack0 = depth * 2,
            stack1 = stack0 + 1;
        while (++i < n) {
          node = nodes[i];
          stack[stack0] = node._parent.__data__, stack[stack1] = i;
          subnodes[i] = visit(node, depth);
        }
      }

      else {
        while (++i < n) {
          if (node = nodes[i]) {
            stack[0] = node.__data__, stack[1] = i;
            subnodes[i] = subnode = selector.apply(node, stack);
            subnode._parent = node;
          }
        }<commit_msg>Comments.<commit_after>var namespace = require("./namespace");

var document = global.document,
    CustomEvent = global.CustomEvent,
    Map = global.Map,
    slice = [].slice,
    filteredEvents = {mouseenter: "mouseover", mouseleave: "mouseout"},
    requoteRe = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;

// Initialize polyfills as necessary.
if (document) {
  for (var type in filteredEvents) { // mouseenter, mouseleave
    if ("on" + type in document) {
      delete filteredEvents[type];
    }
  }

  if (!CustomEvent) {
    CustomEvent = function(type, params) {
      var event = document.createEvent("CustomEvent");
      if (params) event.initCustomEvent(type, params.bubbles, params.cancelable, params.detail);
      else event.initCustomEvent(type, false, false, undefined);
      return event;
    };
    CustomEvent.prototype = global.Event.prototype;
  }

  if (!Map) {
    Map = function() {};
    Map.prototype = {
      set: function(key, value) { this["$" + key] = value; return this; },
      get: function(key) { return this["$" + key]; },
      has: function(key) { return "$" + key in this; }
    };
  }
}

// When depth = 1, root = [Node, …].
// When depth = 2, root = [[Node, …], [Node, …], …].
// When depth = 3, root = [[[Node, …], …], …]. etc.
// Note that [Node, …] and NodeList are used interchangeably.
function Selection(root, depth) {
  root._parent = null;
  this._root = root;
  this._depth = depth;
}

Selection.prototype = {

  // The selector may either be a selector string (e.g., ".foo")
  // or a function that optionally returns the node to select.
  select: function(selector) {
    var depth = this._depth,
        stack = new Array(depth * 2);

    selector = selectorOf(selector);

    function visit(nodes, depth) {
      var i = -1,
          n = nodes.length,
          update,
          node,
          subnode,
          subnodes = new Array(n);

      if (--depth) {
        var stack0 = depth * 2,
            stack1 = stack0 + 1;
        while (++i < n) {
          node = nodes[i];
          stack[stack0] = node._parent.__data__, stack[stack1] = i;
          subnodes[i] = visit(node, depth);
        }
      }

      else {
        update = nodes._update;
        while (++i < n) {
          if (node = nodes[i]) {
            stack[0] = node.__data__, stack[1] = i;
            if (subnode = selector.apply(node, stack)) {
              if ("__data__" in node) subnode.__data__ = node.__data__;
              if (update) update[i] = subnode;
              subnodes[i] = subnode;
            }
          }
        }
      }

      subnodes._parent = nodes._parent;
      return subnodes;
    }

    return new Selection(visit(this._root, depth), depth);
  },

  // The selector may either be a selector string (e.g., ".foo")
  // or a function that optionally returns an array of nodes to select.
  // This is the only operation that increases the depth of a selection.
  selectAll: function(selector) {
    var depth = this._depth,
        stack = new Array(depth * 2);

    selector = selectorAllOf(selector);

    function visit(nodes, depth) {
      var i = -1,
          n = nodes.length,
          node,
          subnode,
          subnodes = new Array(n);

      if (--depth) {
        var stack0 = depth * 2,
            stack1 = stack0 + 1;
        while (++i < n) {
          node = nodes[i];
          stack[stack0] = node._parent.__data__, stack[stack1] = i;
          subnodes[i] = visit(node, depth);
        }
      }

      else {
        while (++i < n) {
          if (node = nodes[i]) {
            stack[0] = node.__data__, stack[1] = i;
            subnodes[i] = subnode = selector.apply(node, stack);
            subnode._parent = node;
          }
        }
{'fraction_non_alphanumeric': 0.14475287745429927, 'fraction_numerical': 0.006364251861882194, 'mean_word_length': 1.9890732496964791, 'pattern_counts': {'":': 0, '<': 11, '<?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': '2086953', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2246, 'n_words': 641}