Top Description Inners Fields Constructors Methods
org.python.core.buffer

public Class Strided1DBuffer

extends BaseArrayBuffer
Class Inheritance
Known Direct Subclasses
org.python.core.buffer.Strided1DBuffer.SlicedView, org.python.core.buffer.Strided1DWritableBuffer
Imports
org.python.core.BufferProtocol, .PyBuffer, .PyException

Read-only buffer API over a one-dimensional array of one-byte items, that are evenly-spaced in a storage array. The buffer has storage, index0 and count properties in the usual way, designating a slice (or all) of a byte array, but also a stride property (equal to getStrides()[0]).

Let the underlying buffer be the byte array u(i) for i=0..N-1, let x be the Strided1DBuffer, and let the stride be p. The storage works as follows. Designate by x(j), for j=0..L-1, the byte at index j, that is, the byte retrieved by x.byteAt(j). Thus, we store x(j) at u(a+pj), that is, x(0) = u(a). When we construct such a buffer, we have to supply a = index0, L = count, and p = stride as the constructor arguments. The last item in the slice x(L-1) is stored at u(a+p(L-1)). For the simple case of positive stride, constructor argument index0 is the low index of the range occupied by the data. When the stride is negative, that is to say p<0, and L>1, this will be to the left of u(a), and the constructor argument index0 is not then the low index of the range occupied by the data. Clearly both these indexes must be in the range 0 to N-1 inclusive, a rule enforced by the constructors (unless L=0, when it is assumed no array access will take place).

The class may be used by exporters to create a strided slice (e.g. to export the diagonal of a matrix) and in particular by other buffers to create strided slices of themselves, such as to create the memoryview that is returned as an extended slice of a memoryview.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Strided1DBuffer.SlicedView

A Strided1DBuffer.SlicedView represents a non-contiguous subsequence of a simple buffer.

Field Summary

Modifier and TypeField and Description
protected int
stride

Step size in the underlying buffer essential to correct translation of an index (or indices) into an index into the storage.

Inherited from org.python.core.buffer.BaseArrayBuffer:
storage

Constructor Summary

AccessConstructor and Description
protected
Strided1DBuffer(BufferProtocol
exporting object (or null)
obj
,
byte[]
raw byte array containing exported data
storage
,
int
index into storage of item[0]
index0
,
int
number of items in the slice
count
,
int
in between successive elements of the new PyBuffer
stride
)

Provide an instance of Strided1DBuffer with navigation variables initialised, for sub-class use.

public
Strided1DBuffer(int
consumer requirements
flags
,
BufferProtocol
exporting object (or null)
obj
,
byte[]
raw byte array containing exported data
storage
,
int
index into storage of item[0]
index0
,
int
number of items in the slice
count
,
int
byte-index distance from one element to the next in the new PyBuffer
stride
)

Provide an instance of Strided1DBuffer on a particular array of bytes specifying a starting index, the number of items in the result, and a byte-indexing stride.

Method Summary

Modifier and TypeMethod and Description
public final int
byteIndex(int
item-index from consumer
index
)

Overrides org.python.core.buffer.BaseBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

Convert an item index (for a one-dimensional buffer) to an absolute byte index in the storage shared by the exporter.

public PyBuffer
getBufferSlice(int
specifying features demanded and the navigational capabilities of the consumer
flags
,
int
index in the current buffer
start
,
int
number of items in the required slice
count
,
int
index-distance in the current buffer between consecutive items in the slice
stride
)

Implements org.python.core.PyBuffer.getBufferSlice.

Get a PyBuffer that represents a slice of the current one described in terms of a start index, number of items to include in the slice, and the stride in the current buffer.

public PyBuffer.Pointer
getPointer(int
in the buffer to position the pointer
index
)

Overrides org.python.core.buffer.BaseBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

References Deprecated PyBuffer.Pointer is deprecated or references (maybe indirectly) at least one deprecated element.

