0

I want to hide multiple elements in a valarray<int> which has consecutive integers starting from 0. For example, from {0, 1, 2, 3, 4, 5} to {0, 2, 3, 5}. I have found that I can use indirect array to specify elements indices with valarray<size_t>. However, I don't know how to generate valarray<size_t> with indices I want in O(1) complexity. O(1) complexity or at most O(logn) complexity is very important to me. So, I think gslice may be able to solve the problem, but I still can't figure out how to implement it.

Note: I use c++11

cigien
  • 57,834
  • 11
  • 73
  • 112
  • I'm not sure what you mean by `O(1)`.Wouldn't you need to make at least a single pass over the original `valarry` to generate a new one? – cigien Nov 07 '20 at 15:54
  • https://en.cppreference.com/w/cpp/numeric/valarray/gslice, it says `gslice` in `valarray` has constant complexity. – Austin Chen Nov 07 '20 at 15:59
  • The only mention of constant complexity on that page is for `start`, `size` and `stride`, not for the constructor of `gslice`. – cigien Nov 07 '20 at 16:02
  • But I have tested `gslice` with very large `valarray`, and it always shows 0ms. – Austin Chen Nov 07 '20 at 16:05
  • Actual run-time is not the same thing as algorithmic complexity. Run the program with increasing sizes of `valarray`s and plot the corresponding time taken. The change in the time taken is what you need to look at. Otherwise, the constant factors in the complexity bounds will completely mess up your analysis. – cigien Nov 07 '20 at 16:08
  • I just tested `gslice` on `valarray` with sizes differ from 9, 90, 900, ..., 90000000. They all showed 0 nanoseconds. I think gslice is really constant in complexity. The gslice I used has 3 as start index, {19, 4, 1} as sizes, {2, 4, 3} as strides. – Austin Chen Nov 07 '20 at 16:26
  • Increase your `gslice` sizes much more. Make them proportional to the sizes of the valarrys from which you're constructing them, and see if that changes anything. – cigien Nov 07 '20 at 16:29
  • Now the `strides` are all {2, 4, 3}, and the start indices are all 3. `valarray` pairs to gslice with same size of `sizes` (filled with random numbers varied from 0 to `the size`). The results are: sizes 9 <-> 0 nanoseconds, sizes 90 <-> 0 nanoseconds, sizes 900 <-> 0 nanoseconds, sizes 9000 <-> 0 nanoseconds, sizes 90000 <-> 1000000 nanoseconds, sizes 900000 <-> 9996000 nanoseconds, sizes 9000000 <-> 59965000 nanoseconds. – Austin Chen Nov 07 '20 at 16:54
  • Yeah, looks at least linear, i.e. `O(n)` to me. – cigien Nov 07 '20 at 16:58
  • What if it's possible to hide specific indices with gslice of small sizes. – Austin Chen Nov 07 '20 at 17:00
  • I'm not sure what you mean exactly. Sure, you could `gslice` with small sizes. It would still be `O(n)` though, but it wouldn't matter because `n` is small. – cigien Nov 07 '20 at 18:00
  • Yeah, that is exactly what I meant. – Austin Chen Nov 07 '20 at 18:18

0 Answers0