123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /**
- * 字典型的数据存取类。
- */
- export class Dictionary<T> {
- private m_keys: any[] = [];
- private m_values: T[] = [];
- public get length(): number {
- return this.m_keys.length;
- }
- /**
- * 获取所有的子元素列表。
- */
- public get values(): T[] {
- return this.m_values.concat();
- }
- /**
- * 获取所有的子元素键名列表。
- */
- public get keys(): any[] {
- return this.m_keys.concat();
- }
- /**
- * 获取指定对象的键名索引。
- * @param key 键名对象。
- * @return 键名索引。
- */
- public indexOfKey(key: any): number {
- return this.m_keys.indexOf(key);
- }
- public indexOfValue(val: T) {
- return this.m_values.indexOf(val);
- }
- /**
- * 通过value得到对应的key
- */
- public getKeyByValue(value: T): any {
- return this.m_keys[this.indexOfValue(value)];
- }
- /**
- * 添加指定键名的值。
- * @param key 键名。
- * @param value 值。
- */
- public add(key: any, value: T): void {
- var index: number = this.indexOfKey(key);
- if (index >= 0) {
- this.m_values[index] = value;
- } else {
- this.m_keys.push(key);
- this.m_values.push(value);
- }
- }
- /**
- * 返回指定键名的值。
- * @param key 键名对象。
- * @return 指定键名的值。
- */
- public get(key: any): T {
- var index: number = this.indexOfKey(key);
- if (index >= 0) {
- return this.m_values[index];
- }
- return null;
- }
- /**
- * 设置指定key的值。
- * @param key 键名对象。
- * @return 指定键名的值。
- */
- public set(key: any, value: T) {
- var index: number = this.indexOfKey(key);
- if (index >= 0) {
- this.m_values[index] = value;
- }else{
- this.add(key, value);
- }
- }
- /**
- * 移除指定键名的值。
- * @param key 键名对象。
- * @return 是否成功移除。
- */
- public remove(key: any): T {
- var index: number = this.indexOfKey(key);
- if (index >= 0) {
- this.m_keys.splice(index, 1);
- return this.m_values.splice(index, 1)[0];
- }
- return null;
- }
- public containsKey(key: any): boolean {
- var index: number = this.indexOfKey(key);
- if (index >= 0) {
- return true;
- }
- return false;
- }
- /**
- * 清除此对象的键名列表和键值列表。
- */
- public clear() {
- this.m_keys.length = 0;
- this.m_values.length = 0;
- }
- public setLength(num: number) {
- this.m_keys.length = num;
- this.m_values.length = num;
- }
- /**
- * 随机获取一条数据
- */
- public getRandomData(): T {
- var index: number = Math.random() * this.keys.length << 0;
- return this.m_values[index];
- }
- }
|