Return a structure describing the position in a byte array of a single item from the data being exported to the consumer.

public PyBuffer.Pointer
getPointer(int...
multidimensional index at which to position the pointer
indices
)

Overrides org.python.core.buffer.BaseBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

References Deprecated PyBuffer.Pointer is deprecated or references (maybe indirectly) at least one deprecated element.

Return a structure describing the position in a byte array of a single item from the data being exported to the consumer, in the case that array may be multi-dimensional.

public boolean
isReadonly()

Overrides org.python.core.buffer.BaseBuffer.isReadonly.

Implements org.python.core.PyBUF.isReadonly.

Determine whether the consumer is entitled to write to the exported storage.

Inherited from org.python.core.buffer.BaseArrayBuffer:
byteAtImplbyteIndexcopyFromcopyFromcopyFromcopyTogetBufgetNIOByteBufferImplstoreAtImpl

Field Detail

strideback to summary
protected int stride

Step size in the underlying buffer essential to correct translation of an index (or indices) into an index into the storage. The value is returned by getStrides() is an array with this as the only element.

Constructor Detail

Strided1DBufferback to summary
protected Strided1DBuffer(BufferProtocol obj, byte[] storage, int index0, int count, int stride) throws ArrayIndexOutOfBoundsException, NullPointerException

Provide an instance of Strided1DBuffer with navigation variables initialised, for sub-class use. The buffer (storage, index0), and the navigation ( shape array and stride) will be initialised from the arguments (which are checked for range).

The sub-class constructor should check that the intended access is compatible with this object by calling checkRequestFlags(int). (See the source of Strided1DWritableBuffer for an example of this use.)

Parameters
obj:BufferProtocol

exporting object (or null)

storage:byte[]

raw byte array containing exported data

index0:int

index into storage of item[0]

count:int

number of items in the slice

stride:int

in between successive elements of the new PyBuffer

Exceptions
ArrayIndexOutOfBoundsException:
if index0, count and stride are inconsistent with storage.length
NullPointerException:
if storage is null
Strided1DBufferback to summary
public Strided1DBuffer(int flags, BufferProtocol obj, byte[] storage, int index0, int count, int stride) throws ArrayIndexOutOfBoundsException, NullPointerException, PyException

Provide an instance of Strided1DBuffer on a particular array of bytes specifying a starting index, the number of items in the result, and a byte-indexing stride. The result of byteAt(i) will be equal to storage[index0+stride*i] (whatever the sign of stride), valid for 0<=i<count. The constructor checks that all these indices lie within the storage array (unless count=0).

The constructed PyBuffer meets the consumer's expectations as expressed in the flags argument, or an exception will be thrown if these are incompatible with the type (e.g. the consumer does not specify that it understands the strides array). Note that the actual range in the storage array, the lowest and highest index, is not explicitly passed, but is implicit in index0, count and stride. The constructor checks that these indices lie within the storage array (unless count=0).

Parameters
flags:int

consumer requirements

obj:BufferProtocol

exporting object (or null)

storage:byte[]

raw byte array containing exported data

index0:int

index into storage of item[0]

count:int

number of items in the slice

stride:int

byte-index distance from one element to the next in the new PyBuffer

Exceptions
ArrayIndexOutOfBoundsException:
if index0, count and stride are inconsistent with storage.length
NullPointerException:
if storage is null
PyException:
BufferError when expectations do not correspond with the type

Method Detail

byteIndexback to summary
public final int byteIndex(int index) throws IndexOutOfBoundsException

Overrides org.python.core.buffer.BaseBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

Doc from org.python.core.PyBuffer.byteIndex.

Convert an item index (for a one-dimensional buffer) to an absolute byte index in the storage shared by the exporter. The storage exported as a PyBuffer is a linearly-indexed sequence of bytes, although it may not actually be a heap-allocated Java byte[] object. The purpose of this method is to allow the exporter to define the relationship between the item index (as used in byteAt(int)) and the byte-index (as used with the ByteBuffer returned by getNIOByteBuffer()). See byteIndex(int[]) for discussion of the multi-dimensional case.

