简体   繁体   中英

Selecting elements from vector by index range

Despite some googling I can't find anything about how to do this.

If I want to take some elements of an array I can do

var partOfArray = myArray[firstIndex..lastIndex];
var anotherpartOfArray = myArray[firstIndex..^1];

Can I do the same for something of type Vector? Something like

// Vector<double> myVector; 
var partOfVector = myVector(firstIndex..lastIndex);

Currently my only workaround is to convert the vector back to an array, index it, then convert it back to a vector. There must be an easier way to do this.

This does not seem to be available due to the "Countable" requirement for using implicit range indexing :

  1. The type is Countable.
  2. The type has an accessible instance indexer which takes a single int as the argument.
  3. The type does not have an accessible instance indexer which takes an Index as the first parameter. The Index must be the only parameter or the remaining parameters must be optional.

"Countable" in this context means having a Count or Length instance property, Vector<T> only have a static Count property.

Note that Vector<T> is intended for high performance SIMD optimizations, ie it is not equivalent to a c++ vector, and this unfortunately often means convenience will be sacrificed for performance. If you are converting the vector back to an array and back again will probably significantly reduce the benefit of vectorization. It is difficult to provide better suggestions without knowing the specific application.

I would also suggest reading about SIMD intrinstics . My takeaway from that article is that the gains from Vector<T> is rather modest compared to that of intrinstics, even if the later ends up being platform specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM