#include "position.h"

position::position(): x{0}, y{0} {}

position::position(int nx, int ny): x{nx}, y{ny} {}

position position::operator+(const position &other) const {
        position result{x + other.x, y + other.y};
        return result;
}

position &position::operator=(const position &other) {
        x = other.x;
        y = other.y;
        return *this;
}

position &position::operator+=(const position &other) {
        return *this = *this + other;
}

bool position::operator==(const position &other) const {
        return x == other.x && y == other.y;
}

bool position::operator!=(const position &other) const {
        return x != other.x || y != other.y;
}

bool position::operator<(const position &other) const {
        return y < other.y || x < other.x;
}

#include <iostream>

size_t find(const std::vector<position> &sorted_list,
            const position &target) {
        // TODO: implement binary searching

        for (size_t i = 0; i < sorted_list.size(); ++i)
                if (sorted_list[i] == target)
                        return i;

        return sorted_list.size();
}

#include <algorithm>

std::vector<position> remove_from_list(const std::vector<position>
                                       &sorted_positions,
                                       std::vector<position> excluded) {
        std::sort(excluded.begin(), excluded.end());

        std::vector<position> result{sorted_positions.size() - excluded.size()};

        auto exc = excluded.begin();

        for (auto src : sorted_positions) {
                if (exc != excluded.end() && src == *exc)
                        ++exc;
                else
                        result.push_back(src);
        }

        return result;
}

void remove_from_list(std::vector<position>
                      &positions,
                      const position &excluded) {
        for (auto i = positions.begin(); i != positions.end(); ++i)
                if (*i == excluded) {
                        positions.erase(i);
                        return;
                }
}

#include "math.h"

float distance(const position &a, const position &b) {
        return sqrt(distance_sqr(a, b));
}

int distance_sqr(const position &a, const position &b) {
        return pow(a.x - b.x, 2) + pow(a.y - b.y, 2);
}

bool is_adjacent(const position &a, const position &b) {
        return (a.x - b.x >= -1 && a.x - b.x <= 1) &&
               (a.y - b.y >= -1 && a.y - b.y <= 1);
}
