good dictionary type for wxwidgets - dictionary
What would be a good dictionary type to use with wxWidgets?
Is there such a type somewhere in the framework?
Should I just stick with arrays? I've tried unordered_map but giving me errors with the wxString type.
#include <unordered_map>
std::unordered_map<wxString,int>mapsi;
mapsi={
{"first",1},
{"second",2},
};
errors:
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h: In ins
tantiation of 'struct std::__detail::_Hash_code_base<wxString, std::pair<const w
xString, int>, std::__detail::_Select1st, std::hash<wxString>, std::__detail::_M
od_range_hashing, std::__detail::_Default_ranged_hash, true>':
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1402:10
: required from 'struct std::__detail::_Hashtable_base<wxString, std::pair<con
st wxString, int>, std::__detail::_Select1st, std::equal_to<wxString>, std::hash
<wxString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_ha
sh, std::__detail::_Hashtable_traits<true, false, true> >'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:174:11: requ
ired from 'class std::_Hashtable<wxString, std::pair<const wxString, int>, std::
allocator<std::pair<const wxString, int> >, std::__detail::_Select1st, std::equa
l_to<wxString>, std::hash<wxString>, std::__detail::_Mod_range_hashing, std::__d
etail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail:
:_Hashtable_traits<true, false, true> >'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unordered_map.h:100:18:
required from 'class std::unordered_map<wxString, int>'
E:\bootsi\New folder\TEST.cpp:96:34: required from here
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1070:12
: error: invalid use of incomplete type 'struct std::hash<wxString>'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1070:12
: error: invalid use of incomplete type 'struct std::hash<wxString>'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1082:53
: error: invalid use of incomplete type 'struct std::hash<wxString>'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1082:53
: error: invalid use of incomplete type 'struct std::hash<wxString>'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
E:\bootsi\New folder\TEST.cpp: In constructor 'std::unordered_map<_Key,
_Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash,
_Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type
&) [with _Key = wxString; _Tp = int; _Hash = std::hash<wxString>; _Pred = std::e
qual_to<wxString>; _Alloc = std::allocator<std::pair<const wxString, int> >; std
::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = unsigned int; std:
:unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<wxString>; s
td::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<wx
String>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::allocator_type = s
td::allocator<std::pair<const wxString, int> >]':
E:\bootsi\New folder\TEST.cpp:96:34: error: invalid use of incomplete t
ype 'std::unordered_map<wxString, int>::hasher {aka struct std::hash<wxString>}'
std::unordered_map<wxString,int>mapsi;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'std::unordered_map<wxString, int>::hasher {aka struct std:
:hash<wxString>}'
struct hash;
E:\bootsi\New folder\TEST.cpp:96:34: note: when instantiating default
argument for call to std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unord
ered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const h
asher&, const key_equal&, const allocator_type&) [with _Key = wxString; _Tp = in
t; _Hash = std::hash<wxString>; _Pred = std::equal_to<wxString>; _Alloc = std::a
llocator<std::pair<const wxString, int> >; std::unordered_map<_Key, _Tp, _Hash,
_Pred, _Alloc>::size_type = unsigned int; std::unordered_map<_Key, _Tp, _Hash, _
Pred, _Alloc>::hasher = std::hash<wxString>; std::unordered_map<_Key, _Tp, _Hash
, _Pred, _Alloc>::key_equal = std::equal_to<wxString>; std::unordered_map<_Key,
_Tp, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const wxSt
ring, int> >]
std::unordered_map<wxString,int>mapsi;
^
E:\bootsi\New folder\TEST.cpp: At global scope:
E:\bootsi\New folder\TEST.cpp:149:19: error: expected constructor, dest
ructor, or type conversion before ';' token
CreateStatusBar();
^
E:\bootsi\New folder\TEST.cpp:150:15: error: expected constructor, dest
ructor, or type conversion before '(' token
SetStatusText("Welcome to wxWidgets!");
^
E:\bootsi\New folder\TEST.cpp:151:1: error: expected declaration before
'}' token
}
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h: In ins
tantiation of 'std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H
2, std::__detail::_Default_ranged_hash, true>::_Hash_code_base(const _ExtractKey
&, const _H1&, const _H2&, const std::__detail::_Default_ranged_hash&) [with _Ke
y = wxString; _Value = std::pair<const wxString, int>; _ExtractKey = std::__deta
il::_Select1st; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_hashi
ng]':
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1463:65
: required from 'std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Eq
ual, _H1, _H2, _Hash, _Traits>::_Hashtable_base(const _ExtractKey&, const _H1&,
const _H2&, const _Hash&, const _Equal&) [with _Key = wxString; _Value = std::pa
ir<const wxString, int>; _ExtractKey = std::__detail::_Select1st; _Equal = std::
equal_to<wxString>; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_h
ashing; _Hash = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_H
ashtable_traits<true, false, true>]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:828:24: requ
ired from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::_Hashtable(std::_Hashtable<_Key, _Value, _Alloc,
_ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type, const
_H1&, const _H2&, const _Hash&, const _Equal&, const _ExtractKey&, const alloca
tor_type&) [with _Key = wxString; _Value = std::pair<const wxString, int>; _Allo
c = std::allocator<std::pair<const wxString, int> >; _ExtractKey = std::__detail
::_Select1st; _Equal = std::equal_to<wxString>; _H1 = std::hash<wxString>; _H2 =
std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash;
_RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_
Hashtable_traits<true, false, true>; std::_Hashtable<_Key, _Value, _Alloc, _Extr
actKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type = unsigned i
nt; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
_RehashPolicy, _Traits>::allocator_type = std::allocator<std::pair<const wxStrin
g, int> >]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:397:26: requ
ired from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::_Hashtable(std::_Hashtable<_Key, _Value, _Alloc,
_ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type, const
_H1&, const key_equal&, const allocator_type&) [with _Key = wxString; _Value =
std::pair<const wxString, int>; _Alloc = std::allocator<std::pair<const wxString
, int> >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<wxStri
ng>; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_hashing; _Hash =
std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_reha
sh_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::_
Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPol
icy, _Traits>::size_type = unsigned int; std::_Hashtable<_Key, _Value, _Alloc, _
ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::key_equal = std::e
qual_to<wxString>; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H
1, _H2, _Hash, _RehashPolicy, _Traits>::allocator_type = std::allocator<std::pai
r<const wxString, int> >]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unordered_map.h:142:35:
required from 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_ma
p(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&,
const key_equal&, const allocator_type&) [with _Key = wxString; _Tp = int; _Has
h = std::hash<wxString>; _Pred = std::equal_to<wxString>; _Alloc = std::allocato
r<std::pair<const wxString, int> >; std::unordered_map<_Key, _Tp, _Hash, _Pred,
_Alloc>::size_type = unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _
Alloc>::hasher = std::hash<wxString>; std::unordered_map<_Key, _Tp, _Hash, _Pred
, _Alloc>::key_equal = std::equal_to<wxString>; std::unordered_map<_Key, _Tp, _H
ash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const wxString, i
nt> >]'
E:\bootsi\New folder\TEST.cpp:96:34: required from here
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1099:63
: error: invalid use of incomplete type 'struct std::hash<wxString>'
: __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1099:63
: error: invalid use of incomplete type 'struct std::hash<wxString>'
: __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
There is always wxHashMap
WX_DECLARE_STRING_HASH_MAP( int, // type of the values
myMapType); // name of the class
myMapType myMap;
myMap["test"]=321;
To use wxString in a unordered_map, you will need to provide a hash function for wxString. It might be simpler to use std::wstring.
So you have the choices:
Use wxHashMap which provides support for wxString immediately, but not for all the latest C+11 goodness.
Use unordered_map with a home brewed wxString hash function.
Use unordered_map with std::wstring. You will have to convert between wxString and std::wstring, but will get complete support for C++11
Wait of wxWidgets v3.1.0
I recommend option #3, for now
Thanks to this question, wxWidgets 3.1.0 will have std::hash<wxString> specialization (see this commit, and so your example should work out of the box.
Related
How to avoid the ambiguous overloaded operator problem when using list and numeric vector in Rcpp?
Goal The code below is a very simplified version of my original code for creating a reproducible example. In this code, I am trying to generate a vector that is filled up by using 2 values in each iteration from a for-loop. These values are first multiplied with a parameter p_BL that I provide in a parameters list. You can see that at the end of the code I multiply the parameter (from a list) to a integer in a NumericVector. That's where I get the overloaded operator error. How can I resolve this? Code in the simple_function.cpp file #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector my_func(List parameters, int number_simulations) { NumericVector vector_trig_times (number_simulations); NumericVector vector_trig_times1 (number_simulations); NumericVector vector_trig_times2 (number_simulations); for (int i_simulation = 0; i_simulation < number_simulations; i_simulation++) { int vector_activation1 = i_simulation + 2; int vector_activation2 = i_simulation + 5; vector_trig_times1[i_simulation] = vector_activation1; vector_trig_times2[i_simulation] = vector_activation2; vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); } // i_simulations for loop return vector_trig_times; } Compiling the file: library(Rcpp) sourceCpp("simple_function.cpp") Error Note: Line 28 refers to: vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); Edit: Text of the error message C:/RBuildTools/3.5/mingw_64/bin/g++ -I"C:/PROGRA~1/R/R-36~1.1/include" -DNDEBUG -I"C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include" -I"C:/Users/durraniu/GOOGLE~1/DISSER~1" -O2 -Wall -mtune=generic -c simple_function.cpp -o simple_function.o simple_function.cpp: In function 'Rcpp::NumericVector my_func(Rcpp::List, int)': simple_function.cpp:28:59: error: no match for 'operator*' (operand types are 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' and 'Rcpp::traits::storage_type<14>::type {aka double}') vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ simple_function.cpp:28:59: note: candidates are: In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/RcppCommon.h:136:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:27, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/complex.h:25:17: note: Rcomplex operator*(const Rcomplex&, const Rcomplex&) inline Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs) { ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/complex.h:25:17: note: no known conversion for argument 2 from 'Rcpp::traits::storage_type<14>::type {aka double}' to 'const Rcomplex&' In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:473:1: note: template<int RTYPE, bool LHS_NA, class LHS_T, bool RHS_NA, class RHS_T> Rcpp::sugar::Times_Vector_Vector<RTYPE, LHS_NA, LHS_T, RHS_NA, RHS_T> Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const Rcpp::VectorBase<RTYPE, RHS_NA, RHS_T>&) operator*( ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:473:1: note: template argument deduction/substitution failed: simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>' vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:459:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::sugar::NonaPrimitive<U>&, const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&) operator*( ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:459:1: note: template argument deduction/substitution failed: simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::sugar::NonaPrimitive<U>' vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:450:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const Rcpp::sugar::NonaPrimitive<U>&) operator*( ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:450:1: note: template argument deduction/substitution failed: simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>' vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:439:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive<RTYPE, NA, T> >::type Rcpp::operator*(const U&, const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&) operator*( ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:439:1: note: template argument deduction/substitution failed: simple_function.cpp:28:92: note: mismatched types 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>' and 'Rcpp::traits::storage_type<14>::type {aka double}' vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:429:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const U&) operator*( ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:429:1: note: template argument deduction/substitution failed: simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>' vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/Vector.h:58:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:40, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:271:14: note: template<int RTYPE, template<class> class StoragePolicy, class T> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<T>::type, typename Rcpp::Matrix<RTYPE, StoragePolicy>::stored_type>::value, Rcpp::Matrix<RTYPE, StoragePolicy> >::type Rcpp::operator*(const T&, const Rcpp::Matrix<RTYPE, StoragePolicy>&) operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) { \ ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:280:1: note: in expansion of macro 'RCPP_GENERATE_SCALAR_MATRIX_OPERATOR' RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(*) ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:271:14: note: template argument deduction/substitution failed: operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) { \ ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:280:1: note: in expansion of macro 'RCPP_GENERATE_SCALAR_MATRIX_OPERATOR' RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(*) ^ simple_function.cpp:28:92: note: mismatched types 'const Rcpp::Matrix<RTYPE, StoragePolicy>' and 'Rcpp::traits::storage_type<14>::type {aka double}' vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/Vector.h:58:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:40, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:254:14: note: template<int RTYPE, template<class> class StoragePolicy, class T> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<T>::type, typename Rcpp::Matrix<RTYPE, StoragePolicy>::stored_type>::value, Rcpp::Matrix<RTYPE, StoragePolicy> >::type Rcpp::operator*(const Rcpp::Matrix<RTYPE, StoragePolicy>&, const T&) operator __OPERATOR__ (const Matrix<RTYPE, StoragePolicy> &lhs, const T &rhs) { \ ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:262:1: note: in expansion of macro 'RCPP_GENERATE_MATRIX_SCALAR_OPERATOR' RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(*) ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:254:14: note: template argument deduction/substitution failed: operator __OPERATOR__ (const Matrix<RTYPE, StoragePolicy> &lhs, const T &rhs) { \ ^ C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:262:1: note: in expansion of macro 'RCPP_GENERATE_MATRIX_SCALAR_OPERATOR' RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(*) ^ simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::Matrix<RTYPE, StoragePolicy>' vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ simple_function.cpp:28:100: error: ambiguous overload for 'operator-' (operand types are 'int' and 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}') vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]); ^ simple_function.cpp:28:100: note: candidates are: In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/date_datetime.h:29:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:62, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/Datetime.h:158:20: note: double Rcpp::operator-(const Rcpp::Datetime&, const Rcpp::Datetime&) inline double operator-(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt; } ^ In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/date_datetime.h:25:0, from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:62, from simple_function.cpp:1: C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/Date.h:164:19: note: double Rcpp::operator-(const Rcpp::Date&, const Rcpp::Date&) inline double operator-( const Date& d1, const Date& d2) { return d1.m_d - d2.m_d; } ^ make: *** [C:/PROGRA~1/R/R-36~1.1/etc/x64/Makeconf:215: simple_function.o] Error 1 Error in sourceCpp("simple_function.cpp") : Error 1 occurred building shared library.
Here's how you could implement the spot-on suggestions of Roland and Ralf; notice I've also cut out the unnecessary vector creation and assignment for your i+2 and i+5 values (I also used i as a counter because I don't think i_simulation is more informative, so it makes your code less readable as the line either has to go way over 80 characters or you have to split the calculation over two lines): #include <Rcpp.h> // [[Rcpp::export]] NumericVector my_func2(List parameters, int number_simulations) { NumericVector vector_trig_times (number_simulations); double p_BL = as<NumericVector>(parameters["p_BL"])[0]; for ( int i = 0; i < number_simulations; ++i ) { vector_trig_times[i] = (p_BL * (i + 5)) + ((1 - p_BL) * (i + 2)); } return vector_trig_times; } This compiles just fine, and the result is as expected: Rcpp::sourceCpp("so.cpp") my_func2(list(p_BL = 0.5), 10) # [1] 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 12.5
flow: cast mixed type to an array of shape
I need to cast a mixed type to a shaped array in flow. Tryflow link. type aShapedArray = Array<{a:string}>; //externally defined type const transform = ():mixed => [{a: 'hello'}]; const b = transform(); if (Array.isArray(b)) { const a: aShapedArray = b; } The error is 9: const a: aShapedArray = b; ^ array. Has some incompatible type argument with 9: const a: aShapedArray = b; ^ aShapedArray Type argument `T` is incompatible: 7: const b = transform(); ^ mixed. This type is incompatible with 2: type aShapedArray = Array<{a:string}>; ^ object type
Sadly the only answer I found is to add an any cast to the array. Which pretty much equivalent to flow: ignore this line. /* #flow */ type aShapedArray = Array<{a:string}>; //externally defined type const transform = ():mixed => [{a: 'hello'}]; const b: any = transform(); if (Array.isArray(b)) { const a: aShapedArray = b; } try flow: link
How to pass parameters to ocl kernel using pyopencl?
how to pass some parameters, which will be treated in the .cl file as a preprocessor define using pyopencl? Meaning: foo.cl # define LIMIT 12 typedef struct { uint i[LIMIT]; } foomatic; turns to foo_nodefs.cl typedef struct { uint i[LIMIT]; // python script passing LIMIT to set it } foomatic; Thanks, John
Edit: extending the answer, making it maximally detailed. There are two ways to do that: (metaprogramming) Add your preprocessor directives directly to the string with the source code, or even run your own preprocessor using some templating engine. import pyopencl as cl import numpy import numpy.linalg as la a = numpy.random.rand(50000).astype(numpy.float32) b = numpy.random.rand(50000).astype(numpy.float32) ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) mf = cl.mem_flags a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a) b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b) dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes) defines = """ #define AXIS 0 #define COEFF 1 """ prg = cl.Program(ctx, defines + """ __kernel void sum(__global const float *a, __global const float *b, __global float *c) { int gid = get_global_id(AXIS); c[gid] = a[gid] + b[gid] + COEFF; } """).build() prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf) a_plus_b = numpy.empty_like(a) cl.enqueue_copy(queue, a_plus_b, dest_buf) print(la.norm(a_plus_b - (a+b+1)), la.norm(a_plus_b)) (C-way) use options keyword of Program.build to pass build options directly to clBuildProgram(): import pyopencl as cl import numpy import numpy.linalg as la a = numpy.random.rand(50000).astype(numpy.float32) b = numpy.random.rand(50000).astype(numpy.float32) ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) mf = cl.mem_flags a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a) b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b) dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes) prg = cl.Program(ctx, """ __kernel void sum(__global const float *a, __global const float *b, __global float *c) { int gid = get_global_id(AXIS); c[gid] = a[gid] + b[gid] + COEFF; } """).build(options=['-D', 'AXIS=0', '-D', 'COEFF=1']) prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf) a_plus_b = numpy.empty_like(a) cl.enqueue_copy(queue, a_plus_b, dest_buf) print(la.norm(a_plus_b - (a+b+1)), la.norm(a_plus_b)) (I have used the modifed source code from the main page of PyOpenCL docs. Tested on pyopencl 2013.1).
initialising a 4d vector
I wrote this code: #include<vector> using namespace std; int i; int j; cin>>i>>j; vector<vector<vector<vector<double> > > > boy_max(i,vector<double>(i,vector<double>(j,vector<double>(j,0)))); During compilation the following error came up: prog.cpp: In function ‘int main()’: prog.cpp:164: error: no matching function for call to ‘std::vector<double, std::allocator<double> >::vector(int&, std::vector<double, std::allocator<double> >)’ /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:247: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = double, _Alloc = std::allocator<double>] /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:234: note: std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = double, _Alloc = std::allocator<double>] /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:221: note: std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = double, _Alloc = std::allocator<double>] /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:213: note: std::vector<_Tp, _Alloc>::vector() [with _Tp = double, _Alloc = std::allocator<double>]
Your initialization line is wrong. It should be more like this: vector<vector<vector<vector<double> > > > boy_max(i,vector<vector<vector<double> > >(i,vector<vector<double> >(j,vector<double>(j,0))));
How to wrap a C function whose parameter is wchar_t pointer with cython
I want to use cython to wrap a C library. One function in the library is like int hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen); There are two questions: What can I do with the wchar_t in cython; How to convert the string pointer in my .pyx file.
Declare wchar_t: cdef extern from "stddef.h": ctypedef void wchar_t Or import from libc module: from libc.stddef cimport wchar_t A function to convert wchar_t to python string by using WideCharToMultiByte (see CefStringToPyString): # Declare these in .pxd file: # # cdef extern from "Windows.h": # cdef int CP_UTF8 # cdef int WideCharToMultiByte(int, int, wchar_t*, int, char*, int, char*, int*) cdef object WideCharToPyString(wchar_t *wcharstr): cdef int charstr_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, NULL, 0, NULL, NULL) # Do not use malloc, otherwise you get trash data when string is empty. cdef char* charstr = <char*>calloc(charstr_bytes, sizeof(char)) cdef int copied_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, charstr, charstr_bytes, NULL, NULL) if bytes == str: pystring = "" + charstr # Python 2.7 else: pystring = (b"" + charstr).decode("utf-8", "ignore") # Python 3 free(charstr) return pystring Since Python 3.2 you can do this with the use of PyUnicode_FromWideChar(wcharstr, -1), see the comment by compostus.