在Perl中执行base36算术的最佳方法是什么? [英] What's the best way to do base36 arithmetic in Perl?

查看:71
本文介绍了在Perl中执行base36算术的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中执行base36算术的最佳方法是什么?

更具体地说,我需要执行以下操作:

To be more specific, I need to be able to do the following:

  • 使用基数为36的正N位数字(例如,数字为0-9 A-Z)

  • Operate on positive N-digit numbers in base 36 (e.g. digits are 0-9 A-Z)

N是有限的,例如9

N is finite, say 9

提供基本算术,至少包括以下3个:

Provide basic arithmetic, at the very least the following 3:

  • 加法(A + B)

  • Addition (A+B)

减法(A-B)

整体划分,例如地板(A/B).

Whole division, e.g. floor(A/B).

严格来说,我并不需要base10转换功能-数字100%的时间在base36中.因此,如果该解决方案没有实现从base36到base10的转换,反之亦然,我会很好.

Strictly speaking, I don't really need a base10 conversion ability - the numbers will 100% of time be in base36. So I'm quite OK if the solution does NOT implement conversion from base36 back to base10 and vice versa.

我不太在乎解决方案是蛮力地转换为base 10然后返回"还是转换为二进制,还是更优雅的方法自然地"执行baseN操作(如上所述,到/从base10转换)不是必需的).我唯一的三个注意事项是:

I don't much care whether the solution is brute-force "convert to base 10 and back" or converting to binary, or some more elegant approach "natively" performing baseN operations (as stated above, to/from base10 conversion is not a requirement). My only 3 considerations are:

  1. 符合上述最低要求

  1. It fits the minimum specifications above

这是标准".当前,我们使用的是基于手动完成的base10转换的旧的自产模块,该模块有很多错误.

It's "standard". Currently we're using and old homegrown module based on base10 conversion done by hand that is buggy and sucks.

我宁愿用一些常用的CPAN解决方案代替它,而不是从头开始重写我自己的自行车,但是如果没有更好的标准可能性,我完全有能力制造它.

I'd much rather replace that with some commonly used CPAN solution instead of re-writing my own bicycle from scratch, but I'm perfectly capable of building it if no better standard possibility exists.

它必须耐高温(尽管不能闪电般快速).花费1秒才能得出2个9位base36数字的总和比我自己可以放的任何东西都要糟糕:)

It must be fast-ish (though not lightning fast). Something that takes 1 second to sum up 2 9-digit base36 numbers is worse than anything I can roll on my own :)

P.S.只是为了提供一些背景信息,以防人们决定回答我上面的技术问题之外,还为我解决XY问题:)

P.S. Just to provide some context in case people decide to solve my XY problem for me in addition to answering the technical question above :)

我们有一棵相当大的树(作为边缘存储在DB中),我们需要在该树的子集上叠加顺序.树木的尺寸在深度和宽度上都很大.该树非常活跃地进行了更新(插入,删除和分支移动).

We have a fairly large tree (stored in DB as a bunch of edges), and we need to superimpose order on a subset of that tree. The tree dimentions are big both depth- and breadth- wise. The tree is VERY actively updated (inserts and deletes and branch moves).

目前,这是通过使用第二个表包含3列来完成的:parent_vertex, child_vertex, local_order,其中local_order是由A-Z0-9组成的9个字符的字符串(例如,基数为36的数字).

This is currently done by having a second table with 3 columns: parent_vertex, child_vertex, local_order, where local_order is an 9-character string built of A-Z0-9 (e.g. base 36 number).

其他注意事项:

  • 要求每个孩子的本地顺序是唯一的(显然每个家长是唯一的),

  • It is required that the local order is unique per child (and obviously unique per parent),

对父级进行任何完全重新排序都比较昂贵,因此,实现方法是尝试为具有X个子级的父级分配在0到36 ** 10-之间分配的订单. 1,因此几乎没有树插入会导致完全重新排序.

Any complete re-ordering of a parent is somewhat expensive, and thus the implementation is to try and assign - for a parent with X children - the orders which are somewhat evenly distributed between 0 and 36**10-1, so that almost no tree inserts result in a full re-ordering.

推荐答案

Math :: Base36 ?

这篇关于在Perl中执行base36算术的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