Skip to content

Commit c06ffd3

Browse files
committed
format and refactor
1 parent 0756d24 commit c06ffd3

File tree

7 files changed

+187
-168
lines changed

7 files changed

+187
-168
lines changed

stock_emu_lib/Stock.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#pragma once
2-
#include <cstddef>
32
#ifndef INCLUDE_STOCK_HPP_
43
#define INCLUDE_STOCK_HPP_
4+
#include <cstddef>
55
#include <format>
6-
#include <iostream>
76
#include <stock_emu_lib/money.hpp>
87
#include <type_traits>
98

@@ -162,7 +161,6 @@ static_assert([]() {
162161
}());
163162

164163
inline StockCount operator/(Money& money, const StockPrice& price) {
165-
std::cout << money.getValue() << "/" << price.getValue() << std::endl;
166164
return {money.getValue() / price.getValue()};
167165
}
168166

@@ -198,4 +196,4 @@ struct std::formatter<StockPrice> : std::formatter<int> {
198196
return std::formatter<int>::format(c.getValue(), ctx);
199197
}
200198
};
201-
#endif
199+
#endif // INCLUDE_STOCK_HPP_

stock_emu_lib/trade/Trade.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,62 @@ void StockMarket::updatePricePerValue(const TradeBoard& ref) {
145145
std::abs(ref.getHistory().getCurrentPrice().latest.getValue() - ref.StockValue().getValue());
146146
}
147147

