Program Listing for File grid2d.h

Return to documentation for file (include/kami/grid2d.h)

/*-
 * Copyright (c) 2020 The Johns Hopkins University Applied Physics
 * Laboratory LLC
 *
 * 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.
 */

#pragma once
#ifndef KAMI_GRID2D_H
#define KAMI_GRID2D_H

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <kami/domain.h>
#include <kami/grid.h>
#include <kami/kami.h>

namespace kami {

    class LIBKAMI_EXPORT GridCoord2D
            : public GridCoord {
    public:
        GridCoord2D(
                int x_coord,
                int y_coord
        );

        [[nodiscard]] int x() const;

        [[nodiscard]] int y() const;

        [[nodiscard]] std::string to_string() const override;

        double distance(std::shared_ptr<Coord>& p) const override;

        double
        distance(
                std::shared_ptr<GridCoord2D>& p,
                GridDistanceType distance_type = GridDistanceType::Euclidean
        ) const;

        friend bool operator==(
                const GridCoord2D&,
                const GridCoord2D&
        );

        friend bool operator!=(
                const GridCoord2D&,
                const GridCoord2D&
        );

        friend std::ostream& operator<<(
                std::ostream&,
                const GridCoord2D&
        );

        inline friend GridCoord2D operator+(
                const GridCoord2D& lhs,
                const GridCoord2D& rhs
        );

        inline friend GridCoord2D operator-(
                const GridCoord2D& lhs,
                const GridCoord2D& rhs
        );

        inline friend GridCoord2D operator*(
                const GridCoord2D& lhs,
                const double rhs
        );

        inline friend GridCoord2D operator*(
                const double lhs,
                const GridCoord2D& rhs
        );

    protected:
        inline double distance_chebyshev(std::shared_ptr<GridCoord2D>& p) const;

        inline double distance_euclidean(std::shared_ptr<GridCoord2D>& p) const;

        inline double distance_manhattan(std::shared_ptr<GridCoord2D>& p) const;

    private:
        int _x_coord, _y_coord;
    };

    class LIBKAMI_EXPORT Grid2D
            : public GridDomain {
    public:
        explicit Grid2D(
                unsigned int maximum_x,
                unsigned int maximum_y,
                bool wrap_x = false,
                bool wrap_y = false
        );

        virtual AgentID add_agent(
                AgentID agent_id,
                const GridCoord2D& coord
        ) = 0;

        AgentID delete_agent(AgentID agent_id);

        AgentID delete_agent(
                AgentID agent_id,
                const GridCoord2D& coord
        );

        AgentID move_agent(
                AgentID agent_id,
                const GridCoord2D& coord
        );

        [[nodiscard]] bool is_location_empty(const GridCoord2D& coord) const;

        [[nodiscard]] bool is_location_valid(const GridCoord2D& coord) const;

        virtual
        GridCoord2D get_location_by_agent(const AgentID& agent_id) const;

        [[nodiscard]] std::shared_ptr<std::set<AgentID>>
        get_location_contents(const GridCoord2D& coord) const;

        [[nodiscard]] bool get_wrap_x() const;

        [[nodiscard]] bool get_wrap_y() const;

        virtual
        std::shared_ptr<std::unordered_set<GridCoord2D>>
        get_neighborhood(
                AgentID agent_id,
                bool include_center,
                GridNeighborhoodType neighborhood_type
        ) const;

        [[nodiscard]] std::shared_ptr<std::unordered_set<GridCoord2D>>
        get_neighborhood(
                const GridCoord2D& coord,
                bool include_center,
                GridNeighborhoodType neighborhood_type
        ) const;

        [[nodiscard]] unsigned int get_maximum_x() const;

        [[nodiscard]] unsigned int get_maximum_y() const;

    protected:
        const std::vector<GridCoord2D> directions_vonneumann = {GridCoord2D(0, 1), GridCoord2D(1, 0),
                GridCoord2D(0, -1), GridCoord2D(-1, 0)};

        const std::vector<GridCoord2D> directions_moore = {GridCoord2D(0, 1), GridCoord2D(1, 1),
                GridCoord2D(1, 0), GridCoord2D(1, -1),
                GridCoord2D(0, -1), GridCoord2D(-1, -1),
                GridCoord2D(-1, 0), GridCoord2D(-1, 1)};

        std::unique_ptr<std::unordered_multimap<GridCoord2D, AgentID>> _agent_grid;

        std::unique_ptr<std::map<AgentID, GridCoord2D>> _agent_index;

        [[nodiscard]] GridCoord2D coord_wrap(const GridCoord2D& coord) const;

    private:
        unsigned int _maximum_x, _maximum_y;
        bool _wrap_x, _wrap_y;
    };

}  // namespace kami

namespace std {
    template<>
    struct hash<kami::GridCoord2D> {
        size_t operator()(const kami::GridCoord2D& key) const {
            return ((hash<int>()(key.x()) ^ (hash<int>()(key.y()) << 1)) >> 1);
        }
    };
}  // namespace std

#endif  // KAMI_GRID2D_H