<commit_before>package com.thoughtworks.xstream.core.util;

import java.util.Iterator;

/**
 * List that allows items to be added with a priority that will affect the order in which they are later iterated over.
 *
 * Objects with a high priority will appear before objects with a low priority in the list. If two objects of the same
 * priority are added to the list, the most recently added one will be iterated over first.
 *
 * @author <NAME>
 */
public class PrioritizedList {

    /**
     * Start of forward only linked list. Each item contains a value, priority and pointer to next item.
     */
    private LinkedItem first;

    /**
     * Add an item with a default priority of zero.
     */
    public void add(Object item) {
        add(item, 0);
    }

    public void add(Object item, int priority) {
        if (first == null || priority >= first.priority) {
            first = new LinkedItem(item, priority, first);
        } else {
            // Note: this is quite efficient if the client tends to add low priority items before high priority items
            // as it will not have to iterate over much of the list. However for the other way round, maybe some
            // optimizations can be made? -joe
            for (LinkedItem current = first; current != null; current = current.next) {
                if (current.next == null || priority >= current.next.priority) {
                    current.next = new LinkedItem(item, priority, current.next);
                    return;
                }
            }
        }
    }

    public Iterator iterator() {
        return new LinkedItemIterator(first);
    }

    private static class LinkedItem {

        final Object value;
        final int priority;

        LinkedItem next;

        public LinkedItem(Object value, int priority, LinkedItem next) {
            this.value = value;
            this.priority = priority;
            this.next = next;
        }

    }

    private static class LinkedItemIterator implements Iterator {

        private LinkedItem current;

        public LinkedItemIterator(LinkedItem current) {
            this.current = current;<commit_msg>Cleaned up algorithm<commit_after>package com.thoughtworks.xstream.core.util;

import java.util.Iterator;

/**
 * List that allows items to be added with a priority that will affect the order in which they are later iterated over.
 *
 * Objects with a high priority will appear before objects with a low priority in the list. If two objects of the same
 * priority are added to the list, the most recently added one will be iterated over first.
 *
 * @author <NAME>
 */
public class PrioritizedList {

    /**
     * Start of forward only linked list. Each item contains a value, priority and pointer to next item.
     * The first item does not contain a value, rather just a pointer to the next real item. This makes
     * the add() algorithm easier as there is no special case for adding to the beginning of the list.
     */
    private final LinkedItem pointerToFirst = new LinkedItem(null, 0, null);

    /**
     * Add an item with a default priority of zero.
     */
    public void add(Object item) {
        add(item, 0);
    }

    public void add(Object item, int priority) {
        // Note: this is quite efficient if the client tends to add low priority items before high priority items
        // as it will not have to iterate over much of the list. However for the other way round, maybe some
        // optimizations can be made? -joe
        LinkedItem current = pointerToFirst;
        while(current.next != null && priority < current.next.priority) {
            current = current.next;
        }
        current.next = new LinkedItem(item, priority, current.next);
    }

    public Iterator iterator() {
        return new LinkedItemIterator(pointerToFirst.next);
    }

    private static class LinkedItem {

        final Object value;
        final int priority;

        LinkedItem next;

        public LinkedItem(Object value, int priority, LinkedItem next) {
            this.value = value;
            this.priority = priority;
            this.next = next;
        }

    }

    private static class LinkedItemIterator implements Iterator {

        private LinkedItem current;

        public LinkedItemIterator(LinkedItem current) {
            this.current = current;
{'fraction_non_alphanumeric': 0.06917086578103528, 'fraction_numerical': 0.0006871278057718735, 'mean_word_length': 2.3058289174867523, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, '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': '5529305', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1049, 'n_words': 483}