Implementation transit edge flags with the help of bools.

This commit is contained in:
Vladimir Byko-Ianko 2017-11-15 14:57:35 +03:00 committed by mpimenov
parent d2315807f9
commit 1a4edbc916
4 changed files with 43 additions and 67 deletions

View file

@ -264,7 +264,7 @@ void DeserializerFromJson::operator()(EdgeFlags & edgeFlags, char const * name)
// Note. Only |transfer| field of |edgeFlags| may be set at this point because the
// other fields of |edgeFlags| are unknown.
edgeFlags.SetFlags(0);
edgeFlags.SetTransfer(transfer);
edgeFlags.m_transfer = transfer;
}
void DeserializerFromJson::operator()(StopIdRanges & rs, char const * name)

View file

@ -132,10 +132,10 @@ public:
GetEdgeFlags(e.GetTransfer(), e.GetStop1Id(), e.GetStop2Id(), e.GetShapeIds());
(*this)(flags);
if (flags.IsEmptyShapeIds() || flags.IsShapeIdTheSame() || flags.IsShapeIdReversed())
if (flags.m_isShapeIdsEmpty || flags.m_isShapeIdsSame || flags.m_isShapeIdsReversed)
return;
if (flags.IsSingleShapeId())
if (flags.m_isShapeIdsSingle)
{
CHECK_EQUAL(e.GetShapeIds().size(), 1, ());
(*this)(e.GetShapeIds()[0]);
@ -253,22 +253,22 @@ public:
(*this)(e.m_flags);
e.m_shapeIds.clear();
if (e.m_flags.IsEmptyShapeIds())
if (e.m_flags.m_isShapeIdsEmpty)
return;
if (e.m_flags.IsShapeIdTheSame())
if (e.m_flags.m_isShapeIdsSame)
{
e.m_shapeIds.emplace_back(e.GetStop1Id(), e.GetStop2Id());
return;
}
if (e.m_flags.IsShapeIdReversed())
if (e.m_flags.m_isShapeIdsReversed)
{
e.m_shapeIds.emplace_back(e.GetStop2Id(), e.GetStop1Id());
return;
}
if (e.m_flags.IsSingleShapeId())
if (e.m_flags.m_isShapeIdsSingle)
{
e.m_shapeIds.resize(1 /* single shape id */);
(*this)(e.m_shapeIds.back());

View file

@ -227,20 +227,13 @@ bool ShapeId::IsValid() const
}
// EdgeFlags --------------------------------------------------------------------------------------
EdgeFlags::EdgeFlags()
: m_transfer(0)
, m_isShapeIdsEmpty(0)
, m_isShapeIdsSingle(0)
, m_isShapeIdsSame(0)
, m_isShapeIdsReversed(0)
{
}
uint8_t EdgeFlags::GetFlags() const
{
return m_transfer + m_isShapeIdsEmpty * kEmptyShapeIdsMask +
m_isShapeIdsSingle * kSingleShapeIdMask + m_isShapeIdsSame * kShapeIdIsSameMask +
m_isShapeIdsReversed * kShapeIdIsReversedMask;
return BoolToUint(m_transfer) * kTransferMask +
BoolToUint(m_isShapeIdsEmpty) * kEmptyShapeIdsMask +
BoolToUint(m_isShapeIdsSingle) * kSingleShapeIdMask +
BoolToUint(m_isShapeIdsSame) * kShapeIdIsSameMask +
BoolToUint(m_isShapeIdsReversed) * kShapeIdIsReversedMask;
}
void EdgeFlags::SetFlags(uint8_t flags)
@ -255,11 +248,11 @@ void EdgeFlags::SetFlags(uint8_t flags)
string DebugPrint(EdgeFlags const & f)
{
std::ostringstream ss;
ss << "EdgeFlags [m_transfer:" << f.IsTransfer();
ss << ", m_isShapeIdsEmpty:" << f.IsEmptyShapeIds();
ss << ", m_isShapeIdsSingle:" << f.IsSingleShapeId();
ss << ", m_isShapeIdsSame:" << f.IsShapeIdTheSame();
ss << ", m_isShapeIdsReversed:" << f.IsShapeIdReversed() << "]";
ss << "EdgeFlags [m_transfer:" << f.m_transfer;
ss << ", m_isShapeIdsEmpty:" << f.m_isShapeIdsEmpty;
ss << ", m_isShapeIdsSingle:" << f.m_isShapeIdsSingle;
ss << ", m_isShapeIdsSame:" << f.m_isShapeIdsSame;
ss << ", m_isShapeIdsReversed:" << f.m_isShapeIdsReversed << "]";
return ss.str();
}
@ -388,16 +381,16 @@ EdgeFlags GetEdgeFlags(bool transfer, StopId stopId1, StopId stopId2,
vector<ShapeId> const & shapeIds)
{
EdgeFlags flags;
flags.SetTransfer(transfer);
flags.SetEmptyShapeIds(shapeIds.empty());
flags.m_transfer = transfer;
flags.m_isShapeIdsEmpty = shapeIds.empty();
bool const singleShapeId = (shapeIds.size() == 1);
flags.SetSingleShapeId(singleShapeId);
flags.m_isShapeIdsSingle = singleShapeId;
if (singleShapeId)
{
flags.SetShapeIdTheSame(stopId1 == shapeIds[0].GetStop1Id() &&
stopId2 == shapeIds[0].GetStop2Id());
flags.SetShapeIdReversed(stopId1 == shapeIds[0].GetStop2Id() &&
stopId2 == shapeIds[0].GetStop1Id());
flags.m_isShapeIdsSame =
(stopId1 == shapeIds[0].GetStop1Id() && stopId2 == shapeIds[0].GetStop2Id());
flags.m_isShapeIdsReversed =
stopId1 == shapeIds[0].GetStop2Id() && stopId2 == shapeIds[0].GetStop1Id();
}
return flags;
}

View file

@ -259,54 +259,37 @@ class EdgeFlags
{
public:
DECLARE_TRANSIT_TYPE_FRIENDS
EdgeFlags();
bool IsTransfer() const { return m_transfer == 1; }
void SetTransfer(bool transfer) { m_transfer = BoolToUint(transfer); }
/// \returns true if |Edge::m_shapeIds| is empty.
bool IsEmptyShapeIds() const { return m_isShapeIdsEmpty == 1; }
void SetEmptyShapeIds(bool emptyShapeIds) { m_isShapeIdsEmpty = BoolToUint(emptyShapeIds); }
/// \returns true if |Edge::m_shapeIds| contains only one item.
bool IsSingleShapeId() const { return m_isShapeIdsSingle == 1; }
void SetSingleShapeId(bool singleShapeId) { m_isShapeIdsSingle = BoolToUint(singleShapeId); }
/// \note It's valid only if IsSingleShapeId() returns true.
/// \returns true if
/// |Edge::m_stop1Id == m_shapeIds[0].m_stop1Id && Edge::m_stop2Id == m_shapeIds[0].m_stop2Id|.
bool IsShapeIdTheSame() const { return m_isShapeIdsSame == 1; }
void SetShapeIdTheSame(bool same) { m_isShapeIdsSame = BoolToUint(same); }
/// \note It's valid only if IsSingleShapeId() returns true.
/// \returns true if
/// |Edge::m_stop1Id == m_shapeIds[0].m_stop2Id && Edge::m_stop2Id == m_shapeIds[0].m_stop1Id|.
bool IsShapeIdReversed() const { return m_isShapeIdsReversed == 1; }
void SetShapeIdReversed(bool reversed) { m_isShapeIdsReversed = BoolToUint(reversed); }
friend string DebugPrint(EdgeFlags const & f);
uint8_t GetFlags() const;
void SetFlags(uint8_t flags);
// |m_transfer == true| if |Edge::m_shapeIds| is empty.
bool m_transfer = false;
/// |m_isShapeIdsEmpty == true| if |Edge::m_shapeIds| is empty.
bool m_isShapeIdsEmpty = false;
// |m_isShapeIdsSingle == true| if |Edge::m_shapeIds| contains only one item.
bool m_isShapeIdsSingle = false;
// Note. If |m_isShapeIdsSingle == true| |m_isShapeIdsSame| is set to
// |Edge::m_stop1Id == m_shapeIds[0].m_stop1Id && Edge::m_stop2Id == m_shapeIds[0].m_stop2Id|.
// |m_isShapeIdsSingle| is invalid otherwise.
bool m_isShapeIdsSame = false;
// Note. If |m_isShapeIdsSingle == true| |m_isShapeIdsReversed| is set to
// |Edge::m_stop1Id == m_shapeIds[0].m_stop2Id && Edge::m_stop2Id == m_shapeIds[0].m_stop1Id|.
// |m_isShapeIdsReversed| is invalid otherwise.
bool m_isShapeIdsReversed = false;
private:
uint8_t BoolToUint(bool b) { return static_cast<uint8_t>(b ? 1 : 0); }
uint8_t GetBit(uint8_t flags, uint8_t mask) { return BoolToUint(flags & mask); }
uint8_t BoolToUint(bool b) const { return static_cast<uint8_t>(b ? 1 : 0); }
uint8_t GetBit(uint8_t flags, uint8_t mask) const { return BoolToUint(flags & mask); }
static uint8_t constexpr kTransferMask = 1;
static uint8_t constexpr kEmptyShapeIdsMask = (1 << 1);
static uint8_t constexpr kSingleShapeIdMask = (1 << 2);
static uint8_t constexpr kShapeIdIsSameMask = (1 << 3);
static uint8_t constexpr kShapeIdIsReversedMask = (1 << 4);
uint8_t m_transfer : 1;
uint8_t m_isShapeIdsEmpty : 1;
uint8_t m_isShapeIdsSingle : 1;
uint8_t m_isShapeIdsSame : 1;
uint8_t m_isShapeIdsReversed : 1;
};
static_assert(sizeof(EdgeFlags) == 1, "Wrong EdgeFlags size.");
std::string DebugPrint(EdgeFlags const & f);
class Edge
@ -328,7 +311,7 @@ public:
StopId GetStop2Id() const { return m_stop2Id; }
Weight GetWeight() const { return m_weight; }
LineId GetLineId() const { return m_lineId; }
bool GetTransfer() const { return m_flags.IsTransfer(); }
bool GetTransfer() const { return m_flags.m_transfer; }
std::vector<ShapeId> const & GetShapeIds() const { return m_shapeIds; }
private: