1 #ifndef SASS_SASS_UTIL_H
2 #define SASS_SASS_UTIL_H
14 This is for ports of functions in the Sass:Util module.
19 # Return a Node collection of all possible paths through the given Node collection of Node collections.
21 # @param arrs [NodeCollection<NodeCollection<Node>>]
22 # @return [NodeCollection<NodeCollection<Node>>]
25 # paths([[1, 2], [3, 4], [5]]) #=>
31 Node paths(const Node& arrs, Context& ctx);
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.
38 class DefaultLcsComparator {
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}
53 typedef std::vector<std::vector<int> > LCSTable;
57 This is the equivalent of ruby's Sass::Util.lcs_backtrace.
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
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)
66 if (i == 0 || j == 0) {
67 DEBUG_PRINTLN(LCS, "RETURNING EMPTY")
68 return Node::createCollection();
71 NodeDeque& xChildren = *(x.collection());
72 NodeDeque& yChildren = *(y.collection());
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);
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);
87 DEBUG_PRINTLN(LCS, "FINAL RETURN")
88 return lcs_backtrace(c, x, y, i - 1, j, comparator);
93 This is the equivalent of ruby's Sass::Util.lcs_table.
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
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)
102 NodeDeque& xChildren = *(x.collection());
103 NodeDeque& yChildren = *(y.collection());
105 LCSTable c(xChildren.size(), std::vector<int>(yChildren.size()));
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}
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();
115 if (comparator(xChildren[i], yChildren[j], compareOut)) {
116 c[i][j] = c[i - 1][j - 1] + 1;
118 c[i][j] = std::max(c[i][j - 1], c[i - 1][j]);
128 This is the equivalent of ruby's Sass::Util.lcs.
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`.
134 # @param x [NodeCollection]
135 # @param y [NodeCollection]
136 # @comparator An equality check between elements of `x` and `y`.
137 # @return [NodeCollection] The LCS
139 http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
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)
145 Node newX = Node::createCollection();
146 newX.collection()->push_back(Node::createNil());
149 Node newY = Node::createCollection();
150 newY.collection()->push_back(Node::createNil());
154 lcs_table(newX, newY, comparator, table);
156 return lcs_backtrace(table, newX, newY, static_cast<int>(newX.collection()->size()) - 1, static_cast<int>(newY.collection()->size()) - 1, comparator);
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
166 # Flattens the first `n` nested arrays. If n == -1, all arrays will be flattened
168 # @param arr [NodeCollection] The array to flatten
169 # @param n [int] The number of levels to flatten
170 # @return [NodeCollection] The flattened array
172 Node flatten(Node& arr, Context& ctx, int n = -1);
176 This is the equivalent of ruby's Sass::Util.group_by_to_a.
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
183 # @param enum [Enumerable]
184 # @return [Array<[Object, Array]>] An array of pairs.
186 TODO: update @param and @return once I know what those are.
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.
191 def group_by_to_a(enum, &block)
200 unless order.include?(key)
201 order[key] = order.size
204 if not grouped.has_key?(key) then
211 grouped.each do |key, vals|
212 arr[order[key]] = [key, vals]
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*/) {
222 std::map<unsigned int, KeyType> order;
224 std::map<size_t, std::vector<EnumType> > grouped;
226 for (typename std::vector<EnumType>::iterator enumIter = enumeration.begin(), enumIterEnd = enumeration.end(); enumIter != enumIterEnd; enumIter++) {
227 EnumType& e = *enumIter;
229 KeyType key = keyFunc(e);
231 if (grouped.find(key->hash()) == grouped.end()) {
232 order.insert(std::make_pair((unsigned int)order.size(), key));
234 std::vector<EnumType> newCollection;
235 newCollection.push_back(e);
236 grouped.insert(std::make_pair(key->hash(), newCollection));
238 std::vector<EnumType>& collection = grouped.at(key->hash());
239 collection.push_back(e);
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());
247 std::pair<KeyType, std::vector<EnumType> > grouping = std::make_pair(key, values);
249 arr.push_back(grouping);