Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert a slice to a standard D array (T[]) ? #18

Closed
timotheecour opened this issue Feb 23, 2017 · 14 comments
Closed

how to convert a slice to a standard D array (T[]) ? #18

timotheecour opened this issue Feb 23, 2017 · 14 comments

Comments

@timotheecour
Copy link

timotheecour commented Feb 23, 2017

@9il
what do i use for toArray?

auto s=slice!double(3);
double[]s2= s.toArray;

actually not super clear from docs

Also, from old experimental.nslice in https://dlang.org/phobos/std_experimental_ndslice.html:

T median(Range, T)(Slice!(2, Range) sl, T[] buf)
{
    import std.algorithm.sorting : topN;
    // copy sl to the buffer
    size_t n;
    foreach (r; sl)
        foreach (e; r)
            buf[n++] = e;
    buf[0 .. n].topN(n / 2);
    return buf[n / 2];
}

this snippet (copy sl to the buffer) seems to indicate no such utility to flatten elements into an array?

EDIT: OK i guess it's here:
http://docs.algorithm.dlang.io/latest/mir_ndslice_allocation.html

  • somehow i find it hard to find, not sure if i'm the only one.

  • IMO this should be more prominent since we want to make it easy to go from standard D to slices (plenty of examples) as well as opposite direction (very sparse in docs)

  • not sure why median above doesn't use ndarray

  • the docs (Creates a common n-dimensional array from a slice.) are not clear whether it allocates or merely returns a view over the data

@9il
Copy link
Member

9il commented Feb 23, 2017

this snippet (copy sl to the buffer) seems to indicate no such utility to flatten elements into an array?

You can use flattened to use flat representation (it is not an array).
http://docs.algorithm.dlang.io/latest/mir_ndslice_topology.html#.flattened

not sure why median above doesn't use ndarray

median uses preallocated buffer.

@9il
Copy link
Member

9il commented Feb 23, 2017

BTW, for Slice!(Contiguous, packs, T*) you can always do sl.iterator[0 .. sl.elementsCount].

@9il
Copy link
Member

9il commented Feb 23, 2017

... this will return a common D array without allocation

@wilzbach
Copy link
Member

How about adding such common questions into the main README or the wiki?
I hope that @timotheecour won't be the last one to try to transition ;-)

@9il
Copy link
Member

9il commented Feb 24, 2017

Looks good

@timotheecour
Copy link
Author

@9il

  • arrayview

BTW, for Slice!(Contiguous, packs, T*) you can always do sl.iterator[0 .. sl.elementsCount].

IMO this should be exposed as a method, it's a common case to interoperate between mir and non-mir code.

How about:

auto arrayview(S)(S slice) if (isContiguous!S){
  return sl.iterator[0 .. sl.elementsCount];
}
  • ndarray/flattened with existing buffer

not sure why median above doesn't use ndarray
median uses preallocated buffer.

Likewise, ndarray/flattened using a preallocated buffer (or using Appender!T or an arbitrary allocator) is a common case, and should be exposed in the library. What do you suggest?
How about overloads:

auto darray=myslice.ndarray(Mallocator.instance);
auto darray=myslice.flattened(Mallocator.instance);

(or maybe using ndarrayAlloc flattenedAlloc if name clash is an issue)

@wilzbach
Copy link
Member

is there a plan to upgrade the reference example in https://dlang.org/phobos/std_experimental_ndslice.html with the new mir? (hopefully with a better buf[n++] = e; replaced as discussed above)

ndslice will be removed with the next release

@9il
Copy link
Member

9il commented Feb 24, 2017

auto arrayview(S)(S slice) if (isContiguous!S){
return sl.iterator[0 .. sl.elementsCount];
}

auto asArray(size_t[] packs, T)(Slice!(Contiguous, packs, T*) sl)
{
    return sl.unpack.iterator[0..sl.elementsCount];
}

auto darray=myslice.ndarray(Mallocator.instance);
auto darray=myslice.flattened(Mallocator.instance);

makeSlice already allows to create new Contiguous slice with a data from another slice.
Then asArray can be used to get array representation.

hopefully with a better buf[n++] = e; replaced as discussed above

We need other method, something like copy to an array.

@wilzbach
Copy link
Member

Is there a plan to upgrade the reference example in https://dlang.org/phobos/std_experimental_ndslice.html with the new mir?
ndslice will be removed with the next release

Just sent out the request for removal:
dlang/phobos#5187

@timotheecour
Copy link
Author

timotheecour commented Feb 24, 2017

is there a plan to upgrade the reference example in https://dlang.org/phobos/std_experimental_ndslice.html with the new mir? (hopefully with a better buf[n++] = e; replaced as discussed above)

ndslice will be removed with the next release

ya but adding this example (or whatever other similar good example snippet) and upgrading it in the new mir repo

@timotheecour
Copy link
Author

@9il
asArray sounds good assuming all the asX do not allocate as well.

To be clear, that doesn't exist yet right? (couldn't find it)

makeSlice already allows to create new Contiguous slice with a data from another slice.
Then asArray can be used to get array representation.
We need other method, something like copy to an array.

is that the simplest?

import std.algorithm:copy;
myslice.makeSlice.asArray.copy(buffer);

// or something like: myslice.flattened(Allocater from buffer); ?

@9il
Copy link
Member

9il commented Feb 25, 2017

@9il
asArray sounds good assuming all the asX do not allocate as well.

To be clear, that doesn't exist yet right? (couldn't find it)

makeSlice already allows to create new Contiguous slice with a data from another slice.
Then asArray can be used to get array representation.
We need other method, something like copy to an array.

is that the simplest?

import std.algorithm:copy;
myslice.makeSlice.asArray.copy(buffer);

makeSlice allocates data. So you do not need to use it here.

I do not understand the problem with buffer filling and current API. The code in the example will remain the same because the buffer size is very small (like 4x4 pixels), so manual coping will be faster in this case.

Current API allows

buf.ptr.sliced(sl.shape)[] = sl;

This looks pretty clear and simple, IMO

@9il 9il closed this as completed Feb 25, 2017
@timotheecour
Copy link
Author

timotheecour commented Jun 8, 2017

BTW, for Slice!(Contiguous, packs, T*) you can always do sl.iterator[0 .. sl.elementsCount]

should that be simply sl.iterator.array ? (ie a.toArray can be done via a.iterator.array)

@9il
Copy link
Member

9il commented Jun 8, 2017

BTW, for Slice!(Contiguous, packs, T*) you can always do sl.iterator[0 .. sl.elementsCount]
should that be simply sl.iterator.array ? (ie a.toArray can be done via a.iterator.array)

For Slice!(Contiguous, packs, T*) slices it is just sl.field (without data reallocation).

For all slices sl.field.array allocates new array and copies slice data to it, array is from std.array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants