Commit a3ad5ce8 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Renamed ElementType to ValueType and Element to Value

This is closer to value_type which is used in STL containers.
parent 69870fc5
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -45,15 +45,15 @@ void export_Array(py::module & m, const char* name)
                // Pointer to buffer
                a.getData(),
                // Size of one scalar
                sizeof( typename ArrayType::ElementType ),
                sizeof( typename ArrayType::ValueType ),
                // Python struct-style format descriptor
                py::format_descriptor< typename ArrayType::ElementType >::format(),
                py::format_descriptor< typename ArrayType::ValueType >::format(),
                // Number of dimensions
                1,
                // Buffer dimensions
                { a.getSize() },
                // Strides (in bytes) for each index
                { sizeof( typename ArrayType::ElementType ) }
                { sizeof( typename ArrayType::ValueType ) }
            );
        })
    ;
+4 −4
Original line number Diff line number Diff line
@@ -9,15 +9,15 @@ template< typename Array, typename Scope >
void tnl_indexing( Scope & scope )
{
    using Index = typename Array::IndexType;
    using Element = typename Array::ElementType;
    using Value = typename Array::ValueType;

    scope.def("__len__", &Array::getSize);

    scope.def("__iter__",
        []( Array& array ) {
            return py::make_iterator(
                        RawIterator<Element>(array.getData()),
                        RawIterator<Element>(array.getData() + array.getSize()) );
                        RawIterator<Value>(array.getData()),
                        RawIterator<Value>(array.getData() + array.getSize()) );
        },
        py::keep_alive<0, 1>()  // keep array alive while iterator is used
    );
