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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <unittest/unittest.h>
#include <thrust/binary_search.h>

#include <thrust/detail/allocator/allocator_traits.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/iterator/retag.h>


//////////////////////
// Vector Functions //
//////////////////////

// convert xxx_vector<T1> to xxx_vector<T2> 
template <class ExampleVector, typename NewType> 
struct vector_like
{
    typedef typename ExampleVector::allocator_type alloc;
    typedef typename thrust::detail::allocator_traits<alloc> alloc_traits;
    typedef typename alloc_traits::template rebind_alloc<NewType> new_alloc;
    typedef thrust::detail::vector_base<NewType, new_alloc> type;
};

template <class Vector>
void TestVectorLowerBoundSimple(void)
{
    Vector vec(5);

    vec[0] = 0;
    vec[1] = 2;
    vec[2] = 5;
    vec[3] = 7;
    vec[4] = 8;

    Vector input(10);
    thrust::sequence(input.begin(), input.end());

    typedef typename Vector::difference_type int_type;
    typedef typename vector_like<Vector, int_type>::type IntVector;

    // test with integral output type
    IntVector integral_output(10);
    thrust::lower_bound(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin());
    
    typename IntVector::iterator output_end = thrust::lower_bound(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin());

    ASSERT_EQUAL((output_end - integral_output.begin()), 10);

    ASSERT_EQUAL(integral_output[0], 0);
    ASSERT_EQUAL(integral_output[1], 1);
    ASSERT_EQUAL(integral_output[2], 1);
    ASSERT_EQUAL(integral_output[3], 2);
    ASSERT_EQUAL(integral_output[4], 2);
    ASSERT_EQUAL(integral_output[5], 2);
    ASSERT_EQUAL(integral_output[6], 3);
    ASSERT_EQUAL(integral_output[7], 3);
    ASSERT_EQUAL(integral_output[8], 4);
    ASSERT_EQUAL(integral_output[9], 5);

//    // test with interator output type
//    typedef typename vector_like<Vector, typename Vector::iterator>::type IteratorVector;
//    IteratorVector iterator_output(10);
//    thrust::lower_bound(vec.begin(), vec.end(), input.begin(), input.end(), iterator_output.begin());
//
//    ASSERT_EQUAL(iterator_output[0] - vec.begin(), 0);
//    ASSERT_EQUAL(iterator_output[1] - vec.begin(), 1);
//    ASSERT_EQUAL(iterator_output[2] - vec.begin(), 1);
//    ASSERT_EQUAL(iterator_output[3] - vec.begin(), 2);
//    ASSERT_EQUAL(iterator_output[4] - vec.begin(), 2);
//    ASSERT_EQUAL(iterator_output[5] - vec.begin(), 2);
//    ASSERT_EQUAL(iterator_output[6] - vec.begin(), 3);
//    ASSERT_EQUAL(iterator_output[7] - vec.begin(), 3);
//    ASSERT_EQUAL(iterator_output[8] - vec.begin(), 4);
//    ASSERT_EQUAL(iterator_output[9] - vec.begin(), 5);
}
DECLARE_VECTOR_UNITTEST(TestVectorLowerBoundSimple);


template<typename ForwardIterator, typename InputIterator, typename OutputIterator>
OutputIterator lower_bound(my_system &system, ForwardIterator,ForwardIterator,InputIterator,InputIterator,OutputIterator output)
{
    system.validate_dispatch();
    return output;
}

void TestVectorLowerBoundDispatchExplicit()
{
    thrust::device_vector<int> vec(1);

    my_system sys(0);
    thrust::lower_bound(sys,
                        vec.begin(),
                        vec.end(),
                        vec.begin(),
                        vec.end(),
                        vec.begin());

    ASSERT_EQUAL(true, sys.is_valid());
}
DECLARE_UNITTEST(TestVectorLowerBoundDispatchExplicit);


template<typename ForwardIterator, typename InputIterator, typename OutputIterator>
OutputIterator lower_bound(my_tag, ForwardIterator,ForwardIterator,InputIterator,InputIterator,OutputIterator output)
{
    *output = 13;
    return output;
}

void TestVectorLowerBoundDispatchImplicit()
{
    thrust::device_vector<int> vec(1);

    thrust::lower_bound(thrust::retag<my_tag>(vec.begin()),
                        thrust::retag<my_tag>(vec.end()),
                        thrust::retag<my_tag>(vec.begin()),
                        thrust::retag<my_tag>(vec.end()),
                        thrust::retag<my_tag>(vec.begin()));

    ASSERT_EQUAL(13, vec.front());
}
DECLARE_UNITTEST(TestVectorLowerBoundDispatchImplicit);


