diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-25 06:44:40 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-25 06:44:40 +0100 |
commit | fa8f99cb63cd04bb8006e3f568d7f2494723528f (patch) | |
tree | 4c64aa1766219877f0e382acfef8f819c6e805d6 | |
parent | 4282a9d069b3bedc8888a4e6202f2b066233d4ec (diff) |
Added Tiles::sort().
-rw-r--r-- | common/tile.cpp | 16 | ||||
-rw-r--r-- | common/tile.h | 20 |
2 files changed, 30 insertions, 6 deletions
diff --git a/common/tile.cpp b/common/tile.cpp index 15d0d9d..29e0c84 100644 --- a/common/tile.cpp +++ b/common/tile.cpp @@ -1,5 +1,7 @@ #include "tile.h" +#include <algorithm> + Tile::Tile() { } @@ -8,7 +10,7 @@ Tile::Tile(Tile::Type t, bool re, bool ro) : type(t), red(re), rotated(ro){ } -int Tile::get_num() { +int Tile::get_num() const { if(type >= Man_1 && type <= Sou_9) { return (type - Man_1) % 9 + 1; } else { @@ -16,11 +18,19 @@ int Tile::get_num() { } } -bool Tile::operator==(const Tile& other) { +bool Tile::operator==(const Tile& other) const { return type == other.type; } +bool Tile::operator<(const Tile& other) const { + return type == other.type ? red : type < other.type; +} + Tile Tile::operator++(int) { type = Type(type + 1); return *this; -}
\ No newline at end of file +} + +void Tiles::sort() { + std::sort(begin(), end()); +} diff --git a/common/tile.h b/common/tile.h index 93bdb8d..84552aa 100644 --- a/common/tile.h +++ b/common/tile.h @@ -3,6 +3,7 @@ #include <stdint.h> #include <vector> +#include <boost/serialization/base_object.hpp> class Tile { public: @@ -60,10 +61,13 @@ class Tile { virtual ~Tile(){}; //! Get the numeric value of the tile (if one of the man/pin/sou-sets). - int get_num(); + int get_num() const; //! Compare two tiles. Equal if type matches; flags are not compared. - bool operator==(const Tile& other); + bool operator==(const Tile& other) const; + + //! Compare two tiles. Ordered by type, red < non-red. + bool operator<(const Tile& other) const; //! Increment type. Useful for iterating over all possible types. Tile operator++(int); @@ -76,6 +80,16 @@ class Tile { } }; -typedef std::vector<Tile> Tiles; +//! List of tiles. +class Tiles : public std::vector<Tile> { + public: + //! Sort the list of tiles. + void sort(); + + template<class Archive> + void serialize(Archive & ar, const unsigned int version) { + ar & boost::serialization::base_object<std::vector<Tile> >(*this); + } +}; #endif |