master
IoTcat 5 years ago
parent c4db7f3de5
commit fc6cc4e7f2
  1. 48
      lib/ovo.h
  2. 7
      src/car.h
  3. 25
      src/main.cpp
  4. 61
      src/park.h
  5. 18
      src/plot.h

@ -325,6 +325,20 @@ namespace ovo{
this->_data[this->toStr(key)] = this->toStr(val);
};
/* attach data */
template <typename T>
inline void attachData(const T& key, ovo::data data){
this->isExist(key);
this->_data[this->toStr(key)] = this->dataToStr(data);
};
/* attach data */
template <typename T>
inline void attachData(const T& key, string s){
this->isExist(key);
this->_data[this->toStr(key)] = s;
};
/* overload [] */
template <typename T>
string& operator[](const T& i){
@ -394,6 +408,39 @@ namespace ovo{
}
}
/* data to string */
string dataToStr(ovo::data& d){
string s = "__OVO_DATA__";
d.classify();
d.forEach([&](string first, string second){
s += first + "$$||$$" + second + "$$||$$";
});
return s;
};
/* string to data */
ovo::data strToData(string s){
ovo::data d;
if(s.length() <= 12 || s.substr(0, 12) != "__OVO_DATA__") return d;
s = s.substr(12);
std::vector<string> v;
S.split(s, v, "$$||$$");
for(int i = 0; i < v.size() - 1; i += 2){
d[v[i]] = v[i + 1];
}
return d;
};
/* for each */
void forEach(auto f){
map<string, string>::iterator t_iter = this->_data.begin();
@ -423,6 +470,7 @@ namespace ovo{
private:
map<string, string> _data;
ovo::String S;
/* string to string */
inline string toStr(const string& from) const{
return from;

@ -23,11 +23,13 @@ public:
this->_d["plot"] = "null";
this->_d["LastInTime"] = "null";
this->_d["LastOutTime"] = "null";
this->_isExist = true;
};
Car(const string& licenseNum){
this->_d = db.getData(licenseNum);
this->_isExist = true;
};
~Car(){
@ -36,6 +38,10 @@ public:
db.pushData(this->_d["id"], this->_d);
};
inline bool isExist(){
return this->_isExist;
};
inline string getID(){
return this->_d["id"];
};
@ -52,6 +58,7 @@ public:
private:
ovo::data _d;
ovo::db db;
bool _isExist;
};

@ -1,13 +1,24 @@
#include <iostream>
#include <vector>
#include <map>
#include <ctime>
#include "park.h"
#include "../lib/ovo.h"
using namespace std;
int main(int argc, char const *argv[])
{
{/*
Plot p("eeeee", 3, "222");
ovo::data d;
d["eeeee"] = p.getStrContent();
cout << d.showAll();*/
Park p;
cout << p.isExist();
@ -16,7 +27,7 @@ int main(int argc, char const *argv[])
map<string, int> m;
m["Bycycle"] = 5;
m["Bycycle"] = 599;
m["Cars"] = 2;
v.push_back(m);
@ -24,11 +35,13 @@ int main(int argc, char const *argv[])
m["Carsss"] = 99;
v.push_back(m);
//p.ini(v);
p.ini(v);
//cout << p.checkType("Cadrs");
cout << p.getPlots(1).size();
p.join();
int t = time(NULL);
cout << p.getPlotsID().size();
cout << " " << time(NULL) - t;
return 0;
}

@ -6,6 +6,7 @@
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <map>
#include <thread>
#include "../lib/ovo.h"
@ -34,15 +35,8 @@ public:
this->_plotsList = db.getData(this->_d["plotsList"]);
this->_carsList = db.getData(this->_d["carsList"]);
this->_plotsList.forEach([&](string first, string second){
this->_plots.push_back(Plot(first));
});
this->_carsList.forEach([&](string first, string second){
this->_cars.push_back(Car(first));
});
this->_threadFinished = true;
});
@ -53,7 +47,6 @@ public:
~Park(){
if(!this->_threadFinished) this->_t->join();
delete this->_t;
this->_d.classify();
this->_plotsList.classify();
@ -64,7 +57,12 @@ public:
};
void join(){
if(!this->_threadFinished) this->_t->join();
if(!this->_threadFinished)
{
this->_t->join();
delete this->_t;
this->_threadFinished = true;
}
}
bool isExist(){
@ -79,20 +77,39 @@ public:
if(!this->_threadFinished) this->_t->join();
return this->_levels;
}
/*
bool checkType(const string& type){
if(!this->_threadFinished) this->_t->join();
if(find(this->_types.begin(), this->_types.end(), type) == this->_types.end()){
return false;
}
return true;
};
};*/
std::vector<Plot> getPlots(){
if(!this->_threadFinished) this->_t->join();
std::vector<Plot> v;
this->_plotsList.forEach([&](string first, string second){
v.push_back(Plot(second));
});
return v;
};
std::vector<string> getPlotsID(){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
v.push_back(this->_plotsList.strToData(second)["id"]);
});
return v;
};
/*
std::vector<Plot> getPlots(bool isOccupied){
if(!this->_threadFinished) this->_t->join();
return this->_plots;
};
std::vector<Plot> getPlots(const int& level){
if(!this->_threadFinished) this->_t->join();
int t = time(NULL);
@ -197,7 +214,7 @@ public:
v1 = this->getCars(type);
v2 = this->getCars(level);
/* 待实现 */
// 待实现
cout << endl << time(NULL) - t << endl;
return v;
@ -227,7 +244,7 @@ public:
this->_carsList[licenseNum] = time(NULL);
return c;
};
*/
void ini(std::vector<std::map<string, int>>& v){
@ -246,10 +263,6 @@ public:
this->_d["types"] += i + "|||$$|||";
}
this->_setupPlots(v);
for(Plot i : this->_plots){
this->_plotsList[i.getID()] = time(NULL);
}
};
@ -261,8 +274,6 @@ public:
ovo::math m;
ovo::String S;
ovo::db db;
std::vector<Plot> _plots;
std::vector<Car> _cars;
std::thread *_t;
bool _threadFinished;
@ -282,18 +293,26 @@ public:
void _setupPlots(std::vector<std::map<string, int>>& v){
int t = time(NULL);
for(unsigned int i = 0; i < v.size(); i ++){
for(auto ii : v[i]){
for(int iii = 0; iii < ii.second; iii ++){
this->_plots.push_back(Plot(m.randStr(), i, ii.first));
string s = m.randStr();
this->_plotsList[s] = this->_simplePlot(s, i, ii.first);
}
}
}
cout << "Used" << time(NULL) - t;
};
string _simplePlot(const string& id, const int& level, const string& type){
return "__OVO_DATA__id$$||$$" + id + "$$||$$level$$||$$" + to_string(level) + "$$||$$type$$||$$"
+ type + "$$||$$car$$||$$null$$||$$LastOperateTime$$||$$null$$||$$CreatedTime$$||$$null$$||$$";
}
};

@ -21,16 +21,14 @@ public:
this->_d["level"] = to_string(level);
this->_d["type"] = type;
this->_d["car"] = "null";
this->_d["LastOperateTime"] = time(NULL);
this->_d["CreatedTime"] = time(NULL);
this->_d["LastOperateTime"] = to_string(time(NULL));
this->_d["CreatedTime"] = to_string(time(NULL));
this->_isExist = true;
};
Plot(const string& id){
Plot(const string& s){
this->_d = db.getData(id);
if(this->_d["_isExist"] == "NO") throw "Plot::Recover Data From NOTHING!!!";
this->_d.clear("_isExist");
this->_d = this->_d.strToData(s);
this->_isExist = true;
};
@ -77,10 +75,16 @@ public:
this->_d["type"] = type;
};
private:
friend class Park;
//private:
ovo::data _d;
ovo::db db;
bool _isExist;
string getStrContent(){
return this->_d.dataToStr(this->_d);
};
};

Loading…
Cancel
Save