// Copyright (c) 2020-2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <thrust/device_vector.h>
#include <thrust/random.h>
#include <thrust/sort.h>

#include <iomanip>
#include <iostream>

#include "include/host_device.h"

// Helper routines

void initialize(thrust::device_vector<int>& v)
{
  thrust::default_random_engine rng(123456);
  thrust::uniform_int_distribution<int> dist(10, 99);
  for (size_t i = 0; i < v.size(); i++)
  {
    v[i] = dist(rng);
  }
}

void initialize(thrust::device_vector<float>& v)
{
  thrust::default_random_engine rng(123456);
  thrust::uniform_int_distribution<int> dist(2, 19);
  for (size_t i = 0; i < v.size(); i++)
  {
    v[i] = dist(rng) / 2.0f;
  }
}

void initialize(thrust::device_vector<thrust::pair<int, int>>& v)
{
  thrust::default_random_engine rng(123456);
  thrust::uniform_int_distribution<int> dist(0, 9);
  for (size_t i = 0; i < v.size(); i++)
  {
    int a = dist(rng);
    int b = dist(rng);
    v[i]  = thrust::make_pair(a, b);
  }
}

void initialize(thrust::device_vector<int>& v1, thrust::device_vector<int>& v2)
{
  thrust::default_random_engine rng(123456);
  thrust::uniform_int_distribution<int> dist(10, 99);
  for (size_t i = 0; i < v1.size(); i++)
  {
    v1[i] = dist(rng);
    v2[i] = static_cast<int>(i);
  }
}

void print(const thrust::device_vector<int>& v)
{
  for (size_t i = 0; i < v.size(); i++)
  {
    std::cout << " " << v[i];
  }
  std::cout << "\n";
}

void print(const thrust::device_vector<float>& v)
{
  for (size_t i = 0; i < v.size(); i++)
  {
    std::cout << " " << std::fixed << std::setprecision(1) << v[i];
  }
  std::cout << "\n";
}

void print(const thrust::device_vector<thrust::pair<int, int>>& v)
{
  for (size_t i = 0; i < v.size(); i++)
  {
    thrust::pair<int, int> p = v[i];
    std::cout << " (" << p.first << "," << p.second << ")";
  }
  std::cout << "\n";
}

void print(thrust::device_vector<int>& v1, thrust::device_vector<int> v2)
{
  for (size_t i = 0; i < v1.size(); i++)
  {
    std::cout << " (" << v1[i] << "," << std::setw(2) << v2[i] << ")";
  }
  std::cout << "\n";
}

// user-defined comparison operator that acts like less<int>,
// except even numbers are considered to be smaller than odd numbers
struct evens_before_odds
{
  __host__ __device__ bool operator()(int x, int y)
  {
    if (x % 2 == y % 2)
    {
      return x < y;
    }
    else if (x % 2)
    {
      return false;
    }
    else
    {
      return true;
    }
  }
};

int main(void)
{
  size_t N = 16;

  std::cout << "sorting integers\n";
  {
    thrust::device_vector<int> keys(N);
    initialize(keys);
    print(keys);
    thrust::sort(keys.begin(), keys.end());
    print(keys);
  }

  std::cout << "\nsorting integers (descending)\n";
  {
    thrust::device_vector<int> keys(N);
    initialize(keys);
    print(keys);
    thrust::sort(keys.begin(), keys.end(), thrust::greater<int>());
    print(keys);
  }

  std::cout << "\nsorting integers (user-defined comparison)\n";
  {
    thrust::device_vector<int> keys(N);
    initialize(keys);
    print(keys);
    thrust::sort(keys.begin(), keys.end(), evens_before_odds());
    print(keys);
  }

  std::cout << "\nsorting floats\n";
  {
    thrust::device_vector<float> keys(N);
    initialize(keys);
    print(keys);
    thrust::sort(keys.begin(), keys.end());
    print(keys);
  }

  std::cout << "\nsorting pairs\n";
  {
    thrust::device_vector<thrust::pair<int, int>> keys(N);
    initialize(keys);
    print(keys);
    thrust::sort(keys.begin(), keys.end());
    print(keys);
  }

  std::cout << "\nkey-value sorting\n";
  {
    thrust::device_vector<int> keys(N);
    thrust::device_vector<int> values(N);
    initialize(keys, values);
    print(keys, values);
    thrust::sort_by_key(keys.begin(), keys.end(), values.begin());
    print(keys, values);
  }

  std::cout << "\nkey-value sorting (descending)\n";
  {
    thrust::device_vector<int> keys(N);
    thrust::device_vector<int> values(N);
    initialize(keys, values);
    print(keys, values);
    thrust::sort_by_key(keys.begin(), keys.end(), values.begin(), thrust::greater<int>());
    print(keys, values);
  }

  return 0;
}
