Skip to content

Commit

Permalink
List migration to typescript - continuuation
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrypula committed Apr 16, 2018
1 parent a63ea87 commit a29ac7b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 38 deletions.
3 changes: 3 additions & 0 deletions src/common/list/di-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

export const LIST_FACTORY = 'LIST_FACTORY';
10 changes: 7 additions & 3 deletions src/common/list/list.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

interface IList {
interface IList<T> {
clone(): IList<T>;
append(value: T): boolean;
setSizeMax(sizeMax: number): void;
getSizeMax(): number;
}

interface IListStatic {
new(): IList;
interface IListStatic<T> {
new(sizeMax: number): IList<T>;
}

export {
Expand Down
47 changes: 47 additions & 0 deletions src/common/list/list.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import List from './list';

describe('List', () => {
const SIZE_MAX: number = 4;
let list: List<number>;

beforeEach(() => {
list = new List<number>(SIZE_MAX);
});

it('should clone properly', () => {
let listClone: List<number>;

list.append(1);
list.append(2);

listClone = list.clone();

expect(listClone.getSizeMax()).toBe(SIZE_MAX);
// TODO more checks
});

it('should create proper instance', () => {
expect(list).toBeInstanceOf(List);
});

it('should properly add items up to size max', () => {
expect(list.append(1)).toBe(true);
expect(list.append(2)).toBe(true);
expect(list.append(3)).toBe(true);
expect(list.append(4)).toBe(true);
expect(list.append(5)).toBe(false);
});

it('should return same size max value as passed to constructor', () => {
expect(list.getSizeMax()).toBe(SIZE_MAX);
});

it('should properly update size max and return new value', () => {
const SIZE_MAX_NEW: number = 20;

list.setSizeMax(SIZE_MAX_NEW);
expect(list.getSizeMax()).toBe(SIZE_MAX_NEW);
});
});
76 changes: 42 additions & 34 deletions src/common/list/list.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,60 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

class List {
constructor(/*sizeMax*/) {
/*
this.$$data = [];
this.$$positionStart = null;
this.$$positionEnd = null;
this.$$size = null;
this.$$sizeMax = null;
import { staticImplements } from 'rr-tsdi';

import { IList, IListStatic } from './list.interface';

@staticImplements<IListStatic<T>>()
class List<T> implements IList<T> {
private data: T[];
private positionStart: number;
private positionEnd: number;
private size: number;
private sizeMax: number;

constructor(sizeMax: number) {
this.data = [];
this.setSizeMax(sizeMax);
*/
}

/*
List.prototype.clone = function () {
var
buffer = new List(this.$$sizeMax),
dataLength = this.$$data.length,
i;
public clone(): List<T> {
const list = new List<T>(this.sizeMax);
const dataLength = this.data.length;
let i;

buffer.$$positionStart = this.$$positionStart;
buffer.$$positionEnd = this.$$positionEnd;
buffer.$$size = this.$$size;
list.positionStart = this.positionStart;
list.positionEnd = this.positionEnd;
list.size = this.size;

for (i = 0; i < dataLength; i++) {
buffer[i] = this.$$data[i];
list.data[i] = this.data[i];
}

return buffer;
return list;
}

List.prototype.setSizeMax = function (sizeMax) {
this.$$positionStart = 0;
this.$$positionEnd = 0;
this.$$size = 0;
this.$$sizeMax = sizeMax;
this.$$data.length = 0; // drop all data
this.$$data.length = sizeMax;
public setSizeMax(sizeMax: number): void {
this.positionStart = 0;
this.positionEnd = 0;
this.size = 0;
this.sizeMax = sizeMax;
this.data.length = 0; // drop all data
this.data.length = sizeMax; // pre-allokate space
}

List.prototype.push = function (value) {
if (this.$$size === this.$$sizeMax) {
public append(value: T): boolean {
if (this.size === this.sizeMax) {
return false;
}

this.$$data[this.$$positionEnd] = value;
this.$$positionEnd = (this.$$positionEnd + 1) % this.$$sizeMax;
this.$$size++;
this.data[this.positionEnd] = value;
this.positionEnd = (this.positionEnd + 1) % this.sizeMax;
this.size++;

return true;
}

/*
List.prototype.pushEvenIfFull = function (value) {
if (this.isFull()) {
this.pop();
Expand Down Expand Up @@ -82,11 +86,13 @@ class List {
List.prototype.getSize = function () {
return this.$$size;
}
*/

List.prototype.getSizeMax = function () {
return this.$$sizeMax;
public getSizeMax(): number {
return this.sizeMax;
}

/*
List.prototype.isFull = function () {
return this.$$size === this.$$sizeMax;
}
Expand All @@ -112,3 +118,5 @@ class List {
}
*/
}

export default List;
1 change: 0 additions & 1 deletion src/dsp/complex/di-token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

export const COMPLEX = 'COMPLEX';
export const COMPLEX_FACTORY = 'COMPLEX_FACTORY';

0 comments on commit a29ac7b

Please sign in to comment.