148-
void TradeBoard::sell_limit_destruct::run(std::deque<SellTradeRequest>& q) {
148+
void TradeBoard::sell_limit_destruct::run(std::deque<Trade::SellTradeRequest>& q) {
149149
for (auto& i : q) {
150150
i.reject();
151151
}
152152
}
153153

154-
void TradeBoard::buy_limit_destruct::run(std::deque<BuyTradeRequest>& q) {
154+
void TradeBoard::buy_limit_destruct::run(std::deque<Trade::BuyTradeRequest>& q) {
155155
for (auto& i : q) {
156156
i.reject();
157157
}
158158
}
159+
160+
void TradeBoard::printAll() {
161+
constexpr int show_limit = 10;
162+
163+
std::printf("%10s|price|\n", " ");
164+
165+
struct count_price {
166+
StockPrice price;
167+
StockCount count;
168+
};
169+
170+
std::array<count_price, show_limit> prices;
171+
size_t price_current_count = 0;
172+
173+
for (auto i = sell.order_list.begin(); i != sell.order_list.end(); ++i) {
174+
const auto& price = i->first;
175+
StockCount amount_sum = 0;
176+
for (const auto& j : i->second) {
177+
amount_sum += j->amount.to_StockCount();
178+
}
179+
if (amount_sum == 0) continue;
180+
prices[price_current_count] = {price, amount_sum};
181+
price_current_count++;
182+
if (price_current_count == show_limit) break;
183+
// std::cout << std::format("{: >10} {: ^5} \n", amount_sum, price.getValue());
184+
}
185+
for (auto i = prices.rbegin(); i != prices.rend(); i++) {
186+
if (i->count.value == 0) {
187+
continue;
188+
}
189+
std::cout << std::format("{: >10} {: ^5} \n", i->count, i->price.getValue());
190+
}
191+
192+
int count = 0;
193+
for (auto i = buy.order_list.rbegin(); i != buy.order_list.rend(); ++i, ++count) {
194+
if (count > show_limit) {
195+
break;
196+
}
197+
198+
const auto& price = i->first;
199+
StockCount amount_sum = 0;
200+
for (const auto& j : i->second) {
201+
amount_sum += j->amount;
202+
}
203+
if (amount_sum == 0) continue;
204+
std::cout << std::format("{:10} {: ^5} {: >10}\n", "", price.getValue(), amount_sum);
205+
}
206+
}

stock_emu_lib/trade/Trade.hpp

Lines changed: 6 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
#ifndef INCLUDE_TRADE_HPP_
33
#define INCLUDE_TRADE_HPP_
44

5-
#include <array>
65
#include <csignal>
76
#include <cstddef>
8-
#include <cstdio>
97
#include <deque>
10-
#include <iostream>
11-
#include <map>
128
#include <memory>
139
#include <utility>
1410
#include <vector>
@@ -19,74 +15,14 @@
1915
#include <stock_emu_lib/util/RingQueue.hpp>
2016

2117
#include "TradeHistory.hpp"
18+
#include "stock_emu_lib/trade/TradeRequestBoard.hpp"
2219

2320
struct TradeBoard;
2421
struct Trader;
2522
class StockMarket;
2623
using StockMarketRef = std::shared_ptr<StockMarket>;
2724
using Tick_t = int;
2825

29-
struct Trader {
30-
StockId id;
31-
Money money;
32-
33-
struct StockData_t : public std::map<StockId, StockHoldingCount> {
34-
static StockData_t create(const std::vector<std::pair<StockId, StockCount_data_t>>& data);
35-
};
36-
37-
StockData_t stock;
38-
39-
void printStat() {
40-
// std::printf("user %d\n money: %d\n stock: %d\n", id, money.getValue(), stock[1]);
41-
std::cout << std::format("id:{}\n money: {}\n stock:\n", id, money);
42-
for (const auto& i : stock) {
43-
std::cout << std::format(" {}: {}\n", i.first, i.second);
44-
}
45-
}
46-
47-
Trader(StockId id_, Money&& money_, StockData_t&& stock_ = {})
48-
: id(id_), money(std::move(money_)), stock(std::move(stock_)) {}
49-
50-
void sell(StockPrice price, StockCount amount, StockId id, StockMarketRef ref);
51-
void buy(StockPrice price, StockCount amount, StockId id, StockMarketRef ref);
52-
};
53-
54-
template<class T>
55-
concept TradeRequest = requires(T& t) { t.amount == 0; };
56-
57-
struct SellTradeRequest {
58-
StockHoldingCount amount;
59-
Trader& trader;
60-
StockId id;
61-
SellTradeRequest** refer = nullptr;
62-
63-
SellTradeRequest(StockHoldingCount&& amount_, Trader& trader_, StockId id_)
64-
: amount(std::move(amount_)), trader(trader_), id(id_) {}
65-
66-
void reject() {
67-
amount.move(amount.to_StockCount()).to(trader.stock[id]);
68-
// *refer = nullptr;
69-
}
70-
};
71-
72-
struct BuyTradeRequest {
73-
StockCount amount;
74-
Trader& trader;
75-
Money money;
76-
BuyTradeRequest** refer = nullptr;
77-
78-
BuyTradeRequest(StockCount amount_, Trader& trader_, Money&& money_)
79-
: amount(amount_), trader(trader_), money(std::move(money_)) {}
80-
81-
void reject() {
82-
money.move(money.as_Price()).to(trader.money);
83-
this->amount = 0;
84-
// *refer = nullptr;
85-
}
86-
87-
BuyTradeRequest() = delete;
88-
};
89-
9026
struct TradeBoard {
9127
public:
9228
StockId id;
@@ -102,53 +38,7 @@ struct TradeBoard {
10238
void MarketOrder_sell(StockCount count, Trader&);
10339
void MarketOrder_Buy(StockCount count, Trader&);
10440

105-
void printAll() {
106-
constexpr int show_limit = 10;
107-
108-
std::printf("%10s|price|\n", " ");
109-
110-
struct count_price {
111-
StockPrice price;
112-
StockCount count;
113-
};
114-
115-
std::array<count_price, show_limit> prices;
116-
size_t price_current_count = 0;
117-
118-
for (auto i = sell.order_list.begin(); i != sell.order_list.end(); ++i) {
119-
const auto& price = i->first;
120-
StockCount amount_sum = 0;
121-
for (const auto& j : i->second) {
122-
amount_sum += j->amount.to_StockCount();
123-
}
124-
if (amount_sum == 0) continue;
125-
prices[price_current_count] = {price, amount_sum};
126-
price_current_count++;
127-
if (price_current_count == show_limit) break;
128-
// std::cout << std::format("{: >10} {: ^5} \n", amount_sum, price.getValue());
129-
}
130-
for (auto i = prices.rbegin(); i != prices.rend(); i++) {
131-
if (i->count.value == 0) {
132-
continue;
133-
}
134-
std::cout << std::format("{: >10} {: ^5} \n", i->count, i->price.getValue());
135-
}
136-
137-
int count = 0;
138-
for (auto i = buy.order_list.rbegin(); i != buy.order_list.rend(); ++i, ++count) {
139-
if (count > show_limit) {
140-
break;
141-
}
142-
143-
const auto& price = i->first;
144-
StockCount amount_sum = 0;
145-
for (const auto& j : i->second) {
146-
amount_sum += j->amount;
147-
}
148-
if (amount_sum == 0) continue;
149-
std::cout << std::format("{:10} {: ^5} {: >10}\n", "", price.getValue(), amount_sum);
150-
}
151-
}
41+
void printAll();
15242

15343
constexpr const auto& getBuyBoard() const {
15444
return buy;
@@ -179,60 +69,16 @@ struct TradeBoard {
17969
}
18070

18171
private:
182-
template<TradeRequest Request_t, class limit_destructor_t>
183-
struct TradeRequestBoard {
184-
class RequestList_Wrapper : public std::deque<Request_t*> {
185-
public:
186-
bool is_empty() const {
187-
for (const auto& i : *this) {
188-
if (i != nullptr) {
189-
if (i->amount != 0) {
190-
return false;
191-
}
192-
}
193-
}
194-
return true;
195-
}
196-
};
197-
198-
std::map<StockPrice, RequestList_Wrapper> order_list = {};
199-
util::RingQueue<std::deque<Request_t>, 24, limit_destructor_t> limit_queue;
200-
201-
template<class... Args>
202-
void add(size_t expire, StockPrice price, Args&&... arg) {
203-
limit_queue.get(expire).emplace_back(std::forward<Args>(arg)...);
204-
auto* elem = &limit_queue.get(expire).back();
205-
206-
order_list[price].emplace_back(elem);
207-
auto* ref_ptr = &order_list[price].back();
208-
elem->refer = ref_ptr;
209-
}
210-
211-
bool OrderExists(StockPrice price) const {
212-
if (const auto find = order_list.find(price); find == order_list.end()) {
213-
return false;
214-
}
215-
if (order_list.at(price).empty()) {
216-
return false;
217-
}
218-
if (order_list.at(price).is_empty()) {
219-
printf("???\n");
220-
return false;
221-
}
222-
return true;
223-
}
224-
};
225-
22672
struct buy_limit_destruct {
227-
static void run(std::deque<BuyTradeRequest>&);
73+
static void run(std::deque<Trade::BuyTradeRequest>&);
22874
};
22975

23076
struct sell_limit_destruct {
231-
static void run(std::deque<SellTradeRequest>&);
77+
static void run(std::deque<Trade::SellTradeRequest>&);
23278
};
23379

234-
TradeRequestBoard<BuyTradeRequest, buy_limit_destruct> buy;
235-
TradeRequestBoard<SellTradeRequest, sell_limit_destruct> sell;
80+
Trade::TradeRequestBoard<Trade::BuyTradeRequest, buy_limit_destruct> buy;
81+
Trade::TradeRequestBoard<Trade::SellTradeRequest, sell_limit_destruct> sell;
23682

23783
StockMarket& market_ref;
23884
StockPrice stock_value = {300};

stock_emu_lib/trade/TradeHistory.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
2-
32
#ifndef INCLUDE_TRADE_TRADEHISTORY_HPP
43
#define INCLUDE_TRADE_TRADEHISTORY_HPP
54

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#pragma once
2+
#ifndef INCLUDE_TRADE_TRADEREQUESTBOARD_HPP_
3+
#define INCLUDE_TRADE_TRADEREQUESTBOARD_HPP_
4+
#include <map>
5+
#include <stock_emu_lib/Stock.hpp>
6+
#include <stock_emu_lib/trade/Trader.hpp>
7+
#include <stock_emu_lib/util/RingQueue.hpp>
8+
9+
template<class T>
10+
concept TradeRequest = requires(T& t) { t.amount == 0; };
11+
12+
namespace Trade {
13+
14+
struct SellTradeRequest {
15+
StockHoldingCount amount;
16+
Trader& trader;
17+
StockId id;
18+
SellTradeRequest** refer = nullptr;
19+
20+
SellTradeRequest(StockHoldingCount&& amount_, Trader& trader_, StockId id_)
21+
: amount(std::move(amount_)), trader(trader_), id(id_) {}
22+
23+
void reject() {
24+
amount.move(amount.to_StockCount()).to(trader.stock[id]);
25+
// *refer = nullptr;
26+
}
27+
};
28+
29+
struct BuyTradeRequest {
30+
StockCount amount;
31+
Trader& trader;
32+
Money money;
33+
BuyTradeRequest** refer = nullptr;
34+
35+
BuyTradeRequest(StockCount amount_, Trader& trader_, Money&& money_)
36+
: amount(amount_), trader(trader_), money(std::move(money_)) {}
37+
38+
void reject() {
39+
money.move(money.as_Price()).to(trader.money);
40+
this->amount = 0;
41+
// *refer = nullptr;
42+
}
43+
44+
BuyTradeRequest() = delete;
45+
};
46+
47+
template<TradeRequest Request_t, class limit_destructor_t>
48+
struct TradeRequestBoard {
49+
class RequestList_Wrapper : public std::deque<Request_t*> {
50+
public:
51+
bool is_empty() const {
52+
for (const auto& i : *this) {
53+
if (i != nullptr) {
54+
if (i->amount != 0) {
55+
return false;
56+
}
57+
}
58+
}
59+
return true;
60+
}
61+
};
62+
63+
std::map<StockPrice, RequestList_Wrapper> order_list = {};
64+
util::RingQueue<std::deque<Request_t>, 24, limit_destructor_t> limit_queue;
65+
66+
template<class... Args>
67+
void add(size_t expire, StockPrice price, Args&&... arg) {
68+
limit_queue.get(expire).emplace_back(std::forward<Args>(arg)...);
69+
auto* elem = &limit_queue.get(expire).back();
70+
71+
order_list[price].emplace_back(elem);
72+
auto* ref_ptr = &order_list[price].back();
73+
elem->refer = ref_ptr;
74+
}
75+
76+
bool OrderExists(StockPrice price) const {
77+
if (const auto find = order_list.find(price); find == order_list.end()) {
78+
return false;
79+
}
80+
if (order_list.at(price).empty()) {
81+
return false;
82+
}
83+
if (order_list.at(price).is_empty()) {
84+
return false;
85+
}
86+
return true;
87+
}
88+
};
89+
}
90+
#endif // !INCLUDE_TRADE_TRADEREQUESTBOARD_HPP_

0 commit comments

Comments
 (0)