// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2018 Andy Davis <andydavis@google.com>
// Copyright (C) 2018 Eugene Zhulenev <ezhulenev@google.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "main.h"

#include <algorithm>
#include <set>

#include <Eigen/CXX11/Tensor>

using Eigen::Tensor;
using Eigen::Index;
using Eigen::RowMajor;
using Eigen::ColMajor;
using Eigen::internal::TensorBlockShapeType;

template<typename T>
static const T& choose(int layout, const T& col, const T& row) {
  return layout == ColMajor ? col : row;
}

static TensorBlockShapeType RandomShape() {
  return internal::random<bool>()
         ? TensorBlockShapeType::kUniformAllDims
         : TensorBlockShapeType::kSkewedInnerDims;
}

template <int NumDims>
static size_t RandomTargetSize(const DSizes<Index, NumDims>& dims) {
  return internal::random<size_t>(1, dims.TotalSize());
}

template <int NumDims>
static DSizes<Index, NumDims> RandomDims() {
  array<Index, NumDims> dims;
  for (int i = 0; i < NumDims; ++i) {
    dims[i] = internal::random<int>(1, 20);
  }
  return DSizes<Index, NumDims>(dims);
}

template <typename T>
static T* GenerateRandomData(const Index& size) {
  T* data = new T[size];
  for (int i = 0; i < size; ++i) {
    data[i] = internal::random<T>();
  }
  return data;
}

template <int NumDims>
static void Debug(DSizes<Index, NumDims> dims) {
  for (int i = 0; i < NumDims; ++i) {
    std::cout << dims[i] << "; ";
  }
  std::cout << std::endl;
}

template <int Layout>
static void test_block_mapper_sanity()
{
  typedef internal::TensorBlockMapper<2, Layout> TensorBlockMapper;

  DSizes<Index, 2> tensor_dims(100, 100);

  // Test uniform blocks.
  TensorBlockMapper uniform_block_mapper(
      tensor_dims, {TensorBlockShapeType::kUniformAllDims, 100});

  VERIFY_IS_EQUAL(uniform_block_mapper.blockCount(), 100);
  VERIFY_IS_EQUAL(uniform_block_mapper.blockTotalSize(), 100);

  // 10x10 blocks
  auto uniform_b0 = uniform_block_mapper.blockDescriptor(0);
  VERIFY_IS_EQUAL(uniform_b0.dimensions().at(0), 10);
  VERIFY_IS_EQUAL(uniform_b0.dimensions().at(1), 10);

  // Test skewed to inner dims blocks.
  TensorBlockMapper skewed_block_mapper(
      tensor_dims, {TensorBlockShapeType::kSkewedInnerDims, 100});

  VERIFY_IS_EQUAL(skewed_block_mapper.blockCount(), 100);
  VERIFY_IS_EQUAL(skewed_block_mapper.blockTotalSize(), 100);

  // 1x100 (100x1) rows/cols depending on a tensor layout.
  auto skewed_b0 = skewed_block_mapper.blockDescriptor(0);
  VERIFY_IS_EQUAL(skewed_b0.dimensions().at(0), choose(Layout, 100, 1));
  VERIFY_IS_EQUAL(skewed_b0.dimensions().at(1), choose(Layout, 1, 100));
}

// Given a TensorBlock "visit" every element accessible though it, and a keep an
// index in the visited set. Verify that every coeff accessed only once.
template<int NumDims, int Layout>
static void UpdateCoeffSet(
    const DSizes<Index, NumDims>& tensor_strides,
    const internal::TensorBlockDescriptor<NumDims>& block,
    Index first_coeff_index, int dim_index, std::set<Index>* visited_coeffs) {
  const DSizes<Index, NumDims>& block_sizes = block.dimensions();

  for (int i = 0; i < block_sizes[dim_index]; ++i) {
    if (tensor_strides[dim_index] == 1) {
      typedef std::pair<std::set<Index>::iterator, bool> ReturnType;
      ReturnType inserted = visited_coeffs->insert(first_coeff_index + i);
      VERIFY_IS_EQUAL(inserted.second, true);
    } else {
      int next_dim_index = dim_index + choose(Layout, -1, 1);
      UpdateCoeffSet<NumDims, Layout>(tensor_strides, block, first_coeff_index,
                                         next_dim_index, visited_coeffs);
      first_coeff_index += tensor_strides[dim_index];
    }
  }
}

template <typename T, int NumDims, int Layout>
static void test_block_mapper_maps_every_element() {
  typedef internal::TensorBlockMapper<NumDims, Layout> TensorBlockMapper;

  DSizes<Index, NumDims> dims = RandomDims<NumDims>();
  DSizes<Index, NumDims> strides = internal::strides<Layout>(dims);

  // Keep track of elements indices available via block access.
  std::set<Index> coeff_set;

  // Try different combinations of block types and sizes.
  TensorBlockMapper block_mapper(dims, {RandomShape(), RandomTargetSize(dims)});

  for (int i = 0; i < block_mapper.blockCount(); ++i) {
    auto block = block_mapper.blockDescriptor(i);
    UpdateCoeffSet<NumDims, Layout>(strides, block, block.offset(),
                                    choose(Layout, NumDims - 1, 0),
                                    &coeff_set);
  }

  // Verify that every coefficient in the original Tensor is accessible through
  // TensorBlock only once.
  Index total_coeffs = dims.TotalSize();
  VERIFY_IS_EQUAL(Index(coeff_set.size()), total_coeffs);
  VERIFY_IS_EQUAL(*coeff_set.begin(), 0);
  VERIFY_IS_EQUAL(*coeff_set.rbegin(), total_coeffs - 1);
}

