<commit_before>package com.tomgibara.tries;

import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class IndexedTrie<E> extends Trie<E> {

	// fields
	
	//TODO is this needed?
	private int rootIndex = 0;

	// constructors
	
	IndexedTrie(Tries<E> tries, TrieNodes nodes) {
		super(tries, nodes);
	}
	
	IndexedTrie(TrieSerialization<E> serialization, TrieNodes nodes) {
		super(serialization, nodes);
	}
	
	IndexedTrie(IndexedTrie<E> trie, TrieNodes nodes) {
		super(trie, nodes);
	}

	@Override
	public IndexedTrie<E> subTrie(E root) {
		return (IndexedTrie<E>) super.subTrie(root);
	}
	
	// trie methods
	
	public E get(int index) {
		if (index < 0) throw new IllegalArgumentException("negative index");
		TrieNode node = root();
		if (node == null || index >= node.getCount()) throw new IllegalArgumentException("index too large");
		int count = node.getCount();
		serialization.set(prefix);
		while (!node.isTerminal() || index != 0) {
			if (index < count) {
				if (node.isTerminal()) index--;
				node = node.getChild();
				serialization.push(node.getValue());
			} else {
				index -= count;
				node = node.getSibling();
				serialization.replace(node.getValue());
			}
			count = node.getCount();
		}
		return serialization.get();
	}
	
	public int indexOf(E e) {
		checkSerializable(e);
		serialization.set(e);
		if (!serialization.startsWith(prefix)) throw new IllegalArgumentException("element not in sub-trie");
		return indexOf(serialization.buffer(), serialization.length());
	}
	
	public List<E> asList() {
		return new TrieList();
	}

	// package scoped methods
	
	@Override
	//TODO pity we can't perform count and find at the same time
	TrieNode findRoot(byte[] bytes, int length) {
		TrieNode node = nodes.root();
		int index = 0;
		for (int i = 0; i < length; i++) {
			byte value = bytes[i];
			index += node.countToChild(value);
			node = node.findChild(value);
			if (node == null) break;
		}
		rootIndex = index;
		return node;

	}

	@Override
	IndexedTrie<E> newTrie(TrieSerialization<E> s) {
		return new IndexedTrie<E>(s, nodes);
	}

	// mutability methods
	
	@Override
	public IndexedTrie<E> immutableView() {<commit_msg>Removes redundant rootIndex from indexed trie.<commit_after>package com.tomgibara.tries;

import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class IndexedTrie<E> extends Trie<E> {

	// constructors
	
	IndexedTrie(Tries<E> tries, TrieNodes nodes) {
		super(tries, nodes);
	}
	
	IndexedTrie(TrieSerialization<E> serialization, TrieNodes nodes) {
		super(serialization, nodes);
	}
	
	IndexedTrie(IndexedTrie<E> trie, TrieNodes nodes) {
		super(trie, nodes);
	}

	@Override
	public IndexedTrie<E> subTrie(E root) {
		return (IndexedTrie<E>) super.subTrie(root);
	}
	
	// trie methods
	
	public E get(int index) {
		if (index < 0) throw new IllegalArgumentException("negative index");
		TrieNode node = root();
		if (node == null || index >= node.getCount()) throw new IllegalArgumentException("index too large");
		int count = node.getCount();
		serialization.set(prefix);
		while (!node.isTerminal() || index != 0) {
			if (index < count) {
				if (node.isTerminal()) index--;
				node = node.getChild();
				serialization.push(node.getValue());
			} else {
				index -= count;
				node = node.getSibling();
				serialization.replace(node.getValue());
			}
			count = node.getCount();
		}
		return serialization.get();
	}
	
	public int indexOf(E e) {
		checkSerializable(e);
		serialization.set(e);
		if (!serialization.startsWith(prefix)) throw new IllegalArgumentException("element not in sub-trie");
		return indexOf(serialization.buffer(), serialization.length());
	}
	
	public List<E> asList() {
		return new TrieList();
	}

	// package scoped methods
	
	@Override
	TrieNode findRoot(byte[] bytes, int length) {
		TrieNode node = nodes.root();
		for (int i = 0; i < length; i++) {
			byte value = bytes[i];
			node = node.findChild(value);
			if (node == null) break;
		}
		return node;

	}

	@Override
	IndexedTrie<E> newTrie(TrieSerialization<E> s) {
		return new IndexedTrie<E>(s, nodes);
	}

	// mutability methods
	
	@Override
	public IndexedTrie<E> immutableView() {
{'fraction_non_alphanumeric': 0.13712837059230237, 'fraction_numerical': 0.0018437427978796957, 'mean_word_length': 4.040650406504065, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 29, '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': '5445217', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1461, 'n_words': 361}