| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include "json.hpp"
- #include "priorityqueue.h"
- int main(int argc, char** argv) {
- // read JSON file and store in jsonObject
- std::ifstream file;
- file.open(argv[1]);
- nlohmann::json jsonObject;
- // Store the contents filename into jsonObject
- if (file.is_open()) {
- file >> jsonObject;
- }
- std::string sampleName; // Sample key name to iterate
- nlohmann::json outputJSON; // output JSON file
- int currValue = 0; // current key value
- int n = jsonObject["metadata"]["arraySize"];
- std::vector<int> A(n, 0);
- class PriorityQueue sampleHeap;
- sampleHeap.initiateHeap(n);
- for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
- sampleName = itr.key();
- if (sampleName != "metadata") {
- nlohmann::json topOfHeap;
- for (auto arrayItr = jsonObject[sampleName].begin(); arrayItr != jsonObject[sampleName].end(); ++arrayItr) {
- // insert each array element into heap
- currValue = *arrayItr;
- sampleHeap.insert(currValue);
- }
- std::cout << "before sorting" << std::endl;
- sampleHeap.printArray();
- for (int i = n - 1; i >= 0; i--) {
- // insert heap.top into array A backwards to sort the array from min to max
- A[i] = sampleHeap.returnMax();
- // remove the max value from the heap
- sampleHeap.removeMax();
- std::cout << "after sorting" << std::endl;
- sampleHeap.printArray();
- }
- for (int j = 0; j <= n - 1; j++) {
- // adding values directly to JSON object
- topOfHeap.push_back(A[j]);
- std::cout << A[j] << ", ";
- }
- std::cout << std::endl;
- outputJSON[sampleName] = topOfHeap;
- }
- }
- std::cout << outputJSON << std::endl;
- // clean up memory
- file.close();
- sampleHeap.deleteHeap();
- return 0;
- }
|