<commit_before>package org.trvedata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class OrderedList<T> implements Iterable<T> {
	static class Item<T> {
		ItemID insertId;
		ItemID deleteTs;
		T value;
		Item<T> prev;
		Item<T> next;

		public Item(ItemID insertId, ItemID deleteTs, T value, Item<T> previous, Item<T> next) {
			this.insertId = insertId;
			this.deleteTs = deleteTs;
			this.value = value;
			this.prev = previous;
			this.next = next;
		}
	}

	private Peer<T> peer;
	private long lamportClock;
	private HashMap<ItemID, Item<T>> itemsById;
	private Item<T> head, tail;
	private Set<CRDTEventListener> eventListeners;

	public OrderedList(Peer<T> peer) {
		if (peer == null)
			throw new RuntimeException("OrderedList: peer must be set");
		this.peer = peer;
		this.lamportClock = 0;
		this.itemsById = new HashMap<ItemID, Item<T>>();
		this.head = null;
		this.tail = null;
		this.eventListeners = new HashSet<CRDTEventListener>();
	}

	public OrderedList<T> insert(int index, T value) {
		ItemID leftId = null;
		if (index > 0) {
			Item<T> rightItem = this.itemByIndex(index);
			leftId = rightItem != null ? rightItem.prev.insertId : this.tail.insertId;
		}

		Item<T> item = this.insertAfterId(leftId, this.peer.nextId(), value);
		InsertOp<T> op = new InsertOp<T>(item.prev == null ? null : item.prev.insertId, item.insertId, item.value);
		this.peer.sendOperation(op);
		return this;
	}

	/**
	 * Inserts a new item before the existing item identified by cursorId (local operation). If cursorId is nil, appends
	 * to the end of the list.
	 */
	public ItemID insertBeforeId(ItemID cursorId, T value) {
		ItemID leftId;
		if (cursorId == null) {
			leftId = this.tail == null ? null : this.tail.insertId;
		} else {
			Item<T> rightItem = this.itemsById.get(cursorId);
			if (rightItem == null)
				throw new RuntimeException("insertBeforeId: unknown cursorId: " + cursorId);
			leftId = rightItem.prev == null ? null : rightItem.prev.insertId;
		}

		Item<T> item = this.insertAfterId(leftId, this.peer.nextId(), value);
		this.peer.sendOperation(new InsertOp<T>(item.prev == null ? null : item.prev.insertId, item.insertId, item.value));
		return item.insertId;
	}

	/**
	 * Deletes the item at the given index in the list (local operation).
	 */
	public OrderedList<T> remove(int index) {
		Item<T> item = this.itemByIndex(index);
		if (item == null)
			throw new RuntimeException("remove: unknown item with index " + index);

		item.deleteTs = this.peer.nextId();
		item.value = null;
		this.peer.sendOperation(new DeleteOp(item.insertId, item.deleteTs));
		return this;
	}

	/**
	 * Deletes numItems items from the list (local operation). The items to be deleted are to the left of the item
	 * identified by cursorId (not including the item identified by cursorId itself). If cursorId is nil, deletes
	 * numItems from the end of the list. Returns the ID of the last non-deleted item before the sequence of deleted
	 * items.
	 */
	public ItemID removeBeforeId(ItemID cursorId, int numItems) {
		Item<T> item, cursor;
		if (cursorId == null) {
			item = this.tail;
		} else {
			cursor = this.itemsById.get(cursorId);
			if (cursor == null)
				throw new RuntimeException("removeBeforeId: unknown cursorId: " + cursorId);
			item = cursor.prev;
		}

		while (item != null && (numItems > 0 || item.deleteTs != null)) {
			if (item.deleteTs == null) {
				item.deleteTs = this.peer.nextId();
				item.value = null;
				this.peer.sendOperation(new DeleteOp(item.insertId, item.deleteTs));
				numItems--;
			}
			item = item.prev;
		}

		return item == null ? null : item.insertId;
	}

	// Deletes numItems items from the list (local operation). The item
	public ItemID removeAfterId(ItemID cursorId, int numItems) {
		Item<T> item = this.itemsById.get(cursorId);
		if (item == null)
			throw new RuntimeException("removeAfterId: unknown item with cursorId " + cursorId);

		while (item != null && (numItems > 0 || item.deleteTs != null)) {
			if (item.deleteTs == null) {
				item.deleteTs = this.peer.nextId();
				item.value = null;
				this.peer.sendOperation(new DeleteOp(item.insertId, item.deleteTs));
				numItems--;<commit_msg>Some refactoring and reformating of Java implementation<commit_after>package org.trvedata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class OrderedList<T> implements Iterable<T> {
	static class Item<T> {
		ItemID insertId;
		ItemID deleteTs;
		T value;
		Item<T> prev;
		Item<T> next;

		public Item(ItemID insertId, ItemID deleteTs, T value, Item<T> previous, Item<T> next) {
			this.insertId = insertId;
			this.deleteTs = deleteTs;
			this.value = value;
			this.prev = previous;
			this.next = next;
		}
	}

	private Peer<T> peer;
	private long lamportClock;
	private HashMap<ItemID, Item<T>> itemsById;
	private Item<T> head, tail;
	private Set<CRDTEventListener> eventListeners;

	public OrderedList(Peer<T> peer) {
		if (peer == null)
			throw new RuntimeException("OrderedList: peer must be set");
		this.peer = peer;
		this.lamportClock = 0;
		this.itemsById = new HashMap<ItemID, Item<T>>();
		this.head = null;
		this.tail = null;
		this.eventListeners = new HashSet<CRDTEventListener>();
	}

	public OrderedList<T> insert(int index, T value) {
		ItemID leftId = null;
		if (index > 0) {
			Item<T> rightItem = this.itemByIndex(index);
			leftId = rightItem != null ? rightItem.prev.insertId : this.tail.insertId;
		}

		Item<T> item = this.insertAfterId(leftId, this.peer.nextId(), value);
		InsertOp<T> op = new InsertOp<T>(item.prev == null ? null : item.prev.insertId, item.insertId, item.value);
		this.peer.sendOperation(op);
		return this;
	}

	/**
	 * Inserts a new item before the existing item identified by cursorId (local operation). If cursorId is nil, appends
	 * to the end of the list.
	 */
	public ItemID insertBeforeId(ItemID cursorId, T value) {
		ItemID leftId;
		if (cursorId == null) {
			leftId = this.tail == null ? null : this.tail.insertId;
		} else {
			Item<T> rightItem = this.itemsById.get(cursorId);
			if (rightItem == null)
				throw new RuntimeException("insertBeforeId: unknown cursorId: " + cursorId);
			leftId = rightItem.prev == null ? null : rightItem.prev.insertId;
		}

		Item<T> item = this.insertAfterId(leftId, this.peer.nextId(), value);
		this.peer.sendOperation(new InsertOp<T>(item.prev == null ? null : item.prev.insertId, item.insertId, item.value));
		return item.insertId;
	}

	/**
	 * Deletes the item at the given index in the list (local operation).
	 */
	public OrderedList<T> remove(int index) {
		Item<T> item = this.itemByIndex(index);
		if (item == null)
			throw new RuntimeException("remove: unknown item with index " + index);

		item.deleteTs = this.peer.nextId();
		item.value = null;
		this.peer.sendOperation(new DeleteOp(item.insertId, item.deleteTs));
		return this;
	}

	/**
	 * Deletes numItems items from the list (local operation). The items to be deleted are to the left of the item
	 * identified by cursorId (not including the item identified by cursorId itself). If cursorId is nil, deletes
	 * numItems from the end of the list. Returns the ID of the last non-deleted item before the sequence of deleted
	 * items.
	 */
	public ItemID removeBeforeId(ItemID cursorId, int numItems) {
		Item<T> item, cursor;
		if (cursorId == null) {
			item = this.tail;
		} else {
			cursor = this.itemsById.get(cursorId);
			if (cursor == null)
				throw new RuntimeException("removeBeforeId: unknown cursorId: " + cursorId);
			item = cursor.prev;
		}

		while (item != null && (numItems > 0 || item.deleteTs != null)) {
			if (item.deleteTs == null) {
				item.deleteTs = this.peer.nextId();
				item.value = null;
				this.peer.sendOperation(new DeleteOp(item.insertId, item.deleteTs));
				numItems--;
			}
			item = item.prev;
		}

		return item == null ? null : item.insertId;
	}

	/**
	 * Deletes numItems items from the list (local operation). The item
	 */
	public ItemID removeAfterId(ItemID cursorId, int numItems) {
		Item<T> item = this.itemsById.get(cursorId);
		if (item == null)
			throw new RuntimeException("removeAfterId: unknown item with cursorId " + cursorId);

		while (item != null && (numItems > 0 || item.deleteTs != null)) {
			if (item.deleteTs == null) {
				item.deleteTs = this.peer.nextId();
				item.value = null;
				this.peer.sendOperation(new DeleteOp(item.insertId, item.deleteTs));
				numItems--;
{'fraction_non_alphanumeric': 0.12575546257554626, 'fraction_numerical': 0.0009298000929800093, 'mean_word_length': 3.772601220188575, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 65, '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': '2644078', 'n_tokens_mistral': 3124, 'n_tokens_neox': 2807, 'n_words': 813}