Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / vm / IntArrayFields.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.vm;
20
21 import gov.nasa.jpf.util.HashData;
22 import gov.nasa.jpf.util.IntVector;
23
24 import java.io.PrintStream;
25
26 /**
27  * element values for int[] objects
28  */
29 public class IntArrayFields extends ArrayFields {
30
31   int[] values;
32
33   public IntArrayFields (int length) {
34     values = new int[length];
35   }
36
37   @Override
38   public int[] asIntArray() {
39     return values;
40   }
41
42   @Override
43   public void copyElements (ArrayFields src, int srcPos, int dstPos, int len){
44     IntArrayFields a = (IntArrayFields) src;
45     System.arraycopy(a.values, srcPos, values, dstPos, len);
46   }
47   
48   @Override
49   protected void printValue(PrintStream ps, int idx){
50     ps.print(values[idx]);
51   }
52   
53   @Override
54   public Object getValues(){
55     return values;
56   }
57
58   @Override
59   public int arrayLength() {
60     return values.length;
61   }
62
63   @Override
64   public int getHeapSize() {  // in bytes
65     return values.length * 4;
66   }
67
68   @Override
69   public void appendTo (IntVector v) {
70     v.append(values);
71   }
72
73   @Override
74   public IntArrayFields clone(){
75     IntArrayFields f = (IntArrayFields)cloneFields();
76     f.values = values.clone();
77     return f;
78   }
79
80   @Override
81   public boolean equals (Object o) {
82     if (o instanceof IntArrayFields) {
83       IntArrayFields other = (IntArrayFields)o;
84
85       int[] v = values;
86       int[] vOther = other.values;
87       if (v.length != vOther.length) {
88         return false;
89       }
90
91       for (int i=0; i<v.length; i++) {
92         if (v[i] != vOther[i]) {
93           return false;
94         }
95       }
96
97       return compareAttrs(other);
98
99     } else {
100       return false;
101     }
102   }
103
104   @Override
105   public void setIntValue (int pos, int newValue) {
106     values[pos] = newValue;
107   }
108
109   @Override
110   public int getIntValue (int pos) {
111     return values[pos];
112   }
113
114
115   @Override
116   public void hash(HashData hd) {
117     int[] v = values;
118     for (int i=0; i < v.length; i++) {
119       hd.add(v[i]);
120     }
121   }
122
123 }