Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / subset_map.cpp
1 #include "sass.hpp"
2 #include "ast.hpp"
3 #include "subset_map.hpp"
4
5 namespace Sass {
6
7   void Subset_Map::put(const Compound_Selector_Obj& sel, const SubSetMapPair& value)
8   {
9     if (sel->empty()) throw std::runtime_error("internal error: subset map keys may not be empty");
10     size_t index = values_.size();
11     values_.push_back(value);
12     for (size_t i = 0, S = sel->length(); i < S; ++i)
13     {
14       hash_[(*sel)[i]].push_back(std::make_pair(sel, index));
15     }
16   }
17
18   std::vector<SubSetMapPair> Subset_Map::get_kv(const Compound_Selector_Obj& sel)
19   {
20     SimpleSelectorDict dict(sel->begin(), sel->end()); // XXX Set
21     std::vector<size_t> indices;
22     for (size_t i = 0, S = sel->length(); i < S; ++i) {
23       if (!hash_.count((*sel)[i])) {
24         continue;
25       }
26       const std::vector<std::pair<Compound_Selector_Obj, size_t> >& subsets = hash_[(*sel)[i]];
27       for (const std::pair<Compound_Selector_Obj, size_t>& item : subsets) {
28         bool include = true;
29         for (const Simple_Selector_Obj& it : item.first->elements()) {
30           auto found = dict.find(it);
31           if (found == dict.end()) {
32             include = false;
33             break;
34           }
35         }
36         if (include) indices.push_back(item.second);
37       }
38     }
39     sort(indices.begin(), indices.end());
40     std::vector<size_t>::iterator indices_end = unique(indices.begin(), indices.end());
41     indices.resize(distance(indices.begin(), indices_end));
42
43     std::vector<SubSetMapPair> results;
44     for (size_t i = 0, S = indices.size(); i < S; ++i) {
45       results.push_back(values_[indices[i]]);
46     }
47     return results;
48   }
49
50   std::vector<SubSetMapPair> Subset_Map::get_v(const Compound_Selector_Obj& sel)
51   {
52     return get_kv(sel);
53   }
54
55 }