1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { ForeachMapAction, IMap } from "./IMap";
- export class NumMap<T> implements IMap<number, T>{
- private items: { [index: number]: T } = {};
- private count: number = 0;
- public Count(): number {
- return this.count;
- }
- public Add(key: number, value: T): T {
- if (!this.items.hasOwnProperty(key.toString()))
- this.count++;
- this.items[key] = value;
- return value;
- }
- public Remove(key: number): T {
- var val = this.items[key];
- delete this.items[key];
- this.count--;
- return val;
- }
- public ContainsKey(key: number): boolean {
- return this.items.hasOwnProperty(key.toString());
- }
- public Value(key: number): T {
- return this.items[key];
- }
- public Replace(key: number, value: T): boolean {
- if (this.ContainsKey(key)) {
- this.items[key] = value;
- return true;
- }
- return false;
- }
- public Keys(): number[] {
- var keys: number[] = [];
- for (var prop in this.items) {
- if (this.items.hasOwnProperty(prop)) {
- keys.push(Number(prop));
- }
- }
- return keys;
- }
- public Values(): T[] {
- var values: T[] = [];
- for (var prop in this.items) {
- if (this.items.hasOwnProperty(prop)) {
- values.push(this.items[prop]);
- }
- }
- return values;
- }
- public Foreach(callback: ForeachMapAction<number, T>, thisArg?: any): void {
- if (callback) {
- for (var prop in this.items) {
- var key = Number(prop);
- callback.call(thisArg, key, this.items[prop]);
- }
- }
- }
- public Clear(): void {
- this.items = {};
- this.count = 0;
- }
- }
|