Simplify initialization of Pathfinder using C++11 constructs.

This commit is contained in:
Tom Ritchford
2014-07-08 20:58:49 -04:00
committed by Vinnie Falco
parent b4735b5931
commit bbbae072ea
2 changed files with 80 additions and 61 deletions

View File

@@ -928,6 +928,13 @@ std::string Pathfinder::pathTypeToString(PathType_t const& type)
return ret; return ret;
} }
void Pathfinder::fillPaths(PaymentType type, PathCostList const& costs)
{
auto& list = mPathTable[type];
for (auto& cost: costs)
list.push_back ({cost.first, makePath(cost.second)});
}
// Costs: // Costs:
// 0 = minimum to make some payments possible // 0 = minimum to make some payments possible
// 1 = include trivial paths to make common cases work // 1 = include trivial paths to make common cases work
@@ -936,71 +943,77 @@ std::string Pathfinder::pathTypeToString(PathType_t const& type)
// 10 = most agressive // 10 = most agressive
void Pathfinder::initPathTable() void Pathfinder::initPathTable()
{ // CAUTION: Do not include rules that build default paths {
{ // XRP to XRP // CAUTION: Do not include rules that build default paths
// do not remove this - it's necessary to build the table. fillPaths(
/*CostedPathList_t& list =*/ mPathTable[pt_XRP_to_XRP]; pt_XRP_to_XRP, {});
// list.push_back(CostedPath_t(8, makePath("sbxd"))); // source -> book -> book_to_XRP -> destination
// list.push_back(CostedPath_t(9, makePath("sbaxd"))); // source -> book -> gateway -> to_XRP ->destination
}
{ // XRP to non-XRP fillPaths(
CostedPathList_t& list = mPathTable[pt_XRP_to_nonXRP]; pt_XRP_to_nonXRP, {
{1, "sfd"}, // source -> book -> gateway
{3, "sfad"}, // source -> book -> account -> destination
{5, "sfaad"}, // source -> book -> account -> account -> destination
{6, "sbfd"}, // source -> book -> book -> destination
{8, "sbafd"}, // source -> book -> account -> book -> destination
{9, "sbfad"}, // source -> book -> book -> account -> destination
{10, "sbafad"}
});
list.push_back(CostedPath_t(1, makePath("sfd"))); // source -> book -> gateway fillPaths(
list.push_back(CostedPath_t(3, makePath("sfad"))); // source -> book -> account -> destination pt_XRP_to_nonXRP, {
list.push_back(CostedPath_t(5, makePath("sfaad"))); // source -> book -> account -> account -> destination {1, "sfd"},
list.push_back(CostedPath_t(6, makePath("sbfd"))); // source -> book -> book -> destination {3, "sfad"}, // source -> book -> account -> destination
list.push_back(CostedPath_t(8, makePath("sbafd"))); // source -> book -> account -> book -> destination {5, "sfaad"}, // source -> book -> account -> account -> destination
list.push_back(CostedPath_t(9, makePath("sbfad"))); // source -> book -> book -> account -> destination {6, "sbfd"}, // source -> book -> book -> destination
list.push_back(CostedPath_t(10, makePath("sbafad"))); {8, "sbafd"}, // source -> book -> account -> book -> destination
} {9, "sbfad"}, // source -> book -> book -> account -> destination
{10, "sbafad"}
});
{ // non-XRP to XRP fillPaths(
CostedPathList_t& list = mPathTable[pt_nonXRP_to_XRP]; pt_nonXRP_to_XRP, {
{1, "sxd"}, // gateway buys XRP
{2, "saxd"}, // source -> gateway -> book(XRP) -> dest
{6, "saaxd"},
{7, "sbxd"},
{8, "sabxd"},
{9, "sabaxd"}
});
list.push_back(CostedPath_t(1, makePath("sxd"))); // gateway buys XRP // non-XRP to non-XRP (same currency)
list.push_back(CostedPath_t(2, makePath("saxd"))); // source -> gateway -> book(XRP) -> dest fillPaths(
list.push_back(CostedPath_t(6, makePath("saaxd"))); pt_nonXRP_to_same, {
list.push_back(CostedPath_t(7, makePath("sbxd"))); {1, "sad"}, // source -> gateway -> destination
list.push_back(CostedPath_t(8, makePath("sabxd"))); {1, "sfd"}, // source -> book -> destination
list.push_back(CostedPath_t(9, makePath("sabaxd"))); {4, "safd"}, // source -> gateway -> book -> destination
} {4, "sfad"},
{5, "saad"},
{ // non-XRP to non-XRP (same currency) {5, "sbfd"},
CostedPathList_t& list = mPathTable[pt_nonXRP_to_same]; {6, "sxfad"},
{6, "safad"},
list.push_back(CostedPath_t(1, makePath("sad"))); // source -> gateway -> destination {6, "saxfd"}, // source -> gateway -> book to XRP -> book ->
list.push_back(CostedPath_t(1, makePath("sfd"))); // source -> book -> destination // destination
list.push_back(CostedPath_t(4, makePath("safd"))); // source -> gateway -> book -> destination {6, "saxfad"},
list.push_back(CostedPath_t(4, makePath("sfad"))); {6, "sabfd"}, // source -> gateway -> book -> book -> destination
list.push_back(CostedPath_t(5, makePath("saad"))); {6, "sabfd"},
list.push_back(CostedPath_t(5, makePath("sbfd"))); {7, "saaad"},
list.push_back(CostedPath_t(6, makePath("sxfad"))); });
list.push_back(CostedPath_t(6, makePath("safad")));
list.push_back(CostedPath_t(6, makePath("saxfd"))); // source -> gateway -> book to XRP -> book -> destination
list.push_back(CostedPath_t(6, makePath("saxfad")));
list.push_back(CostedPath_t(6, makePath("sabfd"))); // source -> gateway -> book -> book -> destination
list.push_back(CostedPath_t(7, makePath("saaad")));
}
{ // non-XRP to non-XRP (different currency)
CostedPathList_t& list = mPathTable[pt_nonXRP_to_nonXRP];
list.push_back(CostedPath_t(1, makePath("sfad")));
list.push_back(CostedPath_t(1, makePath("safd")));
list.push_back(CostedPath_t(3, makePath("safad")));
list.push_back(CostedPath_t(4, makePath("sxfd")));
list.push_back(CostedPath_t(5, makePath("saxfd")));
list.push_back(CostedPath_t(5, makePath("sxfad")));
list.push_back(CostedPath_t(5, makePath("sbfd")));
list.push_back(CostedPath_t(6, makePath("saxfad")));
list.push_back(CostedPath_t(6, makePath("sabfd")));
list.push_back(CostedPath_t(7, makePath("saafd")));
list.push_back(CostedPath_t(8, makePath("saafad")));
list.push_back(CostedPath_t(9, makePath("safaad")));
}
// non-XRP to non-XRP (different currency)
fillPaths(
pt_nonXRP_to_nonXRP, {
{1, "sfad"},
{1, "safd"},
{3, "safad"},
{4, "sxfd"},
{5, "saxfd"},
{5, "sxfad"},
{6, "saxfad"},
{6, "sabfd"},
{7, "saafd"},
{8, "saafad"},
{9, "safaad"},
});
} }
} // ripple } // ripple

View File

@@ -97,7 +97,13 @@ private:
typedef std::pair<int, PathType_t> CostedPath_t; typedef std::pair<int, PathType_t> CostedPath_t;
typedef std::vector<CostedPath_t> CostedPathList_t; typedef std::vector<CostedPath_t> CostedPathList_t;
// returns true if any building paths are now complete? typedef std::pair<int, const char*> PathCost;
typedef std::vector<PathCost> PathCostList;
/** Fill a CostedPathList_t from its description. */
static void fillPaths(PaymentType type, PathCostList const& costs);
/** @return true if any building paths are now complete. */
bool checkComplete (STPathSet& retPathSet); bool checkComplete (STPathSet& retPathSet);
static std::string pathTypeToString(PathType_t const&); static std::string pathTypeToString(PathType_t const&);