Rcpp Version 1.1.2
Loading...
Searching...
No Matches
ListOf.h
Go to the documentation of this file.
1// ListOf.h: Rcpp R/C++ interface class library -- templated List container
2//
3// Copyright (C) 2014 - 2025 Dirk Eddelbuettel, Romain François and Kevin Ushey
4// Copyright (C) 2026 Dirk Eddelbuettel, Romain François, Kevin Ushey and Iñaki Ucar
5//
6// This file is part of Rcpp.
7//
8// Rcpp is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rcpp is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef Rcpp_vector_ListOf_h_
22#define Rcpp_vector_ListOf_h_
23
24namespace Rcpp {
25
26template <typename T>
27class ListOf
28 : public NamesProxyPolicy<ListOf<T>>
29 , public AttributeProxyPolicy<ListOf<T>>
30 , public RObjectMethods<ListOf<T>>
31{
32
33public:
36
37 ListOf(): list(R_NilValue) {}
38
39 ListOf(SEXP data_): list(data_) {
40 std::transform(list.begin(), list.end(), list.begin(), as<T>);
41 }
42
43 template <typename U>
44 ListOf(const U& data_): list(data_) {
45 std::transform(list.begin(), list.end(), list.begin(), as<T>);
46 }
47
48 ListOf(const ListOf& other): list(other.list) {}
49
50 ListOf& operator=(const ListOf& other) {
51 if (this != &other) {
52 list = other.list;
53 }
54 return *this;
55 }
56
57 template <typename U>
58 ListOf& operator=(const U& other) {
59 list = as<List>(other);
60 return *this;
61 }
62
63 // subsetting operators
64
66 return ChildVector<T>(list[i], list, i);
67 }
68
69 const ChildVector<T> operator[](R_xlen_t i) const {
70 return ChildVector<T>(list[i], list, i);
71 }
72
73 ChildVector<T> operator[](const std::string& str) {
74 return ChildVector<T>(list[str], list, list.findName(str));
75 }
76
77 const ChildVector<T> operator[](const std::string& str) const {
78 return ChildVector<T>(list[str], list, list.findName(str));
79 }
80
81 // iteration operators pass down to list
82
83 inline iterator begin() {
84 return list.begin();
85 }
86
87 inline iterator end() {
88 return list.end();
89 }
90
91 inline const_iterator begin() const {
92 return list.begin();
93 }
94
95 inline const_iterator end() const {
96 return list.end();
97 }
98
99 inline R_xlen_t size() const {
100 return list.size() ;
101 }
102
103 inline List get() const {
104 return list;
105 }
106
107 // conversion operators
108 operator SEXP() const { return wrap(list); }
109 operator List() const { return list; }
110
111private:
112
114
115}; // ListOf<T>
116
117// sapply, lapply wrappers
118
119namespace sugar {
120
121template <int RTYPE, bool NA, typename T, typename Function>
122class Lapply;
123
124template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
125class Sapply;
126
127}
128
129template <typename T, typename Function>
131 return lapply(t.get(), fun);
132}
133
134template <typename T, typename Function>
135T sapply(const ListOf<T>& t, Function fun) {
136 return sapply(t.get(), fun);
137}
138
139} // Rcpp
140
141#endif
traits::r_vector_const_iterator< VECSXP >::type const_iterator
Definition ListOf.h:35
R_xlen_t size() const
Definition ListOf.h:99
ChildVector< T > operator[](const std::string &str)
Definition ListOf.h:73
ListOf & operator=(const U &other)
Definition ListOf.h:58
ListOf(const ListOf &other)
Definition ListOf.h:48
ChildVector< T > operator[](R_xlen_t i)
Definition ListOf.h:65
List list
Definition ListOf.h:113
List get() const
Definition ListOf.h:103
const ChildVector< T > operator[](R_xlen_t i) const
Definition ListOf.h:69
iterator begin()
Definition ListOf.h:83
const_iterator begin() const
Definition ListOf.h:91
ListOf(SEXP data_)
Definition ListOf.h:39
const_iterator end() const
Definition ListOf.h:95
const ChildVector< T > operator[](const std::string &str) const
Definition ListOf.h:77
ListOf & operator=(const ListOf &other)
Definition ListOf.h:50
ListOf(const U &data_)
Definition ListOf.h:44
traits::r_vector_iterator< VECSXP >::type iterator
Definition ListOf.h:34
iterator end()
Definition ListOf.h:87
Rcpp API.
Definition algo.h:28
Function_Impl< PreserveStorage > Function
Definition Function.h:144
sugar::Sapply< RTYPE, NA, T, Function, traits::same_type< typename ::Rcpp::traits::result_of< Function, T >::type, typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of< Function, T >::type >::rtype >::type >::value > sapply(const Rcpp::VectorBase< RTYPE, NA, T > &t, Function fun)
Definition sapply.h:109
Vector< VECSXP > List
T as(SEXP x)
Definition as.h:150
sugar::Lapply< RTYPE, NA, T, Function > lapply(const Rcpp::VectorBase< RTYPE, NA, T > &t, Function fun)
Definition lapply.h:56
SEXP wrap(const Date &date)
Definition Date.h:38
const storage_type< RTYPE >::type * type
Definition proxy.h:254
storage_type< RTYPE >::type * type
Definition proxy.h:250