• API
• FAQ
• Tools
• Archive
daily pastebin goal
57%
SHARE
TWEET

# Untitled

a guest Mar 15th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /// "Each with each other" random range
2. module eerange.randomrange;
3.
4. import std.traits;
5. import std.stdio; //FIXME: remove
6.
7. @safe:
8. //@nogc: //FIXME: enable
9.
10. ///
11. struct EachWithEachOtherRandomAccessRange(R)
12. {
13.     private R srcRange;
14.     const size_t squareWidth;
15.
16.     ///
17.     this(R r)
18.     {
19.         srcRange = r;
20.         squareWidth = srcRange.length + 1;
21.     }
22.
23.     ///
24.     size_t length() const
25.     {
26.         const len = srcRange.length;
27.
28.         return (len * len - len) / 2;
29.     }
30.
31.     private static struct Coords
32.     {
33.         size_t x;
34.         size_t y;
35.     }
36.
37.     private Coords coordsInSquare(size_t idx)
38.     {
39.         Coords ret;
40.         ret.x = idx % squareWidth;
41.         ret.y = idx / squareWidth;
42.
43.         return ret;
44.     }
45.
46.     private auto getElemBySquareCoords(Coords c)
47.     {
48.         // elements shifted relative to entire square
49.         c.x -= 1; // elements shifted relative to entire square
50.         //~ c.y -= 1; // elements shifted relative to entire square
51.         writeln(c);
52.         return [srcRange[c.x], srcRange[c.y]]; // TODO: replace by static array?
53.     }
54.
55.     ///
56.     auto opIndex(const size_t idx)
57.     {
58.         assert(idx < length);
59.
60.         Coords coords = coordsInSquare(idx);
61.
62.         if(coords.x <= coords.y) // under diagonal line?
63.         {
64.             const latestIdx = srcRange.length;
65.
66.             // Mirroring coords
67.             coords.x = latestIdx - coords.x;
68.             coords.y = latestIdx - coords.y - 1; // shifted above diagonal
69.         }
70.
71.         return getElemBySquareCoords(coords);
72.     }
73. }
74.
75. ///
76. auto eeRandomRange(T)(T inputRange)
77. {
78.     return EachWithEachOtherRandomAccessRange!(T)(inputRange);
79. }
80.
81. unittest
82. {
83.     import std.range.primitives;
84.
85.     //~ static assert(isRandomAccessRange!(EachWithEachOtherRandomAccessRange!(int[])));
86. }
87.
88. unittest
89. {
90.     import std.parallelism;
91.     import std.algorithm.searching: count;
92.
93.     int[] arr = [0, 1, 2, 3];
94.
95.     auto r = arr.eeRandomRange;
96.
97.     size_t cnt;
98.
99.     foreach(i; 0 .. r.length)
100.     {
101.         writeln("i=", i);
102.
103.         auto unused = r[i];
104.
105.         cnt++;
106.     }
107.
108.     //~ auto mapped = taskPool.amap!((a){ return a; } )(r);
109.
110.     assert(cnt == 6);
111.     assert(arr.eeRandomRange.length == 6);
112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top