Commit 38e6c86e authored by André Anjos's avatar André Anjos 💬
Browse files

Remove dependencies to the old bob::core, build-in everything

parent da3d005b
......@@ -12,7 +12,7 @@
#include <bob.blitz/cppapi.h>
#include <bob.blitz/cleanup.h>
#include <bob/core/array_convert.h>
#include <bob.core/array_convert.h>
template <typename Tdst, typename Tsrc, int N>
PyObject* inner_convert (PyBlitzArrayObject* src,
......
/**
* @date Tue Nov 8 15:34:31 2011 +0100
* @author Andre Anjos <andre.anjos@idiap.ch>
*
* @brief Some buffer stuff
*
* Copyright (C) Idiap Research Institute, Martigny, Switzerland
*/
#include <boost/format.hpp>
#include "array.h"
bob::core::array::typeinfo::typeinfo():
dtype(bob::core::array::t_unknown),
nd(0)
{
}
bob::core::array::typeinfo::typeinfo(const bob::core::array::typeinfo& other):
dtype(other.dtype)
{
set_shape(other.nd, other.shape);
}
bob::core::array::typeinfo& bob::core::array::typeinfo::operator= (const bob::core::array::typeinfo& other) {
dtype = other.dtype;
set_shape(other.nd, other.shape);
return *this;
}
void bob::core::array::typeinfo::reset() {
dtype = bob::core::array::t_unknown;
nd = 0;
}
bool bob::core::array::typeinfo::is_valid() const {
return (dtype != bob::core::array::t_unknown) && (nd > 0) && (nd <= (BOB_MAX_DIM+1)) && has_valid_shape();
}
void bob::core::array::typeinfo::update_strides() {
switch (nd) {
case 0:
return;
case 1:
stride[0] = 1;
return;
case 2:
stride[1] = 1;
stride[0] = shape[1];
return;
case 3:
stride[2] = 1;
stride[1] = shape[2];
stride[0] = shape[1]*shape[2];
return;
case 4:
stride[3] = 1;
stride[2] = shape[3];
stride[1] = shape[2]*shape[3];
stride[0] = shape[1]*shape[2]*shape[3];
return;
case 5:
stride[4] = 1;
stride[3] = shape[4];
stride[2] = shape[3]*shape[4];
stride[1] = shape[2]*shape[3]*shape[4];
stride[0] = shape[1]*shape[2]*shape[3]*shape[4];
return;
default:
break;
}
throw std::runtime_error("unsupported number of dimensions");
}
size_t bob::core::array::typeinfo::size() const {
size_t retval = 1;
for (size_t k=0; k<nd; ++k) retval *= shape[k];
return retval;
}
size_t bob::core::array::typeinfo::buffer_size() const {
return size()*bob::core::array::getElementSize(dtype);
}
static bool same_shape(size_t nd, const size_t* s1, const size_t* s2) {
for (size_t k=0; k<nd; ++k) if (s1[k] != s2[k]) return false;
return true;
}
bool bob::core::array::typeinfo::is_compatible(const bob::core::array::typeinfo& other) const {
return (dtype == other.dtype) && (nd == other.nd) && same_shape(nd, shape, other.shape);
}
std::string bob::core::array::typeinfo::str() const {
boost::format s("dtype: %s (%d); shape: [%s]; size: %d bytes");
size_t sz = 0;
size_t buf_sz = 0;
if (dtype != bob::core::array::t_unknown) {
//otherwise it throws
sz = item_size();
buf_sz = buffer_size();
}
s % item_str() % sz;
switch (nd) {
case 0:
s % "";
break;
case 1:
s % (boost::format("%d") % shape[0]).str();
break;
case 2:
s % (boost::format("%d,%d") % shape[0] % shape[1]).str();
break;
case 3:
s % (boost::format("%d,%d,%d") % shape[0] % shape[1] % shape[2]).str();
break;
case 4:
s % (boost::format("%d,%d,%d,%d") % shape[0] % shape[1] % shape[2] % shape[3]).str();
break;
default:
s % ">4 dimensions?";
break;
}
s % buf_sz;
return s.str();
}
void bob::core::array::typeinfo::reset_shape() {
shape[0] = 0;
}
bool bob::core::array::typeinfo::has_valid_shape() const {
return shape[0] != 0;
}
/**
* @date Tue Nov 8 15:34:31 2011 +0100
* @author Andre Anjos <andre.anjos@idiap.ch>
*
* @brief The array API describes a non-specific way to handle N dimensional
* array data.
*
* Copyright (C) Idiap Research Institute, Martigny, Switzerland
*/
#ifndef BOB_CORE_ARRAY_INTERFACE_H
#define BOB_CORE_ARRAY_INTERFACE_H
#include <stdexcept>
#include <string>
#include <boost/shared_ptr.hpp>
#include <blitz/array.h>
#include "array_type.h"
/* MinGW flags */
#ifdef _WIN32
#undef interface
#endif
/**
* @addtogroup CORE_ARRAY core_array
* @brief Array submodule API of the core module
*/
namespace bob { namespace core {
/**
* @ingroup CORE
* @ingroup CORE_ARRAY
*/
namespace array {
/**
* @ingroup CORE_ARRAY
* @{
*/
/**
* @brief Encapsulation of special type information of interfaces.
*/
struct typeinfo {
ElementType dtype; ///< data type
size_t nd; ///< number of dimensions
size_t shape[BOB_MAX_DIM+1]; ///< length along each dimension
size_t stride[BOB_MAX_DIM+1]; ///< strides along each dimension
/**
* @brief Default constructor
*/
typeinfo();
/**
* @brief Simplification to build a typeinfo from a size
*/
template <typename T> typeinfo(ElementType dtype_, T nd_) {
set(dtype_, nd_);
}
/**
* @brief Simplification to build a typeinfo from a shape pointer.
*/
template <typename T> typeinfo(ElementType dtype_, T nd_, const T* shape_) {
set(dtype_, nd_, shape_);
}
/**
* @brief Copies information from another typeinfo
*/
typeinfo(const typeinfo& other);
/**
* @brief Assignment
*/
typeinfo& operator= (const typeinfo& other);
/**
* @brief Builds with type and number of dimensions, but set the shape and
* strides to all zeros.
*/
template <typename T>
void set(ElementType dtype_, T nd_) {
dtype = dtype_;
nd = nd_;
reset_shape();
}
/**
* @brief Set to specific values
*/
template <typename T>
void set(ElementType dtype_, T nd_, const T* shape_) {
dtype = dtype_;
set_shape(nd_, shape_);
}
/**
* @brief Set to specific values, including strides
*/
template <typename T>
void set(ElementType dtype_, T nd_, const T* shape_,
const T* stride_) {
dtype = dtype_;
nd = nd_;
for (size_t k=0; k<nd; ++k) {
shape[k] = shape_[k];
stride[k] = stride_[k];
}
}
/**
* @brief Reset to defaults -- as if uninitialized.
*/
void reset();
/**
* @brief Is this a valid type information?
*/
bool is_valid() const;
/**
* @brief Does this has a valid shape information?
*/
bool has_valid_shape() const;
/**
* @brief sets the shape
*/
template <typename T> void set_shape(T nd_, const T* shape_) {
if (nd_ > (BOB_MAX_DIM+1))
throw std::runtime_error("unsupported number of dimensions");
nd = nd_;
for (size_t k=0; k<nd; ++k) shape[k] = shape_[k];
update_strides();
}
/**
* @brief resets the shape to all zeros
*/
void reset_shape();
/**
* @brief Update my own stride vector. Called automatically after any use
* of set_shape().
*/
void update_strides();
/**
* @brief Returns the total number of elements available
*/
size_t size() const;
/**
* @brief Returns the size of each element
*/
inline size_t item_size() const { return getElementSize(dtype); }
/**
* @brief Returns the total size (in bytes) of the buffer that I'm
* associated with.
*/
size_t buffer_size() const;
/**
* @brief Returns the item type description
*/
const char* item_str() const { return stringize(dtype); }
/**
* @brief Checks compatibility with other typeinfo
*/
bool is_compatible(const typeinfo& other) const;
/**
* @brief Formats and returns a string containing the full typeinfo
* description.
*/
std::string str() const;
/**
* @brief Make it easy to set for blitz::Array<T,N>
*/
template <typename T, int N> void set(const blitz::Array<T,N>& array) {
dtype = getElementType<T>();
set_shape(array.shape());
}
template <typename T, int N>
void set(boost::shared_ptr<blitz::Array<T,N> >& array) {
dtype = getElementType<T>();
set_shape(array->shape());
}
template <int N> void set_shape(const blitz::TinyVector<int,N>& tv_shape) {
nd = N;
for (size_t k=0; k<nd; ++k) shape[k] = tv_shape(k);
update_strides();
}
};
/**
* @brief The interface manager introduces a concept for managing the
* interfaces that can be handled as C-style arrays. It encapsulates methods
* to store and delete the buffer contents in a safe way.
*
* The interface is an entity that either stores a copy of its own data or
* refers to data belonging to another interface.
*/
class interface {
public: //api
/**
* @brief By default, the interface is never freed. You must override
* this method to do something special for your class type.
*/
virtual ~interface() { }
/**
* @brief Copies the data from another interface.
*/
virtual void set(const interface& other) =0;
/**
* @brief Refers to the data of another interface.
*/
virtual void set(boost::shared_ptr<interface> other) =0;
/**
* @brief Re-allocates this interface taking into consideration new
* requirements. The internal memory should be considered uninitialized.
*/
virtual void set (const typeinfo& req) =0;
/**
* @brief Type information for this interface.
*/
virtual const typeinfo& type() const =0;
/**
* @brief Borrows a reference from the underlying memory. This means
* this object continues to be responsible for deleting the memory and
* you should make sure that it outlives the usage of the returned
* pointer.
*/
virtual void* ptr() =0;
virtual const void* ptr() const =0;
/**
* @brief Returns a representation of the internal cache using shared
* pointers.
*/
virtual boost::shared_ptr<void> owner() =0;
virtual boost::shared_ptr<const void> owner() const =0;
};
/**
* @}
*/
}}}
#endif /* BOB_CORE_ARRAY_INTERFACE_H */
/**
* @date Sat Apr 9 18:10:10 2011 +0200
* @author Laurent El Shafey <Laurent.El-Shafey@idiap.ch>
*
* @brief Some type-related array utilities
*
* Copyright (C) Idiap Research Institute, Martigny, Switzerland
*/
#include "array_type.h"
#include <boost/format.hpp>
static const char* t_bool_string = "bool";
static const char* t_int8_string = "int8";
static const char* t_int16_string = "int16";
static const char* t_int32_string = "int32";
static const char* t_int64_string = "int64";
static const char* t_uint8_string = "uint8";
static const char* t_uint16_string = "uint16";
static const char* t_uint32_string = "uint32";
static const char* t_uint64_string = "uint64";
static const char* t_float32_string = "float32";
static const char* t_float64_string = "float64";
static const char* t_float128_string = "float128";
static const char* t_complex64_string = "complex64";
static const char* t_complex128_string = "complex128";
static const char* t_complex256_string = "complex256";
static const char* t_unknown_string = "unknown";
size_t bob::core::array::getElementSize(ElementType t) {
switch(t) {
case bob::core::array::t_bool:
return sizeof(bool);
case bob::core::array::t_int8:
return sizeof(int8_t);
case bob::core::array::t_int16:
return sizeof(int16_t);
case bob::core::array::t_int32:
return sizeof(int32_t);
case bob::core::array::t_int64:
return sizeof(int64_t);
case bob::core::array::t_uint8:
return sizeof(uint8_t);
case bob::core::array::t_uint16:
return sizeof(uint16_t);
case bob::core::array::t_uint32:
return sizeof(uint32_t);
case bob::core::array::t_uint64:
return sizeof(uint64_t);
case bob::core::array::t_float32:
return sizeof(float);
case bob::core::array::t_float64:
return sizeof(double);
case bob::core::array::t_float128:
return sizeof(long double);
case bob::core::array::t_complex64:
return sizeof(std::complex<float>);
case bob::core::array::t_complex128:
return sizeof(std::complex<double>);
case bob::core::array::t_complex256:
return sizeof(std::complex<long double>);
default:
{
boost::format m("unsupported element type (%d)");
m % (int)t;
throw std::runtime_error(m.str());
}
}
}
const char* bob::core::array::stringize(ElementType t) {
switch(t) {
case bob::core::array::t_bool:
return t_bool_string;
case bob::core::array::t_int8:
return t_int8_string;
case bob::core::array::t_int16:
return t_int16_string;
case bob::core::array::t_int32:
return t_int32_string;
case bob::core::array::t_int64:
return t_int64_string;
case bob::core::array::t_uint8:
return t_uint8_string;
case bob::core::array::t_uint16:
return t_uint16_string;
case bob::core::array::t_uint32:
return t_uint32_string;
case bob::core::array::t_uint64:
return t_uint64_string;
case bob::core::array::t_float32:
return t_float32_string;
case bob::core::array::t_float64:
return t_float64_string;
case bob::core::array::t_float128:
return t_float128_string;
case bob::core::array::t_complex64:
return t_complex64_string;
case bob::core::array::t_complex128:
return t_complex128_string;
case bob::core::array::t_complex256:
return t_complex256_string;
default:
return t_unknown_string;
}
}
bob::core::array::ElementType bob::core::array::unstringize(const char* s) {
std::string sc(s);
if (sc == t_bool_string) return bob::core::array::t_bool;
if (sc == t_int8_string) return bob::core::array::t_int8;
if (sc == t_int16_string) return bob::core::array::t_int16;
if (sc == t_int32_string) return bob::core::array::t_int32;
if (sc == t_int64_string) return bob::core::array::t_int64;
if (sc == t_uint8_string) return bob::core::array::t_uint8;
if (sc == t_uint16_string) return bob::core::array::t_uint16;
if (sc == t_uint32_string) return bob::core::array::t_uint32;
if (sc == t_uint64_string) return bob::core::array::t_uint64;
if (sc == t_float32_string) return bob::core::array::t_float32;
if (sc == t_float64_string) return bob::core::array::t_float64;
if (sc == t_float128_string) return bob::core::array::t_float128;
if (sc == t_complex64_string) return bob::core::array::t_complex64;
if (sc == t_complex128_string) return bob::core::array::t_complex128;
if (sc == t_complex256_string) return bob::core::array::t_complex256;
return bob::core::array::t_unknown;
}
/**
* @date Sat Apr 9 18:10:10 2011 +0200
* @author Laurent El Shafey <Laurent.El-Shafey@idiap.ch>
*
* @brief This file contains information about the supported arrays
*
* Copyright (C) Idiap Research Institute, Martigny, Switzerland
*/
#ifndef BOB_CORE_ARRAY_TYPE_H
#define BOB_CORE_ARRAY_TYPE_H
#include <stdint.h>
#include <cstdlib>
#include <complex>
/**
* @ingroup CORE_ARRAY
* @brief This macro defines the maximum number of dimensions supported by bob.
* A variable in the bob.core.array namespace is created from this macro
* receiving the same value. Use that variable on your programs, or this macro
* on your preprocessor code.
*/
#define BOB_MAX_DIM 4
namespace bob {
namespace core { namespace array {
/**
* @ingroup CORE_ARRAY
* @{
*/
/**
* @brief Enumeration of the supported type for multidimensional arrays
* @warning float128 and complex256 are defined but currently not
* supported
*/
typedef enum ElementType {
t_unknown=0,
t_bool=1,
t_int8=2,
t_int16=3,
t_int32=4,
t_int64=5,
t_uint8=6,
t_uint16=7,
t_uint32=8,
t_uint64=9,
t_float32=10,
t_float64=11,
t_float128=12,
t_complex64=13,
t_complex128=14,
t_complex256=15
} ElementType;
/**
* @brief Maximum number of supported dimensions for multidimensional
* arrays.
*/
const size_t N_MAX_DIMENSIONS_ARRAY = BOB_MAX_DIM;
/**
* @brief These are some type to element type conversions
*/
template<typename T> ElementType getElementType() {
return t_unknown;
}
/**
* @brief Some specializations that convert type to element type.
*/
template<> inline ElementType getElementType<bool>() { return t_bool; }
template<> inline ElementType getElementType<int8_t>() { return t_int8; }