Parameters
index:int

item-index from consumer

Returns:int

corresponding byte-index in actual storage

Annotations
@Override

getBufferSliceback to summary
public PyBuffer getBufferSlice(int flags, int start, int count, int stride)

Implements org.python.core.PyBuffer.getBufferSlice.

Doc from org.python.core.PyBuffer.getBufferSlice.

Get a PyBuffer that represents a slice of the current one described in terms of a start index, number of items to include in the slice, and the stride in the current buffer. A consumer that obtains a PyBuffer with getBufferSlice must release it with PyBuffer#release just as if it had been obtained with PyBuffer#getBuffer(int)

Suppose that x(i) denotes the ith element of the current buffer, that is, the byte retrieved by this.byteAt(i) or the unit indicated by this.getPointer(i). A request for a slice where start = s, count = N and stride = m, results in a buffer y such that y(k) = x(s+km) where k=0..(N-1). In Python terms, this is the slice x[s : s+(N-1)m+1 : m] (if m>0) or the slice x[s : s+(N-1)m-1 : m] (if m<0). Implementations should check that this range is entirely within the current buffer.

In a simple buffer backed by a contiguous byte array, the result is a strided PyBuffer on the same storage but where the offset is adjusted by s and the stride is as supplied. If the current buffer is already strided and/or has an item size larger than single bytes, the new start index, count and stride will be translated from the arguments given, through this buffer's stride and item size. The caller always expresses start and strides in terms of the abstract view of this buffer.

Strided1DBuffer provides an implementation for slicing already-strided bytes in one dimension. In that case, x(i) = u(r+ip) for i = 0..L-1 where u is the underlying buffer, and r, p and L are the start, stride and count with which x was created from u. Thus y(k) = u(r+sp+kmp), that is, the composite index0 is r+sp and the composite stride is mp.

Parameters
flags:int

specifying features demanded and the navigational capabilities of the consumer

start:int

index in the current buffer

count:int

number of items in the required slice

stride:int

index-distance in the current buffer between consecutive items in the slice

Returns:PyBuffer

a buffer representing the slice

Annotations
@Override

getPointerback to summary
public PyBuffer.Pointer getPointer(int index)

Overrides org.python.core.buffer.BaseBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

Doc from org.python.core.PyBuffer.getPointer.

References Deprecated

PyBuffer.Pointer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Return a structure describing the position in a byte array of a single item from the data being exported to the consumer. For a one-dimensional contiguous buffer, assuming the following client code where obj has type BufferProtocol:

int k = ... ;
PyBuffer a = obj.getBuffer(PyBUF.FULL);
int itemsize = a.getItemsize();
PyBuffer.Pointer b = a.getPointer(k);
the item with index k is in the array b.storage at index [b.offset] to [b.offset + itemsize - 1] inclusive. And if itemsize==1, the item is simply the byte b.storage[b.offset]

Essentially this is a method for computing the offset of a particular index. The client is free to navigate the underlying buffer b.storage without respecting these boundaries.

Parameters
index:int

in the buffer to position the pointer

Returns:PyBuffer.Pointer

structure defining the byte[] slice that is the shared data

Annotations
@SuppressWarnings:deprecation
@Override

getPointerback to summary
public PyBuffer.Pointer getPointer(int... indices)

Overrides org.python.core.buffer.BaseBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

Doc from org.python.core.PyBuffer.getPointer.

References Deprecated

PyBuffer.Pointer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Return a structure describing the position in a byte array of a single item from the data being exported to the consumer, in the case that array may be multi-dimensional. For a 3-dimensional contiguous buffer, assuming the following client code where obj has type BufferProtocol:

