Browse Source

feat: try less function calls

master
Dnomd343 2 years ago
parent
commit
d6eaef8864
  1. 7
      src/core/core.cc
  2. 13
      src/core/core.h
  3. 27
      src/fast_cal/fast_cal.cc
  4. 1
      src/fast_cal/fast_cal.h

7
src/core/core.cc

@ -172,9 +172,10 @@ void Core::next_step(uint64_t code, uint64_t mask) { // search next step cases
if (cache_size != 1) { // found one or more next cases
// TODO: try to send multi-items data
for (int i = 1; i < cache_size; ++i) {
release(cache[i].code, cache[i].mask); // release next cases
}
// for (int i = 1; i < cache_size; ++i) {
// release(cache[i].code, cache[i].mask); // release next cases
// }
release(cache, cache_size);
cache_size = 1; // reset cache size
}

13
src/core/core.h

@ -11,12 +11,7 @@
class Core {
public:
typedef std::function<void(uint64_t, uint64_t)> release_t;
void next_step(uint64_t code, uint64_t mask);
explicit Core(release_t release_func) : release(std::move(release_func)) {}
private:
struct cache_t {
uint64_t code; // case raw code
uint64_t mask; // only 000 or 111
@ -24,6 +19,14 @@ private:
int addr; // (0 ~ 19) * 3
};
// typedef std::function<void(uint64_t, uint64_t)> release_t;
typedef std::function<void(cache_t*, int)> release_t;
void next_step(uint64_t code, uint64_t mask);
explicit Core(release_t release_func) : release(std::move(release_func)) {}
private:
int cache_size = 1;
cache_t cache[16] = {0};
release_t release; // release code and mask

27
src/fast_cal/fast_cal.cc

@ -12,7 +12,8 @@ void FastCal::fast_cal(uint64_t code) {
auto core = Core(
[this](auto &&p1, auto &&p2) {
add_new_case(std::forward<decltype(p1)>(p1), std::forward<decltype(p2)>(p2));
// add_new_case(std::forward<decltype(p1)>(p1), std::forward<decltype(p2)>(p2));
add_new_case_demo(std::forward<decltype(p1)>(p1), std::forward<decltype(p2)>(p2));
}
);
@ -66,3 +67,27 @@ void FastCal::add_new_case(uint64_t code, uint64_t mask) {
cache.emplace(&cases[code]);
}
void FastCal::add_new_case_demo(Core::cache_t *cache_dat, int cache_size) {
for (int i = 1; i < cache_size; ++i) {
uint64_t code = cache_dat[i].code;
uint64_t mask = cache_dat[i].mask;
auto exist_case = cases.find(code);
if (exist_case != cases.end()) {
exist_case->second.mask |= mask;
continue;
}
cases[code] = fast_cal_t {
.code = code,
.mask = mask,
.last = cache.front(),
};
cache.emplace(&cases[code]);
}
}

1
src/fast_cal/fast_cal.h

@ -18,5 +18,6 @@ public:
void fast_cal(uint64_t code);
void add_new_case(uint64_t code, uint64_t mask);
void add_new_case_demo(Core::cache_t *cache_dat, int cache_size);
};

Loading…
Cancel
Save