| 56 | |
| 57 | template <int RTYPE, typename TABLE_T> |
| 58 | class Table { |
| 59 | public: |
| 60 | typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ; |
| 61 | |
| 62 | Table( const TABLE_T& table ): hash(), map() { |
| 63 | // populate the initial hash |
| 64 | std::for_each( table.begin(), table.end(), Inserter(hash) ) ; |
| 65 | |
| 66 | // populate the map, sorted by keys |
| 67 | map.insert( hash.begin(), hash.end() ) ; |
| 68 | } |
| 69 | |
| 70 | inline operator IntegerVector() const { |
| 71 | // fill the result |
| 72 | R_xlen_t n = map.size() ; |
| 73 | IntegerVector result = no_init(n) ; |
| 74 | CharacterVector names = no_init(n) ; |
| 75 | std::for_each( map.begin(), map.end(), Grabber<SORTED_MAP,RTYPE>(result, names) ) ; |
| 76 | result.names() = names ; |
| 77 | return result ; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | typedef std::unordered_map<STORAGE, int> HASH ; |
| 82 | typedef CountInserter<HASH,STORAGE> Inserter ; |
| 83 | HASH hash ; |
| 84 | |
| 85 | typedef std::map<STORAGE, int, internal::NAComparator<STORAGE> > SORTED_MAP ; |
| 86 | SORTED_MAP map ; |
| 87 | |
| 88 | }; |
| 89 | |
| 90 | } // sugar |
| 91 | |