-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRiverRangeManager.cpp
81 lines (62 loc) · 2.31 KB
/
RiverRangeManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by Xuefeng Huang on 2020/1/31.
//
#include "include/ranges/RiverRangeManager.h"
#include <utility>
RiverRangeManager::RiverRangeManager() = default;
RiverRangeManager::RiverRangeManager(shared_ptr<Compairer> handEvaluator) {
this->handEvaluator = std::move(handEvaluator);
this->maplock = std::make_shared<std::mutex>();
}
const vector<RiverCombs> &
RiverRangeManager::getRiverCombos(int player, const vector<PrivateCards> &riverCombos, const vector<int> &board) {
uint64_t board_long = Card::boardInts2long(board);
return this->getRiverCombos(player,riverCombos,board_long);
}
const vector<RiverCombs> &
RiverRangeManager::getRiverCombos(int player, const vector<PrivateCards> &preflopCombos, uint64_t board_long) {
unordered_map<uint64_t , vector<RiverCombs>>* riverRanges;
if (player == 0)
riverRanges = &p1RiverRanges;
else if (player == 1)
riverRanges = &p2RiverRanges;
else
throw runtime_error(tfm::format("player %s not found",player));
uint64_t key = board_long;
this->maplock->lock();
if (riverRanges->find(key) != riverRanges->end()) {
const vector<RiverCombs> &retval = (*riverRanges)[key];
this->maplock->unlock();
return retval;
}
this->maplock->unlock();
int count = 0;
for (auto one_hand : preflopCombos) {
if (!Card::boardsHasIntercept(
one_hand.toBoardLong(), board_long
))
count++;
}
int index = 0;
vector<RiverCombs> riverCombos = vector<RiverCombs>(count);
for (int hand = 0; hand < preflopCombos.size(); hand++)
{
PrivateCards preflopCombo = preflopCombos[hand];
if (Card::boardsHasIntercept(
preflopCombo.toBoardLong(), board_long
)){
continue;
}
int rank = this->handEvaluator->get_rank(preflopCombo.toBoardLong(),board_long);
RiverCombs riverCombo = RiverCombs(Card::long2board(board_long),preflopCombo,rank, hand);
riverCombos[index++] = riverCombo;
}
std::sort(riverCombos.begin(),riverCombos.end(),[ ]( const RiverCombs& lhs, const RiverCombs& rhs )
{
return lhs.rank > rhs.rank;
});
this->maplock->lock();
(*riverRanges)[key] = std::move(riverCombos);
this->maplock->unlock();
return (*riverRanges)[key];
}