<commit_before><?php


class CommentTreeNode {
	public $id;
	public $children;
	public $level;

	public function __construct($id, $child = false) {
		$this->children = array();
		$this->id = $id;
		if ($child) {
			$this->addChild($child);
		}
	}

	public function addChild($child) {
		if (! in_array($child, $this->children)) {
			$this->children[] = $child;
			return True;
		}
		return False;
	}

	public function deepFirst($max = 100, $level = 0, $seen = False) {
		if ($seen === false) {
			$seen = array();
		}

		if ($level >= $max || in_array($this->id, $seen)) {
			return $seen;
		}

		$this->level = $level;
		$seen[] = $this->id;
		foreach ($this->children as $child) {
			if (! in_array($child->id, $seen)) {
				$seen = $child->deepFirst($max, $level + 1, $seen);
			}
		}
		return $seen;
	}
}

class CommentTree {
	public $rootsIds;
	public $roots;
	public $nodesIds;

	public function __construct() {
		$this->nodesIds = array();
		$this->rootsIds = array();
		$this->roots = array();
	}

	protected function getNodeById($id) {
		if (isset($this->nodesIds[$id])) {
			return $this->nodesIds[$id];
		} else {
			$node = new CommentTreeNode($id);
			return $node;
		}
	}

	protected function addToIndexes($node, $parent = false) {
		if (! isset($this->nodesIds[$node->id])) {
			$this->nodesIds[$node->id] = $node;
			if (! $parent) {
				if( ! isset($this->rootsIds[$node->id])) {
					$this->rootsIds[$node->id] = $node;
					$this->roots[] = $node;
				}
			}
		}

		if ($parent && isset($this->rootsIds[$node->id]) ) {
			unset($this->rootsIds[$node->id]);
			if(($key = array_search($node, $this->roots)) !== false) {
				unset($this->roots[$key]);
			}
		}
	}

	public function addByIds($parent_id, $child_id = 0) {
		if ($parent_id == $child_id) {
			return false;
		}

		$parent = $this->getNodeById($parent_id);
		$this->addToIndexes($parent);

		if ($child_id > 0) {
			$child = $this->getNodeById($child_id);
			$parent->addChild($child);
			$this->addToIndexes($child, $parent);
		}
	}

	public function deepFirst($max = 100) {
		$seen = array();
		ksort($this->rootsIds);
		foreach ($this->rootsIds as $n) {
			$seen = $n->deepFirst($max, 0, $seen);
		}
		return $seen;
	}<commit_msg>Don't remove from root if the parent if not in root.<commit_after><?php


class CommentTreeNode {
	public $id;
	public $children;
	public $level;

	public function __construct($id, $child = false) {
		$this->children = array();
		$this->id = $id;
		if ($child) {
			$this->addChild($child);
		}
	}

	public function addChild($child) {
		if (! in_array($child, $this->children)) {
			$this->children[] = $child;
			return True;
		}
		return False;
	}

	public function deepFirst($max = 100, $level = 0, $seen = False) {
		if ($seen === false) {
			$seen = array();
		}

		if ($level >= $max || in_array($this->id, $seen)) {
			return $seen;
		}

		$this->level = $level;
		$seen[] = $this->id;
		foreach ($this->children as $child) {
			if (! in_array($child->id, $seen)) {
				$seen = $child->deepFirst($max, $level + 1, $seen);
			}
		}
		return $seen;
	}
}

class CommentTree {
	public $rootsIds;
	public $roots;
	public $nodesIds;

	public function __construct() {
		$this->nodesIds = array();
		$this->rootsIds = array();
		$this->roots = array();
	}

	protected function getNodeById($id) {
		if (isset($this->nodesIds[$id])) {
			return $this->nodesIds[$id];
		} else {
			$node = new CommentTreeNode($id);
			return $node;
		}
	}

	protected function addToIndexes($node, $parent = false) {
		if (! isset($this->nodesIds[$node->id])) {
			$this->nodesIds[$node->id] = $node;
			if (! $parent) {
				if( ! isset($this->rootsIds[$node->id])) {
					$this->rootsIds[$node->id] = $node;
					$this->roots[] = $node;
				}
			}
		}

		if ($parent && isset($this->rootsIds[$node->id]) 
			&& isset($this->rootsIds[$parent->id]) ) { // Only remove if the parent is root
			unset($this->rootsIds[$node->id]);
			if(($key = array_search($node, $this->roots)) !== false) {
				unset($this->roots[$key]);
			}
		}
	}

	public function addByIds($parent_id, $child_id = 0) {
		if ($parent_id == $child_id) {
			$child_id = 0;
		}

		$parent = $this->getNodeById($parent_id);
		$this->addToIndexes($parent);

		if ($child_id > 0) {
			$child = $this->getNodeById($child_id);
			$parent->addChild($child);
			$this->addToIndexes($child, $parent);
		}
	}

	public function deepFirst($max = 100) {
		$seen = array();
		ksort($this->rootsIds);
		foreach ($this->rootsIds as $n) {
			$seen = $n->deepFirst($max, 0, $seen);
		}
		return $seen;
	}
{'fraction_non_alphanumeric': 0.20871862615587847, 'fraction_numerical': 0.005063848524878908, 'mean_word_length': 3.4495592556317334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, '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': '4615007', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1682, 'n_words': 348}