From b9d3cc85303ac09bd7e5f92bec9c6317a9f07536 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 12 Mar 2022 16:22:55 +0800 Subject: [PATCH] refactor --- src/__comm__/_str_hash.scad | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/__comm__/_str_hash.scad b/src/__comm__/_str_hash.scad index 691c6bcd..5c382453 100644 --- a/src/__comm__/_str_hash.scad +++ b/src/__comm__/_str_hash.scad @@ -1,8 +1,20 @@ use <../util/sum.scad>; function _str_hash(value) = - let(s = str(value)) - sum([ - for(i = len(s) - 1; i > -1; i = i - 1) - ord(s[i]) * 31 ^ i - ]); \ No newline at end of file + let( + chars = str(value), + end = len(chars) - 1 + ) + end == 0 ? ord(chars[0]) : + let(cum_total = [for(i = 0, s = ord(chars[i]); i < end; i = i + 1, s = s + (ord(chars[i]) * 31 ^ i)) s]) + cum_total[len(cum_total) - 1] + (ord(chars[end]) * 31 ^ i); + + // sum([ + // for(i = len(s) - 1; i > -1; i = i - 1) + // ord(s[i]) * 31 ^ i + // ]); + + let(end = len(lt) - 1) + end == 0 ? lt[0] : + let(cum_total = [for(i = 0, s = lt[0]; i < end; i = i + 1, s = s + lt[i]) s]) + cum_total[len(cum_total) - 1] + lt[end]; \ No newline at end of file