Skip to content
Snippets Groups Projects
Commit b7e9d7f9 authored by Louis Gombert's avatar Louis Gombert :speech_balloon:
Browse files

Add GetActualMemorySize function and trait for vtkImplicitArray

parent 737ca17e
No related branches found
No related tags found
No related merge requests found
......@@ -44,6 +44,11 @@ struct IsNotDefaultConstructible
IsNotDefaultConstructible(int& i) { i++; }
};
struct CanGetMemorySize
{
unsigned long getMemorySize() const { return 1ul; }
};
}
int TestImplicitArrayTraits(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
......@@ -340,5 +345,18 @@ int TestImplicitArrayTraits(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
result = EXIT_FAILURE;
}
//--------------------------------------------------------------------------------
if (!vtk::detail::can_get_memory_size_trait<::CanGetMemorySize>::value)
{
std::cout << "Failed get memory size check on CanGetMemorySize" << std::endl;
result = EXIT_FAILURE;
}
if (vtk::detail::can_get_memory_size_trait<::HasNothing>::value)
{
std::cout << "Failed get memory size check on HasNothing" << std::endl;
result = EXIT_FAILURE;
}
return result;
}
......@@ -67,6 +67,8 @@ struct ConstComponentStruct
int map(int idx) const { return this->mapComponent(idx / 3, idx % 3); }
// used for GetTypedComponent
int mapComponent(int vtkNotUsed(idx), int comp) const { return this->Tuple[comp]; }
unsigned long getMemorySize() const { return 16; }
};
};
......@@ -223,6 +225,14 @@ int TestImplicitArraysBase(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
}
}
// test GetActualMemorySize fallback default implementation
if (genericTupleConstArr->GetActualMemorySize() != 1)
{
res = EXIT_FAILURE;
std::cout
<< "generic TupleConstArr does not use the default GetActualMemorySize implementation";
}
// test backend with mapComponent
vtkNew<vtkImplicitArray<::ConstComponentStruct>> genericComponentConstArr;
genericComponentConstArr->ConstructBackend(tuple);
......@@ -257,5 +267,12 @@ int TestImplicitArraysBase(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
}
}
// test GetActualMemorySize implementation
if (genericComponentConstArr->GetActualMemorySize() != 16)
{
res = EXIT_FAILURE;
std::cout << "generic ConstComponentStruct does not return its actual size";
}
return res;
}
......@@ -130,6 +130,11 @@
* arr43->Fill(43);
* @endcode
*
* Optionnally, `vtkImplicitArray`s backends can return their memory usage in KiB by defining
* the function `getMemorySize` returning `unsigned long`. `vtkImplicitArray` then exposes this
* function through the `GetActualMemorySize` function. If the backend does not define it,
* `GetActualMemorySize` always returns 1.
*
* @sa
* vtkGenericDataArray vtkImplicitArrayTraits vtkDataArray
*/
......@@ -258,6 +263,20 @@ public:
this->Squeeze();
}
/**
* Return the memory in kibibytes (1024 bytes) consumed by this implicit data array.
*
* The value returned is guaranteed to be greater than or equal to the memory required to
* actually represent the data represented by this object.
*
* Implicit array backends can implement the `getMemorySize` function to override the default
* implementation, which always returns 1.
*/
inline unsigned long GetActualMemorySize() const override
{
return this->GetActualMemorySizeImpl<BackendT>();
}
/**
* Specific DeepCopy for implicit arrays
*
......@@ -439,6 +458,31 @@ private:
}
///@}
///@{
/**
* Static call to get memory size for compatible backends
*/
template <typename U>
typename std::enable_if<vtk::detail::implicit_array_traits<U>::can_get_memory_size,
unsigned long>::type
GetActualMemorySizeImpl() const
{
return this->Backend->getMemorySize();
}
/**
* Static call to get memory size for incompatible backends
* For those backends, dhe default memory size is 1KiB.
*/
template <typename U>
typename std::enable_if<!vtk::detail::implicit_array_traits<U>::can_get_memory_size,
unsigned long>::type
GetActualMemorySizeImpl() const
{
return 1;
}
///@}
friend class vtkGenericDataArray<vtkImplicitArray<BackendT>, ValueTypeT>;
};
......
......@@ -334,6 +334,36 @@ struct can_map_component_trait<T, void_t<typename has_map_component_trait<T>::rt
};
///@}
///@{
/**
* \struct can_get_memory_size_trait
* \brief used to check whether the template type has a method named getMemorySize
*/
template <typename, typename = void>
struct can_get_memory_size_trait : std::false_type
{
};
template <typename T>
struct can_get_memory_size_trait<T,
void_t<decltype(&std::remove_reference<T>::type::getMemorySize)>>
: public can_get_memory_size_trait<decltype(&std::remove_reference<T>::type::getMemorySize)>
{
using type = T;
static constexpr bool value = true;
};
template <typename T>
struct can_get_memory_size_trait<T*> : public can_get_memory_size_trait<T>
{
};
template <typename T>
struct can_get_memory_size_trait<const T> : public can_get_memory_size_trait<T>
{
};
///@}
/**
* \struct implicit_array_traits
* \brief A composite trait for handling all the different capabilities a "backend" to an
......@@ -351,6 +381,7 @@ struct implicit_array_traits
static constexpr bool default_constructible = std::is_default_constructible<T>::value;
static constexpr bool can_direct_read_tuple = can_map_tuple_trait<T>::value;
static constexpr bool can_direct_read_component = can_map_component_trait<T>::value;
static constexpr bool can_get_memory_size = can_get_memory_size_trait<T>::value;
};
VTK_ABI_NAMESPACE_END
......
......@@ -219,3 +219,7 @@ The following strategies (in order of complexity) have been implemented so far:
- `vtkToAffineArrayStrategy`: transform an explicit array that follows an affine dependence on its indexes into a `vtkAffineArray`
- `vtkToImplicitTypeErasureStrategy`: transform an explicit integral array (with more range in its value type than necessary for describing it) into a reduced memory explicit integral array wrapped in an implicit array.
- `vtkToImplicitRamerDouglasPeuckerStrategy`: transform an explicit memory array into a `vtkCompositeArray` with constant (`vtkConstantArray`) and affine (`vtkAffineArray`) parts following a Ramer-Douglas-Peucker algorithm.
## `GetActualMemorySize` function for `vtkImplicitArray`s
`vtkImplicitArray` backends can also define the method `getMemorySize` returning an `unsigned long` value corresponding to their memory usage in KiB. This value can then be accessed through `vtkImplicitArray`'s method `GetActualMemorySize`. If the backend does not define `getMemorySize`, the value returned by `GetActualMemorySize` will be 1.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment