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

gov.sandia.cognition.collection.AbstractLogNumberMap Maven / Gradle / Ivy


/*
 * File:                AbstractLogNumberMap.java
 * Authors:             Kevin R. Dixon
 * Company:             Sandia National Laboratories
 * Project:             Incremental Learning Core
 *
 * Copyright July 11, 2012, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government. Export
 * of this program may require a license from the United States Government.
 *
 */
package gov.sandia.cognition.collection;

import gov.sandia.cognition.collection.ScalarMap.Entry;
import gov.sandia.cognition.math.LogNumber;
import gov.sandia.cognition.math.matrix.InfiniteVector;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;

/**
 * A partial implementation of a ScalarMap with a LogNumber value
 * @param 
 * Type of key in the Map
 * @author Kevin R. Dixon
 * @since  3.4.2
 */
public class AbstractLogNumberMap
    extends AbstractScalarMap
{

    /** 
     * Creates a new instance of AbstractMutableDoubleMap 
     * @param   map
     *      The backing map that the data is stored in.
     */
    public AbstractLogNumberMap(
        final Map map )
    {
        super( map );
    }
 
    @Override
    public AbstractLogNumberMap clone()
    {
        return (AbstractLogNumberMap) super.clone();
    }

    @Override
    public double get(
        KeyType key)
    {
        final LogNumber value = this.map.get(key);
        if( value == null )
        {
            return 0.0;
        }
        else
        {
            return value.doubleValue();
        }
    }

    /**
     * Gets the LogNumber at the given key
     * @param key
     * Key to get from the Map
     * @return
     * Gets the LogNumber from the Map, null if no entry at the given key.
     */
    public LogNumber getLog(
        KeyType key )
    {
        return this.map.get(key);
    }
    

    @Override
    public void set(
        KeyType key,
        double value)
    {
        // This is sort of inefficient here, but if you're using
        // the logarithm-space version, setting rectangular values
        // should be more expensive, I think.
        // But when this gets overridden in LogWeightedDataDistribution,
        // you'll thank me...
        LogNumber newEntry = new LogNumber();
        newEntry.setValue(value);
        this.setLog(key, newEntry);
    }    
    
    /**
     * Sets the LogNumber at the given key
     * @param key
     * Key to set into the Map
     * @param logValue
     * Sets the LogNumber into the Map
     */
    public void setLog(
        KeyType key,
        LogNumber logValue )
    {
        final LogNumber entry = this.map.get(key);
        if( entry == null )
        {
            this.map.put( key, logValue );
        }
        else
        {
            entry.setLogValue( logValue.getLogValue() );
        }
    }
    
    @Override
    public SimpleEntrySet entrySet()
    {
        return new SimpleEntrySet( this.map );
    }

    /**
     * Simple Entry Set for DefaultInfiniteVector
     * @param 
     *      The type of the key in the map.
     */
    protected static class SimpleEntrySet
        extends AbstractSet>
    {

        /**
         * Backing map
         */
        protected Map map;

        /**
         * Creates a new instance of SimpleEntrySet
         * @param map
         * Backing map
         */
        public SimpleEntrySet(
            Map map)
        {
            this.map = map;
        }

        @Override
        public Iterator> iterator()
        {
            return new SimpleIterator( map.entrySet().iterator() );
        }

        @Override
        public int size()
        {
            return map.size();
        }

    }

    /**
     * Simple iterator for DefaultInfiniteVector
     * @param 
     *      The type of the key in the map.
     */
    protected static class SimpleIterator
        implements Iterator>
    {

        /**
         * Iterator that does all the work
         */
        private Iterator> delegate;

        /**
         * Default constructor
         * @param delegate
         * Iterator that does all the work
         */
        public SimpleIterator(
            Iterator> delegate )
        {
            this.delegate = delegate;
        }

        @Override
        public boolean hasNext()
        {
            return this.delegate.hasNext();
        }

        @Override
        public SimpleEntry next()
        {
            Map.Entry entry = this.delegate.next();
            return new SimpleEntry(
                entry.getKey(), entry.getValue() );
        }

        @Override
        public void remove()
        {
            this.delegate.remove();
        }

    }


    /**
     * Entry for the AbstractScalarMap
     * @param 
     *      The type of the key in the map.
     */
    protected static class SimpleEntry
        implements ScalarMap.Entry,
        InfiniteVector.Entry
    {

        /**
         * Key associated with this entry
         */
        protected KeyType key;

        /**
         * Value associated with the entry
         */
        protected LogNumber value;

        /**
         * Creates a new instance of SimpleEntry
         * @param key
         * Key represented by the Entry
         * @param value
         * Value associated with the Entry
         */
        public SimpleEntry(
            final KeyType key,
            final LogNumber value )
        {
            this.key = key;
            this.value = value;
        }

        @Override
        public KeyType getKey()
        {
            return this.key;
        }

        @Override
        public double getValue()
        {
            return value.getValue();
        }

        @Override
        public void setValue(
            double value)
        {
            this.value.setValue(value);
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy