Questions tagged [array-view]

An array_view is a C++ class template that is a non-owning reference to an array.

The C++ class template array_view describes an object that can refer to a constant contiguous sequence of objects with the first element of the sequence at position zero.

An array_view is a potentially multidimensional view on a sequence of uniformly strided objects of a uniform type, contiguous in the least significant dimension.

14 questions
103
votes
3 answers

What's the difference between span and array_view in the gsl library?

In several recent conference presentation I've heard Bjarne Stroustrup and others mention new coding guidelines for C++ and some types supporting them. Specifically, I remember the example of span instead of (T* p, int n) as a parameter to a…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
8
votes
4 answers

C++: std::vector - It is possible to "slice" a vector?

I am writing some code to integrate ODE's. This question is as much a request for coding advice as for a solution, so if you have an alternative suggestion to the one I am about to offer please let me know! The "objects" to be integrated by the ODE…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
7
votes
7 answers

Interpret a std::string as a std::vector of char_type?

I have a template function that takes a const vector&. In said function, I have vectors cbegin(), cend(), size(), and operator[]. As far as I understand it, both string and vector use contiguous space, so I was wondering if I could…
Anzurio
  • 16,780
  • 3
  • 39
  • 49
5
votes
2 answers

array_view alternative for maps, sets, etc

Let's assume I have some class hierarchy that has a couple of virtual functions returning a container reference: #include #include #include #include #include class Interface { public: virtual…
Rostislav
  • 3,857
  • 18
  • 30
3
votes
4 answers

equivalent of int[] + k on vector in c++

I have an algorithm and I'd like to translate my code so instead of using arrays I'd like to use vectors. How would you translate this: (the side of b + j and a) find_kth(a, b + j, i, size_b - j, k - j); where int find_kth(int a[], int b[], int…
2
votes
1 answer

Function template specialization for both view and strided view and const nightmare

I define a class array_view and a class strided_view (think about array_view and strided_array_view http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0122r0.pdf) and i would like to specialize the way i could iterate on it for…
t Retornaz
  • 23
  • 3
2
votes
1 answer

Creating views of user types in julia

The new julia 0.5 has improved support for views of arrays. Is it possible to use this functionality to allow views of custom types? E.g. so I could immutable test a::Vector{Int} b::Vector{Int} end then define a getviewfunction that would…
Michael K. Borregaard
  • 7,864
  • 1
  • 28
  • 35
2
votes
1 answer

gsl::array_view> from std::vector

Assume I have a member variable std::vector in a class and I want to return it from a member function as an immutable view using a combination of gsl::array_view and gsl::cstring_view. Unfortunately, the following doesn't compile: class…
Rostislav
  • 3,857
  • 18
  • 30
2
votes
1 answer

SFINAE-ing any container into a c-style array view

I'm making a simple, non-owning array view class: template class array_view { T* data_; size_t len_; // ... }; I want to construct it from any container that has data() and size() member functions, but SFINAE-d correctly…
Barry
  • 286,269
  • 29
  • 621
  • 977
2
votes
4 answers

C++ creating a 2D array using the size of a given vector, in a memory-safe manner

How do I achieve the following: std::vector vec = { 1, 2, 3 }; const int N = vec.size(); // Now create NxN 2D array. First, I know I could do it with new but I'd have to remember to delete it later, and I'd rather not have to handle…
Ray
  • 7,833
  • 13
  • 57
  • 91
1
vote
1 answer

Python: modifying single dictionary item containing an array view modifies all items

I have two dictionaries with same keys. Each item is an ndarray. from numpy import zeros, random from collections import namedtuple PhaseAmplitude = namedtuple('PhaseAmplitude','phase amplitude') dict_keys = {'K1','K2', 'K3'} J1 =…
1
vote
2 answers

Pointer arithmetic with array_view

I've just seen the first part of Herb Sutter's "Writing Good C++14... By Default" (https://www.youtube.com/watch?v=hEx5DNLWGgA) and i have a (possible stupid) question regarding array_view. The presented case was sending an array_view instead of a…
Andrei
  • 100
  • 2
  • 11
0
votes
0 answers

AttributeError: 'ArrayView' object has no attribute 'A1'

I have to import a processed h5ad file, but it seems that X has been passed as a numpy array instead of a numpy matrix. See below: # Read the data data_path = "/home/bbb5130/snOMICS/maria/msrna.h5ad" adata = sn.pp.read_h5ad(data_path,…
Maria
  • 1
0
votes
1 answer

Return 5 items each time sequentially from array

I want to return first 5 items from array. Once first 5 items are returned, then if I reexecute my function then it can return second 5 items from same array and so on. How can I do this? $myArray =…