This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·190 lines (171 loc) · 5.53 KB
/
main.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
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/filereadstream.h"
#include <fstream>
#include "utility"
#include "map"
#include "vector"
using namespace std;
//using namespace rapidjson;
/**
* returns a rapidjson document representation of the given file name
*/
struct a320{
int max_seats = 180;
int max_buisness = 30;
int used_seats = 0;
int used_buisness = 0;
bool seats[6][31]
};
rapidjson::Document parseFile(string file) {
rapidjson::Document d;
ifstream json_file;
json_file.open(file);
if (json_file.is_open()) {
string json_string;//string containing full json file
string line;//current line in file
while (getline(json_file, line)) {
json_string.append(line).append("\n");//append current line to full string
}
json_file.close();
char *json_charArray = const_cast<char *>(json_string.c_str());
d.Parse(json_charArray);
/*
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
*/
return d;
}
else {
cout << "Unable to open file\n";
cout << json_file.rdstate();
return d;
}
}
/**
* returns size of a passengergroup as int
* 1 if single passenger
*/
int getPassengerGroupSize(rapidjson::Value& value){
if(value.IsArray()){
return value.Size();
}else{
return 1;
}
}
/**
* sortArray
* sorts an Array by groupsize
*/
rapidjson::Value& sortPassengersBySize(rapidjson::Value& array){
assert(array.IsArray());
int array_size = array.Size();
for (int i = 0; i < array_size; ++i) {
int group_size;
if(array[i].IsArray()) {
group_size = array[i].Size();
}else{
group_size = 1;
}
int min = i;
for (int k = 0; k < array_size ; ++k) {
rapidjson::Value& newValue = array[k];
rapidjson::Value& oldValue = array[min];
if(getPassengerGroupSize(newValue) < getPassengerGroupSize(oldValue))
min = k;
}
array[i].Swap(array[min]);
}
return array;
}
/**
* copies a Document
*/
static void copyDocument(rapidjson::Document & newDocument, rapidjson::Document & copiedDocument) {
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
newDocument.Accept(writer);
std::string str = strbuf.GetString();
copiedDocument.Parse<0>(str.c_str());
}
/**
* writes a Document to StdOut
*/
void writeDocument(rapidjson::Document doc){
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
}
void writeFirstID(rapidjson::Value& value){
if(value.IsArray()){
cout << value[0]["ID"].GetInt();
}else{
cout << value["ID"].GetInt();
}
}
bool compareArraysByID(rapidjson::Document& first_doc, rapidjson::Document& second_doc){
rapidjson::Value& first = first_doc["passengers"];
rapidjson::Value& second= second_doc["passengers"];
if(first.Size()==second.Size()){
for (int i = 0; i < first.Size(); ++i) {
if(first[i].IsArray()){
if(second[i].IsArray()){
for (int j = 0; j < first[i].Size(); ++j) {
if(!(first[i][j]["ID"].GetInt()==second[i][j]["ID"])){
return false;
}
}
}else{
return false;
}
}else if(second[i].IsArray()){
return false;
}else if(!(first[i]["ID"].GetInt()==second[i]["ID"])){
return false;
}
}
return true;
}else{
return false;
}
}
/**
* returns a Multimap containing
*/
multimap<string,rapidjson::Value&>splitByDestination(rapidjson::Value& passengers){
multimap<string,rapidjson::Value&> passengers_destination;
for (int i = 0; i < passengers.Size(); ++i) {
if(passengers[i].IsArray()){
// group
rapidjson::Value& current_passenger = passengers[i];
string current_destination = current_passenger[0]["destination"].GetString();
passengers_destination.insert(std::pair<string,rapidjson::Value&>(current_destination, current_passenger));
}else{
// single passenger
string current_destination = passengers[i]["destination"].GetString();
rapidjson::Value& current_passenger = passengers[i];
passengers_destination.insert(std::pair<string,rapidjson::Value&>(current_destination, current_passenger));
}
map<string,rapidjson::Value> passengers_dest;
}
return passengers_destination;
}
int main() {
//Import
rapidjson::Document passengers_doc = parseFile("passengers.json");//import passengers file
rapidjson::Value& passengers = passengers_doc["passengers"];//get passengers array
/*
rapidjson::Document passengers_sorted_doc = parseFile("passengers.json");
rapidjson::Value& passengers_sorted = passengers_sorted_doc["passengers"];
cout << compareArraysByID(passengers_sorted_doc,passengers_doc) << '\n';
sortPassengersBySize(passengers_sorted);
cout << compareArraysByID(passengers_sorted_doc,passengers_doc) << '\n';
*/
//split by destination
multimap<string,rapidjson::Value&> passengers_destination;
passengers_destination = splitByDestination(passengers);
return 0;
}