All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.tsp.ers.SortedHashList Maven / Gradle / Ivy

There is a newer version: 1.12.0
Show newest version
package org.bouncycastle.tsp.ers;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * A sorting list - byte[] are sorted in ascending order.
 */
public class SortedHashList
{
    private static final Comparator hashComp = new ByteArrayComparator();

    private final LinkedList baseList = new LinkedList();

    public SortedHashList()
    {
    }

    public void add(byte[] hash)
    {
        if (baseList.size() == 0)
        {
             baseList.addFirst(hash);
        }
        else
        {
            if (hashComp.compare(hash, baseList.get(0)) < 0)
            {
                baseList.addFirst(hash);
            }
            else
            {
                int index = 1;
                while(index < baseList.size() && hashComp.compare(baseList.get(index), hash) <= 0)
                {
                    index++;
                }

                if (index == baseList.size())
                {
                    baseList.add(hash);
                }
                else
                {
                    baseList.add(index, hash);
                }
            }
        }
    }

    public List toList()
    {
        return new ArrayList(baseList);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy