Rcpp Version 1.0.9
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 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <typename HASH, typename STORAGE>
30 public:
31  CountInserter( HASH& hash_ ) : hash(hash_) {}
32 
33  inline void operator()( STORAGE value ){
34  hash[value]++ ;
35  }
36 
37 private:
38  HASH& hash ;
39 } ;
40 
41 template <typename HASH, int RTYPE>
42 class Grabber{
43 public:
44  Grabber( IntegerVector& res_, CharacterVector& names_ ) : res(res_), names(names_), index(0){}
45 
46  template <typename T>
47  inline void operator()( T pair){
48  res[index] = pair.second ;
49  names[index++] = internal::r_coerce<RTYPE,STRSXP>(pair.first) ;
50  }
51 
52 private:
55  R_xlen_t index ;
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 
62 template <int RTYPE, typename TABLE_T>
63 class Table {
64 public:
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 
80 private:
81  typedef RCPP_UNORDERED_MAP<STORAGE, int, internal::NAComparator<STORAGE> >HASH ;
82  typedef CountInserter<HASH,STORAGE> Inserter ;
83  HASH hash ;
84 };
85 
86 #else
87 
88 template <int RTYPE, typename TABLE_T>
89 class Table {
90 public:
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 
111 private:
112  typedef RCPP_UNORDERED_MAP<STORAGE, int> HASH ;
115 
116  typedef std::map<STORAGE, int, internal::NAComparator<STORAGE> > SORTED_MAP ;
118 
119 };
120 
121 #endif // USING_RCPP_MAP
122 
123 } // sugar
124 
125 template <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
iterator end()
Definition: Vector.h:335
iterator begin()
Definition: Vector.h:334
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
Rcpp API.
Definition: algo.h:28
no_init_vector no_init(R_xlen_t size)
Definition: no_init.h:77
Vector< INTSXP > IntegerVector
Definition: instantiation.h:28
IntegerVector table(const VectorBase< RTYPE, NA, T > &x)
Definition: table.h:126