- 以前敲ACM时习惯了单文件,全局变量的风格,要努力向规范看齐
- 这是一个的数据库中间件程序,软件杯题目
自己以前的风格
memDB.cpp
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include<time.h>
using namespace std;
#define MAX 200000
struct Index{
int time;
int index;
public:
Index(int time, int index){
this -> time = time;
this -> index = index;
}
};
struct Record {
string car;
int posx;
int posy;
int time;
void addrd(string car, int x, int y, int time){
this -> car = car;
this -> posx = x;
this -> posy = y;
this -> time = time;
}
};
typedef struct {
Record rd[MAX * 10];
}Record_Of_Five;
struct Global{
int idx = 0;
Record_Of_Five *rdf;
Record_Of_Five *rdfbk;
map<int, string> mapstr;
vector<Index> vec[MAX];
map<string, vector<Index> > mapvec;
int term_five;
};
void init(Global &global){
global.rdf = new Record_Of_Five();
global.rdfbk = new Record_Of_Five();
char buf[10];
for(int i = 0; i < MAX; i++)
{
int j = 1000000 + i + 1;
sprintf(buf, "%d", j);
string str = "A";
for(int ii = 1; ii < 7; ii++)
str += buf[ii];
global.mapstr[i] = str;
}
for(int i = 0; i < MAX; i++){
global.mapvec[(global.mapstr)[i]] = (global.vec)[i];
}
}
bool insert(Global &global, string str, int x, int y, int time)
{
int term_five_tmp = time / 300;
if(term_five_tmp != global.term_five){
global.term_five = term_five_tmp;
Record_Of_Five *tmp = global.rdf;
global.rdf = global.rdfbk;
global.rdfbk = tmp;
global.idx = 0;
}
global.rdf -> rd[(global.idx)++].addrd(str, x, y, time);
Index index(time, global.idx - 1);
global.mapvec[str].push_back(index);
return true;
}
vector<Index> select(Global &global, string car)
{
vector<Index> tmp = global.mapvec[car];
int len = tmp.size();
for(int i = 0; i < len; i++){
cout << tmp[i].index;
}
return global.mapvec[car];
}
unitTest.cpp
#include <cstdio>
#include "middleware.cpp"
using namespace std;
int main()
{
Global global;
init(global);
clock_t start_time=clock();
for(int i = 0; i < 200000; i++){
string car = global.mapstr[i];
for(int j = 0; j < 5; j++) {
insert(global, car, j, 1, 1);
}
}
cout << "当前已经insert数目: " << global.idx << endl;
clock_t end_time=clock();
cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;
vector<Index> tmp = select(global, "A000002");
return 0;
}
规范代码
memDB.h
#pragma once
#include <vector>
#include <map>
#define MAX 200000
namespace memDB {
using namespace std;
struct Record {
string car;
int posx;
int posy;
int time;
void addrd(string car, int x, int y, int time) {
this->car = car;
this->posx = x;
this->posy = y;
this->time = time;
}
};
struct Record_Of_Five{
Record rd[MAX * 10];
};
struct Index {
int time;
int index;
public:
Index(int time, int index) {
this->time = time;
this->index = index;
}
};
struct Global{
int idx = 0;
Record_Of_Five *rdf;
Record_Of_Five *rdfbk;
map<int, string> mapstr;
vector<Index> vec[MAX];
map<string, vector<Index> > mapvec;
int term_five;
};
void init();
bool insert(string str, int x, int y, int time);
vector<Index> select(string car);
}
memDB.cpp
#include "memDB.h"
#include <iostream>
using namespace memDB;
void init(Global &global){
global.rdf = new Record_Of_Five();
global.rdfbk = new Record_Of_Five();
char buf[10];
for(int i = 0; i < MAX; i++)
{
int j = 1000000 + i + 1;
sprintf(buf, "%d", j);
string str = "A";
for(int ii = 1; ii < 7; ii++)
str += buf[ii];
global.mapstr[i] = str;
}
for(int i = 0; i < MAX; i++){
global.mapvec[(global.mapstr)[i]] = (global.vec)[i];
}
}
bool insert(Global &global, string str, int x, int y, int time)
{
int term_five_tmp = time / 300;
if(term_five_tmp != global.term_five){
global.term_five = term_five_tmp;
Record_Of_Five *tmp = global.rdf;
global.rdf = global.rdfbk;
global.rdfbk = tmp;
global.idx = 0;
}
global.rdf -> rd[(global.idx)++].addrd(str, x, y, time);
Index index(time, global.idx - 1);
global.mapvec[str].push_back(index);
return true;
}
vector<Index> select(Global &global, string car)
{
vector<Index> tmp = global.mapvec[car];
int len = tmp.size();
for(int i = 0; i < len; i++){
cout << tmp[i].index;
}
return global.mapvec[car];
}