template <class Vector>
void TestVectorUpperBoundSimple(void)
{
    Vector vec(5);

    vec[0] = 0;
    vec[1] = 2;
    vec[2] = 5;
    vec[3] = 7;
    vec[4] = 8;

    Vector input(10);
    thrust::sequence(input.begin(), input.end());

    typedef typename Vector::difference_type int_type;
    typedef typename vector_like<Vector, int_type>::type IntVector;

    // test with integral output type
    IntVector integral_output(10);
    typename IntVector::iterator output_end = thrust::upper_bound(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin());

    ASSERT_EQUAL((output_end - integral_output.begin()), 10);

    ASSERT_EQUAL(integral_output[0], 1);
    ASSERT_EQUAL(integral_output[1], 1);
    ASSERT_EQUAL(integral_output[2], 2);
    ASSERT_EQUAL(integral_output[3], 2);
    ASSERT_EQUAL(integral_output[4], 2);
    ASSERT_EQUAL(integral_output[5], 3);
    ASSERT_EQUAL(integral_output[6], 3);
    ASSERT_EQUAL(integral_output[7], 4);
    ASSERT_EQUAL(integral_output[8], 5);
    ASSERT_EQUAL(integral_output[9], 5);

//    // test with interator output type
//    typedef typename vector_like<Vector, typename Vector::iterator>::type IteratorVector;
//    IteratorVector iterator_output(10);
//    thrust::lower_bound(vec.begin(), vec.end(), input.begin(), input.end(), iterator_output.begin());
//
//    ASSERT_EQUAL(iterator_output[0] - vec.begin(), 1);
//    ASSERT_EQUAL(iterator_output[1] - vec.begin(), 1);
//    ASSERT_EQUAL(iterator_output[2] - vec.begin(), 2);
//    ASSERT_EQUAL(iterator_output[3] - vec.begin(), 2);
//    ASSERT_EQUAL(iterator_output[4] - vec.begin(), 2);
//    ASSERT_EQUAL(iterator_output[5] - vec.begin(), 3);
//    ASSERT_EQUAL(iterator_output[6] - vec.begin(), 3);
//    ASSERT_EQUAL(iterator_output[7] - vec.begin(), 4);
//    ASSERT_EQUAL(iterator_output[8] - vec.begin(), 5);
//    ASSERT_EQUAL(iterator_output[9] - vec.begin(), 5);
}
DECLARE_VECTOR_UNITTEST(TestVectorUpperBoundSimple);


template<typename ForwardIterator, typename InputIterator, typename OutputIterator>
OutputIterator upper_bound(my_system &system, ForwardIterator,ForwardIterator,InputIterator,InputIterator,OutputIterator output)
{
    system.validate_dispatch();
    return output;
}

void TestVectorUpperBoundDispatchExplicit()
{
    thrust::device_vector<int> vec(1);

    my_system sys(0);
    thrust::upper_bound(sys,
                        vec.begin(),
                        vec.end(),
                        vec.begin(),
                        vec.end(),
                        vec.begin());

    ASSERT_EQUAL(true, sys.is_valid());
}
DECLARE_UNITTEST(TestVectorUpperBoundDispatchExplicit);


template<typename ForwardIterator, typename InputIterator, typename OutputIterator>
OutputIterator upper_bound(my_tag, ForwardIterator,ForwardIterator,InputIterator,InputIterator,OutputIterator output)
{
    *output = 13;
    return output;
}

void TestVectorUpperBoundDispatchImplicit()
{
    thrust::device_vector<int> vec(1);

    thrust::upper_bound(thrust::retag<my_tag>(vec.begin()),
                        thrust::retag<my_tag>(vec.end()),
                        thrust::retag<my_tag>(vec.begin()),
                        thrust::retag<my_tag>(vec.end()),
                        thrust::retag<my_tag>(vec.begin()));

    ASSERT_EQUAL(13, vec.front());
}
DECLARE_UNITTEST(TestVectorUpperBoundDispatchImplicit);


