Skip to content

Commit

Permalink
Added .data() methods to Array, Array_, Array2, MatrixN
Browse files Browse the repository at this point in the history
  • Loading branch information
aslze committed Nov 23, 2023
1 parent 7a2ca06 commit dd77134
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
24 changes: 18 additions & 6 deletions include/asl/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,37 @@ class Array
*/
void clear() { resize(0); }
/* Frees all elements (with delete) and clears the array; elements must be pointers */
void destroy() { for(int i=0; i<length(); i++) delete _a[i]; clear(); }
ASL_DEPRECATED(void destroy(), "") { for(int i=0; i<length(); i++) delete _a[i]; clear(); }
/**
Returns a pointer to the base of the array
\deprecated this conversion will not be implicit! Use .ptr()
\deprecated this conversion will not be implicit! Use .data()
*/
ASL_DEPRECATED(operator const T*() const, "Use ptr()") { return &_a[0]; }
ASL_DEPRECATED(operator const T*() const, "Use .data()") { return &_a[0]; }
/**
Returns a pointer to the base of the array
\deprecated this conversion will not be implicit! Use .ptr()
\deprecated this conversion will not be implicit! Use .data()
*/
ASL_DEPRECATED(operator T*(), "Use ptr()") { return &_a[0]; }
ASL_DEPRECATED(operator T*(), "Use .data()") { return &_a[0]; }
/**
Returns a pointer to the first element
\deprecated Use .data()
*/
const T* ptr() const { return &_a[0]; }
/**
Returns a pointer to the first element
\deprecated Use .data()
*/
T* ptr() {return &_a[0];}
T* ptr() { return &_a[0]; }

/**
Returns a pointer to the first element
*/
T* data() { return &_a[0]; }

/**
Returns a pointer to the first element
*/
const T* data() const { return &_a[0]; }

bool operator!() const { return d().n == 0; }

Expand Down
11 changes: 11 additions & 0 deletions include/asl/Array2.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,20 @@ class Array2

/**
Returns the internal array holding all values (row-major)
\deprecated This will return a pointer in the future. Use .array()
*/
const Array<T>& data() const { return _a; }

/**
Returns the internal array holding all values (row-major)
*/
Array<T>& array() { return _a; }

/**
Returns the internal array holding all values (row-major)
*/
const Array<T>& array() const { return _a; }

/**
Returns true if both arrays are equal
*/
Expand Down
15 changes: 9 additions & 6 deletions include/asl/Array_.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright(c) 1999-2022 aslze
// Copyright(c) 1999-2023 aslze
// Licensed under the MIT License (http://opensource.org/licenses/MIT)

#ifndef ASL_ARRAYX_H
Expand Down Expand Up @@ -38,11 +38,11 @@ class Array_
Array_(const Array_<K, N>& b)
{
for(int i = 0; i < N; i++)
_a[i]=(T)b[i];
_a[i] = (T)b[i];
}
Array_(const Array_& b)
{
for(int i=0; i<N; i++)
for(int i = 0; i < N; i++)
_a[i] = b._a[i];
}
#ifdef ASL_HAVE_INITLIST
Expand Down Expand Up @@ -98,10 +98,13 @@ class Array_
const T* ptr() const { return &_a[0]; }
T* ptr() { return &_a[0]; }

const T* data() const { return &_a[0]; }
T* data() { return &_a[0]; }

/** Tests for equality of all elements of both arrays*/
bool operator==(const Array_& b) const
{
for(int i=0; i<N; i++)
for (int i = 0; i < N; i++)
if(_a[i] != b._a[i])
return false;
return true;
Expand Down Expand Up @@ -152,9 +155,9 @@ class Array_
/** Assigns array b into this array by reference. */
Array_& operator=(const Array_& b)
{
if(this==&b) return *this;
if(this == &b) return *this;
for(int i = 0; i < N; i++)
_a[i]=b[i];
_a[i] = b[i];
return *this;
}
Array_ reversed() const
Expand Down
5 changes: 4 additions & 1 deletion include/asl/Matrix3.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class Matrix3_
*/
T trace() const { return a[0][0] + a[1][1] + a[2][2]; }

T* data() { return &a[0][0]; }
const T* data() const { return &a[0][0]; }

Matrix3_() {}

/**
Expand Down Expand Up @@ -101,7 +104,7 @@ class Matrix3_
Matrix3_ t() const { return transposed(); }

/** Returns the element at row `i`, column `j`. */
T& operator()(int i, int j) {return a[i][j];}
T& operator()(int i, int j) { return a[i][j]; }
const T& operator()(int i, int j) const { return a[i][j]; }

/**
Expand Down

0 comments on commit dd77134

Please sign in to comment.