Skip to content

Commit

Permalink
Renaming for explicitness, brevity
Browse files Browse the repository at this point in the history
  • Loading branch information
jonclayden committed Jun 17, 2019
1 parent d39a0f5 commit 4a739ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
46 changes: 23 additions & 23 deletions inst/include/lib/NiftiImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class NiftiImageData
virtual ~TypeHandler() {}
virtual size_t size () const { return 0; }
virtual bool hasNaN () const { return false; }
virtual double doubleValue (void *ptr) const = 0;
virtual int intValue (void *ptr) const = 0;
virtual void doubleValue (void *ptr, double value) const = 0;
virtual void intValue (void *ptr, int value) const = 0;
virtual double getDouble (void *ptr) const = 0;
virtual int getInt (void *ptr) const = 0;
virtual void setDouble (void *ptr, const double value) const = 0;
virtual void setInt (void *ptr, const int value) const = 0;
virtual void minmax (void *ptr, const size_t length, double *min, double *max) const = 0;
};

Expand All @@ -79,10 +79,10 @@ class NiftiImageData
{
size_t size () const { return (sizeof(Type)); }
bool hasNaN () const { return std::numeric_limits<Type>::has_quiet_NaN; }
double doubleValue (void *ptr) const { return static_cast<double>(*static_cast<Type*>(ptr)); }
int intValue (void *ptr) const { return static_cast<int>(*static_cast<Type*>(ptr)); }
void doubleValue (void *ptr, double value) const { *(static_cast<Type*>(ptr)) = static_cast<Type>(value); }
void intValue (void *ptr, int value) const { *(static_cast<Type*>(ptr)) = static_cast<Type>(value); }
double getDouble (void *ptr) const { return static_cast<double>(*static_cast<Type*>(ptr)); }
int getInt (void *ptr) const { return static_cast<int>(*static_cast<Type*>(ptr)); }
void setDouble (void *ptr, const double value) const { *(static_cast<Type*>(ptr)) = static_cast<Type>(value); }
void setInt (void *ptr, const int value) const { *(static_cast<Type*>(ptr)) = static_cast<Type>(value); }
void minmax (void *ptr, const size_t length, double *min, double *max) const;
};

