jplus-0.4.7
jarray Class Reference

C++ representation of J array. More...

#include <src/jarray.h>

Inheritance diagram for jarray:
Collaboration diagram for jarray:

Data Structures

struct  header
 J array header. More...
 
struct  MS
 Layout of two words before every array, responsible for J memory management. More...
 
struct  Z
 complex type, equivalent to J More...
 

Public Types

enum  errorType { ERR_CONV =10 , ERR_SHAPE =11 }
 error codes More...
 
enum  elementType {
  T_B01 =1 , T_LIT =2 , T_INT =4 , T_FL =8 ,
  T_CMPX =16
}
 Constants for possible array element types. More...
 
typedef char B
 byte type, equivalent to J
 
typedef char C
 literal type, equivalent to J
 
typedef short S
 short int type, equivalent to J
 
typedef long I
 integer type, equivalent to J
 
typedef double D
 floating point type, equivalent to J
 

Public Member Functions

I getRefcount () const
 returns the array refcount
 
template<class T >
int get (T &v, const int i) const
 Obtains the value of a specified element of ravel.
 
template<class T >
int set (const int i, const T v)
 Assigns the value of the specified element of ravel.
 
template<class T >
int get (std::vector< T > &v) const
 Fits ravel of the array into the specified STL vector.
 
template<class T >
int get (T &v)
 Attempts to fit the whole array into the specified type.
 
 jarray ()
 Invalid array, a valid array can be assigned to it.
 
 jarray (jengine *je_, std::istream &in)
 Loads the array from binary representation in a stream.
 
jarray::I esize () const
 Size (in bytes) of the single element of this array.
 
void addhash (SHA1 &sha)
 Add this array (both shape and values) to the hash.
 
bool write (std::ostream &out)
 Writes binary representation of this array into the specified output stream.
 
bool isValid () const
 Returns true if the array is valid.
 
const I type () const
 Returns array type.
 
const I rank () const
 Rank (dimensionality) of the array.
 
Ishape () const
 Returns pointer to the first element of the shape.
 
int extent (int dimension) const
 Returns the extent of the specified dimension, element of shape.
 
void shape (std::vector< I > &shape) const
 Copies array shape into STL vector.
 
const int size () const
 Number of elements in ravel.
 
Idata () const
 Returns pointer to the beginning of the array data.
 
jenginegetEngine () const
 Returns pointer to jengine, managing memory of this array.
 
 jarray (jengine *je_, void *hdr_)
 Instantiates on top of an existing J array.
 
 jarray (jengine *je_, elementType type, I rank, I *shape)
 Creates new multidimensional array of the given J type.
 
 jarray (jengine *je_, elementType type, const std::vector< I > &shape)
 Creates new multidimensional array of the given J type.
 
 jarray (jengine *je_, const std::string &str)
 Creates T_LIT vector from C++ string.
 
 jarray (const jarray &other)
 Makes a copy of another array (increments refcount).
 
jarrayassign (const jarray &other)
 Assigns another array to this one (increments refcount and frees memory, if necessary)
 
jarrayoperator= (const jarray &other)
 Shortcut to assign.
 
bool operator== (const jarray &rhs) const
 Performs by-element comparison and return true if two arrays are the same.
 
 ~jarray ()
 Decrements refcount, frees array memory, if necessary.
 

Static Public Member Functions

static jarray::I esize (elementType type)
 Size (in bytes) of the particular data type.
 

Protected Member Functions

bool allocate (jengine *je_, elementType type, const I rank, const I *shape)
 Allocates new array in memory.
 
void grab () const
 Increments refcount.
 
void release ()
 Decrements refcount and frees memory, if necessary.
 
I getHeader (bool give_up_ownership=true) const
 

Protected Attributes

headerhdr
 Pointer to the array header, NULL for invalid array.
 
jengineje
 Pointer to jengine, managing the memory of this array.
 

Friends

class jengine
 

Detailed Description

C++ representation of J array.

