Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中使用VLQ(可变长度数量)编码任意大小的整数(How to encode arbitrarily sized integers using VLQ (Variable-Length Quantities) in JavaScript)

So I found a few examples in JavaScript on how to do Variable-Length Quantities (VLQ), like this one to convert an int to an array of bytes:

(所以,我发现了一些在JavaScript中如何做到变长量(VLQ)的例子,像这样一个为int转换为字节数组:)

function varIntBytes(v) {
  const r = [v & 0x7f]
  while ((v >>= 7) > 0) {
    r.unshift(0x80 + (v & 0x7f))
  }
  return r
}

There's also a lot of stuff on source maps using VLQ, but I'm not at all interested in source maps at the moment -- looked through one article but didn't gain the level of detail I'd like in terms of how to implement VLQ.

(使用VLQ的源地图上也有很多东西,但是目前我对源地图一点都不感兴趣-看了一篇文章,但没有获得我想要的详细程度实施VLQ。)

Here's another example, and here's the first google result.

(这是另一个示例, 这是第一个Google搜索结果。)

const LENGTH = 7;
const CONT_BITS = 1 << LENGTH;
const DATA_BITS = CONT_BITS - 1;

const encodeOne = (val) => {
  const buf = [];
  let left = val;

  while (left) {
    const bits = left & DATA_BITS | CONT_BITS; // set continuation everywhere
    left = left >>> LENGTH;
    buf.push(bits);
  }
  buf[0] = buf[0] & DATA_BITS; // cancel the last continuation
  return buf.reverse();
};

const decodeOne = (buf) => {
  let val = 0;

  for (let i = 0; i < buf.length; i++) {
    val = val << LENGTH | buf[i] & DATA_BITS;
  }
  return val >>> 0; // convert to unsigned 32-bit
};

export const encode = (data) => {
  let buf = [];

  for (let i = 0; i < data.length; i++) {
    buf = buf.concat(encodeOne(data[i]));
  }
  return buf;
}

export const decode = (data) => {
  let start = 0;
  const vals = [];

  for (let i = 0; i < data.length; i++) {
    if (~data[i] & CONT_BITS) {
      vals.push(decodeOne(data.slice(start, i + 1)));
      start = i + 1;
    }
  }
  if (start < data.length) {
    throw new Error('Incomplete sequence');
  }
  return vals;
}

Obviously in JavaScript you can't represent integers larger than 32 (or slightly less if I remember correctly) bits.

(显然,在JavaScript中,您不能表示大于32(或者如果我没有记错的话,则略小于)的整数。)

I asked about how to do VLQ using BigInts (before I'd even heard about the VLQ concept, I was just thinking in terms of 8-bits), but it didn't quite get to where I'd like in terms of a practical implementation (and I didn't realize BigInt's were limited to 64-bit integers, I thought they were arbitrarily sized integers ).

(我问过如何使用BigInts做VLQ (在我甚还没有听说过VLQ概念之前,我只是想用8位来思考),但是并没有达到我想要的那样。实际的实现(而且我没有意识到BigInt仅限于64位整数,我认为它们是任意大小的整数 )。)

So my question is if you can (a) explain how to use this last code block above (I don't know what the difference between encodeOne vs. encode[All] ).

(所以我的问题是,您是否可以(a)解释上面的最后一个代码块的使用方法(我不知道encodeOneencode[All]之间有什么区别)。)

Is this allowing for arbitrarily sized (unsigned) integers ?

(这是否允许使用任意大小的(无符号)整数 ?)

If so, please show how to use it.

(如果是这样,请说明如何使用它。)

If not, wondering if you could modify this code to show how you could support encoding arbitrarily-sized integers ( in JavaScript ) using a sequence of 8-bit values.

(如果不是,想知道是否可以修改此代码以显示如何支持使用8位值序列对任意大小的整数( 在JavaScript中 )进行编码。)

The only other thing I can find for "arbitrarily sized numbers in C" (C is a second-best language for me in terms of ease of understanding, though I'm no expert in C) leads to this Coq example , but it's a bit over my head.

(我可以找到的“ C中任意大小的数字”的唯一其他内容(就我的理解而言,C是我的第二好语言,尽管我不是C的专家)导致了这个Coq示例 ,但这是一个有点烦我)

Maybe this is of some use, I'm not too sure yet.

(也许很有用,我还不太确定。)

Or perhaps this ???

(也许 ???)

Nope , it's limited to JS's MAX integer size as well.

( ,它也仅限于JS的MAX整数大小。)

Nope here too.

(都能跟得上这里了。)

  ask by Lance Pollard translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...