From f99dcdf422310400fe9f20f61697328e87485a34 Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Wed, 19 Jan 2022 09:57:14 +0000 Subject: [PATCH] Insertion sort sides instead of hashmap --- click.cpp | 64 +++++++++++++++++-------------- 1 files changed, 35 insertions(+), 29 deletions(-) diff --git a/click.cpp b/click.cpp index 749d649..932e618 100644 --- a/click.cpp +++ b/click.cpp @@ -13,10 +13,10 @@ void initialise() { - mapClick = {{"BUY", Buy}, {"SELL", Sell}, {"FLASH", Flash}, - {"b", Buy}, {"s", Sell}, {"f", Flash}, - {"B", Buy}, {"S", Sell}, {"F", Flash}, - {"DELETE", Delete}, {"D", Delete}, {"d", Delete}}; + mapClick = {{"BUY", Buy}, {"SELL", Sell}, {"FLASH", Flash}, + {"b", Buy}, {"s", Sell}, {"f", Flash}, + {"B", Buy}, {"S", Sell}, {"F", Flash}, + {"DELETE", Delete}, {"D", Delete}, {"d", Delete}}; } void usage() @@ -38,27 +38,32 @@ void buy(std::string& product, double price, uint64_t volume) { json::AddMessage a(json::ADD, product, price, book::Buy, volume); - json::AddedMessage* b = static_cast<json::AddedMessage*>(protocol::addOrder(a)); + json::AddedMessage* b = + static_cast<json::AddedMessage*>(protocol::addOrder(a)); std::cout << b->as_string() << std::endl; } void sell(std::string& product, double price, uint64_t volume) { json::AddMessage a(json::ADD, product, price, book::Sell, volume); - json::AddedMessage* b = static_cast<json::AddedMessage*>(protocol::addOrder(a)); + json::AddedMessage* b = + static_cast<json::AddedMessage*>(protocol::addOrder(a)); std::cout << b->as_string() << std::endl; delete b; } -void flash(std::string& product, double price, uint64_t volume, book::OrderSideEnum side) +void flash(std::string& product, double price, uint64_t volume, + book::OrderSideEnum side) { json::AddMessage a(json::ADD, product, price, side, volume); json::Message* b = static_cast<json::Message*>(protocol::addOrder(a)); - if (b->type == json::ERROR) { - std::cout << static_cast<json::ErrorMessage*>(b)->as_string() << std::endl; - return; - } - json::DeleteMessage c(json::DELETE, product, static_cast<json::AddedMessage*>(b)->id); + if (b->type == json::ERROR) { + std::cout << static_cast<json::ErrorMessage*>(b)->as_string() + << std::endl; + return; + } + json::DeleteMessage c(json::DELETE, product, + static_cast<json::AddedMessage*>(b)->id); json::Message* d = protocol::deleteOrder(c); std::cout << static_cast<json::AddedMessage*>(b)->as_string() << std::endl; if (d->type == json::DELETED) { @@ -74,17 +79,18 @@ void deleteOrder(std::string& product, std::string& id) { - json::DeleteMessage a(json::DELETE, product, id); - json::Message* b = protocol::deleteOrder(a); - if (b->type == json::DELETED) { + json::DeleteMessage a(json::DELETE, product, id); + json::Message* b = protocol::deleteOrder(a); + if (b->type == json::DELETED) { std::cout << static_cast<json::DeletedMessage*>(b)->as_string() << std::endl; } else if (b->type == json::REJECT) { std::cout << static_cast<json::RejectMessage*>(b)->as_string() << std::endl; } else { - std::cout << static_cast<json::ErrorMessage*>(b)->as_string() << std::endl; - } + std::cout << static_cast<json::ErrorMessage*>(b)->as_string() + << std::endl; + } } int main(int argc, char** argv) @@ -106,9 +112,9 @@ case 't': click = mapClick[optarg]; break; - case 's': - side = mapClick[optarg]; - break; + case 's': + side = mapClick[optarg]; + break; case 'p': price = std::stod(optarg); break; @@ -119,9 +125,9 @@ id = std::string(optarg); break; case '?': - std::cout << "*1" << std::endl; + std::cout << "*1" << std::endl; default: - std::cout << "*2 " << (char) c << std::endl; + std::cout << "*2 " << (char)c << std::endl; usage(); exit(0); } @@ -134,13 +140,13 @@ sell(product, price, volume); break; case Flash: - if (side == clickType::Buy) - flash(product, price, volume, book::Buy); - else - flash(product, price, volume, book::Sell); + if (side == clickType::Buy) + flash(product, price, volume, book::Buy); + else + flash(product, price, volume, book::Sell); break; - case Delete: - deleteOrder(product, id); - break; + case Delete: + deleteOrder(product, id); + break; } } -- Gitblit v1.9.3