add log.cpp

This commit is contained in:
lcw 2024-02-09 09:49:36 +08:00
parent d2449abd61
commit 7c156dacb7
4 changed files with 131 additions and 78 deletions

View File

@ -28,59 +28,57 @@ bool ban(VertexType t) {
return t == VertexType::Mountain || t == VertexType::Capital; return t == VertexType::Mountain || t == VertexType::Capital;
} }
template <typename T> template<typename T>
void upmin(T& x, const T& y) { void upmin(T& x, const T& y) {
if (y < x) if (y < x)
x = y; x = y;
} }
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; bool findCut(pos_t w, pos_t h, vector<vector<VertexType>>& vertex_type) {
int idx = 0;
vector<vector<int>> dfn(h, vector<int>(w)), low = dfn;
function<void(int, int, int, int, bool)> tarjan
= [&](int x, int y, int fx, int fy, bool rt) {
dfn[x][y] = low[x][y] = ++idx;
int ch = 0;
for (int i = 0; i < 4; i++) {
const int xx = x + direction_dx[i], yy = y + direction_dy[i];
if (xx < 0 || xx >= h || yy < 0 || yy >= w)
continue;
bool findCut(pos_t w, pos_t h, vector<vector<VertexType>> &vertex_type) { if (vertex_type[xx][yy] == VertexType::Capital)
int idx = 0; vertex_type[x][y] = VertexType::Cut;
vector<vector<int>> dfn(h, vector<int>(w)), low = dfn;
function<void(int, int, int, int, bool)> tarjan =
[&](int x, int y, int fx, int fy, bool rt) {
dfn[x][y] = low[x][y] = ++idx;
int ch = 0;
for (int i = 0; i < 4; i++) {
const int xx = x + dx[i], yy = y + dy[i];
if (xx < 0 || xx >= h || yy < 0 || yy >= w)
continue;
if (vertex_type[xx][yy] == VertexType::Capital) if (ban(vertex_type[xx][yy]) || (xx == fx && yy == fy))
vertex_type[x][y] = VertexType::Cut; continue;
if (ban(vertex_type[xx][yy]) || (xx == fx && yy == fy)) if (!dfn[xx][yy]) {
continue; ch++;
tarjan(xx, yy, x, y, 0);
upmin(low[x][y], low[xx][yy]);
if (!rt && low[xx][yy] >= dfn[x][y])
vertex_type[x][y] = VertexType::Cut;
} else {
upmin(low[x][y], dfn[xx][yy]);
}
}
if (!dfn[xx][yy]) { if (rt && ch >= 2)
ch++; vertex_type[x][y] = VertexType::Cut;
tarjan(xx, yy, x, y, 0); };
upmin(low[x][y], low[xx][yy]);
if (!rt && low[xx][yy] >= dfn[x][y])
vertex_type[x][y] = VertexType::Cut;
} else {
upmin(low[x][y], dfn[xx][yy]);
}
}
if (rt && ch >= 2) bool flag = false;
vertex_type[x][y] = VertexType::Cut; for (pos_t i = 0; i < h; i++) {
}; for (pos_t j = 0; j < w; j++) {
if (!ban(vertex_type[i][j]) && !dfn[i][j]) {
bool flag = false; if (flag)
for (pos_t i = 0; i < h; i++) { return false;
for (pos_t j = 0; j < w; j++) { flag = 1;
if (!ban(vertex_type[i][j]) && !dfn[i][j]) { tarjan(i, j, -1, -1, 1);
if (flag) }
return false; }
flag = 1; }
tarjan(i, j, -1, -1, 1); return true;
}
}
}
return true;
}; };
pair<bool, vector<Point>> generateCapital( pair<bool, vector<Point>> generateCapital(
@ -88,7 +86,6 @@ pair<bool, vector<Point>> generateCapital(
pos_t h, pos_t h,
vector<vector<VertexType>>& vertex_type, vector<vector<VertexType>>& vertex_type,
int r) { int r) {
vector<Point> res(r); vector<Point> res(r);
for (int i = 0; i <= r; i++) { for (int i = 0; i <= r; i++) {
if (!findCut(w, h, vertex_type)) if (!findCut(w, h, vertex_type))
@ -168,25 +165,27 @@ GameBoard ImportedTerrain::makeGameBoard(const InitInfo& init_info) {
vector<Player> rest2; vector<Player> rest2;
arrange(rest1, capitals.back(), rest2); arrange(rest1, capitals.back(), rest2);
for (pos_t x = 0; x < h; x++) { for (pos_t x = 0; x < h; x++) {
for (pos_t y = 0; y < w; y++) { for (pos_t y = 0; y < w; y++) {
if (g.at(x, y).type == TileType::Capital) { if (g.at(x, y).type == TileType::Capital) {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
const int xx = x + dx[i], yy = y + dy[i]; const int xx = x + direction_dx[i],
if (xx < 0 || xx >= h || yy < 0 || yy >= w) yy = y + direction_dy[i];
continue; if (xx < 0 || xx >= h || yy < 0 || yy >= w)
if (g.at(xx, yy).type == TileType::Capital) continue;
throw BadCapitalAssign(); if (g.at(xx, yy).type == TileType::Capital)
} throw BadCapitalAssign();
} }
} }
} }
}
int T = 5; int T = 5;
while (T--) { while (T--) {
vector<vector<VertexType>> vertex_type(h vector<vector<VertexType>> vertex_type(
, vector<VertexType>(w, VertexType::Ordinary)); h,
vector<VertexType>(w, VertexType::Ordinary));
for (pos_t i = 0; i < h; i++) { for (pos_t i = 0; i < h; i++) {
for (pos_t j = 0; j < w; j++) { for (pos_t j = 0; j < w; j++) {
if (g.at(i, j).type == TileType::Mountain) if (g.at(i, j).type == TileType::Mountain)
@ -215,18 +214,18 @@ ImportedTerrain importTerrain(const ImportTerrainConfig& in) {
t.w = in.w; t.w = in.w;
t.h = in.h; t.h = in.h;
t.tiles.resize(t.h, vector<ImportedTerrain::TerrainTile>(t.w)); t.tiles.resize(t.h, vector<ImportedTerrain::TerrainTile>(t.w));
size_t pt = 0; size_t pt = 0;
auto error = [&]() { throw BadImportTerrainConfig(pt); }; auto error = [&]() { throw BadImportTerrainConfig(pt); };
auto view = [&](char c) { auto view = [&](char c) {
return pt < in.value.size() && in.value[pt] == c ? (pt++, true) : false; return pt < in.value.size() && in.value[pt] == c ? (pt++, true) : false;
}; };
auto jump = [&](char c) { auto jump = [&](char c) {
if (!view(c)) if (!view(c))
error(); error();
}; };
auto read = [&](int l = -99999, int r = 99999) { auto read = [&](int l = -99999, int r = 99999) {
bool neg = view('-'); bool neg = view('-');
if (!(pt < in.value.size() && isdigit(in.value[pt]))) if (!(pt < in.value.size() && isdigit(in.value[pt])))
@ -250,8 +249,9 @@ ImportedTerrain importTerrain(const ImportTerrainConfig& in) {
error(); error();
vector<vector<vector<Point>>> vec(27, vector<vector<Point>>(101)); vector<vector<vector<Point>>> vec(27, vector<vector<Point>>(101));
vector<vector<VertexType>> vertex_type(t.h vector<vector<VertexType>> vertex_type(
, vector<VertexType>(t.w, VertexType::Ordinary)); t.h,
vector<VertexType>(t.w, VertexType::Ordinary));
for (pos_t i = 0; i < t.h; i++) { for (pos_t i = 0; i < t.h; i++) {
for (pos_t j = 0; j < t.w; j++) { for (pos_t j = 0; j < t.w; j++) {
auto& p = t.tiles[i][j]; auto& p = t.tiles[i][j];
@ -259,7 +259,7 @@ ImportedTerrain importTerrain(const ImportTerrainConfig& in) {
p.type = TileType::Blank; p.type = TileType::Blank;
} else if (view('m')) { } else if (view('m')) {
p.type = TileType::Mountain; p.type = TileType::Mountain;
vertex_type[i][j] = VertexType::Mountain; vertex_type[i][j] = VertexType::Mountain;
} else if (view('n')) { } else if (view('n')) {
p.type = TileType::Blank; p.type = TileType::Blank;
p.unit = read(); p.unit = read();
@ -288,8 +288,8 @@ ImportedTerrain importTerrain(const ImportTerrainConfig& in) {
if (pt != in.value.size()) if (pt != in.value.size())
error(); error();
if (!findCut(t.w, t.h, vertex_type)) if (!findCut(t.w, t.h, vertex_type))
error(); error();
t.capitals.emplace_back(); t.capitals.emplace_back();
for (int i = 0; i <= 26; i++) { for (int i = 0; i <= 26; i++) {

View File

@ -42,4 +42,3 @@ private:
ImportedTerrain importTerrain(const ImportTerrainConfig& in); ImportedTerrain importTerrain(const ImportTerrainConfig& in);
#endif #endif

51
processor/utility/log.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "log.h"
#include <cstdio>
#include <ctime>
Logger* Logger::getInstance() {
return instance_;
}
void Logger::debug(const std::string& s) const {
_log(LogLevel::Debug, s);
}
void Logger::info(const std::string& s) const {
_log(LogLevel::Info, s);
}
void Logger::warn(const std::string& s) const {
_log(LogLevel::Warn, s);
}
void Logger::error(const std::string& s) const {
_log(LogLevel::Error, s);
}
void Logger::fatal(const std::string& s) const {
_log(LogLevel::Fatal, s);
}
void Logger::setLogLeval(LogLevel v) {
filter = v;
}
Logger::Logger() {
filter = LogLevel::Warn;
}
Logger* const Logger::instance_ = new Logger();
void Logger::_log(LogLevel v, const std::string& s) const {
if (v >= filter) {
time_t now = time(NULL);
static char buff[100];
strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", localtime(&now));
const char* mp = "DIWEF";
fprintf(stderr, "%s [%c] %s\n", buff, mp[(int)v], s.c_str());
}
}
Logger* Log() {
return Logger::getInstance();
}

View File

@ -1,27 +1,30 @@
#ifndef OG_LOG_H_ #ifndef OG_LOG_H_
#define OG_LOG_H_ #define OG_LOG_H_
#include "pc/commcode.h"
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include "pc/commcode.h"
class Logger { class Logger {
public: public:
Logger *getInstance() const; static Logger* getInstance();
void debug(const std::string &) const; void debug(const std::string&) const;
void info(const std::string &) const; void info(const std::string&) const;
void warn(const std::string &) const; void warn(const std::string&) const;
void error(const std::string &) const; void error(const std::string&) const;
void fatal(const std::string &) const; void fatal(const std::string&) const;
void setLogLeval(LogLevel v);
private: private:
Logger(); Logger();
void _log(LogLevel v, const std::string &) const; static Logger* const instance_;
void _log(LogLevel v, const std::string&) const;
LogLevel filter; LogLevel filter;
}; };
Logger *Log(); Logger* Log();
#endif #endif