Rcpp Version 1.1.2
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 - 2024 Dirk Eddelbuettel and Romain Francois
6// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
7//
8// This file is part of Rcpp.
9//
10// Rcpp is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 2 of the License, or
13// (at your option) any later version.
14//
15// Rcpp is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22
23#ifndef Rcpp__sugar__self_match_h
24#define Rcpp__sugar__self_match_h
25
26namespace Rcpp{
27namespace sugar{
28
29template <typename HASH, typename STORAGE>
31public:
32 SelfInserter( HASH& hash_ ) : hash(hash_), index(0) {}
33
34 inline R_xlen_t operator()( STORAGE value ){
35 typename HASH::iterator it = hash.find( value ) ;
36 if( it == hash.end() ){
37 hash.insert( std::make_pair(value, ++index) ) ;
38 return index ;
39 } else {
40 return it->second ;
41 }
42 }
43
44private:
45 HASH& hash ;
46 R_xlen_t index;
47} ;
48
49template <int RTYPE, typename TABLE_T>
50class SelfMatch {
51public:
53
54 SelfMatch( const TABLE_T& table ): hash(), result(table.size()) {
55 std::transform( table.begin(), table.end(), result.begin(), Inserter(hash) ) ;
56 }
57
58 inline operator IntegerVector() const { return result ; }
59
60private:
61 typedef std::unordered_map<STORAGE, int> HASH ;
65};
66
67} // sugar
68
69template <int RTYPE, bool NA, typename T>
74
75
76} // Rcpp
77#endif
78
IntegerVector fill_and_self_match()
Definition SelfHash.h:47
SelfInserter(HASH &hash_)
Definition self_match.h:32
R_xlen_t operator()(STORAGE value)
Definition self_match.h:34
std::unordered_map< STORAGE, int > HASH
Definition self_match.h:61
IntegerVector result
Definition self_match.h:64
SelfInserter< HASH, STORAGE > Inserter
Definition self_match.h:62
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition self_match.h:52
SelfMatch(const TABLE_T &table)
Definition self_match.h:54
Rcpp API.
Definition algo.h:28
Vector< INTSXP > IntegerVector
IntegerVector self_match(const VectorBase< RTYPE, NA, T > &x)
Definition self_match.h:70
IntegerVector table(const VectorBase< RTYPE, NA, T > &x)
Definition table.h:93