Files
dgflash f2fe9d47b6 1. 存储模块全面优化,修复跨平台兼容性问题,完美支持所有Unicode字符
2. 存储模块性能提升,添加LRU缓存、批量操作支持,优化内存使用
3. 多语言模块性能与内存管理优化,组件查询性能提升
4. 时间模块类型安全与性能优化,使用泛型替代any,对象池机制减少内存分配
5. 事件系统修复双重注册、重复注册等严重问题,实现EventData对象池减少GC压力
6. RandomManager修复4个逻辑BUG,包括边界问题和越界问题
7. 音频模块内存与性能优化,避免重复加载,优化数据结构,添加完整清理机制
8. CCView与CCViewVM合并,支持按需启用MVVM
9. Collection模块优化,AsyncQueue添加队列容量限制,Collection查询性能提升
10. ECS系统全面优化,对象池复用减少内存分配,循环性能提升
11. 优化MVVM组件性能
2026-01-09 21:54:05 +08:00

76 lines
2.2 KiB
TypeScript

/*
* @Author: dgflash
* @Date: 2022-07-22 15:54:51
* @LastEditors: dgflash
* @LastEditTime: 2022-09-22 14:47:59
*/
/** 支持Map与Array功能的集合对象 */
export class Collection<K, V> extends Map<K, V> {
private _array: V[] = [];
/** 优化:维护 value 到 index 的映射,避免 indexOf 查找 */
private _valueToIndex: Map<V, number> = new Map();
/** 获取数组对象 */
get array() {
return this._array;
}
/**
* 设置值
* @param key 关键字
* @param value 数据值
*/
set(key: K, value: V) {
if (this.has(key)) {
// 更新现有值
const old = super.get(key)!;
const index = this._valueToIndex.get(old);
if (index !== undefined) {
this._array[index] = value;
this._valueToIndex.delete(old);
this._valueToIndex.set(value, index);
}
}
else {
// 添加新值
const index = this._array.length;
this._array.push(value);
this._valueToIndex.set(value, index);
}
return super.set(key, value);
}
/**
* 删除值
* @param key 关键字
*/
delete(key: K): boolean {
const value = super.get(key);
if (value !== undefined) {
const index = this._valueToIndex.get(value);
if (index !== undefined) {
// 使用快速删除:将最后一个元素移到删除位置
const lastIndex = this._array.length - 1;
if (index !== lastIndex) {
const lastValue = this._array[lastIndex];
this._array[index] = lastValue;
this._valueToIndex.set(lastValue, index);
}
this._array.pop();
this._valueToIndex.delete(value);
}
return super.delete(key);
}
return false;
}
clear(): void {
// 优化:使用 length = 0 更高效
this._array.length = 0;
this._valueToIndex.clear();
super.clear();
}
}