template <int Layout, int NumDims>
static Index GetInputIndex(Index output_index,
                         const array<Index, NumDims>& output_to_input_dim_map,
                         const array<Index, NumDims>& input_strides,
                         const array<Index, NumDims>& output_strides) {
  int input_index = 0;
  if (Layout == ColMajor) {
    for (int i = NumDims - 1; i > 0; --i) {
      const Index idx = output_index / output_strides[i];
      input_index += idx * input_strides[output_to_input_dim_map[i]];
      output_index -= idx * output_strides[i];
    }
    return input_index +
           output_index * input_strides[output_to_input_dim_map[0]];
  } else {
    for (int i = 0; i < NumDims - 1; ++i) {
      const Index idx = output_index / output_strides[i];
      input_index += idx * input_strides[output_to_input_dim_map[i]];
      output_index -= idx * output_strides[i];
    }
    return input_index +
           output_index * input_strides[output_to_input_dim_map[NumDims - 1]];
  }
}

template <int Layout, int NumDims>
static array<Index, NumDims> ComputeStrides(
    const array<Index, NumDims>& sizes) {
  array<Index, NumDims> strides;
  if (Layout == ColMajor) {
    strides[0] = 1;
    for (int i = 1; i < NumDims; ++i) {
      strides[i] = strides[i - 1] * sizes[i - 1];
    }
  } else {
    strides[NumDims - 1] = 1;
    for (int i = NumDims - 2; i >= 0; --i) {
      strides[i] = strides[i + 1] * sizes[i + 1];
    }
  }
  return strides;
}

template<typename Scalar, typename StorageIndex, int Dim>
class EqualityChecker
{
    const Scalar* input_data;
    const DSizes<StorageIndex, Dim> &input_dims, &input_strides, &output_dims, &output_strides;
    void check_recursive(const Scalar* input, const Scalar* output, int depth=0) const
    {
        if(depth==Dim)
        {
            VERIFY_IS_EQUAL(*input, *output);
            return;
        }

        for(int i=0; i<output_dims[depth]; ++i)
        {
            check_recursive(input + i % input_dims[depth] * input_strides[depth], output + i*output_strides[depth], depth+1);
        }
    }
public:
    EqualityChecker(const Scalar* input_data_,
            const DSizes<StorageIndex, Dim> &input_dims_, const DSizes<StorageIndex, Dim> &input_strides_,
            const DSizes<StorageIndex, Dim> &output_dims_, const DSizes<StorageIndex, Dim> &output_strides_)
        : input_data(input_data_)
        , input_dims(input_dims_), input_strides(input_strides_)
        , output_dims(output_dims_), output_strides(output_strides_)
        {}

    void operator()(const Scalar* output_data) const
    {
        check_recursive(input_data, output_data);
    }
};

