View Javadoc
1   /*
2    * Copyright 2007, 2008, 2009 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 eu.simuline.octave.util.StringUtil;
21  
22  import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
23  
24  /**
25   * Represents a Boolean matrix. 
26   * Not so appropriate for sparse matrices. 
27   *
28   * @see OctaveSparseBoolean
29   */
30  public final class OctaveBoolean 
31      extends AbstractGenericMatrix<boolean[], BooleanArrayList> {
32  
33      /**
34       * @param size
35       */
36      // Used in BooleanReader, BooleanSingleReader
37      public OctaveBoolean(final int... size) {
38          super(size);
39      }
40  
41      /**
42       * @param data
43       * @param size
44       */
45      // used in shallowCopy(), used by end user 
46      public OctaveBoolean(final boolean[] data, final int... size) {
47          super(data, size);
48      }
49  
50      private OctaveBooleanf="../../../../eu/simuline/octave/type/OctaveBoolean.html#OctaveBoolean">OctaveBoolean(OctaveBoolean o) {
51          super(o);
52      }
53  
54      protected final BooleanArrayList newL(final int size) {
55  	BooleanArrayList list = new BooleanArrayList(size);
56  	list.size(size);
57  	return list;
58      }
59  
60      protected final int initL(boolean[] data, final int size) {
61      	this.dataL = new BooleanArrayList(data);
62      	this.dataL.size(size);
63      	return data.length;
64      }
65  
66      protected boolean[] getDataA() {
67  	return this.dataL.elements();
68      }
69  
70      /**
71       * Set the value resizing by need. 
72       * 
73       * @param value
74       * @param pos
75       * @see #setPlain(boolean, int)
76       */
77      public final void set(final boolean value, final int... pos) {
78          resizeUp(pos);
79          setPlain(value, pos2ind(pos));
80      }
81  
82      /**
83       * Set the value assuming resize is not necessary. 
84       * 
85       * @param value
86       * @param pos
87       * @see #set(boolean, int[])
88       */
89      public final void setPlain(final boolean value, final int pos) {
90  	this.dataL.set(pos, value);
91      }
92  
93      // api-docs inherited from AbstractGenericMatrix 
94      public final void setPlain(final String value, final int pos) {
95  	this.dataL.set(pos, StringUtil.parseBoolean(value));
96      }
97  
98      /**
99       * Get the value. 
100      * 
101      * @param pos
102      * @return value at pos
103      */
104     public final boolean get(final int... pos) {
105  	return this.dataL.getBoolean(pos2ind(pos));
106     }
107 
108     public final String getPlainString(int pos) {
109 	return StringUtil.toString(this.dataL.getBoolean(pos));
110     }
111 
112     // api-docs inherited from OctaveObject
113     @Override
114     public OctaveBoolean shallowCopy() {
115         return new OctaveBoolean(this);
116     }
117 
118 }