Rcpp Version 1.1.2
Loading...
Searching...
No Matches
SelfHash.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
2//
3// hash.h: Rcpp R/C++ interface class library -- hashing utility, inspired
4// from Simon's fastmatch package
5//
6// Copyright (C) 2010, 2011 Simon Urbanek
7// Copyright (C) 2012 - 2024 Dirk Eddelbuettel and Romain Francois
8// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
9//
10// This file is part of Rcpp.
11//
12// Rcpp is free software: you can redistribute it and/or modify it
13// under the terms of the GNU General Public License as published by
14// the Free Software Foundation, either version 2 of the License, or
15// (at your option) any later version.
16//
17// Rcpp is distributed in the hope that it will be useful, but
18// WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
24
25#ifndef RCPP__HASH__SELF_HASH_H
26#define RCPP__HASH__SELF_HASH_H
27
28namespace Rcpp{
29namespace sugar{
30
31
32 template <int RTYPE>
33 class SelfHash {
34 public:
37
38 SelfHash( SEXP table ) : n(Rf_length(table)), m(2), k(1),
39 src( (STORAGE*)dataptr(table) ), data(), indices(), size_(0)
40 {
41 int desired = n*2 ;
42 while( m < desired ){ m *= 2 ; k++ ; }
43 data.resize( m ) ;
44 indices.resize( m ) ;
45 }
46
49 int* res = INTEGER(result) ;
50 for( int i=0; i<n; i++) res[i] = add_value_get_index(i) ;
51 return result ;
52 }
53
54 inline int size() const {
55 return size_ ;
56 }
57
58 int n, m, k ;
60 std::vector<int> data ;
61 std::vector<int> indices ;
62 int size_ ;
63
64 STORAGE normalize(STORAGE val) const { return val; }
65
66 inline bool not_equal(const STORAGE& lhs, const STORAGE& rhs) {
67 return ! internal::NAEquals<STORAGE>()(normalize(lhs), rhs);
68 }
69
71 STORAGE val = normalize(src[i++]);
72 unsigned int addr = get_addr(val) ;
73 while (data[addr] && not_equal( src[data[addr] - 1], val)) {
74 addr++;
75 if (addr == static_cast<unsigned int>(m)) addr = 0;
76 }
77 if (!data[addr]) {
78 data[addr] = i ;
79 indices[addr] = ++size_ ;
80 }
81 return indices[addr] ;
82 }
83
84 /* NOTE: we are returning a 1-based index ! */
85 unsigned int get_index(STORAGE value) const {
86 unsigned int addr = get_addr(value) ;
87 while (data[addr]) {
88 if (src[data[addr] - 1] == value)
89 return data[addr];
90 addr++;
91 if (addr == static_cast<unsigned int>(m)) addr = 0;
92 }
93 return NA_INTEGER;
94 }
95
96 // defined below
97 unsigned int get_addr(STORAGE value) const ;
98 } ;
99
100 template <>
101 inline double SelfHash<REALSXP>::normalize(double val) const {
102 /* double is a bit tricky - we have to normalize 0.0, NA and NaN */
103 if (val == 0.0) val = 0.0;
104 if (internal::Rcpp_IsNA(val)) val = NA_REAL;
105 else if (internal::Rcpp_IsNaN(val)) val = R_NaN;
106 return val;
107 }
108
109 template <>
110 inline unsigned int SelfHash<INTSXP>::get_addr(int value) const {
111 return RCPP_HASH(value) ;
112 }
113 template <>
114 inline unsigned int SelfHash<REALSXP>::get_addr(double val) const {
115 unsigned int addr;
116 union dint_u {
117 double d;
118 unsigned int u[2];
119 };
120 union dint_u val_u;
121 val_u.d = val;
122 addr = RCPP_HASH(val_u.u[0] + val_u.u[1]);
123 return addr ;
124 }
125
126 template <>
127 inline unsigned int SelfHash<STRSXP>::get_addr(SEXP value) const {
128 intptr_t val = (intptr_t) value;
129 unsigned int addr;
130 #if (defined _LP64) || (defined __LP64__) || (defined WIN64)
131 addr = RCPP_HASH((val & 0xffffffff) ^ (val >> 32));
132 #else
133 addr = RCPP_HASH(val);
134 #endif
135 return addr ;
136 }
137
138} // sugar
139} // Rcpp
140
141#endif
#define RCPP_HASH(X)
Definition IndexHash.h:46
void * dataptr(SEXP)
Definition routines.h:264
STORAGE normalize(STORAGE val) const
Definition SelfHash.h:64
unsigned int get_index(STORAGE value) const
Definition SelfHash.h:85
int size() const
Definition SelfHash.h:54
SelfHash(SEXP table)
Definition SelfHash.h:38
IntegerVector fill_and_self_match()
Definition SelfHash.h:47
unsigned int get_addr(STORAGE value) const
Vector< RTYPE > VECTOR
Definition SelfHash.h:36
std::vector< int > indices
Definition SelfHash.h:61
int add_value_get_index(int i)
Definition SelfHash.h:70
traits::storage_type< RTYPE >::type STORAGE
Definition SelfHash.h:35
bool not_equal(const STORAGE &lhs, const STORAGE &rhs)
Definition SelfHash.h:66
std::vector< int > data
Definition SelfHash.h:60
Rcpp API.
Definition algo.h:28
no_init_vector no_init(R_xlen_t size)
Definition no_init.h:77
Vector< INTSXP > IntegerVector
IntegerVector table(const VectorBase< RTYPE, NA, T > &x)
Definition table.h:93