MCPcopy Create free account
hub / github.com/boostorg/compute / main

Function main

example/longest_vector.cpp:25–58  ·  view source on GitHub ↗

this example shows how to use the max_element() algorithm along with a transform_iterator and the length() function to find the longest 4-component vector in an array of vectors

Source from the content-addressed store, hash-verified

23// a transform_iterator and the length() function to find the longest
24// 4-component vector in an array of vectors
25int main()
26{
27 using compute::float4_;
28
29 // vectors data
30 float data[] = { 1.0f, 2.0f, 3.0f, 0.0f,
31 4.0f, 5.0f, 6.0f, 0.0f,
32 7.0f, 8.0f, 9.0f, 0.0f,
33 0.0f, 0.0f, 0.0f, 0.0f };
34
35 // create device vector with the vector data
36 compute::vector<float4_> vector(
37 reinterpret_cast<float4_ *>(data),
38 reinterpret_cast<float4_ *>(data) + 4
39 );
40
41 // find the longest vector
42 compute::vector<float4_>::const_iterator iter =
43 compute::max_element(
44 compute::make_transform_iterator(
45 vector.begin(), compute::length<float4_>()
46 ),
47 compute::make_transform_iterator(
48 vector.end(), compute::length<float4_>()
49 )
50 ).base();
51
52 // print the index of the longest vector
53 std::cout << "longest vector index: "
54 << std::distance(vector.begin(), iter)
55 << std::endl;
56
57 return 0;
58}

Callers

nothing calls this directly

Calls 4

max_elementFunction · 0.85
make_transform_iteratorFunction · 0.85
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected