-
Notifications
You must be signed in to change notification settings - Fork 0
/
Team.cpp
201 lines (151 loc) · 5.38 KB
/
Team.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// Created by omar_ on 16/02/2024.
//
#include "Team.h"
#include <iostream>
//int Team::getMedals() const {
// return medals;
//}
int Team::getStrength() const {
if(!strengthPlayer || players->getSize() == 0) return 0;
return strengthPlayer->getStrength() * numberOfPlayers;
}
bool Team::addPlayer(int playerStrength) {
shared_ptr<Player> contestant = make_shared<Player>(++numberOfPlayers, playerStrength);
if(!players->insert(contestant) || !playersByStrength->insert(contestant)) return false;
strengthPlayer = playersByStrength->getKthSmallest(numberOfPlayers/2 + 1); // O(logk)
return true;
}
// O(logk)
void Team::removePlayer() {
auto playerID = players->getMax()->getID();
auto playerStrength = players->getMax()->getStrength();
if(!players->remove(playerID) || !playersByStrength->remove(playerID, playerStrength)) return;
numberOfPlayers--;
// find strength player again
if(numberOfPlayers == 0){
strengthPlayer = nullptr;
return;
}
strengthPlayer = playersByStrength->getKthSmallest(numberOfPlayers/2 + 1); // O(logk)
}
int Team::getSize() const {
return numberOfPlayers;
}
void Team::printTeam() const {
cout << "Team " << this->getID() << ": ";
players->inorderPrint(players->root, cout);
cout << "Strength: " << this->getStrength() << endl;
}
shared_ptr<Player> Team::getStrengthPlayer() {
if (numberOfPlayers == 0) return nullptr;
return strengthPlayer;
}
//
//TODO: addWin needs to be a method in Olympics class. Because when you add a win to the team you need to update the extras in STree and maxRankTeam.
//TODO: I will make a method for STree addWin(id, str) --O(logn) which will update the winning team and you can call it from olympics.
//TODO: I will also make a method in the STree getTeamWins(id, str) --O(logn) which will calculate the wins for the team based on the extras.
/*void Team::addWin() {
numberOfWins++;
}
int Team::getNumberOfWins() const {
return numberOfWins;
}
int Team::getRank() const {
if (numberOfPlayers == 0) return numberOfPlayers;
return numberOfWins + this->getStrength();
}
*/
void Team::removeAllPlayers() {
players = make_shared<Tree<Player>>();
playersByStrength = make_shared<PSTree<Player>>();
numberOfPlayers = 0;
strengthPlayer = nullptr;
}
shared_ptr<Player> Team::getNewestPlayer() const {
auto player = players->getMax();
return player;
}
void Team::insert_ptr(const shared_ptr<Player>& player) {
players->insert(player);
playersByStrength->insert(player);
numberOfPlayers++;
strengthPlayer = playersByStrength->getKthSmallest(numberOfPlayers/2 + 1);
}
shared_ptr<Player> Team::findPlayer(int playerID) {
return players->find(playerID);
}
// you can delete this method I was testing something
/*
void Team::addWin() {
numOfWins++;
}
int Team::getNumberOfWins() const {
return numOfWins;
}
*/
shared_ptr<Player>* Team::returnedSortedArrayOfElementsByStrength(){
return playersByStrength->returnSortedArrayOfElements();
}
shared_ptr<Player>* Team::returnedSortedArrayOfElementsByID(){
return players->returnSortedArrayOfElements();
}
void Team::uniteWith(shared_ptr<Team> team2){
int team1Size = numberOfPlayers;
int team2Size = team2->getSize();
// int newSize = team2Size + team1Size;
shared_ptr<Player>* team2Arr = nullptr;
shared_ptr<Player>* team1Arr = nullptr;
shared_ptr<Player>* mergedArr = nullptr;
shared_ptr<Player>* team2StrArr = nullptr;
shared_ptr<Player>* team1StrArr = nullptr;
shared_ptr<Player>* mergedStrArr = nullptr;
try{
team2Arr = team2->returnedSortedArrayOfElementsByID();
team1Arr = returnedSortedArrayOfElementsByID();
mergedArr = new shared_ptr<Player>[team1Size + team2Size];
team2StrArr = team2->returnedSortedArrayOfElementsByStrength();
team1StrArr = returnedSortedArrayOfElementsByStrength();
mergedStrArr = new shared_ptr<Player>[team1Size + team2Size];
}catch (const exception& e){
// free allocated memory
if (team1Arr) delete[] team1Arr;
if (team2Arr) delete[] team2Arr;
if (mergedArr) delete[] mergedArr;
if (team2StrArr) delete[] team2StrArr;
if (team1StrArr) delete[] team1StrArr;
if (mergedStrArr) delete[] mergedStrArr;
throw e;
}
for (int i = 0; i < team1Size; i++){
mergedArr[i] = team1Arr[i];
}
for (int i = 0; i < team2Size; i++){
team2Arr[i]->id = team1Size + i + 1;
mergedArr[team1Size + i] = team2Arr[i];
}
int i = 0, j = 0, k = 0;
while (i < team1Size && j < team2Size) {
if (team1StrArr[i]->getStrength() <= team2StrArr[j]->getStrength()) {
mergedStrArr[k++] = team1StrArr[i++];
} else {
mergedStrArr[k++] = team2StrArr[j++];
}
}
while (i < team1Size) {
mergedStrArr[k++] = team1StrArr[i++];
}
while (j < team2Size) {
mergedStrArr[k++] = team2StrArr[j++];
}
numberOfPlayers += team2Size;
players = make_shared<Tree<Player>>(mergedArr, numberOfPlayers);
playersByStrength = make_shared<PSTree<Player>>(mergedStrArr, numberOfPlayers);
strengthPlayer = playersByStrength->getKthSmallest(numberOfPlayers/2 + 1);
delete[] team1Arr;
delete[] team2Arr;
delete[] team1StrArr;
delete[] team2StrArr;
delete[] mergedArr;
delete[] mergedStrArr;
}