This is the direct mapping of J array into C++ domain, for support of typed arrays (with automatic type conversion and convenient element indexing) see jarray_of_type template class. Only non-sparse array types are currently supported. This class may operate in standalone mode (managing its memory via malloc), or, if J engine is initalized, it will cooperate with J on memory allocation and make use of J garbage collection (AKA tempstack).

Member Typedef Documentation

◆ B

byte type, equivalent to J

◆ C

literal type, equivalent to J

◆ D

floating point type, equivalent to J

◆ I

integer type, equivalent to J

◆ S

short int type, equivalent to J

Member Enumeration Documentation

◆ elementType

Constants for possible array element types.

Enumerator
T_B01 

B boolean

T_LIT 

C literal (character)

T_INT 

I integer

T_FL 

D double (IEEE floating point)

T_CMPX 

Z complex

◆ errorType

error codes

Enumerator
ERR_CONV 

data type conversion error

ERR_SHAPE 

shape mismatch

Constructor & Destructor Documentation

◆ jarray() [1/7]

jarray::jarray ( )

Invalid array, a valid array can be assigned to it.

◆ jarray() [2/7]

jarray::jarray ( jengine je_,
std::istream &  in 
)

Loads the array from binary representation in a stream.

The representation can be created by the write method.

Parameters
je_jengine to own the array's memory.
instream to read the array from.

◆ jarray() [3/7]

jarray::jarray ( jengine je_,
void hdr_ 
)

Instantiates on top of an existing J array.

Parameters
je_jengine to own the array memory.
hdr_pointer to J array header.

◆ jarray() [4/7]

jarray::jarray ( jengine je_,
elementType  type,
I  rank,
I shape 
)

Creates new multidimensional array of the given J type.

Parameters
je_jengine to own the array memory.
typethe type of new array (one of T_XXX).
rankrank of the new array.
shapepointer to elements of shape.

◆ jarray() [5/7]

jarray::jarray ( jengine je_,
elementType  type,
const std::vector< I > &  shape 
)

Creates new multidimensional array of the given J type.

Parameters
je_jengine to own the array memory.
typethe array type (one of T_XXX).
shapethe STL vector, holding elements of shape.

◆ jarray() [6/7]

jarray::jarray ( jengine je_,
const std::string &  str 
)

Creates T_LIT vector from C++ string.

Parameters
je_jengine to own the array memory.
strstring the new array will contain.

◆ jarray() [7/7]

jarray::jarray ( const jarray other)

Makes a copy of another array (increments refcount).

Parameters
otherthe array to copy.

◆ ~jarray()

jarray::~jarray ( )

Decrements refcount, frees array memory, if necessary.

Member Function Documentation

◆ addhash()

void jarray::addhash ( SHA1 sha)

Add this array (both shape and values) to the hash.

Parameters
shahash to add the array to.

◆ allocate()

bool jarray::allocate ( jengine je_,
elementType  type,
const I  rank,
const I shape 
)
protected

Allocates new array in memory.

The memory if either malloc-ed (if the first argument is null), or, if passed non-null je pointer, allocated via jtga function of J engine.

Parameters
je_jengine to manage the memory of this array.
typetype of the new array (one of T_XXXX).
rankrank of the new array.
shapeshape of the new array.
Returns
true if success.

Referenced by jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::jarray_of_type(), and jarray_of_type< T >::jarray_of_type().

◆ assign()

jarray & jarray::assign ( const jarray other)

Assigns another array to this one (increments refcount and frees memory, if necessary)

Parameters
otherarray to assign to this one.
Returns
reference to this array.

Referenced by jarray_of_type< T >::jarray_of_type(), and operator=().

◆ data()

◆ esize() [1/2]

jarray::I jarray::esize ( ) const

Size (in bytes) of the single element of this array.

◆ esize() [2/2]

static jarray::I jarray::esize ( elementType  type)
static

Size (in bytes) of the particular data type.

Parameters
typeone of T_XXX constants).
Returns
size of the array element.

◆ extent()

◆ get() [1/3]

template<class T >
int jarray::get ( std::vector< T > &  v) const
inline

Fits ravel of the array into the specified STL vector.

The type must be convertible. Does copy.

Parameters
vthe STL vector to hold the copy of the array.
Returns
0 on success.

