Blame src/sass_util.hpp

Packit bfcc33
#ifndef SASS_SASS_UTIL_H
Packit bfcc33
#define SASS_SASS_UTIL_H
Packit bfcc33
Packit bfcc33
#include "ast.hpp"
Packit bfcc33
#include "node.hpp"
Packit bfcc33
#include "debug.hpp"
Packit bfcc33
Packit bfcc33
namespace Sass {
Packit bfcc33
Packit bfcc33
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
   This is for ports of functions in the Sass:Util module.
Packit bfcc33
   */
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
    # Return a Node collection of all possible paths through the given Node collection of Node collections.
Packit bfcc33
    #
Packit bfcc33
    # @param arrs [NodeCollection<NodeCollection<Node>>]
Packit bfcc33
    # @return [NodeCollection<NodeCollection<Node>>]
Packit bfcc33
    #
Packit bfcc33
    # @example
Packit bfcc33
    #   paths([[1, 2], [3, 4], [5]]) #=>
Packit bfcc33
    #     # [[1, 3, 5],
Packit bfcc33
    #     #  [2, 3, 5],
Packit bfcc33
    #     #  [1, 4, 5],
Packit bfcc33
    #     #  [2, 4, 5]]
Packit bfcc33
  */
Packit bfcc33
  Node paths(const Node& arrs);
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
  This class is a default implementation of a Node comparator that can be passed to the lcs function below.
Packit bfcc33
  It uses operator== for equality comparision. It then returns one if the Nodes are equal.
Packit bfcc33
  */
Packit bfcc33
  class DefaultLcsComparator {
Packit bfcc33
  public:
Packit bfcc33
    bool operator()(const Node& one, const Node& two, Node& out) const {
Packit bfcc33
      // TODO: Is this the correct C++ interpretation?
Packit bfcc33
      // block ||= proc {|a, b| a == b && a}
Packit bfcc33
      if (one == two) {
Packit bfcc33
        out = one;
Packit bfcc33
        return true;
Packit bfcc33
      }
Packit bfcc33
Packit bfcc33
      return false;
Packit bfcc33
    }
Packit bfcc33
  };
Packit bfcc33
Packit bfcc33
Packit bfcc33
  typedef std::vector<std::vector<int> > LCSTable;
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
  This is the equivalent of ruby's Sass::Util.lcs_backtrace.
Packit bfcc33
Packit bfcc33
  # Computes a single longest common subsequence for arrays x and y.
Packit bfcc33
  # Algorithm from http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Reading_out_an_LCS
Packit bfcc33
  */
Packit bfcc33
  template<typename ComparatorType>
Packit bfcc33
  Node lcs_backtrace(const LCSTable& c, const Node& x, const Node& y, int i, int j, const ComparatorType& comparator) {
Packit bfcc33
    DEBUG_PRINTLN(LCS, "LCSBACK: X=" << x << " Y=" << y << " I=" << i << " J=" << j)
Packit bfcc33
Packit bfcc33
    if (i == 0 || j == 0) {
Packit bfcc33
      DEBUG_PRINTLN(LCS, "RETURNING EMPTY")
Packit bfcc33
      return Node::createCollection();
Packit bfcc33
    }
Packit bfcc33
Packit bfcc33
    NodeDeque& xChildren = *(x.collection());
Packit bfcc33
    NodeDeque& yChildren = *(y.collection());
Packit bfcc33
Packit bfcc33
    Node compareOut = Node::createNil();
Packit bfcc33
    if (comparator(xChildren[i], yChildren[j], compareOut)) {
Packit bfcc33
      DEBUG_PRINTLN(LCS, "RETURNING AFTER ELEM COMPARE")
Packit bfcc33
      Node result = lcs_backtrace(c, x, y, i - 1, j - 1, comparator);
Packit bfcc33
      result.collection()->push_back(compareOut);
Packit bfcc33
      return result;
Packit bfcc33
    }
Packit bfcc33
Packit bfcc33
    if (c[i][j - 1] > c[i - 1][j]) {
Packit bfcc33
      DEBUG_PRINTLN(LCS, "RETURNING AFTER TABLE COMPARE")
Packit bfcc33
      return lcs_backtrace(c, x, y, i, j - 1, comparator);
Packit bfcc33
    }
Packit bfcc33
Packit bfcc33
    DEBUG_PRINTLN(LCS, "FINAL RETURN")
Packit bfcc33
    return lcs_backtrace(c, x, y, i - 1, j, comparator);
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
  This is the equivalent of ruby's Sass::Util.lcs_table.
Packit bfcc33
Packit bfcc33
  # Calculates the memoization table for the Least Common Subsequence algorithm.
Packit bfcc33
  # Algorithm from http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS
Packit bfcc33
  */
Packit bfcc33
  template<typename ComparatorType>
Packit bfcc33
  void lcs_table(const Node& x, const Node& y, const ComparatorType& comparator, LCSTable& out) {
Packit bfcc33
    DEBUG_PRINTLN(LCS, "LCSTABLE: X=" << x << " Y=" << y)
Packit bfcc33
Packit bfcc33
    NodeDeque& xChildren = *(x.collection());
Packit bfcc33
    NodeDeque& yChildren = *(y.collection());
Packit bfcc33
Packit bfcc33
    LCSTable c(xChildren.size(), std::vector<int>(yChildren.size()));
Packit bfcc33
Packit bfcc33
    // These shouldn't be necessary since the vector will be initialized to 0 already.
Packit bfcc33
    // x.size.times {|i| c[i][0] = 0}
Packit bfcc33
    // y.size.times {|j| c[0][j] = 0}
Packit bfcc33
Packit bfcc33
    for (size_t i = 1; i < xChildren.size(); i++) {
Packit bfcc33
      for (size_t j = 1; j < yChildren.size(); j++) {
Packit bfcc33
        Node compareOut = Node::createNil();
Packit bfcc33
Packit bfcc33
        if (comparator(xChildren[i], yChildren[j], compareOut)) {
Packit bfcc33
          c[i][j] = c[i - 1][j - 1] + 1;
Packit bfcc33
        } else {
Packit bfcc33
          c[i][j] = std::max(c[i][j - 1], c[i - 1][j]);
Packit bfcc33
        }
Packit bfcc33
      }
Packit bfcc33
    }
Packit bfcc33
Packit bfcc33
    out = c;
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
  This is the equivalent of ruby's Sass::Util.lcs.
Packit bfcc33
Packit bfcc33
  # Computes a single longest common subsequence for `x` and `y`.
Packit bfcc33
  # If there are more than one longest common subsequences,
Packit bfcc33
  # the one returned is that which starts first in `x`.
Packit bfcc33
Packit bfcc33
  # @param x [NodeCollection]
Packit bfcc33
  # @param y [NodeCollection]
Packit bfcc33
  # @comparator An equality check between elements of `x` and `y`.
Packit bfcc33
  # @return [NodeCollection] The LCS
Packit bfcc33
Packit bfcc33
  http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
Packit bfcc33
  */
Packit bfcc33
  template<typename ComparatorType>
Packit bfcc33
  Node lcs(Node& x, Node& y, const ComparatorType& comparator) {
Packit bfcc33
    DEBUG_PRINTLN(LCS, "LCS: X=" << x << " Y=" << y)
Packit bfcc33
Packit bfcc33
    Node newX = Node::createCollection();
Packit bfcc33
    newX.collection()->push_back(Node::createNil());
Packit bfcc33
    newX.plus(x);
Packit bfcc33
Packit bfcc33
    Node newY = Node::createCollection();
Packit bfcc33
    newY.collection()->push_back(Node::createNil());
Packit bfcc33
    newY.plus(y);
Packit bfcc33
Packit bfcc33
    LCSTable table;
Packit bfcc33
    lcs_table(newX, newY, comparator, table);
Packit bfcc33
Packit bfcc33
    return lcs_backtrace(table, newX, newY, static_cast<int>(newX.collection()->size()) - 1, static_cast<int>(newY.collection()->size()) - 1, comparator);
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
  This is the equivalent of ruby sass' Sass::Util.flatten and [].flatten.
Packit bfcc33
  Sass::Util.flatten requires the number of levels to flatten, while
Packit bfcc33
  [].flatten doesn't and will flatten the entire array. This function
Packit bfcc33
  supports both.
Packit bfcc33
Packit bfcc33
  # Flattens the first `n` nested arrays. If n == -1, all arrays will be flattened
Packit bfcc33
  #
Packit bfcc33
  # @param arr [NodeCollection] The array to flatten
Packit bfcc33
  # @param n [int] The number of levels to flatten
Packit bfcc33
  # @return [NodeCollection] The flattened array
Packit bfcc33
  */
Packit bfcc33
  Node flatten(Node& arr, int n = -1);
Packit bfcc33
Packit bfcc33
Packit bfcc33
  /*
Packit bfcc33
  This is the equivalent of ruby's Sass::Util.group_by_to_a.
Packit bfcc33
Packit bfcc33
  # Performs the equivalent of `enum.group_by.to_a`, but with a guaranteed
Packit bfcc33
  # order. Unlike [#hash_to_a], the resulting order isn't sorted key order;
Packit bfcc33
  # instead, it's the same order as `#group_by` has under Ruby 1.9 (key
Packit bfcc33
  # appearance order).
Packit bfcc33
  #
Packit bfcc33
  # @param enum [Enumerable]
Packit bfcc33
  # @return [Array<[Object, Array]>] An array of pairs.
Packit bfcc33
Packit bfcc33
  TODO: update @param and @return once I know what those are.
Packit bfcc33
Packit bfcc33
  The following is the modified version of the ruby code that was more portable to C++. You
Packit bfcc33
  should be able to drop it into ruby 3.2.19 and get the same results from ruby sass.
Packit bfcc33
Packit bfcc33
    def group_by_to_a(enum, &block)
Packit bfcc33
      order = {}
Packit bfcc33
Packit bfcc33
      arr = []
Packit bfcc33
Packit bfcc33
      grouped = {}
Packit bfcc33
Packit bfcc33
      for e in enum do
Packit bfcc33
        key = block[e]
Packit bfcc33
        unless order.include?(key)
Packit bfcc33
          order[key] = order.size
Packit bfcc33
        end
Packit bfcc33
Packit bfcc33
        if not grouped.has_key?(key) then
Packit bfcc33
          grouped[key] = [e]
Packit bfcc33
        else
Packit bfcc33
          grouped[key].push(e)
Packit bfcc33
        end
Packit bfcc33
      end
Packit bfcc33
Packit bfcc33
      grouped.each do |key, vals|
Packit bfcc33
        arr[order[key]] = [key, vals]
Packit bfcc33
      end
Packit bfcc33
Packit bfcc33
      arr
Packit bfcc33
    end
Packit bfcc33
Packit bfcc33
  */
Packit bfcc33
  template<typename EnumType, typename KeyType, typename KeyFunctorType>
Packit bfcc33
  void group_by_to_a(std::vector<EnumType>& enumeration, KeyFunctorType& keyFunc, std::vector<std::pair<KeyType, std::vector<EnumType> > >& arr /*out*/) {
Packit bfcc33
Packit bfcc33
    std::map<unsigned int, KeyType> order;
Packit bfcc33
Packit bfcc33
    std::map<size_t, std::vector<EnumType> > grouped;
Packit bfcc33
Packit bfcc33
    for (typename std::vector<EnumType>::iterator enumIter = enumeration.begin(), enumIterEnd = enumeration.end(); enumIter != enumIterEnd; enumIter++) {
Packit bfcc33
      EnumType& e = *enumIter;
Packit bfcc33
Packit bfcc33
      KeyType key = keyFunc(e);
Packit bfcc33
Packit bfcc33
      if (grouped.find(key->hash()) == grouped.end()) {
Packit bfcc33
        order.insert(std::make_pair((unsigned int)order.size(), key));
Packit bfcc33
Packit bfcc33
        std::vector<EnumType> newCollection;
Packit bfcc33
        newCollection.push_back(e);
Packit bfcc33
        grouped.insert(std::make_pair(key->hash(), newCollection));
Packit bfcc33
      } else {
Packit bfcc33
        std::vector<EnumType>& collection = grouped.at(key->hash());
Packit bfcc33
        collection.push_back(e);
Packit bfcc33
      }
Packit bfcc33
    }
Packit bfcc33
Packit bfcc33
    for (unsigned int index = 0; index < order.size(); index++) {
Packit bfcc33
      KeyType& key = order.at(index);
Packit bfcc33
      std::vector<EnumType>& values = grouped.at(key->hash());
Packit bfcc33
Packit bfcc33
      std::pair<KeyType, std::vector<EnumType> > grouping = std::make_pair(key, values);
Packit bfcc33
Packit bfcc33
      arr.push_back(grouping);
Packit bfcc33
    }
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
Packit bfcc33
}
Packit bfcc33
Packit bfcc33
#endif