Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / sass_util.hpp
1 #ifndef SASS_SASS_UTIL_H
2 #define SASS_SASS_UTIL_H
3
4 #include "ast.hpp"
5 #include "node.hpp"
6 #include "debug.hpp"
7
8 namespace Sass {
9
10
11
12
13   /*
14    This is for ports of functions in the Sass:Util module.
15    */
16
17
18   /*
19     # Return a Node collection of all possible paths through the given Node collection of Node collections.
20     #
21     # @param arrs [NodeCollection<NodeCollection<Node>>]
22     # @return [NodeCollection<NodeCollection<Node>>]
23     #
24     # @example
25     #   paths([[1, 2], [3, 4], [5]]) #=>
26     #     # [[1, 3, 5],
27     #     #  [2, 3, 5],
28     #     #  [1, 4, 5],
29     #     #  [2, 4, 5]]
30   */
31   Node paths(const Node& arrs, Context& ctx);
32
33
34   /*
35   This class is a default implementation of a Node comparator that can be passed to the lcs function below.
36   It uses operator== for equality comparision. It then returns one if the Nodes are equal.
37   */
38   class DefaultLcsComparator {
39   public:
40     bool operator()(const Node& one, const Node& two, Node& out) const {
41       // TODO: Is this the correct C++ interpretation?
42       // block ||= proc {|a, b| a == b && a}
43       if (one == two) {
44         out = one;
45         return true;
46       }
47
48       return false;
49     }
50   };
51
52
53   typedef std::vector<std::vector<int> > LCSTable;
54
55
56   /*
57   This is the equivalent of ruby's Sass::Util.lcs_backtrace.
58
59   # Computes a single longest common subsequence for arrays x and y.
60   # Algorithm from http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Reading_out_an_LCS
61   */
62   template<typename ComparatorType>
63   Node lcs_backtrace(const LCSTable& c, const Node& x, const Node& y, int i, int j, const ComparatorType& comparator) {
64     DEBUG_PRINTLN(LCS, "LCSBACK: X=" << x << " Y=" << y << " I=" << i << " J=" << j)
65
66     if (i == 0 || j == 0) {
67       DEBUG_PRINTLN(LCS, "RETURNING EMPTY")
68       return Node::createCollection();
69     }
70
71     NodeDeque& xChildren = *(x.collection());
72     NodeDeque& yChildren = *(y.collection());
73
74     Node compareOut = Node::createNil();
75     if (comparator(xChildren[i], yChildren[j], compareOut)) {
76       DEBUG_PRINTLN(LCS, "RETURNING AFTER ELEM COMPARE")
77       Node result = lcs_backtrace(c, x, y, i - 1, j - 1, comparator);
78       result.collection()->push_back(compareOut);
79       return result;
80     }
81
82     if (c[i][j - 1] > c[i - 1][j]) {
83       DEBUG_PRINTLN(LCS, "RETURNING AFTER TABLE COMPARE")
84       return lcs_backtrace(c, x, y, i, j - 1, comparator);
85     }
86
87     DEBUG_PRINTLN(LCS, "FINAL RETURN")
88     return lcs_backtrace(c, x, y, i - 1, j, comparator);
89   }
90
91
92   /*
93   This is the equivalent of ruby's Sass::Util.lcs_table.
94
95   # Calculates the memoization table for the Least Common Subsequence algorithm.
96   # Algorithm from http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS
97   */
98   template<typename ComparatorType>
99   void lcs_table(const Node& x, const Node& y, const ComparatorType& comparator, LCSTable& out) {
100     DEBUG_PRINTLN(LCS, "LCSTABLE: X=" << x << " Y=" << y)
101
102     NodeDeque& xChildren = *(x.collection());
103     NodeDeque& yChildren = *(y.collection());
104
105     LCSTable c(xChildren.size(), std::vector<int>(yChildren.size()));
106
107     // These shouldn't be necessary since the vector will be initialized to 0 already.
108     // x.size.times {|i| c[i][0] = 0}
109     // y.size.times {|j| c[0][j] = 0}
110
111     for (size_t i = 1; i < xChildren.size(); i++) {
112       for (size_t j = 1; j < yChildren.size(); j++) {
113         Node compareOut = Node::createNil();
114
115         if (comparator(xChildren[i], yChildren[j], compareOut)) {
116           c[i][j] = c[i - 1][j - 1] + 1;
117         } else {
118           c[i][j] = std::max(c[i][j - 1], c[i - 1][j]);
119         }
120       }
121     }
122
123     out = c;
124   }
125
126
127   /*
128   This is the equivalent of ruby's Sass::Util.lcs.
129
130   # Computes a single longest common subsequence for `x` and `y`.
131   # If there are more than one longest common subsequences,
132   # the one returned is that which starts first in `x`.
133
134   # @param x [NodeCollection]
135   # @param y [NodeCollection]
136   # @comparator An equality check between elements of `x` and `y`.
137   # @return [NodeCollection] The LCS
138
139   http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
140   */
141   template<typename ComparatorType>
142   Node lcs(Node& x, Node& y, const ComparatorType& comparator, Context& ctx) {
143     DEBUG_PRINTLN(LCS, "LCS: X=" << x << " Y=" << y)
144
145     Node newX = Node::createCollection();
146     newX.collection()->push_back(Node::createNil());
147     newX.plus(x);
148
149     Node newY = Node::createCollection();
150     newY.collection()->push_back(Node::createNil());
151     newY.plus(y);
152
153     LCSTable table;
154     lcs_table(newX, newY, comparator, table);
155
156     return lcs_backtrace(table, newX, newY, static_cast<int>(newX.collection()->size()) - 1, static_cast<int>(newY.collection()->size()) - 1, comparator);
157   }
158
159
160   /*
161   This is the equivalent of ruby sass' Sass::Util.flatten and [].flatten.
162   Sass::Util.flatten requires the number of levels to flatten, while
163   [].flatten doesn't and will flatten the entire array. This function
164   supports both.
165
166   # Flattens the first `n` nested arrays. If n == -1, all arrays will be flattened
167   #
168   # @param arr [NodeCollection] The array to flatten
169   # @param n [int] The number of levels to flatten
170   # @return [NodeCollection] The flattened array
171   */
172   Node flatten(Node& arr, Context& ctx, int n = -1);
173
174
175   /*
176   This is the equivalent of ruby's Sass::Util.group_by_to_a.
177
178   # Performs the equivalent of `enum.group_by.to_a`, but with a guaranteed
179   # order. Unlike [#hash_to_a], the resulting order isn't sorted key order;
180   # instead, it's the same order as `#group_by` has under Ruby 1.9 (key
181   # appearance order).
182   #
183   # @param enum [Enumerable]
184   # @return [Array<[Object, Array]>] An array of pairs.
185
186   TODO: update @param and @return once I know what those are.
187
188   The following is the modified version of the ruby code that was more portable to C++. You
189   should be able to drop it into ruby 3.2.19 and get the same results from ruby sass.
190
191     def group_by_to_a(enum, &block)
192       order = {}
193
194       arr = []
195
196       grouped = {}
197
198       for e in enum do
199         key = block[e]
200         unless order.include?(key)
201           order[key] = order.size
202         end
203
204         if not grouped.has_key?(key) then
205           grouped[key] = [e]
206         else
207           grouped[key].push(e)
208         end
209       end
210
211       grouped.each do |key, vals|
212         arr[order[key]] = [key, vals]
213       end
214
215       arr
216     end
217
218   */
219   template<typename EnumType, typename KeyType, typename KeyFunctorType>
220   void group_by_to_a(std::vector<EnumType>& enumeration, KeyFunctorType& keyFunc, std::vector<std::pair<KeyType, std::vector<EnumType> > >& arr /*out*/) {
221
222     std::map<unsigned int, KeyType> order;
223
224     std::map<size_t, std::vector<EnumType> > grouped;
225
226     for (typename std::vector<EnumType>::iterator enumIter = enumeration.begin(), enumIterEnd = enumeration.end(); enumIter != enumIterEnd; enumIter++) {
227       EnumType& e = *enumIter;
228
229       KeyType key = keyFunc(e);
230
231       if (grouped.find(key->hash()) == grouped.end()) {
232         order.insert(std::make_pair((unsigned int)order.size(), key));
233
234         std::vector<EnumType> newCollection;
235         newCollection.push_back(e);
236         grouped.insert(std::make_pair(key->hash(), newCollection));
237       } else {
238         std::vector<EnumType>& collection = grouped.at(key->hash());
239         collection.push_back(e);
240       }
241     }
242
243     for (unsigned int index = 0; index < order.size(); index++) {
244       KeyType& key = order.at(index);
245       std::vector<EnumType>& values = grouped.at(key->hash());
246
247       std::pair<KeyType, std::vector<EnumType> > grouping = std::make_pair(key, values);
248
249       arr.push_back(grouping);
250     }
251   }
252
253
254 }
255
256 #endif