jplus-0.4.6
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

typedef char jarray::B

byte type, equivalent to J

typedef char jarray::C

literal type, equivalent to J

typedef double jarray::D

floating point type, equivalent to J

typedef long jarray::I

integer type, equivalent to J

typedef short jarray::S

short int type, equivalent to J


Member Enumeration Documentation

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.

error codes

Enumerator:
ERR_CONV 

data type conversion error

ERR_SHAPE 

shape mismatch


Constructor & Destructor Documentation

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

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::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::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::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::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::jarray ( const jarray other)

Makes a copy of another array (increments refcount).

Parameters:
otherthe array to copy.

Decrements refcount, frees array memory, if necessary.


Member Function Documentation

void jarray::addhash ( SHA1 &  sha)

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

Parameters:
shahash to add the array to.
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& 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=().

I* jarray::data ( ) const [inline]

Returns pointer to the beginning of the array data.

Returns:
pointer to array data.

References hdr.

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

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.

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

int jarray::extent ( int  dimension) const [inline]

Returns the extent of the specified dimension, element of shape.

Parameters:
dimensionthe axis, whose size is requested.
Returns:
dimension along the specified axis.

References rank(), and shape().

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

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, T_B01, T_CMPX, T_FL, T_INT, T_LIT, and jarray::header::type.

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

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, T_B01, T_CMPX, T_FL, T_INT, T_LIT, and jarray::header::type.

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.
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().

I jarray::getHeader ( bool  give_up_ownership = true) const [inline, protected]

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

I jarray::getRefcount ( ) const [inline]

returns the array refcount

Returns:
the array refcount.

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

void jarray::grab ( ) const [protected]

Increments refcount.

bool jarray::isValid ( ) const [inline]

Returns true if the array is valid.

Returns:
true if the array is valid.

References hdr.

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

Shortcut to assign.

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

References assign().

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.
const I jarray::rank ( ) const [inline]

Rank (dimensionality) of the array.

Returns:
rank of the array.

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

Referenced by extent(), jarray_of_type< T >::jarray_of_type(), jarray_of_type< T >::operator()(), shape(), and size().

void jarray::release ( ) [protected]

Decrements refcount and frees memory, if necessary.

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, T_B01, T_CMPX, T_FL, T_INT, T_LIT, and jarray::header::type.

I* jarray::shape ( ) const [inline]

Returns pointer to the first element of the shape.

Returns:
pointer to the shape.

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

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

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(), and jarray::header::shape.

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().

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().

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 Function Documentation

friend class jengine [friend]

Field Documentation

header* jarray::hdr [protected]

Pointer to the array header, NULL for invalid array.

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

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: