Rcpp Version 1.1.2
Loading...
Searching...
No Matches
table.h
Go to the documentation of this file.
1// table.h: Rcpp R/C++ interface class library -- table match
2//
3// Copyright (C) 2012 - 2025 Dirk Eddelbuettel, Romain Francois, and Kevin Ushey
4// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois, Kevin Ushey and IƱaki Ucar
5//
6// This file is part of Rcpp.
7//
8// Rcpp is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rcpp is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef Rcpp__sugar__table_h
22#define Rcpp__sugar__table_h
23
24namespace Rcpp{
25namespace sugar{
26
27template <typename HASH, typename STORAGE>
29public:
30 CountInserter( HASH& hash_ ) : hash(hash_) {}
31
32 inline void operator()( STORAGE value ){
33 hash[value]++ ;
34 }
35
36private:
37 HASH& hash ;
38} ;
39
40template <typename HASH, int RTYPE>
41class Grabber{
42public:
43 Grabber( IntegerVector& res_, CharacterVector& names_ ) : res(res_), names(names_), index(0){}
44
45 template <typename T>
46 inline void operator()( T pair){
47 res[index] = pair.second ;
48 names[index++] = internal::r_coerce<RTYPE,STRSXP>(pair.first) ;
49 }
50
51private:
54 R_xlen_t index ;
55} ;
56
57template <int RTYPE, typename TABLE_T>
58class Table {
59public:
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() ;
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
80private:
81 typedef std::unordered_map<STORAGE, int> HASH ;
84
85 typedef std::map<STORAGE, int, internal::NAComparator<STORAGE> > SORTED_MAP ;
87
88};
89
90} // sugar
91
92template <int RTYPE, bool NA, typename T>
96
97
98} // Rcpp
99#endif
VECTOR & get_ref()
Definition VectorBase.h:37
void operator()(STORAGE value)
Definition table.h:32
CountInserter(HASH &hash_)
Definition table.h:30
CharacterVector & names
Definition table.h:53
Grabber(IntegerVector &res_, CharacterVector &names_)
Definition table.h:43
IntegerVector & res
Definition table.h:52
R_xlen_t index
Definition table.h:54
void operator()(T pair)
Definition table.h:46
SORTED_MAP map
Definition table.h:86
std::map< STORAGE, int, internal::NAComparator< STORAGE > > SORTED_MAP
Definition table.h:85
Table(const TABLE_T &table)
Definition table.h:62
std::unordered_map< STORAGE, int > HASH
Definition table.h:81
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition table.h:60
CountInserter< HASH, STORAGE > Inserter
Definition table.h:82
Rcpp API.
Definition algo.h:28
no_init_vector no_init(R_xlen_t size)
Definition no_init.h:77
Vector< STRSXP > CharacterVector
Vector< INTSXP > IntegerVector
IntegerVector table(const VectorBase< RTYPE, NA, T > &x)
Definition table.h:93