Expand Down Expand Up @@ -180,7 +180,7 @@ class NiftiImageData
/**
* Inner class representing a single element in the data blob
**/
struct ElementProxy
struct Element
{
private:
const NiftiImageData &parent;
Expand All @@ -193,7 +193,7 @@ class NiftiImageData
* @param ptr An opaque pointer to the element. If \c NULL, the start of the data blob
* encapsulated by the parent will be used
**/
ElementProxy (const NiftiImageData &parent, void *ptr = NULL)
Element (const NiftiImageData &parent, void *ptr = NULL)
: parent(parent)
{
this->ptr = (ptr == NULL ? parent.dataPtr : ptr);
Expand All @@ -207,14 +207,14 @@ class NiftiImageData
* @return A reference to the callee
**/
template <typename SourceType>
ElementProxy & operator= (const SourceType &value);
Element & operator= (const SourceType &value);

/**
* Copy assignment operator
* @param other Another data element
* @return A reference to the callee
**/
ElementProxy & operator= (const ElementProxy &other);
Element & operator= (const Element &other);

/**
* Implicit type-cast operator, suitable for implicit conversion to basic numeric types
Expand All @@ -223,18 +223,18 @@ class NiftiImageData
operator TargetType() const
{
if (parent.isScaled())
return TargetType(parent.handler->doubleValue(ptr) * parent.slope + parent.intercept);
return TargetType(parent.handler->getDouble(ptr) * parent.slope + parent.intercept);
else if (std::numeric_limits<TargetType>::is_integer)
return TargetType(parent.handler->intValue(ptr));
return TargetType(parent.handler->getInt(ptr));
else
return TargetType(parent.handler->doubleValue(ptr));
return TargetType(parent.handler->getDouble(ptr));
}
};

/**
* Iterator type for \c NiftiImageData, with \c ElementProxy as its value type
* Iterator type for \c NiftiImageData, with \c Element as its value type
**/
class Iterator : public std::iterator<std::random_access_iterator_tag, ElementProxy>
class Iterator : public std::iterator<std::random_access_iterator_tag, Element>
{
private:
const NiftiImageData &parent;
Expand Down Expand Up @@ -289,10 +289,10 @@ class NiftiImageData
bool operator> (const Iterator &other) const { return (ptr > other.ptr); }
bool operator< (const Iterator &other) const { return (ptr < other.ptr); }

const ElementProxy operator* () const { return ElementProxy(parent, ptr); }
ElementProxy operator* () { return ElementProxy(parent, ptr); }
const ElementProxy operator[] (const size_t i) const { return ElementProxy(parent, static_cast<char*>(ptr) + (i * step)); }
ElementProxy operator[] (const size_t i) { return ElementProxy(parent, static_cast<char*>(ptr) + (i * step)); }
const Element operator* () const { return Element(parent, ptr); }
Element operator* () { return Element(parent, ptr); }
const Element operator[] (const size_t i) const { return Element(parent, static_cast<char*>(ptr) + (i * step)); }
Element operator[] (const size_t i) { return Element(parent, static_cast<char*>(ptr) + (i * step)); }
};

/**
Expand Down Expand Up @@ -467,14 +467,14 @@ class NiftiImageData
* @param i Index value, where the first dimension moves fastest
* @return Constant element proxy type
**/
const ElementProxy operator[] (const size_t i) const { return ElementProxy(*this, static_cast<char*>(dataPtr) + (i * bytesPerPixel())); }
const Element operator[] (const size_t i) const { return Element(*this, static_cast<char*>(dataPtr) + (i * bytesPerPixel())); }

/**
* Indexing operator, returning a mutable element
* @param i Index value, where the first dimension moves fastest
* @return Mutable element proxy type
**/
ElementProxy operator[] (const size_t i) { return ElementProxy(*this, static_cast<char*>(dataPtr) + (i * bytesPerPixel())); }
Element operator[] (const size_t i) { return Element(*this, static_cast<char*>(dataPtr) + (i * bytesPerPixel())); }

/**
* Calculate the minimum and maximum values in the blob, as doubles
Expand Down
20 changes: 10 additions & 10 deletions inst/include/lib/NiftiImage_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,45 +285,45 @@ inline void NiftiImageData::ConcreteTypeHandler<Type>::minmax (void *ptr, const
}

template <typename SourceType>
inline NiftiImageData::ElementProxy & NiftiImageData::ElementProxy::operator= (const SourceType &value)
inline NiftiImageData::Element & NiftiImageData::Element::operator= (const SourceType &value)
{
if (internal::isNaN(value))
{
if (!parent.handler->hasNaN())
{
const double zeroValue = parent.isScaled() ? (-parent.intercept / parent.slope) : 0.0;
if (parent.isFloatingPoint())
parent.handler->doubleValue(ptr, zeroValue);
parent.handler->setDouble(ptr, zeroValue);
else
parent.handler->intValue(ptr, static_cast<int>(internal::roundEven(zeroValue)));
parent.handler->setInt(ptr, static_cast<int>(internal::roundEven(zeroValue)));
}
#ifdef USING_R
// Only happens for integer types that admit an NaN/NA value.
// In practice this means int specifically for R, so we don't
// need to worry about the effect of casting INT_MIN to a wider
// or narrower type
else if (parent.isInteger())
parent.handler->intValue(ptr, NA_INTEGER);
parent.handler->setInt(ptr, NA_INTEGER);
#endif
else
parent.handler->doubleValue(ptr, std::numeric_limits<double>::quiet_NaN());
parent.handler->setDouble(ptr, std::numeric_limits<double>::quiet_NaN());
}
else if (parent.isScaled())
{
double reverseScaledValue = (static_cast<double>(value) - parent.intercept) / parent.slope;
if (parent.isFloatingPoint())
parent.handler->doubleValue(ptr, reverseScaledValue);
parent.handler->setDouble(ptr, reverseScaledValue);
else
parent.handler->intValue(ptr, static_cast<int>(internal::roundEven(reverseScaledValue)));
parent.handler->setInt(ptr, static_cast<int>(internal::roundEven(reverseScaledValue)));
}
else if (std::numeric_limits<SourceType>::is_integer)
parent.handler->intValue(ptr, static_cast<int>(value));
parent.handler->setInt(ptr, static_cast<int>(value));
else
parent.handler->doubleValue(ptr, static_cast<double>(value));
parent.handler->setDouble(ptr, static_cast<double>(value));
return *this;
}

inline NiftiImageData::ElementProxy & NiftiImageData::ElementProxy::operator= (const NiftiImageData::ElementProxy &other)
inline NiftiImageData::Element & NiftiImageData::Element::operator= (const NiftiImageData::Element &other)
{
if (other.parent.isScaled() || other.parent.isFloatingPoint())
{
Expand Down

0 comments on commit 4a739ff

Please sign in to comment.