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

is2.data.FVR Maven / Gradle / Ivy

The newest version!
package is2.data;



import gnu.trove.TIntDoubleHashMap;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public final class FVR extends IFV { 
	
	private FVR subfv1;
	private FVR subfv2;
	private boolean negateSecondSubFV = false;
	
    private int size;
   
    
    
	// content of the nodes NxC
	private int m_index[];
	private float m_value[];
	
	// type of the nodes NxT
	
	public FVR() {
		this(10);
	}

	public FVR(int initialCapacity) {
		m_index = new int[initialCapacity];
		m_value = new float[initialCapacity];
	}

/*
	public FVR (FVR fv1, FVR fv2) {
		subfv1 = fv1;
		subfv2 = fv2;
	}
*/
	public FVR (FVR fv1, FVR fv2, boolean negSecond) {
		this(0);
		subfv1 = fv1;
		subfv2 = fv2;
		negateSecondSubFV = negSecond;
	}

	/**
	 * Read a feature vector
	 * @param index
	 * @param value
	 */
	public FVR(DataInputStream dos, int capacity) throws IOException {
		this(capacity);
		size= m_index.length;
		
		for (int i=0; iGraph instance, if
     * necessary, to ensure  that it can hold at least the number of nodes
     * specified by the minimum capacity argument. 
     *
     * @param   minCapacity   the desired minimum capacity.
     */
    private void ensureCapacity(int minCapacity) {
	
    	
    	if (minCapacity > m_index.length) {
    		
    		int oldIndex[] = m_index;
     		float oldValue[] = m_value;
     	    	
    		int newCapacity = ( m_index.length * 3)/2 + 1;
    
    		
    		if (newCapacity < minCapacity) newCapacity = minCapacity;

    		m_index = new int[newCapacity];
     		m_value = new float[newCapacity];
     	    
     		System.arraycopy(oldIndex, 0, m_index, 0, oldIndex.length);
     		System.arraycopy(oldValue, 0, m_value, 0, oldValue.length);
    		
    	}
    }
	
   
    final public int size() {
	  return size;
    }

    final public boolean isEmpty() {
	  return size == 0;
    }

    @Override
	final public void clear() {
    	size = 0;
    }
    
    
    final public int createFeature(int i, float v) {
    	
    	ensureCapacity(size+1); 
    	m_index[size] =i;
    	m_value[size] =v;
    	size++;
    	return size-1;
    }
   /* 
    final public int createFeature(int i) {
    	
    	ensureCapacity(size+1); 
    	m_index[size] =i;
    	size++;
    	return size-1;
    }
   */
    
	final public int getIndex(int i) {
		return m_index[i];
	}

	public void setIndex(int p, int i) {
		m_index[p] = i;
	}
    
	
	 /**
     * Trims the capacity of this Graph instance to true size.
     *  An application can use this operation to minimize
     * the storage of an Graph instance.
     */
    public void trimToSize() {

		if (size < m_index.length) {
		
		
			int oldIndex[] = m_index;
			
			m_index = new int[size];
			System.arraycopy(oldIndex, 0, m_index, 0, size);
			
		}
		
    }

    
	
	
    
 	final public void add(int i) {
        if (i>=0) {
			ensureCapacity(size+1); 
			m_index[size] =i;
			m_value[size] =1.0f;
			size++;
		}
    }

    final public void add(int i, float f) {
        if (i>=0) createFeature(i,f);
    }
   
	
	// fv1 - fv2
	public FVR getDistVector(FVR fl2) {
		return new FVR(this, fl2, true);
	}

	
	public double getScore(double[] parameters, boolean negate) {
		double score = 0.0;

		if (null != subfv1) {
			score += subfv1.getScore(parameters, negate);

			if (null != subfv2) {
				if (negate) score += subfv2.getScore(parameters, !negateSecondSubFV);
				else score += subfv2.getScore(parameters, negateSecondSubFV);
				
			}
		}

		if (negate) for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy