Jump to content

Rank (computer programming): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Reformat code, add rank_v and fix rankof
Line 8: Line 8:


<source lang="cpp">
<source lang="cpp">
#include <type_traits>
#include <cstddef>
#include <cstddef>
Line 16: Line 17:
* it is an array; zero otherwise (which is the usual convention)
* it is an array; zero otherwise (which is the usual convention)
*/
*/
template <typename t> struct rank
template <typename T> struct rank
{
{ static const std::size_t value = 0; };
static const std::size_t value = 0;
};


template<typename t, std::size_t n>
template<typename T, std::size_t N>
struct rank<t[n]>
struct rank<T[N]>
{
{ static const std::size_t value = 1 + rank<t>::value; };
static const std::size_t value = 1 + rank<T>::value;
};

template <typename T>
constexpr auto rank_v = rank<T>::value;


/* Rank of an expression
/* Rank of an expression
Line 27: Line 35:
* Let the rank of an expression be the rank of its type
* Let the rank of an expression be the rank of its type
*/
*/

template <typename t, std::size_t n>
template <typename T>
char(&rankof(t(&)[n]))[n];
using unqualified_t = std::remove_cv_t<std::remove_reference_t<T>>;

template <typename T>
auto rankof(T&& expr)
{
return rank_v<unqualified_t<T>>;
}
</source>
</source>
Line 35: Line 50:
:<source lang="cpp">rank<T>::value</source>
:<source lang="cpp">rank<T>::value</source>
or the shorter form
and the rank of an array-expression ''expr'' by

:<source lang="cpp">rank_v<T></source>

Calculating the rank of an expression can be done using
:<source lang="cpp">sizeof(rankof(expr))</source>
:<source lang="cpp">rankof(expr)</source>


==See also==
==See also==

Revision as of 06:34, 29 October 2017

In computer programming, rank with no further specifications is usually a synonym for (or refers to) "number of dimensions"; thus, a two-dimensional array has rank two, a three-dimensional array has rank three and so on. Strictly, no formal definition can be provided which applies to every programming language, since each of them has its own concepts, semantics and terminology; the term may not even be applicable or, to the contrary, applied with a very specific meaning in the context of a given language.

In the case of APL the notion applies to every operand; and dyads ("binary functions") have a left rank and a right rank.

The box below instead shows how rank of a type and rank of an array expression could be defined (in a semi-formal style) for C++ and illustrates a simple way to calculate them at compile time.

#include <type_traits>
#include <cstddef>
 
/* Rank of a type
 * -------------
 *
 * Let the rank of a type T be the number of its dimensions if
 * it is an array; zero otherwise (which is the usual convention)
 */
template <typename T> struct rank
{
    static const std::size_t value = 0;
};

template<typename T, std::size_t N>
struct rank<T[N]>
{
    static const std::size_t value = 1 + rank<T>::value;
};

template <typename T>
constexpr auto rank_v = rank<T>::value;

/* Rank of an expression
 *
 * Let the rank of an expression be the rank of its type
 */

template <typename T>
using unqualified_t = std::remove_cv_t<std::remove_reference_t<T>>; 

template <typename T>
auto rankof(T&& expr)
{
    return rank_v<unqualified_t<T>>;
}

Given the code above the rank of a type T can be calculated at compile time by

rank<T>::value

or the shorter form

rank_v<T>

Calculating the rank of an expression can be done using

rankof(expr)

See also