@@ -31,7 +31,7 @@ void tnl_indexing( Scope & scope )
    );

    scope.def("__setitem__",
        [](Array &a, Index i, const Element& e) {
        [](Array &a, Index i, const Value& e) {
            if (i >= a.getSize())
                throw py::index_error();
            a[i] = e;
+20 −20
Original line number Diff line number Diff line
@@ -23,24 +23,24 @@ template< int, typename > class StaticArray;
 * Array handles memory allocation and sharing of the same data between more Arrays.
 *
 */
template< typename Element,
template< typename Value,
          typename Device = Devices::Host,
          typename Index = int >
class Array : public Object
{
   public:

      typedef Element ElementType;
      typedef Value ValueType;
      typedef Device DeviceType;
      typedef Index IndexType;
      typedef Containers::Array< Element, Devices::Host, Index > HostType;
      typedef Containers::Array< Element, Devices::Cuda, Index > CudaType;
      typedef Containers::Array< Value, Devices::Host, Index > HostType;
      typedef Containers::Array< Value, Devices::Cuda, Index > CudaType;

      Array();

      Array( const IndexType& size );

      Array( Element* data,
      Array( Value* data,
             const IndexType& size );

      Array( Array& array,
@@ -67,7 +67,7 @@ class Array : public Object
      template< typename ArrayT >
      void setLike( const ArrayT& array );

      void bind( Element* _data,
      void bind( Value* _data,
                 const Index _size );

      template< typename ArrayT >
@@ -76,25 +76,25 @@ class Array : public Object
                 const IndexType& size = 0 );

      template< int Size >
      void bind( StaticArray< Size, Element >& array );
      void bind( StaticArray< Size, Value >& array );

      void swap( Array& array );

      void reset();

      void setElement( const Index& i, const Element& x );
      void setElement( const Index& i, const Value& x );

      Element getElement( const Index& i ) const;
      Value getElement( const Index& i ) const;

      // Checks if there is an element with value v in this array
      bool containsValue( const Element& v ) const;
      bool containsValue( const Value& v ) const;

      // Checks if all elements in this array have the same value v
      bool containsOnlyValue( const Element& v ) const;
      bool containsOnlyValue( const Value& v ) const;

      __cuda_callable__ inline Element& operator[] ( const Index& i );
      __cuda_callable__ inline Value& operator[] ( const Index& i );

      __cuda_callable__ inline const Element& operator[] ( const Index& i ) const;
      __cuda_callable__ inline const Value& operator[] ( const Index& i ) const;

      Array& operator = ( const Array& array );

@@ -107,11 +107,11 @@ class Array : public Object
      template< typename ArrayT >
      bool operator != ( const ArrayT& array ) const;

      void setValue( const Element& e );
      void setValue( const Value& v );

      __cuda_callable__ const Element* getData() const;
      __cuda_callable__ const Value* getData() const;

      __cuda_callable__ Element* getData();
      __cuda_callable__ Value* getData();

      /*!
       * Returns true if non-zero size is set.
@@ -149,7 +149,7 @@ class Array : public Object
      mutable Index size;

      //! Pointer to data
      mutable Element* data;
      mutable Value* data;

      /****
       * Pointer to the originally allocated data. They might differ if one
@@ -158,7 +158,7 @@ class Array : public Object
       * deallocate the array. If outer data (not allocated by TNL) are bind
       * then this pointer is zero since no deallocation is necessary.
       */
      mutable Element* allocationPointer;
      mutable Value* allocationPointer;

      /****
       * Counter of objects sharing this array or some parts of it. The reference counter is
@@ -168,8 +168,8 @@ class Array : public Object
      mutable int* referenceCounter;
};

template< typename Element, typename Device, typename Index >
std::ostream& operator << ( std::ostream& str, const Array< Element, Device, Index >& v );
template< typename Value, typename Device, typename Index >
std::ostream& operator << ( std::ostream& str, const Array< Value, Device, Index >& v );

} // namespace Containers
} // namespace TNL
+12 −12
Original line number Diff line number Diff line
@@ -18,22 +18,22 @@
namespace TNL {
namespace Containers {

template< typename Element,
template< typename Value,
          typename Device,
          typename Index,
          bool Elementwise = std::is_base_of< Object, Element >::value >
          bool Elementwise = std::is_base_of< Object, Value >::value >
class ArrayIO
{};

template< typename Element,
template< typename Value,
          typename Device,
          typename Index >
class ArrayIO< Element, Device, Index, true >
class ArrayIO< Value, Device, Index, true >
{
   public:

   static bool save( File& file,
                     const Element* data,
                     const Value* data,
                     const Index elements )
   {
      for( Index i = 0; i < elements; i++ )
@@ -46,7 +46,7 @@ class ArrayIO< Element, Device, Index, true >
   }

   static bool load( File& file,
                     Element* data,
                     Value* data,
                     const Index elements )
   {
      for( Index i = 0; i < elements; i++ )
@@ -59,25 +59,25 @@ class ArrayIO< Element, Device, Index, true >
   }
};

template< typename Element,
template< typename Value,
          typename Device,
          typename Index >
class ArrayIO< Element, Device, Index, false >
class ArrayIO< Value, Device, Index, false >
{
   public:

   static bool save( File& file,
                     const Element* data,
                     const Value* data,
                     const Index elements )
   {
      return file.write< Element, Device, Index >( data, elements );
      return file.write< Value, Device, Index >( data, elements );
   }

   static bool load( File& file,
                     Element* data,
                     Value* data,
                     const Index elements )
   {
      return file.read< Element, Device, Index >( data, elements );
      return file.read< Value, Device, Index >( data, elements );
   }

};
+38 −38
Original line number Diff line number Diff line
@@ -18,30 +18,30 @@
namespace TNL {
namespace Containers {

template< typename Element, typename Device, typename Index >
template< typename Value, typename Device, typename Index >
class Array;

template< int Size, typename Element >
template< int Size, typename Value >
class StaticArray;

template< typename Element,
template< typename Value,
          typename Device = Devices::Host,
          typename Index = int >
class ArrayView
{
public:
   using ElementType = Element;
   using ValueType = Value;
   using DeviceType = Device;
   using IndexType = Index;
   using HostType = ArrayView< Element, Devices::Host, Index >;
   using CudaType = ArrayView< Element, Devices::Cuda, Index >;
   using HostType = ArrayView< Value, Devices::Host, Index >;
   using CudaType = ArrayView< Value, Devices::Cuda, Index >;

   __cuda_callable__
   ArrayView() = default;

   // explicit initialization by raw data pointer and size
   __cuda_callable__
   ArrayView( Element* data, Index size );
   ArrayView( Value* data, Index size );

   // Copy-constructor does shallow copy, so views can be passed-by-value into
   // CUDA kernels and they can be captured-by-value in __cuda_callable__
@@ -49,10 +49,10 @@ public:
   __cuda_callable__
   ArrayView( const ArrayView& ) = default;

   // "Templated copy-constructor" accepting any cv-qualification of Element
   template< typename Element_ >
   // "Templated copy-constructor" accepting any cv-qualification of Value
   template< typename Value_ >
   __cuda_callable__
   ArrayView( ArrayView< Element_, Device, Index >& array )
   ArrayView( ArrayView< Value_, Device, Index >& array )
   : data(array.getData()), size(array.getSize()) {}

   // default move-constructor
@@ -60,28 +60,28 @@ public:
   ArrayView( ArrayView&& ) = default;

   // initialization from other array containers (using shallow copy)
   template< typename Element_ >  // template catches both const and non-const qualified Element
   template< typename Value_ >  // template catches both const and non-const qualified Value
   __cuda_callable__
   ArrayView( Array< Element_, Device, Index >& array );
   ArrayView( Array< Value_, Device, Index >& array );

   template< int Size, typename Element_ >  // template catches both const and non-const qualified Element
   template< int Size, typename Value_ >  // template catches both const and non-const qualified Value
   __cuda_callable__
   ArrayView( StaticArray< Size, Element_ >& array );
   ArrayView( StaticArray< Size, Value_ >& array );

   // these constructors will be used only when Element is const-qualified
   // these constructors will be used only when Value is const-qualified
   // (const views are initializable by const references)
   template< typename Element_ >  // template catches both const and non-const qualified Element
   template< typename Value_ >  // template catches both const and non-const qualified Value
   __cuda_callable__
   ArrayView( const Array< Element_, Device, Index >& array );
   ArrayView( const Array< Value_, Device, Index >& array );

   template< int Size, typename Element_ >  // template catches both const and non-const qualified Element
   template< int Size, typename Value_ >  // template catches both const and non-const qualified Value
   __cuda_callable__
   ArrayView( const StaticArray< Size, Element_ >& array );
   ArrayView( const StaticArray< Size, Value_ >& array );


   // methods for rebinding (reinitialization)
   __cuda_callable__
   void bind( Element* data, const Index size );
   void bind( Value* data, const Index size );

   // Note that you can also bind directly to Array and other types implicitly
   // convertible to ArrayView.
@@ -93,8 +93,8 @@ public:
   // must match (i.e. copy-assignment cannot resize).
   ArrayView& operator=( const ArrayView& view );

   template< typename Element_, typename Device_, typename Index_ >
   ArrayView& operator=( const ArrayView< Element_, Device_, Index_ >& view );
   template< typename Value_, typename Device_, typename Index_ >
   ArrayView& operator=( const ArrayView< Value_, Device_, Index_ >& view );


   static String getType();
@@ -107,51 +107,51 @@ public:
   void reset();

   __cuda_callable__
   const Element* getData() const;
   const Value* getData() const;

   __cuda_callable__
   Element* getData();
   Value* getData();

   __cuda_callable__
   Index getSize() const;

   void setElement( Index i, Element value );
   void setElement( Index i, Value value );

   Element getElement( Index i ) const;
   Value getElement( Index i ) const;

   __cuda_callable__
   Element& operator[]( Index i );
   Value& operator[]( Index i );

   __cuda_callable__
   const Element& operator[]( Index i ) const;
   const Value& operator[]( Index i ) const;

   template< typename Element_, typename Device_, typename Index_ >
   bool operator==( const ArrayView< Element_, Device_, Index_ >& view ) const;
   template< typename Value_, typename Device_, typename Index_ >
   bool operator==( const ArrayView< Value_, Device_, Index_ >& view ) const;

   template< typename Element_, typename Device_, typename Index_ >
   bool operator!=( const ArrayView< Element_, Device_, Index_ >& view ) const;
   template< typename Value_, typename Device_, typename Index_ >
   bool operator!=( const ArrayView< Value_, Device_, Index_ >& view ) const;

   void setValue( Element value );
   void setValue( Value value );

   // Checks if there is an element with given value in this array
   bool containsValue( Element value ) const;
   bool containsValue( Value value ) const;

   // Checks if all elements in this array have the same given value
   bool containsOnlyValue( Element value ) const;
   bool containsOnlyValue( Value value ) const;

   //! Returns true if non-zero size is set.
   operator bool() const;

protected:
   //! Pointer to allocated data
   Element* data = nullptr;
   Value* data = nullptr;

   //! Number of allocated elements
   Index size = 0;
};

template< typename Element, typename Device, typename Index >
std::ostream& operator<<( std::ostream& str, const ArrayView< Element, Device, Index >& v );
template< typename Value, typename Device, typename Index >
std::ostream& operator<<( std::ostream& str, const ArrayView< Value, Device, Index >& v );

} // namespace Containers
} // namespace TNL
Loading