Removing [U]Int[8|16|32|64] and Float[32|64] in favor of Number (#1336)

finishing off the removal of various number types
This commit is contained in:
Mike Gray
2016-04-27 21:22:10 -04:00
parent bbdcb0ac7c
commit 47a2909c82
8 changed files with 42 additions and 80 deletions

View File

@@ -14,10 +14,9 @@ import {
import BuzHashBoundaryChecker from './buzhash-boundary-checker.js';
import {SequenceChunker} from './sequence-chunker.js';
import type {BoundaryChecker, makeChunkFn} from './sequence-chunker.js';
import type {uint8} from './primitives.js';
export class NomsBlob extends Collection<IndexedSequence<uint8>> {
constructor(sequence: IndexedSequence<uint8>) {
export class NomsBlob extends Collection<IndexedSequence<number>> {
constructor(sequence: IndexedSequence<number>) {
super(blobType, sequence);
}
@@ -59,7 +58,7 @@ export class BlobReader {
}
}
export class BlobLeafSequence extends IndexedSequence<uint8> {
export class BlobLeafSequence extends IndexedSequence<number> {
constructor(cs: ?DataStore, items: Uint8Array) {
// $FlowIssue: The super class expects Array<T> but we sidestep that.
super(cs, blobType, items);
@@ -74,15 +73,15 @@ const blobWindowSize = 64;
const blobPattern = ((1 << 13) | 0) - 1;
function newBlobLeafChunkFn(cs: ?DataStore = null): makeChunkFn {
return (items: Array<uint8>) => {
return (items: Array<number>) => {
const blobLeaf = new BlobLeafSequence(cs, new Uint8Array(items));
const mt = new MetaTuple(blobLeaf, items.length, items.length);
return [mt, blobLeaf];
};
}
function newBlobLeafBoundaryChecker(): BoundaryChecker<uint8> {
return new BuzHashBoundaryChecker(blobWindowSize, 1, blobPattern, (v: uint8) => v);
function newBlobLeafBoundaryChecker(): BoundaryChecker<number> {
return new BuzHashBoundaryChecker(blobWindowSize, 1, blobPattern, (v: number) => v);
}
export function newBlob(bytes: Uint8Array): Promise<NomsBlob> {

View File

@@ -2,9 +2,8 @@
import BuzHash from './buzhash.js';
import {invariant} from './assert.js';
import type {uint8} from './primitives.js';
type getDataFn<T> = (item: T) => (Uint8Array | uint8);
type getDataFn<T> = (item: T) => (Uint8Array | number);
export default class BuzHashBoundaryChecker<T> {
windowSize: number;
@@ -32,7 +31,7 @@ export default class BuzHashBoundaryChecker<T> {
return this._checkPattern();
}
_writeByte(b: uint8): boolean {
_writeByte(b: number): boolean {
this._hash.hashByte(b);
return this._checkPattern();
}

View File

@@ -1,9 +1,8 @@
// @flow
import type {uint8, uint32} from './primitives.js';
// This a javascript port of github.com/kch42/buzhash.
const bytehash:Array<uint32> = [
const bytehash:Array<number> = [
0x12bd9527, 0xf4140cea, 0x987bd6e1, 0x79079850, 0xafbfd539, 0xd350ce0a,
0x82973931, 0x9fc32b9c, 0x28003b88, 0xc30c13aa, 0x6b678c34, 0x5844ef1d,
0xaa552c18, 0x4a77d3e8, 0xd1f62ea0, 0x6599417c, 0xfbe30e7a, 0xf9e2d5ee,
@@ -49,15 +48,15 @@ const bytehash:Array<uint32> = [
0x96ca63ef, 0xa0499838, 0xd9030f59, 0x8185f4d2];
export default class BuzHash {
_state: uint32;
_state: number;
_buf: Uint8Array;
_n: uint32;
_bshiftn: uint32;
_bshiftm: uint32;
_bufpos: uint32;
_n: number;
_bshiftn: number;
_bshiftm: number;
_bufpos: number;
_overflow: boolean;
constructor(n: uint32) {
constructor(n: number) {
this._n = n;
this._bshiftn = n % 32;
this._bshiftm = 32 - this._bshiftn;
@@ -65,7 +64,7 @@ export default class BuzHash {
this.reset();
}
hashByte(b: uint8): uint32 {
hashByte(b: number): number {
if (this._bufpos === this._n) {
this._overflow = true;
this._bufpos = 0;
@@ -92,7 +91,7 @@ export default class BuzHash {
return _state;
}
write(p: Uint8Array): uint32 {
write(p: Uint8Array): number {
for (let i = 0; i < p.length; i++) {
this.hashByte(p[i]);
}
@@ -106,11 +105,11 @@ export default class BuzHash {
this._overflow = false;
}
get blocksize(): uint32 {
get blocksize(): number {
return this._n;
}
get sum32(): uint32 {
get sum32(): number {
return this._state;
}
}

View File

@@ -6,7 +6,6 @@ import MemoryStore from './memory-store.js';
import Ref from './ref.js';
import RefValue from './ref-value.js';
import {default as Struct, StructMirror} from './struct.js';
import type {float64, int32, int64, uint8, uint16, uint32, uint64} from './primitives.js';
import type {TypeDesc} from './type.js';
import type {Value} from './value.js';
import {assert} from 'chai';
@@ -100,12 +99,12 @@ suite('Decode', () => {
await doTest('hi', [Kind.String, 'hi']);
});
test('read list of int 32', async () => {
test('read list of number', async () => {
const ms = new MemoryStore();
const ds = new DataStore(ms);
const a = [Kind.List, Kind.Number, false, ['0', '1', '2', '3']];
const r = new JsonArrayReader(a, ds);
const v:NomsList<int32> = await r.readTopLevelValue();
const v:NomsList<number> = await r.readTopLevelValue();
invariant(v instanceof NomsList);
const tr = makeCompoundType(Kind.List, numberType);
@@ -130,7 +129,7 @@ suite('Decode', () => {
assert.strictEqual(true, await v.get(2));
});
test('read value list of int8', async () => {
test('read value list of number', async () => {
const ms = new MemoryStore();
const ds = new DataStore(ms);
const a = [Kind.Value, Kind.List, Kind.Number, false, ['0', '1', '2']];
@@ -155,7 +154,7 @@ suite('Decode', () => {
new MetaTuple(r2, 2, 2),
new MetaTuple(r3, 3, 3),
];
const l:NomsList<int32> = new NomsList(ltr, new IndexedMetaSequence(ds, ltr, tuples));
const l:NomsList<number> = new NomsList(ltr, new IndexedMetaSequence(ds, ltr, tuples));
const a = [Kind.List, Kind.Number, true,
[r1.toString(), '1', '1', r2.toString(), '2', '2', r3.toString(), '3', '3']];
@@ -165,12 +164,12 @@ suite('Decode', () => {
assert.isTrue(v.ref.equals(l.ref));
});
test('read map of int64 to float64', async () => {
test('read map of number to number', async () => {
const ms = new MemoryStore();
const ds = new DataStore(ms);
const a = [Kind.Map, Kind.Number, Kind.Number, false, ['0', '1', '2', '3']];
const r = new JsonArrayReader(a, ds);
const v:NomsMap<int64, float64> = await r.readTopLevelValue();
const v:NomsMap<number, number> = await r.readTopLevelValue();
invariant(v instanceof NomsMap);
const t = makeCompoundType(Kind.Map, numberType,
@@ -179,14 +178,14 @@ suite('Decode', () => {
assert.isTrue(v.equals(m));
});
test('read map of ref to uint64', async () => {
test('read map of ref to number', async () => {
const ms = new MemoryStore();
const ds = new DataStore(ms);
const a = [Kind.Map, Kind.Ref, Kind.Value, Kind.Number, false,
['sha1-0000000000000000000000000000000000000001', '2',
'sha1-0000000000000000000000000000000000000002', '4']];
const r = new JsonArrayReader(a, ds);
const v:NomsMap<RefValue<Value>, uint64> = await r.readTopLevelValue();
const v:NomsMap<RefValue<Value>, number> = await r.readTopLevelValue();
invariant(v instanceof NomsMap);
const refOfValueType = makeCompoundType(Kind.Ref, valueType);
@@ -200,12 +199,12 @@ suite('Decode', () => {
assert.isTrue(v.equals(m));
});
test('read value map of uint64 to uint32', async () => {
test('read value map of number to number', async () => {
const ms = new MemoryStore();
const ds = new DataStore(ms);
const a = [Kind.Value, Kind.Map, Kind.Number, Kind.Number, false, ['0', '1', '2', '3']];
const r = new JsonArrayReader(a, ds);
const v:NomsMap<uint64, uint32> = await r.readTopLevelValue();
const v:NomsMap<number, number> = await r.readTopLevelValue();
invariant(v instanceof NomsMap);
const t = makeCompoundType(Kind.Map, numberType,
@@ -214,12 +213,12 @@ suite('Decode', () => {
assert.isTrue(v.equals(m));
});
test('read set of uint8', async () => {
test('read set of number', async () => {
const ms = new MemoryStore();
const ds = new DataStore(ms);
const a = [Kind.Set, Kind.Number, false, ['0', '1', '2', '3']];
const r = new JsonArrayReader(a, ds);
const v:NomsSet<uint8> = await r.readTopLevelValue();
const v:NomsSet<number> = await r.readTopLevelValue();
invariant(v instanceof NomsSet);
const t = makeCompoundType(Kind.Set, numberType);
@@ -239,7 +238,7 @@ suite('Decode', () => {
new MetaTuple(r2, 2, 2),
new MetaTuple(r3, 5, 3),
];
const l:NomsSet<int32> = new NomsSet(ltr, new OrderedMetaSequence(ds, ltr, tuples));
const l:NomsSet<number> = new NomsSet(ltr, new OrderedMetaSequence(ds, ltr, tuples));
const a = [Kind.Set, Kind.Number, true,
[r1.toString(), '0', '1', r2.toString(), '2', '2', r3.toString(), '5', '3']];
@@ -249,12 +248,12 @@ suite('Decode', () => {
assert.isTrue(v.ref.equals(l.ref));
});
test('read value set of uint16', async () => {
test('read value set of number', async () => {
const ms = new MemoryStore();
const ds = new DataStore(ms);
const a = [Kind.Value, Kind.Set, Kind.Number, false, ['0', '1', '2', '3']];
const r = new JsonArrayReader(a, ds);
const v:NomsSet<uint16> = await r.readTopLevelValue();
const v:NomsSet<number> = await r.readTopLevelValue();
invariant(v instanceof NomsSet);
const t = makeCompoundType(Kind.Set, numberType);
@@ -440,10 +439,10 @@ suite('Decode', () => {
const ds = new DataStore(ms);
const chunk = Chunk.fromString(
`t [${Kind.Value}, ${Kind.Set}, ${Kind.Number}, false, ["0", "1", "2", "3"]]`);
const v:NomsSet<uint16> = await decodeNomsValue(chunk, new DataStore(new MemoryStore()));
const v:NomsSet<number> = await decodeNomsValue(chunk, new DataStore(new MemoryStore()));
const t = makeCompoundType(Kind.Set, numberType);
const s:NomsSet<uint16> = new NomsSet(t, new SetLeafSequence(ds, t, [0, 1, 2, 3]));
const s:NomsSet<number> = new NomsSet(t, new SetLeafSequence(ds, t, [0, 1, 2, 3]));
assert.isTrue(v.equals(s));
});

View File

@@ -58,17 +58,5 @@ export type {MapEntry} from './map.js';
export type {Splice} from './edit-distance.js';
export type {valueOrPrimitive, Value} from './value.js';
export type {NomsKind} from './noms-kind.js';
export type {
float32,
float64,
int16,
int32,
int64,
int8,
primitive,
uint16,
uint32,
uint64,
uint8,
} from './primitives.js';
export type {primitive} from './primitives.js';
export type {Commit} from './commit.js';

View File

@@ -1,27 +1,7 @@
// @flow
export type uint8 = number;
export type uint16 = number;
export type uint32 = number;
export type uint64 = number;
export type int8 = number;
export type int16 = number;
export type int32 = number;
export type int64 = number;
export type float32 = number;
export type float64 = number;
export type primitive =
uint8 |
uint16 |
uint32 |
uint64 |
int8 |
int16 |
int32 |
int64 |
float32 |
float64 |
number |
string |
boolean;

View File

@@ -2,7 +2,6 @@
import {suite, test} from 'mocha';
import {assert} from 'chai';
import type {int64} from './primitives.js';
import {SequenceChunker} from './sequence-chunker.js';
class ModBoundaryChecker {
@@ -16,12 +15,12 @@ class ModBoundaryChecker {
return 1;
}
write(item: int64): boolean {
write(item: number): boolean {
return item % this.mod === 0;
}
}
function sumChunker(items: Array<int64>): [int64, any] {
function sumChunker(items: Array<number>): [number, any] {
let sum = 0;
for (let i = 0; i < items.length; i++) {
sum += items[i];
@@ -32,7 +31,7 @@ function sumChunker(items: Array<int64>): [int64, any] {
suite('SequenceChunker', () => {
async function testChunking(expect: Array<int64>, from: number, to: number) {
async function testChunking(expect: Array<number>, from: number, to: number) {
const seq = new SequenceChunker(null, sumChunker, sumChunker,
new ModBoundaryChecker(3), () => new ModBoundaryChecker(5));

View File

@@ -3,7 +3,6 @@
import {suite, test} from 'mocha';
import {assert} from 'chai';
import {Sequence, SequenceCursor} from './sequence.js';
import type {int64} from './primitives.js';
import {notNull} from './assert.js';
import {makeCompoundType, valueType} from './type.js';
import {Kind} from './noms-kind.js';
@@ -35,7 +34,7 @@ suite('SequenceCursor', () => {
}
function expect(c: TestSequenceCursor, expectIdx: number,
expectParentIdx: number, expectValid: boolean, expectVal: ?int64) {
expectParentIdx: number, expectValid: boolean, expectVal: ?number) {
assert.strictEqual(expectIdx, c.indexInChunk, 'indexInChunk');
const parent = notNull(c.parent);
assert.strictEqual(expectParentIdx, parent.indexInChunk, 'parentIdx');