移除fastlist(经测试比Array并没有快)
This commit is contained in:
@@ -23,15 +23,13 @@ module es {
|
||||
|
||||
export class TriggerListenerHelper {
|
||||
public static getITriggerListener(entity: Entity, components: ITriggerListener[]){
|
||||
for (let i = 0; i < entity.components._components.length; i++) {
|
||||
let component = entity.components._components.buffer[i];
|
||||
for (let component of entity.components._components) {
|
||||
if (isITriggerListener(component)) {
|
||||
components.push(component);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < entity.components._componentsToAdd.length; i++) {
|
||||
let component = entity.components._componentsToAdd[i];
|
||||
for (let component of entity.components._componentsToAdd) {
|
||||
if (isITriggerListener(component)) {
|
||||
components.push(component);
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ module es {
|
||||
public setUpdateOrder(updateOrder: number){
|
||||
if (this.updateOrder != updateOrder){
|
||||
this.updateOrder = updateOrder;
|
||||
Core.scene._sceneComponents.sort(this);
|
||||
Core.scene._sceneComponents.sort(this.compare);
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
@@ -32,7 +32,7 @@ module es {
|
||||
/**
|
||||
* 全局访问系统
|
||||
*/
|
||||
public _globalManagers: FastList<GlobalManager> = new FastList<GlobalManager>();
|
||||
public _globalManagers: GlobalManager[] = [];
|
||||
public _timerManager: TimerManager = new TimerManager();
|
||||
public width: number;
|
||||
public height: number;
|
||||
@@ -98,7 +98,7 @@ module es {
|
||||
* @param manager
|
||||
*/
|
||||
public static registerGlobalManager(manager: es.GlobalManager) {
|
||||
this._instance._globalManagers.add(manager);
|
||||
this._instance._globalManagers.push(manager);
|
||||
manager.enabled = true;
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ module es {
|
||||
* @param manager
|
||||
*/
|
||||
public static unregisterGlobalManager(manager: es.GlobalManager) {
|
||||
this._instance._globalManagers.remove(manager);
|
||||
new linq.List(this._instance._globalManagers).remove(manager);
|
||||
manager.enabled = false;
|
||||
}
|
||||
|
||||
@@ -117,8 +117,8 @@ module es {
|
||||
*/
|
||||
public static getGlobalManager<T extends es.GlobalManager>(type): T {
|
||||
for (let i = 0; i < this._instance._globalManagers.length; i++) {
|
||||
if (this._instance._globalManagers.buffer[i] instanceof type)
|
||||
return this._instance._globalManagers.buffer[i] as T;
|
||||
if (this._instance._globalManagers[i] instanceof type)
|
||||
return this._instance._globalManagers[i] as T;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -183,8 +183,8 @@ module es {
|
||||
if (currentTime != null) Time.update(currentTime);
|
||||
if (this._scene != null) {
|
||||
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
|
||||
if (this._globalManagers.buffer[i].enabled)
|
||||
this._globalManagers.buffer[i].update();
|
||||
if (this._globalManagers[i].enabled)
|
||||
this._globalManagers[i].update();
|
||||
}
|
||||
|
||||
this._scene.update();
|
||||
|
||||
@@ -35,6 +35,9 @@ module es {
|
||||
* 指定应该调用这个entity update方法的频率。1表示每一帧,2表示每一帧,以此类推
|
||||
*/
|
||||
public updateInterval: number = 1;
|
||||
/**
|
||||
* 返回一个BitSet实例,包含实体拥有的组件的位
|
||||
*/
|
||||
public componentBits: BitSet;
|
||||
|
||||
constructor(name: string) {
|
||||
|
||||
@@ -10,7 +10,7 @@ module es {
|
||||
*/
|
||||
public readonly entityProcessors: EntityProcessorList;
|
||||
|
||||
public readonly _sceneComponents: FastList<SceneComponent> = new FastList<SceneComponent>();
|
||||
public readonly _sceneComponents: SceneComponent[] = [];
|
||||
public _didSceneBegin;
|
||||
|
||||
constructor() {
|
||||
@@ -66,9 +66,9 @@ module es {
|
||||
this.entities.removeAllEntities();
|
||||
|
||||
for (let i = 0; i < this._sceneComponents.length; i++) {
|
||||
this._sceneComponents.buffer[i].onRemovedFromScene();
|
||||
this._sceneComponents[i].onRemovedFromScene();
|
||||
}
|
||||
this._sceneComponents.clear();
|
||||
this._sceneComponents.length = 0;
|
||||
|
||||
Physics.clear();
|
||||
|
||||
@@ -87,8 +87,8 @@ module es {
|
||||
this.entities.updateLists();
|
||||
|
||||
for (let i = this._sceneComponents.length - 1; i >= 0; i--) {
|
||||
if (this._sceneComponents.buffer[i].enabled)
|
||||
this._sceneComponents.buffer[i].update();
|
||||
if (this._sceneComponents[i].enabled)
|
||||
this._sceneComponents[i].update();
|
||||
}
|
||||
|
||||
// 更新我们的实体解析器
|
||||
@@ -109,8 +109,8 @@ module es {
|
||||
public addSceneComponent<T extends SceneComponent>(component: T): T {
|
||||
component.scene = this;
|
||||
component.onEnabled();
|
||||
this._sceneComponents.add(component);
|
||||
this._sceneComponents.sort(component);
|
||||
this._sceneComponents.push(component);
|
||||
this._sceneComponents.sort(component.compare);
|
||||
return component;
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ module es {
|
||||
*/
|
||||
public getSceneComponent<T extends SceneComponent>(type) {
|
||||
for (let i = 0; i < this._sceneComponents.length; i++) {
|
||||
let component = this._sceneComponents.buffer[i];
|
||||
let component = this._sceneComponents[i];
|
||||
if (component instanceof type)
|
||||
return component as T;
|
||||
}
|
||||
@@ -145,12 +145,12 @@ module es {
|
||||
* @param component
|
||||
*/
|
||||
public removeSceneComponent(component: SceneComponent) {
|
||||
if (!this._sceneComponents.contains(component)) {
|
||||
if (!new linq.List(this._sceneComponents).contains(component)) {
|
||||
console.warn(`SceneComponent${component}不在SceneComponents列表中!`);
|
||||
return;
|
||||
}
|
||||
|
||||
this._sceneComponents.remove(component);
|
||||
new linq.List(this._sceneComponents).remove(component);
|
||||
component.onRemovedFromScene();
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ module es {
|
||||
* @param entity
|
||||
*/
|
||||
public addEntity(entity: Entity) {
|
||||
if (this.entities.buffer.contains(entity))
|
||||
if (new linq.List(this.entities.buffer).contains(entity))
|
||||
console.warn(`您试图将同一实体添加到场景两次: ${entity}`);
|
||||
this.entities.add(entity);
|
||||
entity.scene = this;
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
module es {
|
||||
/**
|
||||
* 基本实体处理系统。将其用作处理具有特定组件的许多实体的基础
|
||||
*
|
||||
* 按实体引用遍历实体订阅成员实体的系统
|
||||
* 当你需要处理与Matcher相匹配的实体,并且你更喜欢使用Entity的时候,可以使用这个功能。
|
||||
*/
|
||||
export abstract class EntityProcessingSystem extends EntitySystem {
|
||||
constructor(matcher: Matcher) {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
module es {
|
||||
export class EntitySystem {
|
||||
/**
|
||||
* 追踪实体的子集,但不实现任何排序或迭代。
|
||||
*/
|
||||
export abstract class EntitySystem {
|
||||
private _entities: Entity[] = [];
|
||||
|
||||
constructor(matcher?: Matcher) {
|
||||
|
||||
@@ -10,11 +10,11 @@ module es {
|
||||
/**
|
||||
* 添加到实体的组件列表
|
||||
*/
|
||||
public _components: FastList<Component> = new FastList<Component>();
|
||||
public _components: Component[] = [];
|
||||
/**
|
||||
* 所有需要更新的组件列表
|
||||
*/
|
||||
public _updatableComponents: FastList<IUpdatable> = new FastList<IUpdatable>();
|
||||
public _updatableComponents: IUpdatable[] = [];
|
||||
/**
|
||||
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
|
||||
*/
|
||||
@@ -38,7 +38,7 @@ module es {
|
||||
}
|
||||
|
||||
public get buffer() {
|
||||
return this._components.buffer;
|
||||
return this._components;
|
||||
}
|
||||
|
||||
public markEntityListUnsorted() {
|
||||
@@ -72,21 +72,19 @@ module es {
|
||||
this.handleRemove(this._components[i]);
|
||||
}
|
||||
|
||||
this._components.clear();
|
||||
this._updatableComponents.clear();
|
||||
this._components.length = 0;
|
||||
this._updatableComponents.length = 0;
|
||||
this._componentsToAdd.length = 0;
|
||||
this._componentsToRemove.length = 0;
|
||||
}
|
||||
|
||||
public deregisterAllComponents() {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components.buffer[i];
|
||||
|
||||
for (let component of this._components) {
|
||||
if (!component) continue;
|
||||
|
||||
// 处理IUpdatable
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.remove(component);
|
||||
new linq.List(this._updatableComponents).remove(component);
|
||||
|
||||
if (Core.entitySystemsEnabled) {
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
|
||||
@@ -96,11 +94,9 @@ module es {
|
||||
}
|
||||
|
||||
public registerAllComponents() {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components.buffer[i];
|
||||
|
||||
for (let component of this._components) {
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.add(component);
|
||||
this._updatableComponents.push(component);
|
||||
|
||||
if (Core.entitySystemsEnabled) {
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
|
||||
@@ -116,7 +112,7 @@ module es {
|
||||
if (this._componentsToRemove.length > 0) {
|
||||
for (let i = 0; i < this._componentsToRemove.length; i++) {
|
||||
this.handleRemove(this._componentsToRemove[i]);
|
||||
this._components.remove(this._componentsToRemove[i]);
|
||||
new linq.List(this._components).remove(this._componentsToRemove[i]);
|
||||
}
|
||||
|
||||
this._componentsToRemove.length = 0;
|
||||
@@ -127,14 +123,14 @@ module es {
|
||||
let component = this._componentsToAdd[i];
|
||||
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.add(component);
|
||||
this._updatableComponents.push(component);
|
||||
|
||||
if (Core.entitySystemsEnabled) {
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
|
||||
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
||||
}
|
||||
|
||||
this._components.add(component);
|
||||
this._components.push(component);
|
||||
this._tempBufferList.push(component);
|
||||
}
|
||||
|
||||
@@ -157,7 +153,7 @@ module es {
|
||||
}
|
||||
|
||||
if (this._isComponentListUnsorted) {
|
||||
this._updatableComponents.sort(ComponentList.compareUpdatableOrder);
|
||||
this._updatableComponents.sort(ComponentList.compareUpdatableOrder.compare);
|
||||
this._isComponentListUnsorted = false;
|
||||
}
|
||||
}
|
||||
@@ -166,7 +162,7 @@ module es {
|
||||
if (!component) return;
|
||||
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.remove(component);
|
||||
new linq.List(this._updatableComponents).remove(component);
|
||||
|
||||
if (Core.entitySystemsEnabled) {
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
|
||||
@@ -186,16 +182,14 @@ module es {
|
||||
* @param onlyReturnInitializedComponents
|
||||
*/
|
||||
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components.buffer[i];
|
||||
for (let component of this._components) {
|
||||
if (component instanceof type)
|
||||
return component as T;
|
||||
}
|
||||
|
||||
// 我们可以选择检查挂起的组件,以防addComponent和getComponent在同一个框架中被调用
|
||||
if (!onlyReturnInitializedComponents) {
|
||||
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
||||
let component = this._componentsToAdd[i];
|
||||
for (let component of this._componentsToAdd) {
|
||||
if (component instanceof type)
|
||||
return component as T;
|
||||
}
|
||||
@@ -213,16 +207,14 @@ module es {
|
||||
if (!components)
|
||||
components = [];
|
||||
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components.buffer[i];
|
||||
for (let component of this._components) {
|
||||
if (component instanceof typeName) {
|
||||
components.push(component);
|
||||
}
|
||||
}
|
||||
|
||||
// 我们还检查了待处理的组件,以防在同一帧中调用addComponent和getComponent
|
||||
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
||||
let component = this._componentsToAdd[i];
|
||||
for (let component of this._componentsToAdd) {
|
||||
if (component instanceof typeName) {
|
||||
components.push(component);
|
||||
}
|
||||
@@ -234,15 +226,15 @@ module es {
|
||||
public update() {
|
||||
this.updateLists();
|
||||
for (let i = 0; i < this._updatableComponents.length; i++) {
|
||||
if (this._updatableComponents.buffer[i].enabled)
|
||||
this._updatableComponents.buffer[i].update();
|
||||
if (this._updatableComponents[i].enabled)
|
||||
this._updatableComponents[i].update();
|
||||
}
|
||||
}
|
||||
|
||||
public onEntityTransformChanged(comp: transform.Component) {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
if (this._components.buffer[i].enabled)
|
||||
this._components.buffer[i].onEntityTransformChanged(comp);
|
||||
if (this._components[i].enabled)
|
||||
this._components[i].onEntityTransformChanged(comp);
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
||||
@@ -253,12 +245,12 @@ module es {
|
||||
|
||||
public onEntityEnabled() {
|
||||
for (let i = 0; i < this._components.length; i++)
|
||||
this._components.buffer[i].onEnabled();
|
||||
this._components[i].onEnabled();
|
||||
}
|
||||
|
||||
public onEntityDisabled() {
|
||||
for (let i = 0; i < this._components.length; i++)
|
||||
this._components.buffer[i].onDisabled();
|
||||
this._components[i].onDisabled();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ module es {
|
||||
/**
|
||||
* 场景中添加的实体列表
|
||||
*/
|
||||
public _entities: FastList<Entity> = new FastList<Entity>();
|
||||
public _entities: Entity[] = [];
|
||||
/**
|
||||
* 本帧添加的实体列表。用于对实体进行分组,以便我们可以同时处理它们
|
||||
*/
|
||||
@@ -84,12 +84,12 @@ module es {
|
||||
this.updateLists();
|
||||
|
||||
for (let i = 0; i < this._entities.length; i++) {
|
||||
this._entities.buffer[i]._isDestroyed = true;
|
||||
this._entities.buffer[i].onRemovedFromScene();
|
||||
this._entities.buffer[i].scene = null;
|
||||
this._entities[i]._isDestroyed = true;
|
||||
this._entities[i].onRemovedFromScene();
|
||||
this._entities[i].scene = null;
|
||||
}
|
||||
|
||||
this._entities.clear();
|
||||
this._entities.length = 0;
|
||||
this._entityDict.clear();
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ module es {
|
||||
* @param entity
|
||||
*/
|
||||
public contains(entity: Entity): boolean {
|
||||
return this._entities.contains(entity) || this._entitiesToAdded.contains(entity);
|
||||
return new linq.List(this._entities).contains(entity) || this._entitiesToAdded.contains(entity);
|
||||
}
|
||||
|
||||
public getTagList(tag: number) {
|
||||
@@ -127,8 +127,7 @@ module es {
|
||||
}
|
||||
|
||||
public update() {
|
||||
for (let i = 0; i < this._entities.length; i++) {
|
||||
let entity = this._entities.buffer[i];
|
||||
for (let entity of this._entities) {
|
||||
if (entity.enabled && (entity.updateInterval == 1 || Time.frameCount % entity.updateInterval == 0))
|
||||
entity.update();
|
||||
}
|
||||
@@ -141,7 +140,7 @@ module es {
|
||||
this.removeFromTagList(entity);
|
||||
|
||||
// 处理常规实体列表
|
||||
this._entities.remove(entity);
|
||||
new linq.List(this._entities).remove(entity);
|
||||
entity.onRemovedFromScene();
|
||||
entity.scene = null;
|
||||
|
||||
@@ -153,7 +152,7 @@ module es {
|
||||
|
||||
if (this._entitiesToAdded.getCount() > 0) {
|
||||
this._entitiesToAdded.toArray().forEach(entity => {
|
||||
this._entities.add(entity);
|
||||
this._entities.push(entity);
|
||||
entity.scene = this.scene;
|
||||
|
||||
this.addToTagList(entity);
|
||||
@@ -171,7 +170,7 @@ module es {
|
||||
}
|
||||
|
||||
if (this._isEntityListUnsorted) {
|
||||
this._entities.sort(Entity.entityComparer);
|
||||
this._entities.sort(Entity.entityComparer.compare);
|
||||
this._isEntityListUnsorted = false;
|
||||
}
|
||||
|
||||
@@ -189,8 +188,8 @@ module es {
|
||||
*/
|
||||
public findEntity(name: string) {
|
||||
for (let i = 0; i < this._entities.length; i++) {
|
||||
if (this._entities.buffer[i].name == name)
|
||||
return this._entities.buffer[i];
|
||||
if (this._entities[i].name == name)
|
||||
return this._entities[i];
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._entitiesToAdded.getCount(); i ++){
|
||||
@@ -226,8 +225,8 @@ module es {
|
||||
public entitiesOfType<T extends Entity>(type): T[] {
|
||||
let list = ListPool.obtain<T>();
|
||||
for (let i = 0; i < this._entities.length; i++) {
|
||||
if (this._entities.buffer[i] instanceof type)
|
||||
list.push(this._entities.buffer[i] as T);
|
||||
if (this._entities[i] instanceof type)
|
||||
list.push(this._entities[i] as T);
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._entitiesToAdded.getCount(); i ++){
|
||||
@@ -246,8 +245,8 @@ module es {
|
||||
*/
|
||||
public findComponentOfType<T extends Component>(type): T {
|
||||
for (let i = 0; i < this._entities.length; i++) {
|
||||
if (this._entities.buffer[i].enabled) {
|
||||
let comp = this._entities.buffer[i].getComponent<T>(type);
|
||||
if (this._entities[i].enabled) {
|
||||
let comp = this._entities[i].getComponent<T>(type);
|
||||
if (comp)
|
||||
return comp;
|
||||
}
|
||||
@@ -273,8 +272,8 @@ module es {
|
||||
public findComponentsOfType<T extends Component>(type): T[] {
|
||||
let comps = ListPool.obtain<T>();
|
||||
for (let i = 0; i < this._entities.length; i++) {
|
||||
if (this._entities.buffer[i].enabled)
|
||||
this._entities.buffer[i].getComponents(type, comps);
|
||||
if (this._entities[i].enabled)
|
||||
this._entities[i].getComponents(type, comps);
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {
|
||||
|
||||
@@ -3,7 +3,7 @@ module es {
|
||||
* 提供了一系列立方贝塞尔点,并提供了帮助方法来访问贝塞尔
|
||||
*/
|
||||
export class BezierSpline {
|
||||
public _points: FastList<Vector2> = new FastList<Vector2>();
|
||||
public _points: Vector2[] = [];
|
||||
public _curveCount: number = 0;
|
||||
|
||||
/**
|
||||
@@ -32,15 +32,15 @@ module es {
|
||||
*/
|
||||
public setControlPoint(index: number, point: Vector2) {
|
||||
if (index % 3 == 0) {
|
||||
let delta = Vector2.subtract(point, this._points.buffer[index]);
|
||||
let delta = Vector2.subtract(point, this._points[index]);
|
||||
if (index > 0)
|
||||
this._points.buffer[index - 1].add(delta);
|
||||
this._points[index - 1].add(delta);
|
||||
|
||||
if (index + 1 < this._points.length)
|
||||
this._points.buffer[index + 1].add(delta);
|
||||
this._points[index + 1].add(delta);
|
||||
}
|
||||
|
||||
this._points.buffer[index] = point;
|
||||
this._points[index] = point;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,8 +49,8 @@ module es {
|
||||
*/
|
||||
public getPointAtTime(t: number): Vector2{
|
||||
let i = this.pointIndexAtTime(new Ref(t));
|
||||
return Bezier.getPointThree(this._points.buffer[i], this._points.buffer[i + 1], this._points.buffer[i + 2],
|
||||
this._points.buffer[i + 3], t);
|
||||
return Bezier.getPointThree(this._points[i], this._points[i + 1], this._points[i + 2],
|
||||
this._points[i + 3], t);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,8 +59,8 @@ module es {
|
||||
*/
|
||||
public getVelocityAtTime(t: number): Vector2 {
|
||||
let i = this.pointIndexAtTime(new Ref(t));
|
||||
return Bezier.getFirstDerivativeThree(this._points.buffer[i], this._points.buffer[i + 1], this._points.buffer[i + 2],
|
||||
this._points.buffer[i + 3], t);
|
||||
return Bezier.getFirstDerivativeThree(this._points[i], this._points[i + 1], this._points[i + 2],
|
||||
this._points[i + 3], t);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,11 +81,11 @@ module es {
|
||||
public addCurve(start: Vector2, firstControlPoint: Vector2, secondControlPoint: Vector2, end: Vector2) {
|
||||
// 只有当这是第一条曲线时,我们才会添加起始点。对于其他所有的曲线,前一个曲线的终点应该等于新曲线的起点。
|
||||
if (this._points.length == 0)
|
||||
this._points.add(start);
|
||||
this._points.push(start);
|
||||
|
||||
this._points.add(firstControlPoint);
|
||||
this._points.add(secondControlPoint);
|
||||
this._points.add(end);
|
||||
this._points.push(firstControlPoint);
|
||||
this._points.push(secondControlPoint);
|
||||
this._points.push(end);
|
||||
|
||||
this._curveCount = (this._points.length - 1) / 3;
|
||||
}
|
||||
@@ -94,7 +94,7 @@ module es {
|
||||
* 重置bezier,移除所有点
|
||||
*/
|
||||
public reset() {
|
||||
this._points.clear();
|
||||
this._points.length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
module es {
|
||||
/**
|
||||
* 围绕一个数组的非常基本的包装,当它达到容量时自动扩展。
|
||||
* 注意,在迭代时应该这样直接访问缓冲区,但使用FastList.length字段。
|
||||
*
|
||||
* @tutorial
|
||||
* for( var i = 0; i <= list.length; i++ )
|
||||
* var item = list.buffer[i];
|
||||
*/
|
||||
export class FastList<T> {
|
||||
/**
|
||||
* 直接访问后备缓冲区。
|
||||
* 不要使用buffer.Length! 使用FastList.length
|
||||
*/
|
||||
public buffer: T[];
|
||||
|
||||
/**
|
||||
* 直接访问缓冲区内填充项的长度。不要改变。
|
||||
*/
|
||||
public length: number = 0;
|
||||
|
||||
constructor(size: number = 5) {
|
||||
this.buffer = new Array(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空列表并清空缓冲区中的所有项目
|
||||
*/
|
||||
public clear() {
|
||||
this.buffer.length = 0;
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 和clear的工作原理一样,只是它不会将缓冲区中的所有项目清空。
|
||||
*/
|
||||
public reset() {
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将该项目添加到列表中
|
||||
* @param item
|
||||
*/
|
||||
public add(item: T) {
|
||||
if (this.length == this.buffer.length)
|
||||
this.buffer.length = Math.max(this.buffer.length << 1, 10);
|
||||
this.buffer[this.length++] = item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从列表中删除该项目
|
||||
* @param item
|
||||
*/
|
||||
public remove(item: T){
|
||||
let comp = EqualityComparer.default<T>();
|
||||
for (let i = 0; i < this.length; ++i){
|
||||
if (comp.equals(this.buffer[i], item)){
|
||||
this.removeAt(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从列表中删除给定索引的项目。
|
||||
* @param index
|
||||
*/
|
||||
public removeAt(index: number){
|
||||
if (index >= this.length)
|
||||
throw new Error("index超出范围!");
|
||||
|
||||
this.length --;
|
||||
new linq.List(this.buffer).removeAt(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查项目是否在FastList中
|
||||
* @param item
|
||||
*/
|
||||
public contains(item: T){
|
||||
let comp = EqualityComparer.default<T>();
|
||||
for (let i = 0; i < this.length; ++ i){
|
||||
if (comp.equals(this.buffer[i], item))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果缓冲区达到最大,将分配更多的空间来容纳额外的ItemCount。
|
||||
* @param additionalItemCount
|
||||
*/
|
||||
public ensureCapacity(additionalItemCount: number = 1){
|
||||
if (this.length + additionalItemCount >= this.buffer.length)
|
||||
this.buffer.length = Math.max(this.buffer.length << 1, this.length + additionalItemCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加数组中的所有项目
|
||||
* @param array
|
||||
*/
|
||||
public addRange(array: T[]){
|
||||
for (let item of array)
|
||||
this.add(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对缓冲区中的所有项目进行排序,长度不限。
|
||||
*/
|
||||
public sort(comparer: IComparer<T>){
|
||||
this.buffer.sort(comparer.compare);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user