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

com.asher_stern.crf.utilities.TopK_DateStructure Maven / Gradle / Ivy

Go to download

Implementation of linear-chain Conditional Random Fields (CRF) in pure Java

The newest version!
package com.asher_stern.crf.utilities;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;


/**
 * A data structure that stores the top-k items of the inserted items.
 * It is assumed that the items are comparable, so "top-k" is well defined.
 * 
* Time complexity of each insertion is amortized O(log(k)). * Space complexity is O(k^2) * * @author Asher Stern * Date: Nov 19, 2014 * * @param */ public class TopK_DateStructure { public TopK_DateStructure(int k) { this(k, null); } @SuppressWarnings("unchecked") public TopK_DateStructure(int k, Comparator comparator) { super(); this.k = k; this.comparator = comparator; this.array_length = k*k + k; this.storage = (T[]) Array.newInstance(Object.class, array_length); if (k<=0) {throw new CrfException("k <= 0");} index = 0; } /** * Insert an item to this data-structure. This item might be discarded later when it is absolutely sure that it is not * one of the top-k items that were inserted to this data-structure. * @param item */ public void insert(T item) { storage[index] = item; ++index; if (index>array_length) {throw new CrfException("BUG");} if (index==array_length) { sortAndShrink(); } } /** * Get the top-k items among all the items that were inserted to this data-structure so far. * Note that this method has time complexity of O(k*log(k)). * @return */ public ArrayList getTopK() { if (index>k) { sortAndShrink(); } ArrayList topKAsList = new ArrayList(); for (int i=0;i comparator = null; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy