General.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. export class List<T extends Object> extends cc.ValueType {
  2. private item: T[] = null;
  3. private indexCount: number = 0;
  4. constructor() {
  5. super();
  6. this.item = [];
  7. this.indexCount = 0;
  8. }
  9. public get Count(): number { return this.indexCount; }
  10. //==
  11. public Add(value: T) {
  12. this.item[this.indexCount++] = value;
  13. }
  14. //==
  15. public Remove(value: T) {
  16. if (this.indexCount == 0) throw new Error('collection is not element');
  17. let index = this.ItemIndexOf(value);
  18. if (index == -1) throw new Error('not this element ');
  19. this.RemoveItemUseIndex(index);
  20. }
  21. //==
  22. public RemoveAt(_index: number) {
  23. if (this.indexCount == 0 || _index < 0 || _index >= this.indexCount) throw new Error('Beyond the index ' + _index);
  24. this.RemoveItemUseIndex(_index);
  25. }
  26. //==
  27. private RemoveItemUseIndex(_index: number) {
  28. for (_index; _index < this.indexCount - 1; _index++) {
  29. this.item[_index] = this.item[_index + 1];
  30. }
  31. this.item[_index] = null;
  32. this.indexCount--;
  33. this.item.length = this.indexCount
  34. }
  35. //==
  36. public Contain(value: T): boolean {
  37. return this.indexCount > 0 && this.ItemIndexOf(value) > -1;
  38. }
  39. //==
  40. public ItemIndexOf(value: T): number {
  41. let index = 0;
  42. for (index; index < this.indexCount; index++) {
  43. if (value == this.item[index]) {
  44. break;
  45. }
  46. }
  47. if (index < this.indexCount)
  48. return index;
  49. else
  50. return -1;
  51. }
  52. //==
  53. public Item(index: number): T {
  54. if (index >= 0 && index < this.indexCount)
  55. return this.item[index];
  56. else
  57. throw new Error('Beyond the index ' + index);
  58. }
  59. //==
  60. public SetItem(index: number, t: T) {
  61. if (index >= 0 && index < this.indexCount)
  62. this.item[index] = t;
  63. else
  64. throw new Error('Beyond the index ' + index);
  65. }
  66. //==
  67. public Clear() {
  68. this.item = [];
  69. this.item.length = 0;
  70. this.indexCount = 0;
  71. }
  72. //==
  73. public ToArray(): T[] {
  74. return this.item;
  75. }
  76. }
  77. export class Dictionary<K extends Object, V extends Object> extends cc.ValueType {
  78. private itemKey: List<K>;
  79. private itemValue: List<V>;
  80. private indexCount: number = 0;
  81. constructor() {
  82. super();
  83. this.itemKey = new List<K>();
  84. this.itemValue = new List<V>();
  85. this.indexCount = 0;
  86. }
  87. public get Count(): number { return this.indexCount; }
  88. //==
  89. public Add(k: K, v: V) {
  90. if (this.ContainKey(k))
  91. console.error("contain this key");
  92. //throw new Error("contain this key");
  93. else {
  94. this.itemKey.Add(k);
  95. this.itemValue.Add(v);
  96. this.indexCount++;
  97. }
  98. }
  99. //==
  100. public Remove(k: K) {
  101. if (this.indexCount == 0) throw new Error("collection not element");
  102. let index = this.itemKey.ItemIndexOf(k);
  103. if (index == -1) throw new Error("not this key value");
  104. this.itemKey.RemoveAt(index);
  105. this.itemValue.RemoveAt(index);
  106. this.indexCount--;
  107. }
  108. //==
  109. public RemoveByValue(v: V) {
  110. if (this.indexCount == 0) throw new Error("collection not element");
  111. let index = this.itemValue.ItemIndexOf(v);
  112. if (index == -1) throw new Error("not this key value");
  113. this.itemKey.RemoveAt(index);
  114. this.itemValue.RemoveAt(index);
  115. this.indexCount--;
  116. }
  117. //==
  118. public RemoveAt(_index: number) {
  119. if (this.indexCount == 0 || _index >= this.indexCount || _index < 0) throw new Error("Beyond the index " + _index);
  120. this.itemKey.RemoveAt(_index);
  121. this.itemValue.RemoveAt(_index);
  122. this.indexCount--;
  123. }
  124. //==
  125. public ContainKey(k: K): boolean {
  126. return this.itemKey.Contain(k);
  127. }
  128. //==
  129. public ContainValue(v: V): boolean {
  130. return this.itemValue.Contain(v);
  131. }
  132. //==
  133. public TryGetValue(k: K): V {
  134. if (this.indexCount == 0) return null;// throw new Error("collection not element");
  135. let index = this.itemKey.ItemIndexOf(k);
  136. if (index == -1) return null;// throw new Error("not this key");
  137. return this.itemValue.Item(index);
  138. }
  139. //==
  140. public Item(k: K): V {
  141. if (this.indexCount == 0) throw new Error("collection not element");
  142. let index = this.itemKey.ItemIndexOf(k);
  143. if (index == -1) throw new Error("not this key");
  144. return this.itemValue.Item(index);
  145. }
  146. //==
  147. public SetItem(k: K, v: V) {
  148. if (this.indexCount == 0) throw new Error("collection not element");
  149. let index = this.itemKey.ItemIndexOf(k);
  150. if (index == -1) throw new Error("not this key");
  151. this.itemValue.SetItem(index, v);
  152. }
  153. //==
  154. public ItemKeyofIndex(index: number): K {
  155. return this.itemKey.Item(index);
  156. }
  157. //==
  158. public ItemValueofIndex(index: number): V {
  159. return this.itemValue.Item(index);
  160. }
  161. //==
  162. public GetValue() {
  163. return this.itemValue;
  164. }
  165. public GetKey() {
  166. return this.itemKey;
  167. }
  168. public Clear() {
  169. this.itemKey.Clear();
  170. this.itemValue.Clear();
  171. this.indexCount = 0;
  172. }
  173. }
  174. export class Stack<T extends Object> extends cc.ValueType {
  175. private item: T[];
  176. private indexCount: number = 0;
  177. constructor() {
  178. super();
  179. this.item = [];
  180. this.indexCount = length;
  181. }
  182. public get Count(): number { return this.indexCount; }
  183. //
  184. public Push(value: T) {
  185. this.item[this.indexCount++] = value;
  186. }
  187. //
  188. public Pop(): T {
  189. if (this.indexCount == 0) throw new Error('collection not element');
  190. let temp: T = this.item[this.indexCount - 1];
  191. this.indexCount--;
  192. this.item.length = this.indexCount;
  193. return temp;
  194. }
  195. //
  196. public See(): T {
  197. if (this.indexCount == 0) throw new Error('collection not element');
  198. return this.item[this.item.length - 1];
  199. }
  200. //==
  201. public Contain(value: T): boolean {
  202. return this.ItemIndexOf(value) > -1;
  203. }
  204. //==
  205. private ItemIndexOf(value: T): number {
  206. let index = 0;
  207. for (index; index < this.indexCount; index++) {
  208. if (value == this.item[index])
  209. break;
  210. }
  211. if (index < this.indexCount)
  212. return index;
  213. else
  214. return -1;
  215. }
  216. public Item(index: number): T {
  217. if (index >= 0 && index < this.indexCount)
  218. return this.item[index];
  219. else
  220. throw new Error("Beyond the index " + index);
  221. }
  222. //==
  223. public Clear() {
  224. this.item = [];
  225. this.item.length = 0;
  226. this.indexCount = 0;
  227. }
  228. }
  229. export class Queue<T extends Object> extends cc.ValueType {
  230. private item: T[];
  231. private indexCount: number = 0;
  232. public name: string;
  233. constructor() {
  234. super();
  235. this.item = [];
  236. this.indexCount = 0;
  237. }
  238. public get Count(): number { return this.indexCount; }
  239. //
  240. public Push(value: T) {
  241. this.item[this.indexCount++] = value;
  242. }
  243. //
  244. public Pop(): T {
  245. if (this.indexCount == 0) throw new Error('collection not element');
  246. let tempI = this.item[0];
  247. let index = 1;
  248. for (; index < this.indexCount; index++) {
  249. this.item[index - 1] = this.item[index];
  250. }
  251. this.item[this.indexCount - 1] = null;
  252. this.indexCount--;
  253. this.item.length = this.indexCount;
  254. return tempI;
  255. }
  256. public PopLast(): T {
  257. if (this.indexCount == 0) throw new Error('collection not element');
  258. let temp: T = this.item[this.indexCount - 1];
  259. this.indexCount--;
  260. this.item.length = this.indexCount;
  261. return temp;
  262. }
  263. //
  264. public See(): T {
  265. if (this.indexCount == 0) throw new Error('collection not element');
  266. return this.item[0];
  267. }
  268. //
  269. public SeeLast(): T {
  270. if (this.indexCount == 0) throw new Error('collection not element');
  271. return this.item[this.indexCount - 1];
  272. }
  273. //==
  274. public Contain(value: T): boolean {
  275. if (this.indexCount == 0) throw new Error('collection not element');
  276. return this.ItemIndexOf(value) > -1;
  277. }
  278. //==
  279. private ItemIndexOf(value: T): number {
  280. let index = 0;
  281. for (index; index < this.indexCount; index++) {
  282. if (value == this.item[index])
  283. break;
  284. }
  285. if (index < this.indexCount)
  286. return index;
  287. else
  288. return -1;
  289. }
  290. public Item(index: number): T {
  291. if (index >= 0 && index < this.indexCount)
  292. return this.item[index];
  293. else
  294. throw new Error("Beyond the index " + index);
  295. }
  296. //==
  297. public Clear() {
  298. this.item = [];
  299. this.item.length = 0;
  300. this.indexCount = 0;
  301. }
  302. }
  303. export class Random extends cc.ValueType {
  304. seed: number = 0;
  305. public constructor(_seed: number = 0) {
  306. super();
  307. this.seed = _seed;
  308. }
  309. public Range(min: number, max: number) {
  310. return this.rnd() * (max - min) + min;
  311. }
  312. public RangeInt(min: number, max: number) {
  313. return Math.round(this.rnd() * (max - min - 1) + min);
  314. }
  315. private rnd(): number {
  316. this.seed = (this.seed * 9301 + 49297) % 233280;
  317. return this.seed / (233280.0);
  318. }
  319. }
  320. export class Test<T extends Object> extends cc.ValueType {
  321. private temp: T[];
  322. private length: number;
  323. constructor() {
  324. super();
  325. this.temp = [];
  326. this.length = 0;
  327. }
  328. public get Count() {
  329. return this.length;
  330. }
  331. public Add(value: T) {
  332. this.temp[this.length++] = value;
  333. }
  334. public Contion(value: T): number {
  335. for (let x = 0; x < this.length; x++) {
  336. if (this.temp[x] == value)
  337. return x;
  338. }
  339. return -1;
  340. }
  341. }