template <class Vector>
void TestVectorBinarySearchSimple(void)
{
    Vector vec(5);

    vec[0] = 0;
    vec[1] = 2;
    vec[2] = 5;
    vec[3] = 7;
    vec[4] = 8;

    Vector input(10);
    thrust::sequence(input.begin(), input.end());

    typedef typename vector_like<Vector, bool>::type BoolVector;
    typedef typename Vector::difference_type int_type;
    typedef typename vector_like<Vector,  int_type>::type IntVector;

    // test with boolean output type
    BoolVector bool_output(10);
    typename BoolVector::iterator bool_output_end = thrust::binary_search(vec.begin(), vec.end(), input.begin(), input.end(), bool_output.begin());

    ASSERT_EQUAL((bool_output_end - bool_output.begin()), 10);

    ASSERT_EQUAL(bool_output[0],  true);
    ASSERT_EQUAL(bool_output[1], false);
    ASSERT_EQUAL(bool_output[2],  true);
    ASSERT_EQUAL(bool_output[3], false);
    ASSERT_EQUAL(bool_output[4], false);
    ASSERT_EQUAL(bool_output[5],  true);
    ASSERT_EQUAL(bool_output[6], false);
    ASSERT_EQUAL(bool_output[7],  true);
    ASSERT_EQUAL(bool_output[8],  true);
    ASSERT_EQUAL(bool_output[9], false);
    
    // test with integral output type
    IntVector integral_output(10, 2);
    typename IntVector::iterator int_output_end = thrust::binary_search(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin());

    ASSERT_EQUAL((int_output_end - integral_output.begin()), 10);
    
    ASSERT_EQUAL(integral_output[0], 1);
    ASSERT_EQUAL(integral_output[1], 0);
    ASSERT_EQUAL(integral_output[2], 1);
    ASSERT_EQUAL(integral_output[3], 0);
    ASSERT_EQUAL(integral_output[4], 0);
    ASSERT_EQUAL(integral_output[5], 1);
    ASSERT_EQUAL(integral_output[6], 0);
    ASSERT_EQUAL(integral_output[7], 1);
    ASSERT_EQUAL(integral_output[8], 1);
    ASSERT_EQUAL(integral_output[9], 0);
}
DECLARE_VECTOR_UNITTEST(TestVectorBinarySearchSimple);


template<typename ForwardIterator, typename InputIterator, typename OutputIterator>
OutputIterator binary_search(my_system &system, ForwardIterator,ForwardIterator,InputIterator,InputIterator,OutputIterator output)
{
    system.validate_dispatch();
    return output;
}

void TestVectorBinarySearchDispatchExplicit()
{
    thrust::device_vector<int> vec(1);

    my_system sys(0);
    thrust::binary_search(sys,
                          vec.begin(),
                          vec.end(),
                          vec.begin(),
                          vec.end(),
                          vec.begin());

    ASSERT_EQUAL(true, sys.is_valid());
}
DECLARE_UNITTEST(TestVectorBinarySearchDispatchExplicit);


template<typename ForwardIterator, typename InputIterator, typename OutputIterator>
OutputIterator binary_search(my_tag, ForwardIterator,ForwardIterator,InputIterator,InputIterator,OutputIterator output)
{
    *output = 13;
    return output;
}

void TestVectorBinarySearchDispatchImplicit()
{
    thrust::device_vector<int> vec(1);

    thrust::binary_search(thrust::retag<my_tag>(vec.begin()),
                          thrust::retag<my_tag>(vec.end()),
                          thrust::retag<my_tag>(vec.begin()),
                          thrust::retag<my_tag>(vec.end()),
                          thrust::retag<my_tag>(vec.begin()));

    ASSERT_EQUAL(13, vec.front());
}
DECLARE_UNITTEST(TestVectorBinarySearchDispatchImplicit);


template <typename T>
struct TestVectorLowerBound
{
  void operator()(const size_t n)
  {
    thrust::host_vector<T>   h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end());
    thrust::device_vector<T> d_vec = h_vec;

    thrust::host_vector<T>   h_input = unittest::random_integers<T>(2*n);
    thrust::device_vector<T> d_input = h_input;
    
    typedef typename thrust::host_vector<T>::difference_type int_type;
    thrust::host_vector<int_type>   h_output(2*n);
    thrust::device_vector<int_type> d_output(2*n);

    thrust::lower_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin());
    thrust::lower_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin());

    ASSERT_EQUAL(h_output, d_output);
  }
};
VariableUnitTest<TestVectorLowerBound, SignedIntegralTypes> TestVectorLowerBoundInstance;


