Is it possible in C++/CX to have a Vector of Vectors? I'm just curious how the syntax would look. Currently, I have...
Vector<String^>^
...and I have found that I may actually need to have many of these Vector's, so I was hoping to use another Vector to contain all of these Vector's. Is that possible?
I have tried...
Vector<Vector<String^>^>^ blah;
...and that seems to compile, but then when I try to initialize it using...
Vector<Vector<String^>^>^ blah = ref new Vector<Vector<String^>^>();
...the compile fails with error...
"'get': signature of public member contains native type 'std::equal_to<_Ty>'"
This seems to work...
Vector<IVector<String^>^>^ blah = ref new Vector<IVector<String^>^>();
Note that it's a Vector of IVector now, which apparently compiles.
Related
I'm always battling eclipse underlining properties or methods called upon objects I reference using bracket notation. Here's a simple example. Looking for most efficient and succinct way to tell compiler that this is a file, and therefor .write is valid, and so don't underline it.
For novice readers, I'm making a map on the fly to add the file and it's contents to the arraylist.
def myOutputList = new ArrayList()
def myFileObject = new File("outputFile.txt")
def myFileContents = "Whargarble"
myOutputList.add([
file:myFileObject,
contents:myFileContents
])
myOutputList.each{
it['file'].write(it['contents'])
}
You can state the type explicitly - then Eclipse will cooperate.
In your example:
File f = it['file']
f.write...
You can also use a class instead of a map (again, with explicit types)
If you need collections of pairs, why don't you use map instead of list? :) But be aware that keys are unique in map.
def myOutputMap = [:]
def myFileObject = new File("outputFile.txt")
def myFileContents = "Whargarble"
myOutputMap[myFileObject] = myFileContents
myOutputMap.each { file, content ->
file.write(content)
}
If Eclipse will still complain about type, define type
myOutputMap.each { File file, String content ->
file.write(content)
}
If you want to stay with lists, you might try Raz suggestion.
And one more small thing.
def myOutputList = []
Is much nicer way to define ArrayList! :)
The answer posted by Raz Abramov is a good solution. I do wish there was an "inline" way to do it. I did find one such way (below) but I prefer your way in this case.
it['file'].invokeMethod('write',(it['contents']))
Also, for those wondering, I tested Raz's solution to make sure it wasn't copying the file object into a new variable (and also, not a new one each time it loops). It was not making any copies. This method creates a temporary pointer to the current it['file'] object and is therefor is very efficient as one would hope. Thanks again Raz.
myOutputList.each{
File f = it['file']
assert myFileObject.is(f)
}
lets say I have written a function for adding two numbers.
function [result] = add_twonum(a1,a2)
result = a1+a2;
endfunction
in the main file, when I call
result = add_twonum(1,2)
I expect result = 3 .
However it says
!--error 4
Undefined variable: add_twonum
Please help me. I have used Matlab a lot but never scilab. I tried every possible way I know of. like changing current directory etc etc.
In the main file, you need to execute the function first, with
exec('add_twonum.sci');
assuming add_twonum.sci is the file which contains your function. Then you can call the function:
result = add_twonum(a1,a2);
I am reading a tutorial and I find it hard to understand how to recurse a single list. Could someone give me a quick explanation of what the base case must be and why, and also what to do in the recursion. My code is:
type(string).
type(int).
instance(X,Y):- X, Y.
variable(_).
statement([]).
statement(A|B):- A, statement(B).
Purpose of the code is to make a light type checker to check things like this:
String s; int i; i = s.length();
I am passing this as a test:
statement([instance(type(string), variable(s))]).
I decided to put it in a list and recurse it and then just put it after the if. If it matches one of the rules, it'll be true. Currently I am just making sure I can get the type instantiation to work. Any help would be welcome! Thanks in advance.
You are missing a pair of square brackets in
statement(A|B)
It should be
statement([A|B])
The rest of your recursive rule looks fine.
I'm trying to make a wrapper for some C-based sparse-matrix-handling code (see previous question). In order to call the workhorse C function, I need to create a structure that looks like this:
struct smat {
long rows;
long cols;
long vals; /* Total non-zero entries. */
long *pointr; /* For each col (plus 1), index of first non-zero entry. */
long *rowind; /* For each nz entry, the row index. */
double *value; /* For each nz entry, the value. */
};
These correspond nicely to the slots in a dgCMatrix sparse matrix. So ideally I'd just point to the internal arrays in the dgCMatrix (after verifying that the C function won't twiddle with the data [which I haven't done yet]).
For *value, it looks like I'll be able to use REALSXP or something to get a double[] as desired. But for *pointr and *rowind, I'm not sure the best way to get at an appropriate array. Will I need to loop through the entries and copy them to new arrays, casting as I go? Or can Rcpp provide some sugar here? This is the first time I've really used Rcpp much and I'm not well-versed in it yet.
Thanks.
Edit: I'm also having some linking trouble that I don't understand:
Error in dyn.load(libLFile) :
unable to load shared object '/var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so':
dlopen(/var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so, 6): Symbol not found: __Z8svdLAS2AP4smatl
Referenced from: /var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so
Expected in: flat namespace
in /var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so
Do I need to be creating my library with some special compilation flags?
Edit 2: it looks like my libargs parameter has no effect, so libsvd symbols never make it into the library. I can find no way to include libraries using cxxfunction() - here's what I'd tried, but the extra parameters (wishful-thinkingly-borrowed from cfunction()) are silently gobbled up:
fn <- cxxfunction(sig=c(nrow="integer", mi="long", mp="long", mx="numeric"),
body=code,
includes="#include <svdlib.h>\n",
cppargs="-I/Users/u0048513/Downloads/SVDLIBC",
libargs="-L/Users/u0048513/Downloads/SVDLIBC -lsvd",
plugin="Rcpp",
verbose=TRUE)
I feel like I'm going about this whole process wrong, since nothing's working. Anyone kick me in the right direction?
I decided to also post a query on the Rcpp-devel mailing list, and got some good advice & help from Dirk and Doug:
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-February/001851.html
I'm still not super-facile with this stuff, but getting there. =)
I've done something similar for a [R]-Smalltalk-interface last year and went about it more generic to be able to pass all data back-and-forth by using byte-arrays:
In C i have:
DLLIMPORT void getLengthOfNextMessage(byte* a);
DLLIMPORT void getNextMessage(byte* a);
In R:
getLengthOfNextMessage <- function() {
tmp1 <- as.raw(rep(0,4))
tmp2<-.C("getLengthOfNextMessage", tmp1)
return(bvToInt(tmp2))
}
receiveMessage <- function() {
#if(getNumberOfMessages()==0) {
# print("error: no messages")
# return();
#}
tmp1<-as.raw(rep(0, getLengthOfNextMessage()+getSizeOfMessages()))
tmp2<-.C("getNextMessage", tmp1)
msg<-as.raw(tmp2[[1]])
print(":::confirm received")
print(bvToInt(msg[13:16]))
# confirmReceived(bvToInt(msg[13:16]))
return(msg)
}
I have commented-out the use of the functions getNumberOfMessages() and confirmReceived() which are specific to the problem i had to solve (multiple back-and-forth communication). Essentially, the code uses the argument byte-array to transfer the information, first the 4-byte-long length-info, then the actual data. This seems less elegant (even to me) than to use structs, but i found it to be more generic and i can hook into any dll, transfering any datatype.
Let's say I have a Foo.fsx script that contains this code:
module Bar =
let foobar = "foo"+"bar"
open Bar
let a = System.Reflection.Assembly.GetExecutingAssembly()
let ty = a.GetType("Foo.Bar") // but it returns null here
How can I achieve that? Thanks!
This is a tricky question, because F# interactive compiles all types into types with some mangled names (and the executing assembly can contain multiple versions of the same type).
I managed to do it using a simple trick - you'd add an additional type to the module - then you can use typeof<..> to get information about this type. The type inside a module is compiled as a nested type, so you can get the name of the type representing the module from this (nested) type:
module Bar =
let foobar = "foo"+"bar"
type A = A
// Get type of 'Bar.A', the name will be something like "FSI_0001+Bar+A",
// so we remove the "+A" from the name and get "FSI_0001+Bar"
let aty = typeof<Bar.A>
let barName = aty.FullName.Substring(0, aty.FullName.Length - "+A".Length)
let barTy = aty.Assembly.GetType(barName)
// Get value of the 'foobar' property - this works!
barTy.GetProperty("foobar").GetValue(null, [| |])
You could probably simply search all types in the assembly looking for +Bar. That would work as well. The benefit of the trick above is that you get a reference to the specific version of the type (if you interactively run the code multiple times, you'll get a reference to the module corresponding to the current Bar.A type)
As a side-note, there were some discussions about supporting moduleof<Bar> (or something like that) in future versions of F# - this is a bit inelegant as it is not a real function/value like typeof, but it would very useful!