<commit_before>package org.helioviewer.jhv.plugins.pfss.data;

import org.helioviewer.jhv.plugins.pfss.PfssSettings;

// Datastructure to cache the Pfss-Data with preload function
public class PfssCache {

    private final PfssData[] data = new PfssData[PfssSettings.CACHE_SIZE];
    private int numberOfElementsInCache = 0;

    public void addData(PfssData pfssData) {
        if (numberOfElementsInCache < PfssSettings.CACHE_SIZE) {
            data[numberOfElementsInCache] = pfssData;
            numberOfElementsInCache++;
            bubbleSort();
        }
    }

    private void bubbleSort() {
        boolean swapped = true;
        int j = 0;
        while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < numberOfElementsInCache - j; i++) {
                if (data[i].dateObs.milli > data[i + 1].dateObs.milli) {
                    PfssData tmp = data[i];
                    data[i] = data[i + 1];
                    data[i + 1] = tmp;
                    swapped = true;
                }
            }
        }
    }

    public PfssData getData(long timestamp) {
        if (numberOfElementsInCache <= 0)
            return null;

        int found = binarySearch(timestamp);
        if (found >= 0)
            return data[found];

        if (-found >= numberOfElementsInCache)
            return data[numberOfElementsInCache - 1];

        if (-found - 1 >= 0) {
            long diff1 = Math.abs(data[-found - 1].dateObs.milli - timestamp);
            long diff2 = Math.abs(data[-found].dateObs.milli - timestamp);
            if (diff1 < diff2) {
                return data[-found - 1];
            }
        }
        return data[-found];
    }

    private int binarySearch(long timestamp) {
        int low = 0;
        int high = numberOfElementsInCache - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            long midVal = data[mid].dateObs.milli;

            if (midVal < timestamp)
                low = mid + 1;
            else if (midVal > timestamp)
                high = mid - 1;
            else
                return mid;
        }
        return -(low + 1);
    }

    public void clear() {
        numberOfElementsInCache = 0;
        for (int i = 0; i < data.length; i++) {
            data[i] = null;
        }
    }

}<commit_msg>Replace PfssCache with TreeMap<commit_after>package org.helioviewer.jhv.plugins.pfss.data;

import java.util.TreeMap;

import org.helioviewer.jhv.plugins.pfss.PfssSettings;

public class PfssCache {

    private final TreeMap<Long, PfssData> map = new TreeMap<>();

    public void addData(PfssData data) {
        assert map.size() < PfssSettings.CACHE_SIZE;
        map.put(data.dateObs.milli, data);
    }

    public PfssData getData(long timestamp) {
        Long c = map.ceilingKey(timestamp);
        Long f = map.floorKey(timestamp);

        if (c != null && f != null) {
            if (Math.abs(f - timestamp) < Math.abs(timestamp - c))
                return map.get(f);
            else
                return map.get(c);
        }

        try {
            if (f == null)
                return map.get(c);
            return map.get(f);
        } catch (Exception ignore) {
        }

        return null;
    }

    public void clear() {
        map.clear();
    }

}
{'fraction_non_alphanumeric': 0.10988023952095809, 'fraction_numerical': 0.0074850299401197605, 'mean_word_length': 1.595959595959596, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, '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': '3398883', 'n_tokens_mistral': 1051, 'n_tokens_neox': 982, 'n_words': 242}