Samchon Framework for CPP  1.0.0
Travel.hpp
1 #pragma once
2 #include <samchon/protocol/SharedEntityArray.hpp>
3 #include <samchon/examples/tsp/GeometryPoint.hpp>
4 
5 namespace samchon
6 {
7 namespace examples
8 {
9 namespace tsp
10 {
11  using namespace std;
12 
13  using namespace library;
14  using namespace protocol;
15 
27  class Travel
28  : public SharedEntityArray<GeometryPoint>
29  {
30  private:
32 
33  protected:
40  double distance;
41 
42  public:
43  /* -----------------------------------------------------------
44  CONSTRUCTORS
45  ----------------------------------------------------------- */
50  : super()
51  {
52  distance = INT_MIN;
53  };
54 
63  Travel(const Travel &travel)
64  : super(travel)
65  {
66  distance = INT_MIN;
67  };
68 
72  Travel(Travel &&travel)
73  : super(move(travel))
74  {
75  distance = travel.distance;
76  };
77 
78  virtual ~Travel() = default;
79 
80  virtual void construct(shared_ptr<XML> xml) override
81  {
82  super::construct(xml);
83 
84  if (xml->hasProperty("distance") == true)
85  distance = xml->getProperty<double>("distance");
86  else
87  distance = INT_MIN;
88  };
89 
90  protected:
91  virtual auto createChild(shared_ptr<XML>) -> GeometryPoint*
92  {
93  return new GeometryPoint();
94  };
95 
96  /* -----------------------------------------------------------
97  CALCULATORS
98  ----------------------------------------------------------- */
99  public:
103  auto calcDistance() const -> double
104  {
105  if(this->distance != INT_MIN)
106  return this->distance;
107 
108  double distance = 0.0;
109  for(size_t i = 1; i < size(); i++)
110  distance += at(i-1)->calcDistance(*at(i));
111 
112  ((Travel*)this)->distance = distance;
113  return distance;
114  };
115 
116  public:
124  auto operator<(const Travel &travel) const -> bool
125  {
126  return this->calcDistance() < travel.calcDistance();
127  };
128 
129  /* -----------------------------------------------------------
130  EXPORTER
131  ----------------------------------------------------------- */
132  virtual auto TAG() const -> string override
133  {
134  return "travel";
135  };
136  virtual auto CHILD_TAG() const -> string override
137  {
138  return "point";
139  };
140 
141  virtual auto toXML() const -> shared_ptr<XML> override
142  {
143  shared_ptr<XML> &xml = super::toXML();
144  if (distance != INT_MIN)
145  xml->setProperty("distance", distance);
146 
147  return xml;
148  };
149 
162  auto toString() const -> string
163  {
164  string str =
165  "Distance: " + StringUtil::numberFormat(calcDistance()) + " km\n" +
166  "uid longitude latitude\n";
167 
168  for(size_t i = 0; i < size(); i++)
169  str += at(i)->toString() + "\n";
170 
171  return str;
172  };
173  };
174 };
175 };
176 };
Represent a travel containning Point(s)
Definition: Travel.hpp:27
double distance
Estimated hours to move.
Definition: Travel.hpp:40
auto calcDistance() const -> double
Calculate distance to move.
Definition: Travel.hpp:103
Travel(Travel &&travel)
Move Constructor.
Definition: Travel.hpp:72
auto operator<(const Travel &travel) const -> bool
Compare which object is less.
Definition: Travel.hpp:124
An Entity and a container of children Entity objects.
Definition: EntityGroup.hpp:40
virtual auto toXML() const -> shared_ptr< XML > override
Get an XML object represents the EntityGroup.
Definition: Travel.hpp:141
static auto numberFormat(double val, int precision=2) -> std::string
Definition: StringUtil.hpp:241
auto toString() const -> string
Convert the Travel to String.
Definition: Travel.hpp:162
A geometry coordinates (point)
Travel()
Default Constructor.
Definition: Travel.hpp:49
virtual auto CHILD_TAG() const -> string override
A tag name of children.
Definition: Travel.hpp:136
Travel(const Travel &travel)
Copy Constructor.
Definition: Travel.hpp:63