← comparison · doc/array.md

Arrays

Jngen provides a template class TArray<T> which is derived from std::vector<T> and implements all its functionality... and some more handy things like single-argument sorting (a.sort()) , in-place generating of random arrays (Array::random(n, maxValue)) and more.

There are several typedefs for convenience:

typedef TArray<int> Array;
typedef TArray<long long> Array64;
typedef TArray<double> Arrayf;
typedef TArray<std::pair<int, int>> Arrayp;
typedef TArray<TArray<int>> Array2d;

In this document Array will be mostly used instead of TArray<T>. Usually it means that corresponding method works for arrays of any type; if not, it will be mentioned explicitly.

Generators

template<typename ...Args>
static Array Array::random(size_t size, Args... args)

template<typename ...Args>
static Array Array::randomUnique(size_t size, Args... args)

template<typename ...Args>
static Array Array::randomAll(Args... args)

Array::randomUnique(10, 10)

yields a random permutation on 10 elements (though more optimal way is Array::id(10).shuffled());

Arrayp::random(20, 10, 10, dpair)

yields edges of a random graph with 10 vertices and 20 edges, possibly containing multi-edges, but without loops.

template<typename F, typename ...Args>
static Array Array::randomf(size_t size, F func, Args... args)

template<typename F, typename ...Args>
static Array Array::randomfUnique(size_t size, F func, Args... args)

template<typename F, typename ...Args>
static Array Array::randomfAll(F func, Args... args)

TArray<std::string>::randomf(
    10,
    [](const char* pattern) { return rnd.next(pattern); },
    "[a-z]{5}")

yields an array of 10 strings of 5 letters each.

Array Array::id(size_t size, T start = T())

Modifiers

Most of modifiers have two versions: the one which modifies the object itself and the one which returns the modified copy. They are usually named as verb and verb-ed, e.g. shuffle and shuffled.

Array& shuffle()

Array shuffled() const

Array& reverse()

Array reversed() const

Array& sort()

Array sorted() const

template<typename Comp>
Array& sort(Comp&& comp)

template<typename Comp>
Array sorted(Comp&& comp) const

Array& unique()

Array uniqued() const

Array inverse() const

void extend(size_t requiredSize);

Selectors

template<typename Integer>
Array subseq(const std::vector<Integer>& indices) const;

template<typename Integer>
Array subseq(const std::initializer_list<Integer>& indices) const;

a = a.subseq(Array::id(a.size()).shuffled());

effectively shuffles a. For example, this may be used to shuffle several arrays with the same permutation.

T choice() const;

Array choice(size_t count) const;

Array choiceWithRepetition(size_t count) const;

Operators

Array& operator+=(const Array& other);

Array operator+(const Array& other) const;

Array& operator*=(int k);

Array operator*(int k) const;

operator std::string() const;