int i, j, k;
// ... calculation that assigns i, j, k
PyBuffer a = obj.getBuffer(PyBUF.FULL);
int itemsize = a.getItemsize();
PyBuffer.Pointer b = a.getPointer(i,j,k);
the item with index [i,j,k] is in the array b.storage at index [b.offset] to [b.offset + itemsize - 1] inclusive. And if itemsize==1, the item is simply the byte b.storage[b.offset]

Essentially this is a method for computing the offset of a particular index. The client is free to navigate the underlying buffer b.storage without respecting these boundaries. If the buffer is non-contiguous, the above description is still valid (since a multi-byte item must itself be contiguously stored), but in any additional navigation of b.storage[] to other items, the client must use the shape, strides and sub-offsets provided by the API. Normally one starts b = a.getBuf() in order to establish the offset of index [0,...,0].

Parameters
indices:int[]

multidimensional index at which to position the pointer

Returns:PyBuffer.Pointer

structure defining the byte[] slice that is the shared data

Annotations
@SuppressWarnings:deprecation
@Override

isReadonlyback to summary
public boolean isReadonly()

Overrides org.python.core.buffer.BaseBuffer.isReadonly.

Implements org.python.core.PyBUF.isReadonly.

Doc from org.python.core.PyBUF.isReadonly.

Determine whether the consumer is entitled to write to the exported storage.

Returns:boolean

true if writing is not allowed, false if it is.

Annotations
@Override

org.python.core.buffer back to summary

pack-priv Class Strided1DBuffer.SlicedView

extends Strided1DBuffer
Class Inheritance

A Strided1DBuffer.SlicedView represents a non-contiguous subsequence of a simple buffer.

Field Summary

Modifier and TypeField and Description
pack-priv PyBuffer
root

The buffer on which this is a slice view

Inherited from org.python.core.buffer.Strided1DBuffer:
stride

Constructor Summary

AccessConstructor and Description
public
SlicedView(PyBuffer
on which release must be called when this is released
root
,
int
consumer requirements
flags
,
byte[]
raw byte array containing exported data
storage
,
int
index into storage of item[0]
index0
,
int
number of items in the sliced view
count
,
int
in between successive elements of the new PyBuffer
stride
)

Construct a slice of a one-dimensional byte buffer.

Method Summary

Modifier and TypeMethod and Description
protected PyBuffer
getRoot()

Overrides org.python.core.buffer.BaseBuffer.getRoot.

Some PyBuffers, those created by slicing a PyBuffer, are related to a root PyBuffer.

Inherited from org.python.core.buffer.Strided1DBuffer:
byteIndexgetBufferSlicegetPointergetPointerisReadonly

Field Detail

rootback to summary
pack-priv PyBuffer root

The buffer on which this is a slice view

Constructor Detail

SlicedViewback to summary
public SlicedView(PyBuffer root, int flags, byte[] storage, int index0, int count, int stride) throws PyException

Construct a slice of a one-dimensional byte buffer.

Parameters
root:PyBuffer

on which release must be called when this is released

flags:int

consumer requirements

storage:byte[]

raw byte array containing exported data

index0:int

index into storage of item[0]

count:int

number of items in the sliced view

stride:int

in between successive elements of the new PyBuffer

Exceptions
PyException:
BufferError when expectations do not correspond with the type

Method Detail

getRootback to summary
protected PyBuffer getRoot()

Overrides org.python.core.buffer.BaseBuffer.getRoot.

Doc from org.python.core.buffer.BaseBuffer.getRoot.

Some PyBuffers, those created by slicing a PyBuffer, are related to a root PyBuffer. During creation of such a slice, we need to supply a value for this root. If the present object is not itself a slice, this root is the object itself; if the buffer is already a slice, it is the root it was given at creation time. Often this is the only difference between a slice-view and a directly-exported buffer. Override this method in slices to return the root buffer of the slice.

Returns:PyBuffer

this buffer (or the root buffer if this is a sliced view)

Annotations
@Override