Rcpp Version 1.0.14
Loading...
Searching...
No Matches
table.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// table.h: Rcpp R/C++ interface class library -- table match
4//
5// Copyright (C) 2012 - 2013 Dirk Eddelbuettel, Romain Francois, and Kevin Ushey
6//
7// This file is part of Rcpp.
8//
9// Rcpp is free software: you can redistribute it and/or modify it
10// under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 2 of the License, or
12// (at your option) any later version.
13//
14// Rcpp is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21
22#ifndef Rcpp__sugar__table_h
23#define Rcpp__sugar__table_h
24
25namespace Rcpp{
26namespace sugar{
27
28template <typename HASH, typename STORAGE>
30public:
32
33 inline void operator()( STORAGE value ){
34 hash[value]++ ;
35 }
36
37private:
38 HASH& hash ;
39} ;
40
41template <typename HASH, int RTYPE>
42class Grabber{
43public:
45
46 template <typename T>
47 inline void operator()( T pair){
48 res[index] = pair.second ;
50 }
51
52private:
56} ;
57
58// we define a different Table class depending on whether we are using
59// std::map or not
60#ifdef RCPP_USING_MAP
61
62template <int RTYPE, typename TABLE_T>
63class Table {
64public:
66
67 Table( const TABLE_T& table ): hash() {
68 std::for_each( table.begin(), table.end(), Inserter(hash) ) ;
69 }
70
71 inline operator IntegerVector() const {
72 R_xlen_t n = hash.size() ;
74 CharacterVector names = no_init(n) ;
75 std::for_each( hash.begin(), hash.end(), Grabber<HASH, RTYPE>(result, names) ) ;
76 result.names() = names ;
77 return result ;
78 }
79
80private:
83 HASH hash ;
84};
85
86#else
87
88template <int RTYPE, typename TABLE_T>
89class Table {
90public:
92
93 Table( const TABLE_T& table ): hash(), map() {
94 // populate the initial hash
95 std::for_each( table.begin(), table.end(), Inserter(hash) ) ;
96
97 // populate the map, sorted by keys
98 map.insert( hash.begin(), hash.end() ) ;
99 }
100
101 inline operator IntegerVector() const {
102 // fill the result
103 R_xlen_t n = map.size() ;
105 CharacterVector names = no_init(n) ;
106 std::for_each( map.begin(), map.end(), Grabber<SORTED_MAP,RTYPE>(result, names) ) ;
107 result.names() = names ;
108 return result ;
109 }
110
111private:
115
116 typedef std::map<STORAGE, int, internal::NAComparator<STORAGE> > SORTED_MAP ;
118
119};
120
121#endif // USING_RCPP_MAP
122
123} // sugar
124
125template <int RTYPE, bool NA, typename T>
127 return sugar::Table<RTYPE,T>(x.get_ref()) ;
128}
129
130
131} // Rcpp
132#endif
133
VECTOR & get_ref()
Definition VectorBase.h:37
R_xlen_t size() const
Definition Vector.h:275
iterator end()
Definition Vector.h:334
iterator begin()
Definition Vector.h:333
void operator()(STORAGE value)
Definition table.h:33
CountInserter(HASH &hash_)
Definition table.h:31
CharacterVector & names
Definition table.h:54
Grabber(IntegerVector &res_, CharacterVector &names_)
Definition table.h:44
IntegerVector & res
Definition table.h:53
R_xlen_t index
Definition table.h:55
void operator()(T pair)
Definition table.h:47
RCPP_UNORDERED_MAP< STORAGE, int > HASH
Definition table.h:112
SORTED_MAP map
Definition table.h:117
std::map< STORAGE, int, internal::NAComparator< STORAGE > > SORTED_MAP
Definition table.h:116
Table(const TABLE_T &table)
Definition table.h:93
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition table.h:91
CountInserter< HASH, STORAGE > Inserter
Definition table.h:113
T as(SEXP x, ::Rcpp::traits::r_type_primitive_tag)
Definition as.h:43
Rcpp API.
Definition algo.h:28
no_init_vector no_init(R_xlen_t size)
Definition no_init.h:77
Vector< INTSXP > IntegerVector
T as(SEXP x)
Definition as.h:151
IntegerVector table(const VectorBase< RTYPE, NA, T > &x)
Definition table.h:126