References ERR_CONV, hdr, jarray::header::n, jarray::header::offset, T_B01, T_CMPX, T_FL, T_INT, T_LIT, and jarray::header::type.

◆ get() [2/3]

template<class T >
int jarray::get ( T v)

Attempts to fit the whole array into the specified type.

Currently, this works for literal arrays, which can be fitted into C++ strings.

Parameters
vplace to store the array.
Returns
0 on success.

◆ get() [3/3]

template<class T >
int jarray::get ( T v,
const int  i 
) const
inline

Obtains the value of a specified element of ravel.

The type must be convertible.

Parameters
vthe place to store the value.
izero-based index of the element in ravel.
Returns
0 on success.

References ERR_CONV, hdr, jarray::header::offset, T_B01, T_CMPX, T_FL, T_INT, T_LIT, and jarray::header::type.

Referenced by jarray_of_type< T >::jarray_of_type().

◆ getEngine()

jengine * jarray::getEngine ( ) const
inline

Returns pointer to jengine, managing memory of this array.

Returns
jengine, menaging the memory of this array, or NULL if the memory is managed autonomously (via malloc).

References je.

Referenced by jarray_of_type< T >::jarray_of_type().

◆ getHeader()

I jarray::getHeader ( bool  give_up_ownership = true) const
inlineprotected

References jarray::header::flag, and hdr.

◆ getRefcount()

I jarray::getRefcount ( ) const
inline

returns the array refcount

Returns
the array refcount.

References hdr, and jarray::header::refcnt.

◆ grab()

void jarray::grab ( ) const
protected

Increments refcount.

◆ isValid()

bool jarray::isValid ( ) const
inline

Returns true if the array is valid.

Returns
true if the array is valid.

References hdr.

◆ operator=()

jarray & jarray::operator= ( const jarray other)
inline

Shortcut to assign.

Parameters
otherarray to assign to this one.
Returns
reference to this array.

References assign().

◆ operator==()

bool jarray::operator== ( const jarray rhs) const

Performs by-element comparison and return true if two arrays are the same.

Parameters
rhsarray to compare to.
Returns
true if arrays are the same.

◆ rank()

◆ release()

void jarray::release ( )
protected

Decrements refcount and frees memory, if necessary.

◆ set()

template<class T >
int jarray::set ( const int  i,
const T  v 
)
inline

Assigns the value of the specified element of ravel.

The type must be convertible.

Parameters
izero-based index of the element to assign.
vthe value to put into the array.
Returns
0 on success.

References ERR_CONV, hdr, jarray::header::offset, T_B01, T_CMPX, T_FL, T_INT, T_LIT, and jarray::header::type.

Referenced by jarray_of_type< T >::jarray_of_type().

◆ shape() [1/2]

◆ shape() [2/2]

void jarray::shape ( std::vector< I > &  shape) const
inline

Copies array shape into STL vector.

Parameters
shapethe vector to hold the requested shape.

References hdr, rank(), shape(), and jarray::header::shape.

◆ size()

const int jarray::size ( ) const
inline

Number of elements in ravel.

Returns
number of elements in ravel.

References hdr, rank(), and jarray::header::shape.

Referenced by jarray_of_type< T >::jarray_of_type().

◆ type()

const I jarray::type ( ) const
inline

Returns array type.

One of T_XXX constants.

Returns
type of the array elements.

References hdr, and jarray::header::type.

Referenced by jarray_of_type< T >::jarray_of_type().

◆ write()

bool jarray::write ( std::ostream &  out)

Writes binary representation of this array into the specified output stream.

Parameters
outthe stream to write the array to.
Returns
true if successfully written.

Friends And Related Symbol Documentation

◆ jengine

Field Documentation

◆ hdr

header* jarray::hdr
protected

Pointer to the array header, NULL for invalid array.

Referenced by data(), get(), get(), getHeader(), getRefcount(), isValid(), rank(), set(), shape(), shape(), size(), and type().

◆ je

jengine* jarray::je
protected

Pointer to jengine, managing the memory of this array.

Referenced by getEngine().


The documentation for this class was generated from the following file: