Skip to content

Commit

Permalink
Committing pending changes. THIS PROJECT MAY GET HALTED.
Browse files Browse the repository at this point in the history
  • Loading branch information
zlatko-michailov committed Apr 7, 2016
1 parent 0a3001e commit 87718cd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 54 deletions.
16 changes: 8 additions & 8 deletions src/lib/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,30 @@ export class Global {

static cr(columnIndex: number, rowIndex: number, sheetIndex?: number) : Platform_Ref.CellRef {
return new Platform_Ref.CellRef(
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, columnIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, rowIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, sheetIndex));
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, columnIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, rowIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, sheetIndex));
}

static c$r(columnIndex: number, rowIndex: number, sheetIndex?: number) : Platform_Ref.CellRef {
return new Platform_Ref.CellRef(
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndex, columnIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, rowIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, sheetIndex));
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, rowIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, sheetIndex));
}

static cr$(columnIndex: number, rowIndex: number, sheetIndex?: number) : Platform_Ref.CellRef {
return new Platform_Ref.CellRef(
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, columnIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, columnIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndex, rowIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, sheetIndex));
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, sheetIndex));
}

static c$r$(columnIndex: number, rowIndex: number, sheetIndex?: number) : Platform_Ref.CellRef {
return new Platform_Ref.CellRef(
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndex, columnIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndex, rowIndex),
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ById, sheetIndex));
new Platform_Ref.RefUnit(Platform_Ref.RefKind.ByIndexToId, sheetIndex));
}
}

15 changes: 8 additions & 7 deletions src/platform/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export class App {
}

