Initial commit: HTab spreadsheet engine and GemUI X11 frontend
This commit is contained in:
commit
395f458fee
55 files changed
+12682
No files matched your search
@@ -0,0 +1,222 @@
|
||||
#include "engine/functions/helpers.hpp"
|
||||
#include "engine/functions/registry.hpp"
|
||||
#include "engine/workbook.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
#include <variant>
|
||||
|
||||
namespace htab {
|
||||
|
||||
using namespace htab::fn;
|
||||
|
||||
namespace {
|
||||
|
||||
double compute_variance(const std::vector<double>& nums, bool sample) {
|
||||
double sum = 0.0;
|
||||
for (double v : nums) sum += v;
|
||||
double mean = sum / static_cast<double>(nums.size());
|
||||
double sq_sum = 0.0;
|
||||
for (double v : nums) {
|
||||
double d = v - mean;
|
||||
sq_sum += d * d;
|
||||
}
|
||||
double denom = static_cast<double>(sample ? (nums.size() - 1) : nums.size());
|
||||
return sq_sum / denom;
|
||||
}
|
||||
|
||||
EvalResult fn_average(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.empty()) return err_res(ErrorCode::Value);
|
||||
std::vector<double> nums;
|
||||
if (auto err = collect_numeric_args(args, ctx, nums)) return err_res(*err);
|
||||
if (nums.empty()) return err_res(ErrorCode::Div0);
|
||||
double sum = 0.0;
|
||||
for (double v : nums) sum += v;
|
||||
return EvalResult::from_value(Value::make_checked_number(sum / static_cast<double>(nums.size())));
|
||||
}
|
||||
|
||||
EvalResult fn_count(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.empty()) return err_res(ErrorCode::Value);
|
||||
size_t count = 0;
|
||||
for (const auto& arg : args) {
|
||||
EvalResult res = evaluate_ast(arg.get(), ctx);
|
||||
if (res.is_ref) {
|
||||
for (const auto& rng : res.ranges) {
|
||||
for (int r = rng.r1; r <= rng.r2; ++r) {
|
||||
for (int c = rng.c1; c <= rng.c2; ++c) {
|
||||
Value v = ctx.wb->get_scalar_value(rng.sheet, r, c);
|
||||
if (v.is_number()) ++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (res.value.is_array()) {
|
||||
const auto& arr = res.value.as_array();
|
||||
if (arr) {
|
||||
for (const auto& v : arr->data) {
|
||||
if (v.is_number()) ++count;
|
||||
}
|
||||
}
|
||||
} else if (!res.value.is_error() && !res.value.is_empty()) {
|
||||
auto num = to_number(res.value);
|
||||
if (std::holds_alternative<double>(num)) ++count;
|
||||
}
|
||||
}
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(count)));
|
||||
}
|
||||
|
||||
EvalResult fn_counta(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.empty()) return err_res(ErrorCode::Value);
|
||||
size_t count = 0;
|
||||
for (const auto& arg : args) {
|
||||
EvalResult res = evaluate_ast(arg.get(), ctx);
|
||||
if (res.is_ref) {
|
||||
for (const auto& rng : res.ranges) {
|
||||
for (int r = rng.r1; r <= rng.r2; ++r) {
|
||||
for (int c = rng.c1; c <= rng.c2; ++c) {
|
||||
Value v = ctx.wb->get_scalar_value(rng.sheet, r, c);
|
||||
if (!v.is_empty()) ++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (res.value.is_array()) {
|
||||
const auto& arr = res.value.as_array();
|
||||
if (arr) {
|
||||
for (const auto& v : arr->data) {
|
||||
if (!v.is_empty()) ++count;
|
||||
}
|
||||
}
|
||||
} else if (!res.value.is_empty()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(count)));
|
||||
}
|
||||
|
||||
EvalResult fn_countblank(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() != 1) return err_res(ErrorCode::Value);
|
||||
auto arr_var = result_to_array2d(evaluate_ast(args[0].get(), ctx), *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(arr_var)) return err_res(std::get<ErrorCode>(arr_var));
|
||||
const Array2D& arr = std::get<Array2D>(arr_var);
|
||||
size_t count = 0;
|
||||
for (const auto& v : arr.data) {
|
||||
if (v.is_empty() || (v.is_string() && v.as_string().empty())) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(count)));
|
||||
}
|
||||
|
||||
EvalResult fn_countif(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() != 2) return err_res(ErrorCode::Value);
|
||||
auto arr_var = result_to_array2d(evaluate_ast(args[0].get(), ctx), *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(arr_var)) return err_res(std::get<ErrorCode>(arr_var));
|
||||
const Array2D& arr = std::get<Array2D>(arr_var);
|
||||
Value crit = deref_to_value(evaluate_ast(args[1].get(), ctx), *ctx.wb);
|
||||
if (crit.is_error()) return EvalResult::from_value(crit);
|
||||
|
||||
size_t count = 0;
|
||||
for (const auto& v : arr.data) {
|
||||
if (matches_criteria(v, crit)) ++count;
|
||||
}
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(count)));
|
||||
}
|
||||
|
||||
EvalResult fn_countifs(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.size() < 2 || args.size() % 2 != 0) return err_res(ErrorCode::Value);
|
||||
struct Pair { Array2D range; Value crit; };
|
||||
std::vector<Pair> pairs;
|
||||
int rows = -1, cols = -1;
|
||||
for (size_t i = 0; i < args.size(); i += 2) {
|
||||
auto arr_var = result_to_array2d(evaluate_ast(args[i].get(), ctx), *ctx.wb);
|
||||
if (std::holds_alternative<ErrorCode>(arr_var)) return err_res(std::get<ErrorCode>(arr_var));
|
||||
Array2D arr = std::move(std::get<Array2D>(arr_var));
|
||||
if (rows < 0) {
|
||||
rows = arr.rows;
|
||||
cols = arr.cols;
|
||||
} else if (arr.rows != rows || arr.cols != cols) {
|
||||
return err_res(ErrorCode::Value);
|
||||
}
|
||||
Value crit = deref_to_value(evaluate_ast(args[i + 1].get(), ctx), *ctx.wb);
|
||||
if (crit.is_error()) return EvalResult::from_value(crit);
|
||||
pairs.push_back({std::move(arr), std::move(crit)});
|
||||
}
|
||||
|
||||
size_t count = 0;
|
||||
for (int r = 0; r < rows; ++r) {
|
||||
for (int c = 0; c < cols; ++c) {
|
||||
bool ok = true;
|
||||
for (const auto& p : pairs) {
|
||||
if (!matches_criteria(p.range.at(r, c), p.crit)) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ok) ++count;
|
||||
}
|
||||
}
|
||||
return EvalResult::from_value(Value::make_number(static_cast<double>(count)));
|
||||
}
|
||||
|
||||
EvalResult fn_max(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.empty()) return err_res(ErrorCode::Value);
|
||||
std::vector<double> nums;
|
||||
if (auto err = collect_numeric_args(args, ctx, nums)) return err_res(*err);
|
||||
if (nums.empty()) return EvalResult::from_value(Value::make_number(0.0));
|
||||
double m = *std::max_element(nums.begin(), nums.end());
|
||||
return EvalResult::from_value(Value::make_number(m));
|
||||
}
|
||||
|
||||
EvalResult fn_min(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
||||
if (args.empty()) return err_res(ErrorCode::Value);
|
||||
std::vector<double> nums;
|
||||
if (auto err = collect_numeric_args(args, ctx, nums)) return err_res(*err);
|
||||
if (nums.empty()) return EvalResult::from_value(Value::make_number(0.0));
|
||||
double m = *std::min_element(nums.begin(), nums.end());
|
||||
return EvalResult::from_value(Value::make_number(m));
|
||||
}
|
||||
|
||||
EvalResult fn_var_impl(const std::vector<std::shared_ptr<Expr>>& args,
|
||||
const EvalContext& ctx,
|
||||
bool sample,
|
||||
bool take_sqrt) {
|
||||
if (args.empty()) return err_res(ErrorCode::Value);
|
||||
std::vector<double> nums;
|
||||
if (auto err = collect_numeric_args(args, ctx, nums)) return err_res(*err);
|
||||
if (nums.size() < (sample ? 2U : 1U)) return err_res(ErrorCode::Div0);
|
||||
double v = compute_variance(nums, sample);
|
||||
if (take_sqrt) v = std::sqrt(v);
|
||||
return EvalResult::from_value(Value::make_checked_number(v));
|
||||
}
|
||||
|
||||
EvalResult fn_stdev(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
||||
return fn_var_impl(a, c, true, true);
|
||||
}
|
||||
EvalResult fn_stdevp(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
||||
return fn_var_impl(a, c, false, true);
|
||||
}
|
||||
EvalResult fn_var(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
||||
return fn_var_impl(a, c, true, false);
|
||||
}
|
||||
EvalResult fn_varp(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& c) {
|
||||
return fn_var_impl(a, c, false, false);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_statistical_functions(FunctionRegistry& reg) {
|
||||
reg.register_fn("AVERAGE", fn_average);
|
||||
reg.register_fn("COUNT", fn_count);
|
||||
reg.register_fn("COUNTA", fn_counta);
|
||||
reg.register_fn("COUNTBLANK", fn_countblank);
|
||||
reg.register_fn("COUNTIF", fn_countif);
|
||||
reg.register_fn("COUNTIFS", fn_countifs);
|
||||
reg.register_fn("MAX", fn_max);
|
||||
reg.register_fn("MIN", fn_min);
|
||||
reg.register_fn("STDEV", fn_stdev);
|
||||
reg.register_fn("STDEVP", fn_stdevp);
|
||||
reg.register_fn("VAR", fn_var);
|
||||
reg.register_fn("VARP", fn_varp);
|
||||
}
|
||||
|
||||
} // namespace htab
|
||||
Reference in new issue
Block a user