template <typename T>
struct TestVectorUpperBound
{
  void operator()(const size_t n)
  {
    thrust::host_vector<T>   h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end());
    thrust::device_vector<T> d_vec = h_vec;

    thrust::host_vector<T>   h_input = unittest::random_integers<T>(2*n);
    thrust::device_vector<T> d_input = h_input;
    
    typedef typename thrust::host_vector<T>::difference_type int_type;
    thrust::host_vector<int_type>   h_output(2*n);
    thrust::device_vector<int_type> d_output(2*n);

    thrust::upper_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin());
    thrust::upper_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin());

    ASSERT_EQUAL(h_output, d_output);
  }
};
VariableUnitTest<TestVectorUpperBound, SignedIntegralTypes> TestVectorUpperBoundInstance;

template <typename T>
struct TestVectorBinarySearch
{
  void operator()(const size_t n)
  {
    thrust::host_vector<T>   h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end());
    thrust::device_vector<T> d_vec = h_vec;

    thrust::host_vector<T>   h_input = unittest::random_integers<T>(2*n);
    thrust::device_vector<T> d_input = h_input;
    
    typedef typename thrust::host_vector<T>::difference_type int_type;
    thrust::host_vector<int_type>   h_output(2*n);
    thrust::device_vector<int_type> d_output(2*n);

    thrust::binary_search(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin());
    thrust::binary_search(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin());

    ASSERT_EQUAL(h_output, d_output);
  }
};
VariableUnitTest<TestVectorBinarySearch, SignedIntegralTypes> TestVectorBinarySearchInstance;

template <typename T>
struct TestVectorLowerBoundDiscardIterator
{
  void operator()(const size_t n)
  {
    thrust::host_vector<T>   h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end());
    thrust::device_vector<T> d_vec = h_vec;

    thrust::host_vector<T>   h_input = unittest::random_integers<T>(2*n);
    thrust::device_vector<T> d_input = h_input;
    
    thrust::discard_iterator<> h_result =
      thrust::lower_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), thrust::make_discard_iterator());
    thrust::discard_iterator<> d_result =
      thrust::lower_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), thrust::make_discard_iterator());

    thrust::discard_iterator<> reference(2*n);

    ASSERT_EQUAL_QUIET(reference, h_result);
    ASSERT_EQUAL_QUIET(reference, d_result);
  }
};
VariableUnitTest<TestVectorLowerBoundDiscardIterator, SignedIntegralTypes> TestVectorLowerBoundDiscardIteratorInstance;


template <typename T>
struct TestVectorUpperBoundDiscardIterator
{
  void operator()(const size_t n)
  {
    thrust::host_vector<T>   h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end());
    thrust::device_vector<T> d_vec = h_vec;

    thrust::host_vector<T>   h_input = unittest::random_integers<T>(2*n);
    thrust::device_vector<T> d_input = h_input;

    thrust::discard_iterator<> h_result =
      thrust::upper_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), thrust::make_discard_iterator());
    thrust::discard_iterator<> d_result =
      thrust::upper_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), thrust::make_discard_iterator());

    thrust::discard_iterator<> reference(2*n);

    ASSERT_EQUAL_QUIET(reference, h_result);
    ASSERT_EQUAL_QUIET(reference, d_result);
  }
};
VariableUnitTest<TestVectorUpperBoundDiscardIterator, SignedIntegralTypes> TestVectorUpperBoundDiscardIteratorInstance;

template <typename T>
struct TestVectorBinarySearchDiscardIterator
{
  void operator()(const size_t n)
  {
    thrust::host_vector<T>   h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end());
    thrust::device_vector<T> d_vec = h_vec;

    thrust::host_vector<T>   h_input = unittest::random_integers<T>(2*n);
    thrust::device_vector<T> d_input = h_input;

    thrust::discard_iterator<> h_result =
      thrust::binary_search(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), thrust::make_discard_iterator());
    thrust::discard_iterator<> d_result =
      thrust::binary_search(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), thrust::make_discard_iterator());

    thrust::discard_iterator<> reference(2*n);

    ASSERT_EQUAL_QUIET(reference, h_result);
    ASSERT_EQUAL_QUIET(reference, d_result);
  }
};
VariableUnitTest<TestVectorBinarySearchDiscardIterator, SignedIntegralTypes> TestVectorBinarySearchDiscardIteratorInstance;