Rcpp Version 1.0.14
Loading...
Searching...
No Matches
self_match.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// self_match.h: Rcpp R/C++ interface class library -- self match
4//
5// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
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__self_match_h
23#define Rcpp__sugar__self_match_h
24
25namespace Rcpp{
26namespace sugar{
27
28template <typename HASH, typename STORAGE>
30public:
31 SelfInserter( HASH& hash_ ) : hash(hash_), index(0) {}
32
33 inline R_xlen_t operator()( STORAGE value ){
34 typename HASH::iterator it = hash.find( value ) ;
35 if( it == hash.end() ){
36 hash.insert( std::make_pair(value, ++index) ) ;
37 return index ;
38 } else {
39 return it->second ;
40 }
41 }
42
43private:
44 HASH& hash ;
46} ;
47
48template <int RTYPE, typename TABLE_T>
49class SelfMatch {
50public:
52
53 SelfMatch( const TABLE_T& table ): hash(), result(table.size()) {
54 std::transform( table.begin(), table.end(), result.begin(), Inserter(hash) ) ;
55 }
56
57 inline operator IntegerVector() const { return result ; }
58
59private:
64};
65
66} // sugar
67
68template <int RTYPE, bool NA, typename T>
73
74
75} // Rcpp
76#endif
77
iterator end()
Definition Vector.h:334
iterator begin()
Definition Vector.h:333
IntegerVector fill_and_self_match()
Definition SelfHash.h:46
SelfInserter(HASH &hash_)
Definition self_match.h:31
R_xlen_t operator()(STORAGE value)
Definition self_match.h:33
IntegerVector result
Definition self_match.h:63
SelfInserter< HASH, STORAGE > Inserter
Definition self_match.h:61
RCPP_UNORDERED_MAP< STORAGE, int > HASH
Definition self_match.h:60
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition self_match.h:51
SelfMatch(const TABLE_T &table)
Definition self_match.h:53
Rcpp API.
Definition algo.h:28
Vector< INTSXP > IntegerVector
IntegerVector self_match(const VectorBase< RTYPE, NA, T > &x)
Definition self_match.h:69
T as(SEXP x)
Definition as.h:151
IntegerVector table(const VectorBase< RTYPE, NA, T > &x)
Definition table.h:126