-
Notifications
You must be signed in to change notification settings - Fork 461
/
IntegerArray.java
137 lines (116 loc) · 3.37 KB
/
IntegerArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sysds.runtime.frame.data.columns;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import org.apache.sysds.common.Types.ValueType;
public class IntegerArray extends Array<Integer> {
private int[] _data = null;
public IntegerArray(int[] data) {
_data = data;
_size = _data.length;
}
public int[] get() {
return _data;
}
@Override
public Integer get(int index) {
return _data[index];
}
@Override
public void set(int index, Integer value) {
_data[index] = (value != null) ? value : 0;
}
@Override
public void set(int rl, int ru, Array<Integer> value) {
set(rl, ru, value, 0);
}
@Override
public void set(int rl, int ru, Array<Integer> value, int rlSrc) {
System.arraycopy(((IntegerArray) value)._data, rlSrc, _data, rl, ru - rl + 1);
}
@Override
public void setNz(int rl, int ru, Array<Integer> value) {
int[] data2 = ((IntegerArray) value)._data;
for(int i = rl; i < ru + 1; i++)
if(data2[i] != 0)
_data[i] = data2[i];
}
@Override
public void append(String value) {
append((value != null) ? Integer.parseInt(value.trim()) : null);
}
@Override
public void append(Integer value) {
if(_data.length <= _size)
_data = Arrays.copyOf(_data, newSize());
_data[_size++] = (value != null) ? value : 0;
}
@Override
public void write(DataOutput out) throws IOException {
for(int i = 0; i < _size; i++)
out.writeLong(_data[i]);
}
@Override
public void readFields(DataInput in) throws IOException {
_size = _data.length;
for(int i = 0; i < _size; i++)
_data[i] = in.readInt();
}
@Override
public Array<Integer> clone() {
return new IntegerArray(Arrays.copyOf(_data, _size));
}
@Override
public Array<Integer> slice(int rl, int ru) {
return new IntegerArray(Arrays.copyOfRange(_data, rl, ru + 1));
}
@Override
public void reset(int size) {
if(_data.length < size)
_data = new int[size];
_size = size;
}
@Override
public byte[] getAsByteArray(int nRow) {
ByteBuffer intBuffer = ByteBuffer.allocate(4 * nRow);
intBuffer.order(ByteOrder.LITTLE_ENDIAN);
for(int i = 0; i < nRow; i++)
intBuffer.putInt(_data[i]);
return intBuffer.array();
}
@Override
public ValueType getValueType(){
return ValueType.INT32;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder(_data.length * 5 + 2);
sb.append(super.toString() + ":[");
for(int i = 0; i < _size-1; i++)
sb.append(_data[i] + ",");
sb.append(_data[_size-1]);
sb.append("]");
return sb.toString();
}
}