View Javadoc
1   /*
2    * Copyright 2010 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.type;
17  
18  import eu.simuline.octave.type.matrix.AbstractGenericMatrix;
19  
20  import it.unimi.dsi.fastutil.ints.IntArrayList;
21  
22  /**
23   * Represents a matrix of ints. 
24   */
25  public final class OctaveInt 
26      extends AbstractGenericMatrix<int[], IntArrayList> {
27  
28      /**
29       * Create new matrix. 
30       * 
31       * @param size
32       */
33      // used by reader 
34      public OctaveInt(final int... size) {
35          super(size);
36      }
37  
38      /**
39       * Constructor that reuses the input data. 
40       * 
41       * @param data
42       * @param size
43       */
44      // used by shallowCopy, end user, tests 
45      public OctaveInt(final int[] data, final int... size) {
46          super(data, size);
47      }
48  
49      /**
50       * Copy constructor. 
51       * 
52       * @param o
53       */
54      protected OctaveIntOctaveInt.html#OctaveInt">OctaveInt(final OctaveInt o) {
55          super(o);
56      }
57  
58      protected final IntArrayList newL(final int size) {
59  	IntArrayList list = new IntArrayList(size);
60  	list.size(size);
61  	return list;
62      }
63  
64      protected final int initL(int[] data, final int size) {
65  	this.dataL = new IntArrayList(data);
66  	this.dataL.size(size);
67  	return data.length;
68      }
69  
70      protected int[] getDataA() {
71  	return this.dataL.elements();
72      }
73  
74      /**
75       * Set the value resizing by need. 
76       * 
77       * @param value
78       * @param pos
79       * @see #setPlain(int, int)
80       */
81      public final void set(final int value, final int... pos) {
82          resizeUp(pos);
83          setPlain(value, pos2ind(pos));
84      }
85  
86      /**
87       * Set the value assuming resize is not necessary. 
88       * 
89       * @param value
90       * @param pos
91       * @see #set(int, int[])
92       */
93      public final void setPlain(final int value, final int pos) {
94  	this.dataL.set(pos, value);
95      }
96  
97      // api-docs inherited from AbstractGenericMatrix 
98      public final void setPlain(final String value, final int pos) {
99  	this.dataL.set(pos, Integer.parseInt(value.trim()));
100     }
101 
102     /**
103      * Get the value. 
104      * 
105      * @param pos
106      * @return value at pos
107      */
108     public final int get(final int... pos) {
109 	return this.dataL.getInt(pos2ind(pos));
110     }
111 
112     public final String getPlainString(int pos) {
113 	return Integer.toString(this.dataL.getInt(pos));
114     }
115 
116     // api-docs inherited from OctaveObject
117     @Override
118     public OctaveInt shallowCopy() {
119         return new OctaveInt(this);
120     }
121 
122 }