8 # This is the equivalent of ruby's Sass::Util.paths.
10 # Return an array of all possible paths through the given arrays.
12 # @param arrs [NodeCollection<NodeCollection<Node>>]
13 # @return [NodeCollection<NodeCollection<Node>>]
16 # paths([[1, 2], [3, 4], [5]]) #=>
22 The following is the modified version of the ruby code that was more portable to C++. You
23 should be able to drop it into ruby 3.2.19 and get the same results from ruby sass.
26 // I changed the inject and maps to an iterative approach to make it easier to implement in C++
32 for path in loopStart do
33 permutations.push(path + [e])
36 loopStart = permutations
40 Node paths(const Node& arrs, Context& ctx) {
42 Node loopStart = Node::createCollection();
43 loopStart.collection()->push_back(Node::createCollection());
45 for (NodeDeque::iterator arrsIter = arrs.collection()->begin(), arrsEndIter = arrs.collection()->end();
46 arrsIter != arrsEndIter; ++arrsIter) {
48 Node& arr = *arrsIter;
50 Node permutations = Node::createCollection();
52 for (NodeDeque::iterator arrIter = arr.collection()->begin(), arrIterEnd = arr.collection()->end();
53 arrIter != arrIterEnd; ++arrIter) {
57 for (NodeDeque::iterator loopStartIter = loopStart.collection()->begin(), loopStartIterEnd = loopStart.collection()->end();
58 loopStartIter != loopStartIterEnd; ++loopStartIter) {
60 Node& path = *loopStartIter;
62 Node newPermutation = Node::createCollection();
63 newPermutation.got_line_feed = arr.got_line_feed;
64 newPermutation.plus(path);
65 newPermutation.collection()->push_back(e);
67 permutations.collection()->push_back(newPermutation);
71 loopStart = permutations;
79 This is the equivalent of ruby sass' Sass::Util.flatten and [].flatten.
80 Sass::Util.flatten requires the number of levels to flatten, while
81 [].flatten doesn't and will flatten the entire array. This function
84 # Flattens the first `n` nested arrays. If n == -1, all arrays will be flattened
86 # @param arr [NodeCollection] The array to flatten
87 # @param n [int] The number of levels to flatten
88 # @return [NodeCollection] The flattened array
90 The following is the modified version of the ruby code that was more portable to C++. You
91 should be able to drop it into ruby 3.2.19 and get the same results from ruby sass.
93 def flatten(arr, n = -1)
94 if n != -1 and n == 0 then
101 if e.is_a?(Array) then
102 flattened.concat(flatten(e, n - 1))
111 Node flatten(Node& arr, Context& ctx, int n) {
112 if (n != -1 && n == 0) {
116 Node flattened = Node::createCollection();
117 if (arr.got_line_feed) flattened.got_line_feed = true;
119 for (NodeDeque::iterator iter = arr.collection()->begin(), iterEnd = arr.collection()->end();
120 iter != iterEnd; iter++) {
124 if (e.isCollection()) {
126 // e.collection().got_line_feed = e.got_line_feed;
127 Node recurseFlattened = flatten(e, ctx, n - 1);
129 if(e.got_line_feed) {
130 flattened.got_line_feed = e.got_line_feed;
131 recurseFlattened.got_line_feed = e.got_line_feed;
134 for(auto i : (*recurseFlattened.collection())) {
135 if (recurseFlattened.got_line_feed) {
137 i.got_line_feed = true;
139 flattened.collection()->push_back(i);
143 flattened.collection()->push_back(e);