-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathBase1DBuffer.java
More file actions
117 lines (105 loc) · 4.2 KB
/
Base1DBuffer.java
File metadata and controls
117 lines (105 loc) · 4.2 KB
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
package org.python.core.buffer;
import org.python.core.PyBUF;
/**
* Base implementation of the Buffer API appropriate to 1-dimensional arrays, of any item size,
* independent of the storage implementation. The description of {@link BaseBuffer} mostly applies.
*/
public abstract class Base1DBuffer extends BaseBuffer {
/** The strides array for a contiguous 1D byte buffer. */
protected static final int[] ONE = {1};
/** The shape array for a zero length 1D buffer. */
protected static final int[] ZERO = {0};
/**
* Construct an instance of <code>Base1DBuffer</code> in support of a sub-class, specifying the
* 'feature flags', or at least a starting set to be adjusted later. Also specify the navigation
* ( {@link #index0}, number of elements, and {@link #strides} array. These 'feature flags' are
* the features of the buffer exported, not the flags that form the consumer's request. The
* buffer will be read-only unless {@link PyBUF#WRITABLE} is set. {@link PyBUF#FORMAT} is
* implicitly added to the feature flags.
* <p>
* To complete initialisation, the sub-class normally must create its own wrapped byte-storage,
* and call {@link #checkRequestFlags(int)} passing the consumer's request flags.
*
* @param featureFlags bit pattern that specifies the features allowed
* @param index0 index into storage of <code>item[0]</code>
* @param size number of elements in the view
* @param strides an array of length 1 providing index stride between successive elements
*/
protected Base1DBuffer(int featureFlags, int index0, int size, int[] strides) {
super(featureFlags, index0, size == 0 ? ZERO : new int[] {size}, strides);
}
/**
* Construct an instance of <code>Base1DBuffer</code> in support of a sub-class, specifying the
* 'feature flags', or at least a starting set to be adjusted later. Also specify the navigation
* ( {@link #index0}, number of elements, and byte-index distance from one to the next. These
* 'feature flags' are the features of the buffer exported, not the flags that form the
* consumer's request. The buffer will be read-only unless {@link PyBUF#WRITABLE} is set.
* {@link PyBUF#FORMAT} is implicitly added to the feature flags.
* <p>
* To complete initialisation, the sub-class normally must create its own wrapped byte-storage,
* and call {@link #checkRequestFlags(int)} passing the consumer's request flags.
*
* @param featureFlags bit pattern that specifies the features allowed
* @param index0 index into storage of <code>item[0]</code>
* @param size number of elements in the view
* @param stride byte-index distance from one element to the next
*/
protected Base1DBuffer(int featureFlags, int index0, int size, int stride) {
this(featureFlags, index0, size, stride == 1 ? ONE : new int[] {stride});
}
@Override
protected int getSize() {
return shape[0];
}
@Override
public int getLen() {
return shape[0] * getItemsize();
}
/**
* {@inheritDoc}
* <p>
* Specialised to one-dimensional, possibly strided buffer.
*/
@Override
protected int calcGreatestIndex() {
int stride = strides[0];
if (stride == 1) {
return index0 + shape[0] - 1;
} else if (stride > 0) {
return index0 + (shape[0] - 1) * stride;
} else {
return index0;
}
}
/**
* {@inheritDoc}
* <p>
* Specialised to one-dimensional, possibly strided buffer.
*/
@Override
protected int calcLeastIndex() {
int stride = strides[0];
if (stride < 0) {
return index0 + (shape[0] - 1) * stride;
} else {
return index0;
}
}
/**
* {@inheritDoc}
* <p>
* Specialised in <code>BaseArrayBuffer</code> to one dimension.
*/
@Override
public boolean isContiguous(char order) {
if ("CFA".indexOf(order) < 0) {
return false;
} else {
if (getShape()[0] < 2) {
return true;
} else {
return getStrides()[0] == getItemsize();
}
}
}
}