How to copy values from uint2 to one vector in thrust? - vector

I am having values in vector<uint2> results (size) I just wanna copy the values of results.y alone to a vector<int> count (size). How can I do this using thrust::transform function?

You need to declare some kind of function object (it can be either UnaryFunction or BinaryFunction that will select second element from uint2. You can use lambda if you enable --expt-extended-lambda in nvcc:
auto selector = [&] __device__ (const uint2& pair) { return pair.y; };
You can use function object instead:
struct Selector
{
__host__ __device__ unsigned int operator()(const uint2& pair)
{
return pair.y;
}
};
And then use it in thrust::transform:
thrust::transform(results.begin(), results.end(), count.begin(), selector);
or
Selector selectorObject;
thrust::transform(results.begin(), results.end(), count.begin(), selectorObject);

Related

Function "Find" of vectors with classes

I need some way to know if in my vector n, there is the name that the user writes (although for this example it is "Diana"). I need to know if it is possible to know the position in which it determines that the name attribute of any of the classes of the vector is equal to the name entered.
public:
string name;
Name(string name){
this->name=name;
}
};
int main(){
Name n1("cesar"),n2("diana"),n3("julian");
//vector<int>it={1,2,3,4};
vector<Name> n={n1, n2,n3};
vector<int>::iterator it;
it = find(n.begin(), n.end(), "diana");
if (/* ???? it != end(n) */ ){
cout<<"Encontrado";
}else{
cout<<"no encontrado";
}
system("PAUSE");
return 0;
}

static vector in recursion vs global vector

USING GLOBAL VECTOR
vector<int> r;
vector<int> inorderTraversal(TreeNode* root) {
if(root==NULL)
return r;
inorderTraversal(root->left);
r.push_back(root->val);
inorderTraversal(root->right);
return r;
}
I am getting answer for all test cases but when using below code I am getting answer when running separately but when I finally submit test cases are failing(input of empty tree) like I am getting Output for some other input why is this happenning?
vector<int> inorderTraversal(TreeNode* root) {
static vector<int> r;
if(root==NULL)
return r;
inorderTraversal(root->left);
r.push_back(root->val);
inorderTraversal(root->right);
return r;
}
If you say the first version works, then I assume your "global" variable is actually an instance variable of a class. In that case that variable is a new variable every time the test suite creates a new instance of the class.
A static local variable however will retain its value even when the function is executed on a new instance of the class of which it is a method.
A solution is to not make it static, and then overload the method, so it can take that r as second parameter:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> r;
return inorderTraversal(root, r);
}
vector<int> inorderTraversal(TreeNode* root, vector<int> r) {
if(root==NULL)
return r;
inorderTraversal(root->left, r);
r.push_back(root->val);
inorderTraversal(root->right, r);
return r;
}

Accessing a structure in a function type void

I've seen some other posts similar but I can't seem to figure it out. This function has this function definition:
struct number {
int one;
int two;
int three;
int four;
};
void foo(int move, struct number *f) {
//....
}
bool tester(void (*fp)(int, struct number *)) {
//...
}
Now, in the function 'foo', my function is taking a structure (as seen), but it is changes the values in the structure (that's the purpose of this function 'foo')
Now, inside the function 'tester', I want to be able to access the new structure that 'foo' gives me when I call to it.
For example, if I had this:
struct number x1 = {1, 2, 3, 4}
I was thinking of writing something like this is 'tester':
foo(1, &x1)
But I have no idea how to access the new structure as a result from foo(1, &x1).
I want the function to change the values in of the struct 'x1' and then access them, inside of 'tester'.
You can simply access the changed values by accessing (the now changed) x1, e. g. x1.one.

MQL4/5 CList Search method always returning null pointer

I'm trying to use the CList Search method in an application. I have attached a very simple example below.
In this example, I always get a null pointer in the variable result. I tried it in MQL4 and MQL5. Has anyone ever made the Search method work? If so, where is my mistake? With my question, I refer to this implementation of a linked list in MQL (it's the standard implementation). Of course, in my application, I do not want to find the first list item, but items that match specific criteria. But even this trivial example does not work for me.
#property strict
#include <Arrays\List.mqh>
#include <Object.mqh>
class MyType : public CObject {
private:
int val;
public:
MyType(int val);
int GetVal(void);
};
MyType::MyType(int val): val(val) {}
int MyType::GetVal(void) {
return val;
}
void OnStart() {
CList *list = new CList();
list.Add(new MyType(3));
// This returns a valid pointer with
// the correct value
MyType* first = list.GetFirstNode();
// This always returns NULL, even though the list
// contains its first element
MyType* result = list.Search(first);
delete list;
}
CList is a kind of linked list. A classical arraylist is CArrayObj in MQL4/5 with Search() and some other methods. You have to sort the list (so implement virtual int Compare(const CObject *node,const int mode=0) const method) before calling search.
virtual int MyType::Compare(const CObject *node,const int mode=0) const {
MyType *another=(MyType*)node;
return this.val-another.GetVal();
}
void OnStart(){
CArrayObj list=new CArrayObj();
list.Add(new MyType(3));
list.Add(new MyType(4));
list.Sort();
MyType *obj3=new MyType(3), *obj2=new MyType(2);
int index3=list.Search(obj3);//found, 0
int index2=list.Search(obj2);//not found, -1
delete(obj3);
delete(obj2);
}

Pass a typed function as a parameter in Dart

I know the Function class can be passed as a parameter to another function, like this:
void doSomething(Function f) {
f(123);
}
But is there a way to constrain the arguments and the return type of the function parameter?
For instance, in this case f is being invoked directly on an integer, but what if it was a function accepting a different type?
I tried passing it as a Function<Integer>, but Function is not a parametric type.
Is there any other way to specify the signature of the function being passed as a parameter?
Dart v1.23 added a new syntax for writing function types which also works in-line.
void doSomething(Function(int) f) {
f(123);
}
It has the advantage over the function-parameter syntax that you can also use it for variables or anywhere else you want to write a type.
void doSomething(Function(int) f) {
Function(int) g = f;
g(123);
}
var x = <int Function(int)>[];
int Function(int) returnsAFunction() => (int x) => x + 1;
int Function(int) Function() functionValue = returnsAFunction;
To strongly type a function in dart do the following:
Write down the Function keyword
Function
Prefix it with its return type (for example void)
void Function
Append it with parentheses
void Function()
Put comma separated arguments inside the parentheses
void Function(int, int)
Optionally - give names to your arguments
void Function(int foo, int bar)
Real life example:
void doSomething(void Function(int arg) f) {
f(123);
}
Edit: Note that this answer contains outdated information. See Irn's answer for more up-to-date information.
Just to expand on Randal's answer, your code might look something like:
typedef void IntegerArgument(int x);
void doSomething(IntegerArgument f) {
f(123);
}
Function<int> seems like it would be a nice idea but the problem is that we might want to specify return type as well as the type of an arbitrary number of arguments.
For reference.
int execute(int func(int a, int b)) => func(4, 3);
print(execute((a, b) => a + b));
You can have a function typed parameter or use a typedef
void main() {
doSomething(xToString);
doSomething2(xToString);
}
String xToString(int s) => 's';
typedef String XToStringFn(int s);
void doSomething(String f(int s)) {
print('value: ${f(123)}');
}
void doSomething2(XToStringFn f) {
print('value: ${f(123)}');
}
DartPad example
This is what typedefs are for!

Resources