Program Listing for File grid1d.h
↰ Return to documentation for file (include/kami/grid1d.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_GRID1D_H
#define KAMI_GRID1D_H
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <kami/domain.h>
#include <kami/error.h>
#include <kami/grid.h>
#include <kami/kami.h>
namespace kami {
class LIBKAMI_EXPORT GridCoord1D
: public GridCoord {
public:
explicit GridCoord1D(int x_coord);
[[nodiscard]] int x() const;
[[nodiscard]] std::string to_string() const override;
double distance(std::shared_ptr<Coord>& p) const override;
friend bool operator==(
const GridCoord1D& lhs,
const GridCoord1D& rhs
);
friend bool operator!=(
const GridCoord1D& lhs,
const GridCoord1D& rhs
);
friend std::ostream& operator<<(
std::ostream& lhs,
const GridCoord1D& rhs
);
inline friend GridCoord1D operator+(
const GridCoord1D& lhs,
const GridCoord1D& rhs
);
inline friend GridCoord1D operator-(
const GridCoord1D& lhs,
const GridCoord1D& rhs
);
inline friend GridCoord1D operator*(
const GridCoord1D& lhs,
double rhs
);
inline friend GridCoord1D operator*(
double lhs,
const GridCoord1D& rhs
);
private:
int _x_coord;
};
class LIBKAMI_EXPORT Grid1D
: public GridDomain {
public:
explicit Grid1D(
unsigned int maximum_x,
bool wrap_x = false
);
virtual AgentID add_agent(
AgentID agent_id,
const GridCoord1D& coord
) = 0;
AgentID delete_agent(AgentID agent_id);
AgentID delete_agent(
AgentID agent_id,
const GridCoord1D& coord
);
AgentID move_agent(
AgentID agent_id,
const GridCoord1D& coord
);
[[nodiscard]] bool is_location_empty(const GridCoord1D& coord) const;
[[nodiscard]] bool is_location_valid(const GridCoord1D& coord) const;
[[nodiscard]] GridCoord1D get_location_by_agent(const AgentID& agent_id) const;
[[nodiscard]] std::shared_ptr<std::set<AgentID>>
get_location_contents(const GridCoord1D& coord) const;
[[nodiscard]] bool get_wrap_x() const;
[[nodiscard]] std::shared_ptr<std::unordered_set<GridCoord1D>>
get_neighborhood(
AgentID agent_id,
bool include_center
) const;
[[nodiscard]] std::shared_ptr<std::unordered_set<GridCoord1D>>
get_neighborhood(
const GridCoord1D& coord,
bool include_center
) const;
[[nodiscard]] unsigned int get_maximum_x() const;
protected:
const std::vector<GridCoord1D> directions = {GridCoord1D(1), GridCoord1D(-1)};
std::unique_ptr<std::unordered_multimap<GridCoord1D, AgentID>> _agent_grid;
std::unique_ptr<std::map<AgentID, GridCoord1D>> _agent_index;
[[nodiscard]] GridCoord1D coord_wrap(const GridCoord1D& coord) const;
private:
unsigned int _maximum_x;
bool _wrap_x;
};
} // namespace kami
#define KAMI_GRID1D_H
namespace std {
template<>
struct hash<kami::GridCoord1D> {
size_t operator()(const kami::GridCoord1D& key) const {
return (hash<int>()(key.x()));
}
};
} // namespace std
#endif // KAMI_GRID1D_H