001/* Class defining methods to store and retrieve values in a flexible way.
002
003 Copyright (c) 1999-2013 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.domains.pn.demo.QR;
029
030import java.util.HashMap;
031import java.util.Map;
032
033/**
034
035 This class defines an associative array and methods to store and
036 retrieve data in the array.
037
038 @author Bart Kienhuis
039 @version $Id$
040 @since Ptolemy II 0.4
041 @Pt.ProposedRating Red (kienhuis)
042 @Pt.AcceptedRating Red (kienhuis)
043 */
044public class ArrayIndex {
045    ///////////////////////////////////////////////////////////////////
046    ////                         public methods                    ////
047
048    /** Construct an ArrayIndex object. */
049    public ArrayIndex() {
050        super();
051    }
052
053    /** Create a key for an 1 dimensional index reference. This key is
054     used to store and retrieve values in the associate array.
055     @param i_1 an index value.
056     @return a key representing the index vector.
057     */
058    public String atKey(int i_1) {
059        String key = "(" + i_1 + ")";
060        return key;
061    }
062
063    /** Create a key for an 2 dimensional index reference. This key is
064     used to store and retrieve values in the associate array.
065     @param i_1 an index value.
066     @param i_2 an index value.
067     @return a key representing the index vector.
068     */
069    public String atKey(int i_1, int i_2) {
070        String key = "(" + i_1 + "," + i_2 + ")";
071        return key;
072    }
073
074    /** Create a key for an 3 dimensional index reference. This key is
075     used to store and retrieve values in the associate array.
076     @param i_1 an index value.
077     @param i_2 an index value.
078     @param i_3 an index value.
079     @return a key representing the index vector.
080     */
081    public String atKey(int i_1, int i_2, int i_3) {
082        String key = "(" + i_1 + "," + i_2 + "," + i_3 + ")";
083        return key;
084    }
085
086    /** Create a key for an 4 dimensional index reference. This key is
087     used to store and retrieve values in the associate array.
088     @param i_1 an index value.
089     @param i_2 an index value.
090     @param i_3 an index value.
091     @param i_4 an index value.
092     @return a key representing the index vector.
093     */
094    public String atKey(int i_1, int i_2, int i_3, int i_4) {
095        String key = "(" + i_1 + "," + i_2 + "," + i_3 + "," + i_4 + ")";
096        return key;
097    }
098
099    /** Create a key for an 5 dimensional index reference. This key is
100     used to store and retrieve values in the associate array.
101     @param i_1 an index value.
102     @param i_2 an index value.
103     @param i_3 an index value.
104     @param i_4 an index value.
105     @param i_5 an index value.
106     @return a key representing the index vector.
107     */
108    public String atKey(int i_1, int i_2, int i_3, int i_4, int i_5) {
109        String key = "(" + i_1 + "," + i_2 + "," + i_3 + "," + i_4 + "," + i_5
110                + ")";
111        return key;
112    }
113
114    /** Create a key for an 6 dimensional index reference. This key is
115     used to store and retrieve values in the associate array.
116     @param i_1 an index value.
117     @param i_2 an index value.
118     @param i_3 an index value.
119     @param i_4 an index value.
120     @param i_5 an index value.
121     @param i_6 an index value.
122     @return a key representing the index vector.
123     */
124    public String atKey(int i_1, int i_2, int i_3, int i_4, int i_5, int i_6) {
125        String key = "(" + i_1 + "," + i_2 + "," + i_3 + "," + i_4 + "," + i_5
126                + "," + i_6 + ")";
127        return key;
128    }
129
130    /** Retrieve a value from the associate array using the supplied key.
131     @param aKey the key.
132     @return the stored value.
133     */
134    public double retrieve(String aKey) {
135        Double value = (Double) _map.get(aKey);
136
137        if (value == null) {
138            throw new Error(" --- NULL Value retrieved for key " + aKey);
139        }
140
141        return value.doubleValue();
142    }
143
144    /** Store a data value at a particular location given by the key string.
145     @param aValue the value.
146     @param aKey the key.
147     */
148    public void store(double aValue, String aKey) {
149        _map.put(aKey, Double.valueOf(aValue));
150    }
151
152    /** Read in a matrix with a given name and store it into a
153     *  associative data structure. The associate data structure make
154     *  the access of the data simple. Instead of reading from a file
155     *  system, the method reads from the source matrix supplied by
156     *  SourceMatrix class. This make the access of the matrix
157     *  possible inside a Applet.
158     *
159     *  @param filename The filename.
160     */
161    public void readMatrix(String filename) {
162        if (filename.equals("U_1000x16")) {
163            for (int i = 0; i < 500; i++) {
164                for (int j = 0; j < 16; j++) {
165                    String key = atKey(i + 1, j + 1);
166                    _map.put(key,
167                            Double.valueOf(SourceMatrix.sourcematrix_0[i][j]));
168                }
169            }
170        } else {
171            // CREATE matrix Zeros64x64
172            for (int i = 0; i < 64; i++) {
173                for (int j = 0; j < 64; j++) {
174                    String key = atKey(i, j);
175                    _map.put(key, Double.valueOf(0.0));
176                }
177            }
178        }
179    }
180
181    /** Write the matrix stored in the associate array with a given
182     name. Currently not further implemented.
183     @param filename the filename.
184     */
185    public void writeMatrix(String filename) {
186    }
187
188    ///////////////////////////////////////////////////////////////////
189    ////                         private variables                 ////
190
191    /** Associative Array that is used to store and retrieve data. */
192    private Map _map = new HashMap();
193}