#ifndef PROG_H_ #define PROG_H_ #include #include #include #include #include #include #include using namespace std; string next_op(vector &args, int &i) { if (i < args.size()) { return args[i++]; } else { return ""; } } bool repeat(vector &args, int i, int n, function)> exec) { vector v; v.assign(args.begin() + i, args.end()); v.insert(v.begin(), ""); v.insert(v.begin(), ""); while ( n-- > 1 ) { if ( !exec(v) ) { return false; } } return true; } bool run(string filename, function)> exec) { ifstream ifs(filename); string content((istreambuf_iterator(ifs)), (istreambuf_iterator())); istringstream iss(content); vector args(istream_iterator{iss}, istream_iterator()); args.insert(args.begin(), ""); args.insert(args.begin(), ""); return exec(args); } #include std::string format(const char *fmt...) { char str[128]; va_list args; va_start(args, fmt); vsprintf(str, fmt, args); va_end(args); return str; } #include #ifndef _WIN32 #include #define CREATE_MODE_RW (S_IWUSR | S_IRUSR) /* * file_exists -- checks if file exists */ static inline int file_exists(char const *file) { return access(file, F_OK); } /* * find_last_set_64 -- returns last set bit position or -1 if set bit not found */ static inline int find_last_set_64(uint64_t val) { return 64 - __builtin_clzll(val) - 1; } #else #include #include #include #define CREATE_MODE_RW (S_IWRITE | S_IREAD) /* * file_exists -- checks if file exists */ static inline int file_exists(char const *file) { return _access(file, 0); } /* * find_last_set_64 -- returns last set bit position or -1 if set bit not found */ static inline int find_last_set_64(uint64_t val) { DWORD lz = 0; if (BitScanReverse64(&lz, val)) return (int)lz; else return -1; } #endif #endif