ensureCell(cellRef: Platform_Ref.CellRef) : Cell {
let sheet: Sheet = this.sheets.getByRefUnit(cellRef.sheetRef);
let column: Column = sheet.columns.getByRefUnit(cellRef.columnRef);
let cell: Cell = column.cells.getByRefUnit(cellRef.rowRef);
let sheet: Sheet = this.sheets.ensureByRefUnit(cellRef.sheetRef);
let column: Column = sheet.columns.ensureByRefUnit(cellRef.columnRef);
let cell: Cell = column.cells.ensureByRefUnit(cellRef.rowRef);

return cell;
}
Expand Down Expand Up @@ -209,11 +209,11 @@ export class Cell {

// Add the direct consumer to this cell's consumers list.
let consumerCellRef: Platform_Ref.CellRef = app.sessionState.calcStack.peek();
this.consumerCellRefs.insert(consumerCellRef);
this.consumerCellRefs.add(consumerCellRef);

// Add this cell to the consumer's provider list.
let consumerCell: Cell = app.ensureCell(consumerCellRef);
consumerCell.providerCellRefs.insert(this.ref);
consumerCell.providerCellRefs.add(this.ref);

// Recalc the value.
this.recalcValue();
Expand Down Expand Up @@ -296,17 +296,18 @@ class StorageArray<T> extends Util_Arrays.DualSparseArray<T> {
this.newElement = newElement;
}

getByRefUnit(refUnit: Platform_Ref.RefUnit) : T {
ensureByRefUnit(refUnit: Platform_Ref.RefUnit) : T {
let element: T = undefined;

// TODO: This section should not be needed.
if (refUnit.kind == Platform_Ref.RefKind.ById) {
element = this.getById(refUnit.value);
if (element === undefined) {
element = this.newElement();
this.setById(refUnit.value, element);
}
}
else if (refUnit.kind == Platform_Ref.RefKind.ByIndex) {
else {
element = this.getByIndex(refUnit.value);
if (element === undefined) {
element = this.newElement();
Expand Down
3 changes: 2 additions & 1 deletion src/platform/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class RefUnit {
}

export const enum RefKind {
ById = 0,
ByIndexToId = 0,
ByIndex = 1,
ById = 2,
}
10 changes: 4 additions & 6 deletions src/test/util/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export class Tests {
let passed: boolean = true;

// [ 11, 12 ]
array.insert(0, 2);
array.setByIndex(0, 11);
array.setByIndex(1, 12);

Expand All @@ -85,7 +84,7 @@ export class Tests {
passed = Test_Main.Framework.areEqual(12, array.getById(1), Test_Main.LogLevel.Info, "by id(1)") && passed;

// [11, 21, 22, 23, 24, 12 ]
array.insert(1, 4);
array.shiftUp(1, 4);
array.setByIndex(1, 21);
array.setByIndex(2, 22);
array.setByIndex(3, 23);
Expand All @@ -106,7 +105,7 @@ export class Tests {
passed = Test_Main.Framework.areEqual(24, array.getById(5), Test_Main.LogLevel.Info, "by id(5)") && passed;

// [11, 21, 24, 12 ]
array.delete(2, 2);
array.shiftDown(4, 2);

passed = Test_Main.Framework.areEqual(11, array.getByIndex(0), Test_Main.LogLevel.Info, "by index(0)") && passed;
passed = Test_Main.Framework.areEqual(21, array.getByIndex(1), Test_Main.LogLevel.Info, "by index(1)") && passed;
Expand All @@ -121,9 +120,8 @@ export class Tests {
passed = Test_Main.Framework.areEqual(24, array.getById(5), Test_Main.LogLevel.Info, "by id(5)") && passed;

// [11, 21, 24, 12, 31, 32 ]
array.insert(4, 2);
array.setById(6, 31);
array.setById(7, 32);
array.setByIndex(4, 31);
array.setByIndex(5, 32);

passed = Test_Main.Framework.areEqual(11, array.getByIndex(0), Test_Main.LogLevel.Info, "by index(0)") && passed;
passed = Test_Main.Framework.areEqual(21, array.getByIndex(1), Test_Main.LogLevel.Info, "by index(1)") && passed;
Expand Down
3 changes: 1 addition & 2 deletions src/test/util/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class Tests {
sparseArray[2016] = 77;

let dualSparseArray: Util_Arrays.DualSparseArray<number> = new Util_Arrays.DualSparseArray<number>();
dualSparseArray.insert(0, 3);
dualSparseArray.setByIndex(0, 66);
dualSparseArray.setByIndex(1, 55);
dualSparseArray.setByIndex(2, 44);
Expand All @@ -56,7 +55,7 @@ export class Tests {
};

let actualJSON: string = Util_JSON.Serializer.toJSON(expectedValue);
let expectedJSON: string = '{"a":42,"b":"foo","sub":{"x":[2016,3,2],"y":{"2":99,"42":88,"2016":77},"z":{"_count":3,"_nextId":3,"_byId":{"0":{"index":0,"value":66},"1":{"index":1,"value":55},"2":{"index":2,"value":44}},"_byIndex":{"0":0,"1":1,"2":2}}}}';
let expectedJSON: string = '{"a":42,"b":"foo","sub":{"x":[2016,3,2],"y":{"2":99,"42":88,"2016":77},"z":{"_nextId":3,"_byId":{"0":{"index":0,"value":66},"1":{"index":1,"value":55},"2":{"index":2,"value":44}},"_byIndex":{"0":0,"1":1,"2":2}}}}';
passed = Test_Main.Framework.areEqual(expectedJSON, actualJSON, Test_Main.LogLevel.Info, "toJSON") && passed;

let actualValue: any = Util_JSON.Serializer.fromJSON(actualJSON);
Expand Down
63 changes: 33 additions & 30 deletions src/util/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ export class SparseArray<T> {
return undefined;
}

insert(value: T) : number {
add(value: T) : number {
for (let i: number = 0; i < Number.MAX_VALUE; i++) {
if (this[i] === undefined) {
this[i] = value;
return i;
}
}

throw new Util_Errors.Exception(Util_Errors.ErrorCode.IndexOutOfRange, "Cannot insert a new value because this SparseArray is completely full.");
throw new Util_Errors.Exception(Util_Errors.ErrorCode.IndexOutOfRange, "Cannot add a new value because this SparseArray is completely full.");
}

forEach(callback: (i: number) => void) : void {
Expand All @@ -73,63 +73,56 @@ export class SparseArray<T> {


export class DualSparseArray<T> {
_count: number = 0;
_nextId: number = 0;
_byId: SparseArray<IndexValue<T>> = new SparseArray<IndexValue<T>>();
_byIndex: SparseArray<number> = new SparseArray<number>();

insert(index: number, count?: number) : void {
if (index < 0 || index > this._count) {
shiftUp(index: number, count?: number) : void {
if (index < 0) {
throw new Util_Errors.Exception(Util_Errors.ErrorCode.IndexOutOfRange);
}

if (count == undefined) {
count = 1;
}

// Bump the indeces to open a gap.
// Shift up the indeces higher than index.
this._byId.forEach(i => {
if (index <= this._byId[i].index) {
if (this._byId[i].index >= index) {
// Delete the stale index and stale id.
let staleIndex = this._byId[i].index;
let staleId: number = this._byIndex[staleIndex + count];
delete this._byIndex[staleIndex];
delete this._byId[staleId];

this._byId[i].index += count;
this._byIndex[this._byId[i].index] = i;
}
});

// Init the elements in the gap.
for (let i: number = index; i < index + count; i++) {
this._byId[this._nextId] = { index: i, value: undefined };
this._byIndex[i] = this._nextId;
this._nextId++;
}

this._count += count;
}

delete(index: number, count?: number) : void {
if (index < 0 || index > this._count) {
shiftDown(index: number, count?: number) : void {
if (index < 0) {
throw new Util_Errors.Exception(Util_Errors.ErrorCode.IndexOutOfRange);
}

if (count == undefined) {
count = 1;
}

// Delete elements. Open a gap among the indeces.
for (let i: number = index; i < index + count; i++) {
let id: number = this._byIndex[i];
delete this._byId[id];
delete this._byIndex[i];
}

// Shift back the indeces to close the gap.
// Shift down the indeces higher than index.
this._byId.forEach(i => {
if (index <= this._byId[i].index) {
if (this._byId[i].index >= index) {
// Delete the stale index and stale id.
let staleIndex = this._byId[i].index;
let staleId: number = this._byIndex[staleIndex - count];
delete this._byIndex[staleIndex];
delete this._byId[staleId];

this._byId[i].index -= count;
this._byIndex[this._byId[i].index] = i;
}
});

this._count -= count;
}

getById(id: number) : T {
Expand All @@ -141,6 +134,7 @@ export class DualSparseArray<T> {
// TODO: Remove this method.

if (this._byId[id] === undefined) {
//throw new Util_Errors.Exception(Util_Errors.ErrorCode.IndexOutOfRange);
this._byId[id] = new IndexValue<T>();
}

Expand All @@ -149,11 +143,20 @@ export class DualSparseArray<T> {

getByIndex(index: number) : T {
let id: number = this._byIndex[index];
return this._byId[id].value;
if (id === undefined) {
return undefined;
}

return this.getById(id);
}

setByIndex(index: number, value: T) : void {
let id: number = this._byIndex[index];
if (id === undefined) {
id = this._nextId++;
this._byIndex[index] = id;
}

this._byId[id] = { index: index, value: value };
}

Expand Down

0 comments on commit 87718cd

Please sign in to comment.