template <int Layout>
static void test_uniform_block_shape()
{
  typedef internal::TensorBlockDescriptor<5> TensorBlock;
  typedef internal::TensorBlockMapper<5, Layout> TensorBlockMapper;

  {
    // Test shape 'UniformAllDims' with uniform 'max_coeff count'.
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 5 * 5 * 5 * 5 * 5;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    for (int i = 0; i < 5; ++i) {
      VERIFY_IS_EQUAL(5, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'UniformAllDims' with larger 'max_coeff count' which spills
  // partially into first inner-most dimension.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 7 * 5 * 5 * 5 * 5;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[0]);
    for (int i = 1; i < 5; ++i) {
      VERIFY_IS_EQUAL(5, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 5 * 5 * 5 * 5 * 6;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(6, block.dimensions()[4]);
    for (int i = 3; i >= 0; --i) {
      VERIFY_IS_EQUAL(5, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'UniformAllDims' with larger 'max_coeff count' which spills
  // fully into first inner-most dimension.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 11 * 5 * 5 * 5 * 5;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(11, block.dimensions()[0]);
    for (int i = 1; i < 5; ++i) {
      VERIFY_IS_EQUAL(5, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 5 * 5 * 5 * 5 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    for (int i = 3; i >= 0; --i) {
      VERIFY_IS_EQUAL(5, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'UniformAllDims' with larger 'max_coeff count' which spills
  // fully into first few inner-most dimensions.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(7, 5, 6, 17, 7);
    const Index max_coeff_count = 7 * 5 * 6 * 7 * 5;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[0]);
    VERIFY_IS_EQUAL(5, block.dimensions()[1]);
    VERIFY_IS_EQUAL(6, block.dimensions()[2]);
    VERIFY_IS_EQUAL(7, block.dimensions()[3]);
    VERIFY_IS_EQUAL(5, block.dimensions()[4]);
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(7, 5, 6, 9, 7);
    const Index max_coeff_count = 5 * 5 * 5 * 6 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    VERIFY_IS_EQUAL(6, block.dimensions()[3]);
    VERIFY_IS_EQUAL(5, block.dimensions()[2]);
    VERIFY_IS_EQUAL(5, block.dimensions()[1]);
    VERIFY_IS_EQUAL(5, block.dimensions()[0]);
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'UniformAllDims' with full allocation to all dims.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(7, 5, 6, 17, 7);
    const Index max_coeff_count = 7 * 5 * 6 * 17 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[0]);
    VERIFY_IS_EQUAL(5, block.dimensions()[1]);
    VERIFY_IS_EQUAL(6, block.dimensions()[2]);
    VERIFY_IS_EQUAL(17, block.dimensions()[3]);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(7, 5, 6, 9, 7);
    const Index max_coeff_count = 7 * 5 * 6 * 9 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kUniformAllDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    VERIFY_IS_EQUAL(9, block.dimensions()[3]);
    VERIFY_IS_EQUAL(6, block.dimensions()[2]);
    VERIFY_IS_EQUAL(5, block.dimensions()[1]);
    VERIFY_IS_EQUAL(7, block.dimensions()[0]);
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }
}

template <int Layout>
static void test_skewed_inner_dim_block_shape()
{
  typedef internal::TensorBlockDescriptor<5> TensorBlock;
  typedef internal::TensorBlockMapper<5, Layout> TensorBlockMapper;

  // Test shape 'SkewedInnerDims' with partial allocation to inner-most dim.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 10 * 1 * 1 * 1 * 1;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(10, block.dimensions()[0]);
    for (int i = 1; i < 5; ++i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 1 * 1 * 1 * 1 * 6;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(6, block.dimensions()[4]);
    for (int i = 3; i >= 0; --i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'SkewedInnerDims' with full allocation to inner-most dim.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 11 * 1 * 1 * 1 * 1;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(11, block.dimensions()[0]);
    for (int i = 1; i < 5; ++i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 1 * 1 * 1 * 1 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    for (int i = 3; i >= 0; --i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'SkewedInnerDims' with full allocation to inner-most dim,
  // and partial allocation to second inner-dim.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 11 * 3 * 1 * 1 * 1;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(11, block.dimensions()[0]);
    VERIFY_IS_EQUAL(3, block.dimensions()[1]);
    for (int i = 2; i < 5; ++i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 1 * 1 * 1 * 15 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    VERIFY_IS_EQUAL(15, block.dimensions()[3]);
    for (int i = 2; i >= 0; --i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'SkewedInnerDims' with full allocation to inner-most dim,
  // and partial allocation to third inner-dim.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 11 * 5 * 5 * 1 * 1;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(11, block.dimensions()[0]);
    VERIFY_IS_EQUAL(5, block.dimensions()[1]);
    VERIFY_IS_EQUAL(5, block.dimensions()[2]);
    for (int i = 3; i < 5; ++i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 1 * 1 * 5 * 17 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    VERIFY_IS_EQUAL(17, block.dimensions()[3]);
    VERIFY_IS_EQUAL(5, block.dimensions()[2]);
    for (int i = 1; i >= 0; --i) {
      VERIFY_IS_EQUAL(1, block.dimensions()[i]);
    }
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }

  // Test shape 'SkewedInnerDims' with full allocation to all dims.
  if (Layout == ColMajor) {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 11 * 5 * 6 * 17 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(11, block.dimensions()[0]);
    VERIFY_IS_EQUAL(5, block.dimensions()[1]);
    VERIFY_IS_EQUAL(6, block.dimensions()[2]);
    VERIFY_IS_EQUAL(17, block.dimensions()[3]);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  } else {
    DSizes<Index, 5> dims(11, 5, 6, 17, 7);
    const Index max_coeff_count = 11 * 5 * 6 * 17 * 7;
    TensorBlockMapper
        block_mapper(dims, {TensorBlockShapeType::kSkewedInnerDims,
                            max_coeff_count});
    TensorBlock block = block_mapper.blockDescriptor(0);
    VERIFY_IS_EQUAL(7, block.dimensions()[4]);
    VERIFY_IS_EQUAL(17, block.dimensions()[3]);
    VERIFY_IS_EQUAL(6, block.dimensions()[2]);
    VERIFY_IS_EQUAL(5, block.dimensions()[1]);
    VERIFY_IS_EQUAL(11, block.dimensions()[0]);
    VERIFY(block.dimensions().TotalSize() <= max_coeff_count);
  }
}

template <int Layout>
static void test_empty_dims(const internal::TensorBlockShapeType block_shape)
{
  // Test blocking of tensors with zero dimensions:
  //  - we must not crash on asserts and divisions by zero
  //  - we must not return block with zero dimensions
  //    (recipe for overflows/underflows, divisions by zero and NaNs later)
  //  - total block count must be zero
  {
    typedef internal::TensorBlockMapper<1, Layout> TensorBlockMapper;

    DSizes<Index, 1> dims(0);
    for (size_t max_coeff_count = 0; max_coeff_count < 2; ++max_coeff_count) {
      TensorBlockMapper block_mapper(dims, {block_shape, max_coeff_count});
      VERIFY_IS_EQUAL(block_mapper.blockCount(), 0);
      VERIFY(block_mapper.blockTotalSize() >= 1);
    }
  }

  {
    typedef internal::TensorBlockMapper<2, Layout> TensorBlockMapper;

    for (int dim1 = 0; dim1 < 3; ++dim1) {
      for (int dim2 = 0; dim2 < 3; ++dim2) {
        DSizes<Index, 2> dims(dim1, dim2);
        for (size_t max_coeff_count = 0; max_coeff_count < 2; ++max_coeff_count) {
          TensorBlockMapper block_mapper(dims, {block_shape, max_coeff_count});
          if (dim1 * dim2 == 0) {
            VERIFY_IS_EQUAL(block_mapper.blockCount(), 0);
          }
          VERIFY(block_mapper.blockTotalSize() >= 1);
        }
      }
    }
  }
}

#define TEST_LAYOUTS(NAME) \
  CALL_SUBTEST(NAME<ColMajor>()); \
  CALL_SUBTEST(NAME<RowMajor>())

#define TEST_LAYOUTS_AND_DIMS(TYPE, NAME)    \
  CALL_SUBTEST((NAME<TYPE, 1, ColMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 1, RowMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 2, ColMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 2, RowMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 3, ColMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 3, RowMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 4, ColMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 4, RowMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 5, ColMajor>())); \
  CALL_SUBTEST((NAME<TYPE, 5, RowMajor>()))

#define TEST_LAYOUTS_WITH_ARG(NAME, ARG) \
  CALL_SUBTEST(NAME<ColMajor>(ARG)); \
  CALL_SUBTEST(NAME<RowMajor>(ARG))

EIGEN_DECLARE_TEST(cxx11_tensor_block_access) {
  TEST_LAYOUTS(test_block_mapper_sanity);
  TEST_LAYOUTS_AND_DIMS(float, test_block_mapper_maps_every_element);
  TEST_LAYOUTS(test_uniform_block_shape);
  TEST_LAYOUTS(test_skewed_inner_dim_block_shape);
  TEST_LAYOUTS_WITH_ARG(test_empty_dims, TensorBlockShapeType::kUniformAllDims);
  TEST_LAYOUTS_WITH_ARG(test_empty_dims, TensorBlockShapeType::kSkewedInnerDims);
}

#undef TEST_LAYOUTS
#undef TEST_LAYOUTS_WITH_ARG


#include "guidance/turn_instruction.hpp"

#include "engine/plugins/plugin_base.hpp"
#include "engine/plugins/tile.hpp"

#include "util/coordinate_calculation.hpp"
#include "util/string_view.hpp"
#include "util/vector_tile.hpp"
#include "util/web_mercator.hpp"

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>

#include <vtzero/builder.hpp>
#include <vtzero/geometry.hpp>
#include <vtzero/index.hpp>

#include <algorithm>
#include <numeric>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include <cmath>
#include <cstdint>

namespace osrm
{
namespace engine
{
namespace plugins
{

constexpr const static int MIN_ZOOM_FOR_TURNS = 15;

namespace
{

using RTreeLeaf = datafacade::BaseDataFacade::RTreeLeaf;
// Simple container class for WGS84 coordinates
template <typename T> struct Point final
{
    Point(T _x, T _y) : x(_x), y(_y) {}

    const T x;
    const T y;
};

// Simple container to hold a bounding box
struct BBox final
{
    BBox(const double _minx, const double _miny, const double _maxx, const double _maxy)
        : minx(_minx), miny(_miny), maxx(_maxx), maxy(_maxy)
    {
    }

    double width() const { return maxx - minx; }
    double height() const { return maxy - miny; }

    const double minx;
    const double miny;
    const double maxx;
    const double maxy;
};

// Simple container for integer coordinates (i.e. pixel coords)
struct point_type_i final
{
    point_type_i(std::int64_t _x, std::int64_t _y) : x(_x), y(_y) {}

    const std::int64_t x;
    const std::int64_t y;
};

using FixedPoint = Point<std::int32_t>;
using FloatPoint = Point<double>;

using FixedLine = std::vector<FixedPoint>;
using FloatLine = std::vector<FloatPoint>;

// We use boost::geometry to clip lines/points that are outside or cross the boundary
// of the tile we're rendering.  We need these types defined to use boosts clipping
// logic
typedef boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian> point_t;
typedef boost::geometry::model::linestring<point_t> linestring_t;
typedef boost::geometry::model::box<point_t> box_t;
typedef boost::geometry::model::multi_linestring<linestring_t> multi_linestring_t;
const static box_t clip_box(point_t(-util::vector_tile::BUFFER, -util::vector_tile::BUFFER),
                            point_t(util::vector_tile::EXTENT + util::vector_tile::BUFFER,
                                    util::vector_tile::EXTENT + util::vector_tile::BUFFER));

/**
 * Return the x1,y1,x2,y2 pixel coordinates of a line in a given
 * tile.
 *
 * @param start the first coordinate of the line
 * @param target the last coordinate of the line
 * @param tile_bbox the boundaries of the tile, in mercator coordinates
 * @return a FixedLine with coordinates relative to the tile_bbox.
 */
linestring_t floatLineToTileLine(const FloatLine &geo_line, const BBox &tile_bbox)
{
    linestring_t unclipped_line;

    for (auto const &pt : geo_line)
    {
        double px_merc = pt.x * util::web_mercator::DEGREE_TO_PX;
        double py_merc = util::web_mercator::latToY(util::FloatLatitude{pt.y}) *
                         util::web_mercator::DEGREE_TO_PX;
        // convert lon/lat to tile coordinates
        const auto px = std::round(
            ((px_merc - tile_bbox.minx) * util::web_mercator::TILE_SIZE / tile_bbox.width()) *
            util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE);
        const auto py = std::round(
            ((tile_bbox.maxy - py_merc) * util::web_mercator::TILE_SIZE / tile_bbox.height()) *
            util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE);

        boost::geometry::append(unclipped_line, point_t(px, py));
    }

    return unclipped_line;
}

std::vector<FixedLine> coordinatesToTileLine(const std::vector<util::Coordinate> &points,
                                             const BBox &tile_bbox)
{
    FloatLine geo_line;
    for (auto const &c : points)
    {
        geo_line.emplace_back(static_cast<double>(util::toFloating(c.lon)),
                              static_cast<double>(util::toFloating(c.lat)));
    }

    linestring_t unclipped_line = floatLineToTileLine(geo_line, tile_bbox);

    multi_linestring_t clipped_line;
    boost::geometry::intersection(clip_box, unclipped_line, clipped_line);

    std::vector<FixedLine> result;

    // b::g::intersection might return a line with one point if the
    // original line was very short and coords were dupes
    for (auto const &cl : clipped_line)
    {
        if (cl.size() < 2)
            continue;

        FixedLine tile_line;
        for (const auto &p : cl)
            tile_line.emplace_back(p.get<0>(), p.get<1>());

        result.emplace_back(std::move(tile_line));
    }

    return result;
}

/**
 * Return the x1,y1,x2,y2 pixel coordinates of a line in a given
 * tile.
 *
 * @param start the first coordinate of the line
 * @param target the last coordinate of the line
 * @param tile_bbox the boundaries of the tile, in mercator coordinates
 * @return a FixedLine with coordinates relative to the tile_bbox.
 */
FixedLine coordinatesToTileLine(const util::Coordinate start,
                                const util::Coordinate target,
                                const BBox &tile_bbox)
{
    FloatLine geo_line;
    geo_line.emplace_back(static_cast<double>(util::toFloating(start.lon)),
                          static_cast<double>(util::toFloating(start.lat)));
    geo_line.emplace_back(static_cast<double>(util::toFloating(target.lon)),
                          static_cast<double>(util::toFloating(target.lat)));

    linestring_t unclipped_line = floatLineToTileLine(geo_line, tile_bbox);

    multi_linestring_t clipped_line;
    boost::geometry::intersection(clip_box, unclipped_line, clipped_line);

    FixedLine tile_line;

    // b::g::intersection might return a line with one point if the
    // original line was very short and coords were dupes
    if (!clipped_line.empty() && clipped_line[0].size() == 2)
    {
        for (const auto &p : clipped_line[0])
        {
            tile_line.emplace_back(p.get<0>(), p.get<1>());
        }
    }

    return tile_line;
}

/**
 * Converts lon/lat into coordinates inside a Mercator projection tile (x/y pixel values)
 *
 * @param point the lon/lat you want the tile coords for
 * @param tile_bbox the mercator boundaries of the tile
 * @return a point (x,y) on the tile defined by tile_bbox
 */
FixedPoint coordinatesToTilePoint(const util::Coordinate point, const BBox &tile_bbox)
{
    const FloatPoint geo_point{static_cast<double>(util::toFloating(point.lon)),
                               static_cast<double>(util::toFloating(point.lat))};

    const double px_merc = geo_point.x * util::web_mercator::DEGREE_TO_PX;
    const double py_merc = util::web_mercator::latToY(util::FloatLatitude{geo_point.y}) *
                           util::web_mercator::DEGREE_TO_PX;

    const auto px = static_cast<std::int32_t>(std::round(
        ((px_merc - tile_bbox.minx) * util::web_mercator::TILE_SIZE / tile_bbox.width()) *
        util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE));
    const auto py = static_cast<std::int32_t>(std::round(
        ((tile_bbox.maxy - py_merc) * util::web_mercator::TILE_SIZE / tile_bbox.height()) *
        util::vector_tile::EXTENT / util::web_mercator::TILE_SIZE));

    return FixedPoint{px, py};
}

std::vector<RTreeLeaf> getEdges(const DataFacadeBase &facade, unsigned x, unsigned y, unsigned z)
{
    double min_lon, min_lat, max_lon, max_lat;

    // Convert the z,x,y mercator tile coordinates into WGS84 lon/lat values
    //
    util::web_mercator::xyzToWGS84(
        x, y, z, min_lon, min_lat, max_lon, max_lat, util::web_mercator::TILE_SIZE * 0.10);

    util::Coordinate southwest{util::FloatLongitude{min_lon}, util::FloatLatitude{min_lat}};
    util::Coordinate northeast{util::FloatLongitude{max_lon}, util::FloatLatitude{max_lat}};

    // Fetch all the segments that are in our bounding box.
    // This hits the OSRM StaticRTree
    return facade.GetEdgesInBox(southwest, northeast);
}

std::vector<std::size_t> getEdgeIndex(const std::vector<RTreeLeaf> &edges)
{
    // In order to ensure consistent tile encoding, we need to process
    // all edges in the same order.  Differences in OSX/Linux/Windows
    // sorting methods mean that GetEdgesInBox doesn't return the same
    // ordered array on all platforms.
    // GetEdgesInBox is marked `const`, so we can't sort the array itself,
    // instead we create an array of indexes and sort that instead.
    std::vector<std::size_t> sorted_edge_indexes(edges.size(), 0);
    std::iota(
        sorted_edge_indexes.begin(), sorted_edge_indexes.end(), 0); // fill with 0,1,2,3,...N-1

    // Now, sort that array based on the edges list, using the u/v node IDs
    // as the sort condition
    std::sort(sorted_edge_indexes.begin(),
              sorted_edge_indexes.end(),
              [&edges](const std::size_t &left, const std::size_t &right) -> bool {
                  return (edges[left].u != edges[right].u) ? edges[left].u < edges[right].u
                                                           : edges[left].v < edges[right].v;
              });

    return sorted_edge_indexes;
}

std::vector<NodeID> getSegregatedNodes(const DataFacadeBase &facade,
                                       const std::vector<RTreeLeaf> &edges)
{
    std::vector<NodeID> result;

    for (RTreeLeaf const &e : edges)
    {
        if (e.forward_segment_id.enabled && facade.IsSegregated(e.forward_segment_id.id))
            result.push_back(e.forward_segment_id.id);
    }

    return result;
}

struct SpeedLayer : public vtzero::layer_builder
{

    vtzero::value_index_small_uint uint_index;
    vtzero::value_index<vtzero::double_value_type, float, std::unordered_map> double_index;
    vtzero::value_index_internal<std::unordered_map> string_index;
    vtzero::value_index_bool bool_index;

    vtzero::index_value key_speed;
    vtzero::index_value key_is_small;
    vtzero::index_value key_datasource;
    vtzero::index_value key_weight;
    vtzero::index_value key_duration;
    vtzero::index_value key_name;
    vtzero::index_value key_rate;
    vtzero::index_value key_is_startpoint;

    SpeedLayer(vtzero::tile_builder &tile)
        : layer_builder(tile, "speeds"), uint_index(*this), double_index(*this),
          string_index(*this), bool_index(*this), key_speed(add_key_without_dup_check("speed")),
          key_is_small(add_key_without_dup_check("is_small")),
          key_datasource(add_key_without_dup_check("datasource")),
          key_weight(add_key_without_dup_check("weight")),
          key_duration(add_key_without_dup_check("duration")),
          key_name(add_key_without_dup_check("name")), key_rate(add_key_without_dup_check("rate")),
          key_is_startpoint(add_key_without_dup_check("is_startpoint"))
    {
    }

}; // struct SpeedLayer

class SpeedLayerFeatureBuilder : public vtzero::linestring_feature_builder
{

    SpeedLayer &m_layer;

  public:
    SpeedLayerFeatureBuilder(SpeedLayer &layer, uint64_t id)
        : vtzero::linestring_feature_builder(layer), m_layer(layer)
    {
        set_id(id);
    }

    void set_speed(unsigned int value)
    {
        add_property(m_layer.key_speed, m_layer.uint_index(std::min(value, 127u)));
    }

    void set_is_small(bool value) { add_property(m_layer.key_is_small, m_layer.bool_index(value)); }

    void set_datasource(const std::string &value)
    {
        add_property(m_layer.key_datasource,
                     m_layer.string_index(vtzero::encoded_property_value{value}));
    }

    void set_weight(double value) { add_property(m_layer.key_weight, m_layer.double_index(value)); }

    void set_duration(double value)
    {
        add_property(m_layer.key_duration, m_layer.double_index(value));
    }

    void set_name(const boost::string_ref &value)
    {
        add_property(
            m_layer.key_name,
            m_layer.string_index(vtzero::encoded_property_value{value.data(), value.size()}));
    }

    void set_rate(double value) { add_property(m_layer.key_rate, m_layer.double_index(value)); }

    void set_is_startpoint(bool value)
    {
        add_property(m_layer.key_is_startpoint, m_layer.bool_index(value));
    }

}; // class SpeedLayerFeatureBuilder

struct TurnsLayer : public vtzero::layer_builder
{

    vtzero::value_index<vtzero::sint_value_type, int, std::unordered_map> int_index;
    vtzero::value_index<vtzero::float_value_type, float, std::unordered_map> float_index;
    vtzero::value_index_internal<std::unordered_map> string_index;

    vtzero::index_value key_bearing_in;
    vtzero::index_value key_turn_angle;
    vtzero::index_value key_cost;
    vtzero::index_value key_weight;
    vtzero::index_value key_turn_type;
    vtzero::index_value key_turn_modifier;

    TurnsLayer(vtzero::tile_builder &tile)
        : layer_builder(tile, "turns"), int_index(*this), float_index(*this), string_index(*this),
          key_bearing_in(add_key_without_dup_check("bearing_in")),
          key_turn_angle(add_key_without_dup_check("turn_angle")),
          key_cost(add_key_without_dup_check("cost")),
          key_weight(add_key_without_dup_check("weight")),
          key_turn_type(add_key_without_dup_check("type")),
          key_turn_modifier(add_key_without_dup_check("modifier"))
    {
    }

}; // struct TurnsLayer

class TurnsLayerFeatureBuilder : public vtzero::point_feature_builder
{

    TurnsLayer &m_layer;

  public:
    TurnsLayerFeatureBuilder(TurnsLayer &layer, uint64_t id)
        : vtzero::point_feature_builder(layer), m_layer(layer)
    {
        set_id(id);
    }

    void set_bearing_in(int value)
    {
        add_property(m_layer.key_bearing_in, m_layer.int_index(value));
    }

    void set_turn_angle(int value)
    {
        add_property(m_layer.key_turn_angle, m_layer.int_index(value));
    }

    void set_cost(float value) { add_property(m_layer.key_cost, m_layer.float_index(value)); }

    void set_weight(float value) { add_property(m_layer.key_weight, m_layer.float_index(value)); }

    void set_turn(osrm::guidance::TurnInstruction value)
    {
        const auto type = osrm::guidance::internalInstructionTypeToString(value.type);
        const auto modifier = osrm::guidance::instructionModifierToString(value.direction_modifier);
        add_property(
            m_layer.key_turn_type,
            m_layer.string_index(vtzero::encoded_property_value{type.data(), type.size()}));
        add_property(
            m_layer.key_turn_modifier,
            m_layer.string_index(vtzero::encoded_property_value{modifier.data(), modifier.size()}));
    }
}; // class TurnsLayerFeatureBuilder

void encodeVectorTile(const DataFacadeBase &facade,
                      unsigned x,
                      unsigned y,
                      unsigned z,
                      const std::vector<RTreeLeaf> &edges,
                      const std::vector<std::size_t> &sorted_edge_indexes,
                      const std::vector<routing_algorithms::TurnData> &all_turn_data,
                      const std::vector<NodeID> &segregated_nodes,
                      std::string &pbf_buffer)
{
    vtzero::tile_builder tile;

    const auto get_geometry_id = [&facade](auto edge) {
        return facade.GetGeometryIndex(edge.forward_segment_id.id).id;
    };

    // Convert tile coordinates into mercator coordinates
    double min_mercator_lon, min_mercator_lat, max_mercator_lon, max_mercator_lat;
    util::web_mercator::xyzToMercator(
        x, y, z, min_mercator_lon, min_mercator_lat, max_mercator_lon, max_mercator_lat);
    const BBox tile_bbox{min_mercator_lon, min_mercator_lat, max_mercator_lon, max_mercator_lat};

    // XXX leaving in some superfluous scopes to make diff easier to read.
    {
        {
            // Begin the layer features block
            {
                SpeedLayer speeds_layer{tile};

                // Each feature gets a unique id, starting at 1
                unsigned id = 1;
                for (const auto &edge_index : sorted_edge_indexes)
                {
                    const auto &edge = edges[edge_index];
                    const auto geometry_id = get_geometry_id(edge);

                    // Get coordinates for start/end nodes of segment (NodeIDs u and v)
                    const auto a = facade.GetCoordinateOfNode(edge.u);
                    const auto b = facade.GetCoordinateOfNode(edge.v);
                    // Calculate the length in meters
                    const double length =
                        osrm::util::coordinate_calculation::haversineDistance(a, b);

                    const auto forward_weight_range =
                        facade.GetUncompressedForwardWeights(geometry_id);
                    const auto reverse_weight_range =
                        facade.GetUncompressedReverseWeights(geometry_id);
                    const auto forward_duration_range =
                        facade.GetUncompressedForwardDurations(geometry_id);
                    const auto reverse_duration_range =
                        facade.GetUncompressedReverseDurations(geometry_id);
                    const auto forward_datasource_range =
                        facade.GetUncompressedForwardDatasources(geometry_id);
                    const auto reverse_datasource_range =
                        facade.GetUncompressedReverseDatasources(geometry_id);
                    const auto forward_weight = forward_weight_range[edge.fwd_segment_position];
                    const auto reverse_weight = reverse_weight_range[reverse_weight_range.size() -
                                                                     edge.fwd_segment_position - 1];

                    const auto forward_duration = forward_duration_range[edge.fwd_segment_position];
                    const auto reverse_duration =
                        reverse_duration_range[reverse_duration_range.size() -
                                               edge.fwd_segment_position - 1];
                    const auto forward_datasource_idx =
                        forward_datasource_range(edge.fwd_segment_position);
                    const auto reverse_datasource_idx = reverse_datasource_range(
                        reverse_datasource_range.size() - edge.fwd_segment_position - 1);

                    const auto is_startpoint = edge.is_startpoint;

                    const auto component_id = facade.GetComponentID(edge.forward_segment_id.id);
                    const auto name_id = facade.GetNameIndex(edge.forward_segment_id.id);
                    auto name = facade.GetNameForID(name_id);

                    // If this is a valid forward edge, go ahead and add it to the tile
                    if (forward_duration != 0 && edge.forward_segment_id.enabled)
                    {
                        // Calculate the speed for this line
                        std::uint32_t speed_kmh_idx =
                            static_cast<std::uint32_t>(round(length / forward_duration * 10 * 3.6));

                        // Rate values are in meters per weight-unit - and similar to speeds, we
                        // present 1 decimal place of precision (these values are added as
                        // double/10) lower down
                        std::uint32_t forward_rate =
                            static_cast<std::uint32_t>(round(length / forward_weight * 10.));

                        auto tile_line = coordinatesToTileLine(a, b, tile_bbox);
                        if (!tile_line.empty())
                        {
                            SpeedLayerFeatureBuilder fbuilder{speeds_layer, id};
                            fbuilder.add_linestring_from_container(tile_line);

                            fbuilder.set_speed(speed_kmh_idx);
                            fbuilder.set_is_small(component_id.is_tiny);
                            fbuilder.set_datasource(
                                facade.GetDatasourceName(forward_datasource_idx).to_string());
                            fbuilder.set_weight(forward_weight / 10.0);
                            fbuilder.set_duration(forward_duration / 10.0);
                            fbuilder.set_name(name);
                            fbuilder.set_rate(forward_rate / 10.0);
                            fbuilder.set_is_startpoint(is_startpoint);

                            fbuilder.commit();
                        }
                    }

                    // Repeat the above for the coordinates reversed and using the `reverse`
                    // properties
                    if (reverse_duration != 0 && edge.reverse_segment_id.enabled)
                    {
                        // Calculate the speed for this line
                        std::uint32_t speed_kmh_idx =
                            static_cast<std::uint32_t>(round(length / reverse_duration * 10 * 3.6));

                        // Rate values are in meters per weight-unit - and similar to speeds, we
                        // present 1 decimal place of precision (these values are added as
                        // double/10) lower down
                        std::uint32_t reverse_rate =
                            static_cast<std::uint32_t>(round(length / reverse_weight * 10.));

                        auto tile_line = coordinatesToTileLine(b, a, tile_bbox);
                        if (!tile_line.empty())
                        {
                            SpeedLayerFeatureBuilder fbuilder{speeds_layer, id};
                            fbuilder.add_linestring_from_container(tile_line);

                            fbuilder.set_speed(speed_kmh_idx);
                            fbuilder.set_is_small(component_id.is_tiny);
                            fbuilder.set_datasource(
                                facade.GetDatasourceName(reverse_datasource_idx).to_string());
                            fbuilder.set_weight(reverse_weight / 10.0);
                            fbuilder.set_duration(reverse_duration / 10.0);
                            fbuilder.set_name(name);
                            fbuilder.set_rate(reverse_rate / 10.0);
                            fbuilder.set_is_startpoint(is_startpoint);

                            fbuilder.commit();
                        }
                    }
                }
            }
        }

        // Only add the turn layer to the tile if it has some features (we sometimes won't
        // for tiles of z<16, and tiles that don't show any intersections)
        if (!all_turn_data.empty())
        {
            TurnsLayer turns_layer{tile};
            uint64_t id = 0;
            for (const auto &turn_data : all_turn_data)
            {
                const auto tile_point = coordinatesToTilePoint(turn_data.coordinate, tile_bbox);
                if (boost::geometry::within(point_t(tile_point.x, tile_point.y), clip_box))
                {
                    TurnsLayerFeatureBuilder fbuilder{turns_layer, ++id};
                    fbuilder.add_point(tile_point);

                    fbuilder.set_bearing_in(turn_data.in_angle);
                    fbuilder.set_turn_angle(turn_data.turn_angle);
                    fbuilder.set_cost(turn_data.duration / 10.0);
                    fbuilder.set_weight(turn_data.weight / 10.0);
                    fbuilder.set_turn(turn_data.turn_instruction);

                    fbuilder.commit();
                }
            }
        }

        // OSM Node tile layer
        {
            std::vector<NodeID> internal_nodes;
            internal_nodes.reserve(edges.size() * 2);
            for (const auto &edge : edges)
            {
                internal_nodes.push_back(edge.u);
                internal_nodes.push_back(edge.v);
            }
            std::sort(internal_nodes.begin(), internal_nodes.end());
            auto new_end = std::unique(internal_nodes.begin(), internal_nodes.end());
            internal_nodes.resize(new_end - internal_nodes.begin());

            vtzero::layer_builder osmnodes_layer{tile, "osmnodes"};

            for (const auto &internal_node : internal_nodes)
            {
                const auto coord = facade.GetCoordinateOfNode(internal_node);
                const auto tile_point = coordinatesToTilePoint(coord, tile_bbox);
                if (!boost::geometry::within(point_t(tile_point.x, tile_point.y), clip_box))
                {
                    continue;
                }

                vtzero::point_feature_builder fbuilder{osmnodes_layer};
                fbuilder.set_id(
                    static_cast<OSMNodeID::value_type>(facade.GetOSMNodeIDOfNode(internal_node)));
                fbuilder.add_point(tile_point);
                fbuilder.commit();
            }
        }

        // Internal nodes tile layer
        {
            vtzero::layer_builder internal_nodes_layer{tile, "internal-nodes"};

            for (auto edgeNodeID : segregated_nodes)
            {
                auto const geomIndex = facade.GetGeometryIndex(edgeNodeID);

                std::vector<util::Coordinate> points;
                if (geomIndex.forward)
                {
                    for (auto const nodeID : facade.GetUncompressedForwardGeometry(geomIndex.id))
                        points.push_back(facade.GetCoordinateOfNode(nodeID));
                }
                else
                {
                    for (auto const nodeID : facade.GetUncompressedReverseGeometry(geomIndex.id))
                        points.push_back(facade.GetCoordinateOfNode(nodeID));
                }

                auto tile_lines = coordinatesToTileLine(points, tile_bbox);
                if (!tile_lines.empty())
                {
                    vtzero::linestring_feature_builder fbuilder{internal_nodes_layer};
                    for (auto const &tile_line : tile_lines)
                    {
                        fbuilder.add_linestring_from_container(tile_line);
                    }
                    fbuilder.commit();
                }
            }
        }
    }

    tile.serialize(pbf_buffer);
}
} // namespace

Status TilePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
                                 const api::TileParameters &parameters,
                                 osrm::engine::api::ResultT &result) const
{
    BOOST_ASSERT(parameters.IsValid());

    auto &pbf_buffer = result.get<std::string>();
    const auto &facade = algorithms.GetFacade();
    auto edges = getEdges(facade, parameters.x, parameters.y, parameters.z);
    auto segregated_nodes = getSegregatedNodes(facade, edges);

    auto edge_index = getEdgeIndex(edges);

    std::vector<routing_algorithms::TurnData> turns;

    // If we're zooming into 16 or higher, include turn data.  Why?  Because turns make the map
    // really cramped, so we don't bother including the data for tiles that span a large area.
    if (parameters.z >= MIN_ZOOM_FOR_TURNS && algorithms.HasGetTileTurns())
    {
        turns = algorithms.GetTileTurns(edges, edge_index);
    }

    encodeVectorTile(facade,
                     parameters.x,
                     parameters.y,
                     parameters.z,
                     edges,
                     edge_index,
                     turns,
                     segregated_nodes,
                     pbf_buffer);

    return Status::Ok;
}
} // namespace plugins
} // namespace engine
} // namespace osrm


#include "knn.hpp"
#include "similarities.hpp"
#include <Eigen/Sparse>
#include <cstddef>
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <stdexcept>

namespace py = pybind11;
using Real = double;

PYBIND11_MODULE(_knn, m) {
  py::class_<KNN::CosineSimilarityComputer<Real>>(m, "CosineSimilarityComputer")
      .def(py::init<const KNN::CosineSimilarityComputer<Real>::CSRMatrix &,
                    Real, bool, size_t, size_t>(),
           py::arg("X"), py::arg("shrinkage"), py::arg("normalize"),
           py::arg("n_thread") = 1, py::arg("max_chunk_size") = 128)
      .def("compute_similarity",
           &KNN::CosineSimilarityComputer<Real>::compute_similarity);

  py::class_<KNN::JaccardSimilarityComputer<Real>>(m,
                                                   "JaccardSimilarityComputer")
      .def(py::init<const KNN::JaccardSimilarityComputer<Real>::CSRMatrix &,
                    Real, size_t, size_t>(),
           py::arg("X"), py::arg("shrinkage"), py::arg("n_thread") = 1,
           py::arg("max_chunk_size") = 128)
      .def("compute_similarity",
           &KNN::JaccardSimilarityComputer<Real>::compute_similarity);

  py::class_<KNN::TverskyIndexComputer<Real>>(m, "TverskyIndexComputer")
      .def(py::init<const KNN::TverskyIndexComputer<Real>::CSRMatrix &, Real,
                    Real, Real, size_t, size_t>(),
           py::arg("X"), py::arg("shrinkage"), py::arg("alpha"),
           py::arg("beta"), py::arg("n_thread") = 1,
           py::arg("max_chunk_size") = 128)
      .def("compute_similarity",
           &KNN::TverskyIndexComputer<Real>::compute_similarity);

  py::class_<KNN::AsymmetricCosineSimilarityComputer<Real>>(
      m, "AsymmetricSimilarityComputer")
      .def(py::init<
               const KNN::AsymmetricCosineSimilarityComputer<Real>::CSRMatrix &,
               Real, Real, size_t, size_t>(),
           py::arg("X"), py::arg("shrinkage"), py::arg("alpha"),
           py::arg("n_thread") = 1, py::arg("max_chunk_size") = 128)
      .def("compute_similarity",
           &KNN::AsymmetricCosineSimilarityComputer<Real>::compute_similarity);

  py::class_<KNN::P3alphaComputer<Real>>(m, "P3alphaComputer")
      .def(py::init<const KNN::P3alphaComputer<Real>::CSRMatrix &, Real, size_t,
                    size_t>(),
           py::arg("X"), py::arg("alpha") = 0, py::arg("n_thread") = 1,
           py::arg("max_chunk_size") = 128)
      .def("compute_W", &KNN::P3alphaComputer<Real>::compute_W);

  py::class_<KNN::RP3betaComputer<Real>>(m, "RP3betaComputer")
      .def(py::init<const KNN::RP3betaComputer<Real>::CSRMatrix &, Real, Real,
                    size_t, size_t>(),
           py::arg("X"), py::arg("alpha") = 0, py::arg("beta") = 0,
           py::arg("n_thread") = 1, py::arg("max_chunk_size") = 128)
      .def("compute_W", &KNN::RP3betaComputer<Real>::compute_W);
}