Rcpp Version 1.1.2
Loading...
Searching...
No Matches
setdiff.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// setdiff.h: Rcpp R/C++ interface class library -- setdiff
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__setdiff_h
24#define Rcpp__sugar__setdiff_h
25
26namespace Rcpp{
27namespace sugar{
28
29 template <typename SET>
31 public:
32 RemoveFromSet( SET& set_) : set(set_){}
33
34 template <typename T>
35 void operator()(T value){
36 set.erase( value );
37 }
38
39 private:
40 SET& set ;
41 } ;
42
43 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
44 class SetDiff {
45 public:
47
48 SetDiff( const LHS_T& lhs, const RHS_T& rhs) :
51 {
52
53 std::for_each( rhs_set.begin(), rhs_set.end(), RemoveFromSet<SET>(lhs_set) ) ;
54 }
55
57 R_xlen_t n = lhs_set.size() ;
58 Vector<RTYPE> out = no_init(n) ;
59 std::copy( lhs_set.begin(), lhs_set.end(), out.begin() ) ;
60 return out ;
61 }
62
63 private:
64 typedef std::unordered_set<STORAGE> SET ;
65 typedef typename SET::const_iterator ITERATOR ;
68
69 } ;
70
71 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
72 class SetEqual {
73 public:
75
76 SetEqual( const LHS_T& lhs, const RHS_T& rhs) :
79 {
80 }
81
82 bool get() const {
83 if( lhs_set.size() != rhs_set.size() ) return false ;
84
85 ITERATOR it = lhs_set.begin(), end = lhs_set.end(), rhs_end = rhs_set.end() ;
86 for( ; it != end; ){
87 if( rhs_set.find(*it++) == rhs_end ) return false ;
88 }
89 return true ;
90 }
91
92 private:
93 typedef std::unordered_set<STORAGE> SET ;
94 typedef typename SET::const_iterator ITERATOR ;
97
98 } ;
99
100 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
101 class Intersect {
102 public:
104
105 Intersect( const LHS_T& lhs, const RHS_T& rhs) :
106 intersect()
107 {
108
109 SET lhs_set( get_const_begin(lhs), get_const_end(lhs) ) ;
110 SET rhs_set( get_const_begin(rhs), get_const_end(rhs) ) ;
111
112 ITERATOR end = lhs_set.end() ;
113 ITERATOR rhs_end = rhs_set.end() ;
114 for( ITERATOR it=lhs_set.begin(); it != end; it++){
115 if( rhs_set.find(*it) != rhs_end ) intersect.insert(*it) ;
116 }
117 }
118
120 R_xlen_t n = intersect.size() ;
121 Vector<RTYPE> out = no_init(n) ;
122 std::copy( intersect.begin(), intersect.end(), out.begin() ) ;
123 return out ;
124 }
125
126 private:
127 typedef std::unordered_set<STORAGE> SET ;
128 typedef typename SET::const_iterator ITERATOR ;
130
131 } ;
132
133 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
134 class Union {
135 public:
137
138 Union( const LHS_T& lhs, const RHS_T& rhs) :
140 {
141 result.insert( get_const_begin(rhs), get_const_end(rhs) ) ;
142 }
143
145 R_xlen_t n = result.size() ;
146 Vector<RTYPE> out = no_init(n) ;
147 std::copy( result.begin(), result.end(), out.begin() ) ;
148 return out ;
149 }
150
151 private:
152 typedef std::unordered_set<STORAGE> SET ;
153 typedef typename SET::const_iterator ITERATOR ;
155
156 } ;
157
158
159} // sugar
160
161template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
165
166template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
170
171template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
175
176// we cannot use "union" because it is a keyword
177template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
181
182
183
184} // Rcpp
185#endif
186
VECTOR & get_ref()
Definition VectorBase.h:37
iterator begin()
Definition Vector.h:332
Intersect(const LHS_T &lhs, const RHS_T &rhs)
Definition setdiff.h:105
Vector< RTYPE > get() const
Definition setdiff.h:119
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition setdiff.h:103
SET::const_iterator ITERATOR
Definition setdiff.h:128
std::unordered_set< STORAGE > SET
Definition setdiff.h:127
void operator()(T value)
Definition setdiff.h:35
Vector< RTYPE > get() const
Definition setdiff.h:56
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition setdiff.h:46
std::unordered_set< STORAGE > SET
Definition setdiff.h:64
SetDiff(const LHS_T &lhs, const RHS_T &rhs)
Definition setdiff.h:48
SET::const_iterator ITERATOR
Definition setdiff.h:65
SetEqual(const LHS_T &lhs, const RHS_T &rhs)
Definition setdiff.h:76
SET::const_iterator ITERATOR
Definition setdiff.h:94
bool get() const
Definition setdiff.h:82
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition setdiff.h:74
std::unordered_set< STORAGE > SET
Definition setdiff.h:93
Union(const LHS_T &lhs, const RHS_T &rhs)
Definition setdiff.h:138
std::unordered_set< STORAGE > SET
Definition setdiff.h:152
Vector< RTYPE > get() const
Definition setdiff.h:144
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition setdiff.h:136
SET::const_iterator ITERATOR
Definition setdiff.h:153
sugar_const_iterator_type< T >::type get_const_end(const T &obj)
Definition iterator.h:148
sugar_const_iterator_type< T >::type get_const_begin(const T &obj)
Definition iterator.h:138
Rcpp API.
Definition algo.h:28
Vector< RTYPE > union_(const VectorBase< RTYPE, LHS_NA, LHS_T > &lhs, const VectorBase< RTYPE, RHS_NA, RHS_T > &rhs)
Definition setdiff.h:178
Vector< RTYPE > intersect(const VectorBase< RTYPE, LHS_NA, LHS_T > &lhs, const VectorBase< RTYPE, RHS_NA, RHS_T > &rhs)
Definition setdiff.h:172
no_init_vector no_init(R_xlen_t size)
Definition no_init.h:77
bool setequal(const VectorBase< RTYPE, LHS_NA, LHS_T > &lhs, const VectorBase< RTYPE, RHS_NA, RHS_T > &rhs)
Definition setdiff.h:167
Vector< RTYPE > setdiff(const VectorBase< RTYPE, LHS_NA, LHS_T > &lhs, const VectorBase< RTYPE, RHS_NA, RHS_T > &rhs)
Definition setdiff.h:162