| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- export class List<T extends Object> extends cc.ValueType {
- private item: T[] = null;
- private indexCount: number = 0;
- constructor() {
- super();
- this.item = [];
- this.indexCount = 0;
- }
- public get Count(): number { return this.indexCount; }
- //==
- public Add(value: T) {
- this.item[this.indexCount++] = value;
- }
- //==
- public Remove(value: T) {
- if (this.indexCount == 0) throw new Error('collection is not element');
- let index = this.ItemIndexOf(value);
- if (index == -1) throw new Error('not this element ');
- this.RemoveItemUseIndex(index);
- }
- //==
- public RemoveAt(_index: number) {
- if (this.indexCount == 0 || _index < 0 || _index >= this.indexCount) throw new Error('Beyond the index ' + _index);
- this.RemoveItemUseIndex(_index);
- }
- //==
- private RemoveItemUseIndex(_index: number) {
- for (_index; _index < this.indexCount - 1; _index++) {
- this.item[_index] = this.item[_index + 1];
- }
- this.item[_index] = null;
- this.indexCount--;
- this.item.length = this.indexCount
- }
- //==
- public Contain(value: T): boolean {
- return this.indexCount > 0 && this.ItemIndexOf(value) > -1;
- }
- //==
- public ItemIndexOf(value: T): number {
- let index = 0;
- for (index; index < this.indexCount; index++) {
- if (value == this.item[index]) {
- break;
- }
- }
- if (index < this.indexCount)
- return index;
- else
- return -1;
- }
- //==
- public Item(index: number): T {
- if (index >= 0 && index < this.indexCount)
- return this.item[index];
- else
- throw new Error('Beyond the index ' + index);
- }
- //==
- public SetItem(index: number, t: T) {
- if (index >= 0 && index < this.indexCount)
- this.item[index] = t;
- else
- throw new Error('Beyond the index ' + index);
- }
- //==
- public Clear() {
- this.item = [];
- this.item.length = 0;
- this.indexCount = 0;
- }
- //==
- public ToArray(): T[] {
- return this.item;
- }
- }
- export class Dictionary<K extends Object, V extends Object> extends cc.ValueType {
- private itemKey: List<K>;
- private itemValue: List<V>;
- private indexCount: number = 0;
- constructor() {
- super();
- this.itemKey = new List<K>();
- this.itemValue = new List<V>();
- this.indexCount = 0;
- }
- public get Count(): number { return this.indexCount; }
- //==
- public Add(k: K, v: V) {
- if (this.ContainKey(k))
- console.error("contain this key");
- //throw new Error("contain this key");
- else {
- this.itemKey.Add(k);
- this.itemValue.Add(v);
- this.indexCount++;
- }
- }
- //==
- public Remove(k: K) {
- if (this.indexCount == 0) throw new Error("collection not element");
- let index = this.itemKey.ItemIndexOf(k);
- if (index == -1) throw new Error("not this key value");
- this.itemKey.RemoveAt(index);
- this.itemValue.RemoveAt(index);
- this.indexCount--;
- }
- //==
- public RemoveByValue(v: V) {
- if (this.indexCount == 0) throw new Error("collection not element");
- let index = this.itemValue.ItemIndexOf(v);
- if (index == -1) throw new Error("not this key value");
- this.itemKey.RemoveAt(index);
- this.itemValue.RemoveAt(index);
- this.indexCount--;
- }
- //==
- public RemoveAt(_index: number) {
- if (this.indexCount == 0 || _index >= this.indexCount || _index < 0) throw new Error("Beyond the index " + _index);
- this.itemKey.RemoveAt(_index);
- this.itemValue.RemoveAt(_index);
- this.indexCount--;
- }
- //==
- public ContainKey(k: K): boolean {
- return this.itemKey.Contain(k);
- }
- //==
- public ContainValue(v: V): boolean {
- return this.itemValue.Contain(v);
- }
- //==
- public TryGetValue(k: K): V {
- if (this.indexCount == 0) return null;// throw new Error("collection not element");
- let index = this.itemKey.ItemIndexOf(k);
- if (index == -1) return null;// throw new Error("not this key");
- return this.itemValue.Item(index);
- }
- //==
- public Item(k: K): V {
- if (this.indexCount == 0) throw new Error("collection not element");
- let index = this.itemKey.ItemIndexOf(k);
- if (index == -1) throw new Error("not this key");
- return this.itemValue.Item(index);
- }
- //==
- public SetItem(k: K, v: V) {
- if (this.indexCount == 0) throw new Error("collection not element");
- let index = this.itemKey.ItemIndexOf(k);
- if (index == -1) throw new Error("not this key");
- this.itemValue.SetItem(index, v);
- }
- //==
- public ItemKeyofIndex(index: number): K {
- return this.itemKey.Item(index);
- }
- //==
- public ItemValueofIndex(index: number): V {
- return this.itemValue.Item(index);
- }
- //==
- public GetValue() {
- return this.itemValue;
- }
- public GetKey() {
- return this.itemKey;
- }
- public Clear() {
- this.itemKey.Clear();
- this.itemValue.Clear();
- this.indexCount = 0;
- }
- }
- export class Stack<T extends Object> extends cc.ValueType {
- private item: T[];
- private indexCount: number = 0;
- constructor() {
- super();
- this.item = [];
- this.indexCount = length;
- }
- public get Count(): number { return this.indexCount; }
- //
- public Push(value: T) {
- this.item[this.indexCount++] = value;
- }
- //
- public Pop(): T {
- if (this.indexCount == 0) throw new Error('collection not element');
- let temp: T = this.item[this.indexCount - 1];
- this.indexCount--;
- this.item.length = this.indexCount;
- return temp;
- }
- //
- public See(): T {
- if (this.indexCount == 0) throw new Error('collection not element');
- return this.item[this.item.length - 1];
- }
- //==
- public Contain(value: T): boolean {
- return this.ItemIndexOf(value) > -1;
- }
- //==
- private ItemIndexOf(value: T): number {
- let index = 0;
- for (index; index < this.indexCount; index++) {
- if (value == this.item[index])
- break;
- }
- if (index < this.indexCount)
- return index;
- else
- return -1;
- }
- public Item(index: number): T {
- if (index >= 0 && index < this.indexCount)
- return this.item[index];
- else
- throw new Error("Beyond the index " + index);
- }
- //==
- public Clear() {
- this.item = [];
- this.item.length = 0;
- this.indexCount = 0;
- }
- }
- export class Queue<T extends Object> extends cc.ValueType {
- private item: T[];
- private indexCount: number = 0;
- public name: string;
- constructor() {
- super();
- this.item = [];
- this.indexCount = 0;
- }
- public get Count(): number { return this.indexCount; }
- //
- public Push(value: T) {
- this.item[this.indexCount++] = value;
- }
- //
- public Pop(): T {
- if (this.indexCount == 0) throw new Error('collection not element');
- let tempI = this.item[0];
- let index = 1;
- for (; index < this.indexCount; index++) {
- this.item[index - 1] = this.item[index];
- }
- this.item[this.indexCount - 1] = null;
- this.indexCount--;
- this.item.length = this.indexCount;
- return tempI;
- }
- public PopLast(): T {
- if (this.indexCount == 0) throw new Error('collection not element');
- let temp: T = this.item[this.indexCount - 1];
- this.indexCount--;
- this.item.length = this.indexCount;
- return temp;
- }
- //
- public See(): T {
- if (this.indexCount == 0) throw new Error('collection not element');
- return this.item[0];
- }
- //
- public SeeLast(): T {
- if (this.indexCount == 0) throw new Error('collection not element');
- return this.item[this.indexCount - 1];
- }
- //==
- public Contain(value: T): boolean {
- if (this.indexCount == 0) throw new Error('collection not element');
- return this.ItemIndexOf(value) > -1;
- }
- //==
- private ItemIndexOf(value: T): number {
- let index = 0;
- for (index; index < this.indexCount; index++) {
- if (value == this.item[index])
- break;
- }
- if (index < this.indexCount)
- return index;
- else
- return -1;
- }
- public Item(index: number): T {
- if (index >= 0 && index < this.indexCount)
- return this.item[index];
- else
- throw new Error("Beyond the index " + index);
- }
- //==
- public Clear() {
- this.item = [];
- this.item.length = 0;
- this.indexCount = 0;
- }
- }
- export class Random extends cc.ValueType {
- seed: number = 0;
- public constructor(_seed: number = 0) {
- super();
- this.seed = _seed;
- }
- public Range(min: number, max: number) {
- return this.rnd() * (max - min) + min;
- }
- public RangeInt(min: number, max: number) {
- return Math.round(this.rnd() * (max - min - 1) + min);
- }
- private rnd(): number {
- this.seed = (this.seed * 9301 + 49297) % 233280;
- return this.seed / (233280.0);
- }
- }
- export class Test<T extends Object> extends cc.ValueType {
- private temp: T[];
- private length: number;
- constructor() {
- super();
- this.temp = [];
- this.length = 0;
- }
- public get Count() {
- return this.length;
- }
- public Add(value: T) {
- this.temp[this.length++] = value;
- }
- public Contion(value: T): number {
- for (let x = 0; x < this.length; x++) {
- if (this.temp[x] == value)
- return x;
- }
- return -1;
- }
- }
|