enscript.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /**
  2. * \defgroup Enforce Enforce script essentials
  3. * \note \p float ftime; The deltaTime since last frame
  4. * \note \p float FLT_MAX; The maximum value for float
  5. * \note \p float FLT_MIN; The minimum value for float
  6. * @{
  7. */
  8. //! Super root of all classes in Enforce script
  9. class Class
  10. {
  11. /**
  12. \brief Returns true when instance is of the type, or inherited one.
  13. \param type Class type
  14. \returns \p bool true when 'clType' is the same as 'type', or inherited one.
  15. @code
  16. if (inst && inst.IsInherited(Widget))
  17. {
  18. Print("inst is inherited from Widget class!");
  19. }
  20. @endcode
  21. */
  22. proto native external bool IsInherited(typename type);
  23. /**
  24. \brief Returns name of class-type
  25. \param inst Class
  26. \returns \p string class-type
  27. @code
  28. Man player = g_Game.GetPlayer();
  29. string className = player.ClassName();
  30. Print(className);
  31. >> className = 'Man'
  32. @endcode
  33. */
  34. proto native owned external string ClassName();
  35. string GetDebugName() { return ClassName(); }
  36. /**
  37. \brief Returns typename of object's class
  38. \returns \p typename class-type
  39. @code
  40. Man player = g_Game.GetPlayer();
  41. typename type = player.Type();
  42. Print(type.ToString());
  43. >> 'Man'
  44. @endcode
  45. */
  46. proto native external typename Type();
  47. /**
  48. \brief Returns typename of object's reference
  49. \returns \p typename class-type
  50. @code
  51. EntityAI e;
  52. Print(e.StaticType());
  53. >> 'EntityAI'
  54. @endcode
  55. */
  56. proto external static typename StaticType();
  57. /**
  58. \brief Returns typename of class even without a variable or instance
  59. \returns \p typename class-type
  60. @code
  61. typename eAITypename = StaticGetType(EntityAI);
  62. @endcode
  63. */
  64. static typename StaticGetType(typename t)
  65. {
  66. return t;
  67. }
  68. proto external string ToString();
  69. /**
  70. \brief Try to safely down-cast base class to child class.
  71. \returns down-casted 'from' pointer when cast is successfull (classes are related), or null if casting is invalid
  72. @code
  73. // assume that Man inheites from Object
  74. Object obj = g_Game.GetPlayer();
  75. Man player = Man.Cast(obj);
  76. if (player)
  77. {
  78. // horay!
  79. }
  80. @endcode
  81. */
  82. proto static Class Cast(Class from);
  83. /**
  84. \brief Try to safely down-cast base class to child class.
  85. \returns \p bool true when 'from' is not null and cast successfull, false when casting is not valid or 'from' is null
  86. @code
  87. // assume that Man inheites from Object
  88. Object obj = g_Game.GetPlayer();
  89. Man player;
  90. if (Class.CastTo(player, obj))
  91. {
  92. // horay!
  93. }
  94. @endcode
  95. */
  96. proto static bool CastTo(out Class to, Class from);
  97. //! This function is for internal script usage
  98. private proto static bool SafeCastType(Class type, out Class to, Class from);
  99. };
  100. //! TODO doc
  101. class Managed
  102. {
  103. };
  104. //! TODO doc
  105. class NonSerialized
  106. {
  107. };
  108. //! script representation for C++ RTTI types
  109. typedef int[] TypeID;
  110. //! Module containing compiled scripts.
  111. class ScriptModule
  112. {
  113. private void ~ScriptModule();
  114. /*!dynamic call of function
  115. when inst == NULL, it's global function call, otherwise it's method of class
  116. returns true, when success
  117. The call creates new thread, so it's legal to use sleep/wait
  118. */
  119. proto volatile int Call(Class inst, string function, void parm);
  120. /*!dynamic call of function
  121. when inst == NULL, it's global function call, otherwise it's method of class
  122. returns true, when success
  123. The call do not create new thread!!!!
  124. */
  125. proto volatile int CallFunction(Class inst, string function, out void returnVal, void parm);
  126. proto volatile int CallFunctionParams(Class inst, string function, out void returnVal, Class parms);
  127. proto native void Release();
  128. /**
  129. \brief Do load script and create ScriptModule for it
  130. \param parentModule Module
  131. \param scriptFile Script path
  132. \param listing ??
  133. \returns \p ScriptModule Loaded scripted module
  134. @code
  135. ???
  136. @endcode
  137. */
  138. static proto native ScriptModule LoadScript(ScriptModule parentModule, string scriptFile, bool listing);
  139. };
  140. //main script module (contains script.c and this file)
  141. //ScriptModule g_Script;
  142. class EnScript
  143. {
  144. private void EnScript() {}
  145. private void ~EnScript() {}
  146. /**
  147. \brief Dynamic read of variable value by its name
  148. \param inst When inst == NULL, it's for global variable, otherwise it's class member
  149. \param index Is index when variable is array
  150. \param[out] result Variable must be of the same type!
  151. \returns \p int true when success
  152. @code
  153. float count = 0;
  154. bool success = EnScript.GetClassVar(myClass, "m_Counter", 0, count);
  155. Print(count);
  156. Print(success);
  157. >> count = 5
  158. >> success = 1
  159. @endcode
  160. */
  161. static proto int GetClassVar(Class inst, string varname,int index, out void result);
  162. /**
  163. \brief Dynamic write to variable by its name
  164. \param inst when inst == NULL, it's for global variable, otherwise it's class member
  165. \param varname
  166. \param index Is index when variable is array
  167. \param input Input variable must be of the same type!
  168. \returns \p int Returns true(1) when success
  169. @code
  170. Print(myClass.m_Counter);
  171. >> m_Counter = 0
  172. bool success = EnScript.SetClassVar(myClass, "m_Counter", 0, 5.0);
  173. Print(myClass.m_Counter);
  174. Print(success);
  175. >> m_Counter = 5
  176. >> success = 1
  177. @endcode
  178. */
  179. static proto int SetClassVar(Class inst, string varname, int index, void input);
  180. /**
  181. \brief Sets variable value by value in string
  182. \param[out] var
  183. \param value
  184. \returns int
  185. @code
  186. ???
  187. @endcode
  188. */
  189. static proto int SetVar(out void var, string value);
  190. /**
  191. \brief Debug tool for watching certain variable. Invokes debugger whenever is variable used
  192. \param var Certain variable for watching
  193. \param flags = 1 means it will break even when not modified
  194. \return \p void
  195. */
  196. static proto void Watch(void var, int flags);
  197. };
  198. /**
  199. \brief Sorts static array of integers(ascendically) / floats(ascendically) / strings(alphabetically)
  200. \param param_array \p array Array to sort
  201. \param num \p int How many items will be sorted in array
  202. \return \p void
  203. @code
  204. string arrStr[3] = {"Dog", "Car", "Apple"};
  205. Sort(arrStr, 2)
  206. for ( int x = 0; x < 3; x++ )
  207. {
  208. Print( arrStr[x] );
  209. }
  210. >> 'Car'
  211. >> 'Dog'
  212. >> 'Apple'
  213. @endcode
  214. */
  215. proto void Sort(void param_array[], int num);
  216. proto void reversearray(void param_array);
  217. proto void copyarray(void destArray, void srcArray);
  218. /**
  219. \brief Parses one token from input string. Result is put into token string, and type of token is returned. Input string is left-truncated by the resulting token length.
  220. \param[in,out] input \p string String for parse\ Output is without founded token
  221. \param[out] token \p string Founded string token
  222. \return \p int Type of token
  223. \verbatim
  224. Token types:
  225. 0 - error, no token
  226. 1 - defined token (special characters etc. . / * )
  227. 2 - quoted string. Quotes are removed -> TODO
  228. 3 - alphabetic string
  229. 4 - number
  230. 5 - end of line -> TODO
  231. \endverbatim
  232. @code
  233. string input = "Hello*World";
  234. string token1;
  235. string token2;
  236. int result1 = ParseStringEx(input, token1);
  237. int result2 = ParseStringEx(input, token2);
  238. Print( String( "Token1 = '" + token1 + "' Type = " + result1.ToString() ) );
  239. Print( String( "Token2 = '" + token2 + "' Type = " + result2.ToString() ) );
  240. Print( input );
  241. >> 'Toke1 = 'Hello' Type = 3'
  242. >> 'Toke1 = '*' Type = 1'
  243. @endcode
  244. */
  245. proto int ParseStringEx(inout string input, string token);
  246. /**
  247. \brief Parses string into array of tokens returns number of tokens
  248. \param input \p string String for parse
  249. \param[out] tokens \p array[] Parsed string in array
  250. \return \p int Number of tokens
  251. @code
  252. string token[2];
  253. int result = ParseString("Hello World", token);
  254. for( int i = 0; i < 2; i++ )
  255. {
  256. Print(token[i]);
  257. }
  258. >> 'Hello'
  259. >> 'World'
  260. @endcode
  261. */
  262. proto int ParseString(string input, out string tokens[]);
  263. /**
  264. \brief Kills thread.
  265. \param owner Can be NULL for global threads.
  266. \param name Name of the first function on stack
  267. \return \p int ???
  268. @code
  269. ???
  270. @endcode
  271. */
  272. proto native int KillThread(Class owner, string name);
  273. /**
  274. Yiels execution to other threads and then it continues. Obsolete...
  275. */
  276. proto volatile void Idle();
  277. /**
  278. \brief Debug function. Returns current function on stack of the thread
  279. \param owner Can be NULL for global threads
  280. \param name Name of the first function on stack
  281. \param backtrace ???
  282. \param linenumber ???
  283. \return \p string ???
  284. @code
  285. ???
  286. @endcode
  287. */
  288. proto owned string ThreadFunction(Class owner, string name, int backtrace, out int linenumber);
  289. //!Helper for passing string expression to functions with void parameter. Example: Print(String("Hello " + var));
  290. string String(string s)
  291. {
  292. return s;
  293. }
  294. //!Helper for printing out string expression. Example: PrintString("Hello " + var);
  295. void PrintString(string s)
  296. {
  297. Print(s);
  298. }
  299. class array<Class T>
  300. {
  301. /*!
  302. O(1) complexity.
  303. \return Number of elements of the array
  304. */
  305. proto native int Count();
  306. /*!
  307. Destroyes all elements of the array and sets the Count to 0.
  308. The underlying memory of the array is not freed.
  309. */
  310. proto native void Clear();
  311. /*!
  312. Sets n-th element to given value.
  313. */
  314. proto void Set(int n, T value);
  315. /*!
  316. Tries to find the first occurance of given value in the array.
  317. \return Index of the first occurance of `value` if found, -1 otherwise
  318. */
  319. proto int Find(T value);
  320. /*!
  321. \return Element at the index `n`
  322. */
  323. proto T Get(int n);
  324. /*!
  325. Inserts element at the end of array.
  326. \param value
  327. Element to be inserted
  328. \return
  329. Position at which element is inserted
  330. */
  331. proto int Insert(T value);
  332. /*!
  333. Inserts element at certain position and moves all elements behind
  334. this position by one.
  335. \param value
  336. Element to be inserted
  337. \param index
  338. Position at which element is inserted. Must be less than Array::GetCardinality()
  339. \return
  340. Number of elements after insertion
  341. */
  342. proto int InsertAt(T value, int index);
  343. /**
  344. \brief Inserts all elements from array
  345. \param from \p array<T> array from which all elements will be added
  346. @code
  347. TStringArray arr1 = new TStringArray;
  348. arr1.Insert( "Dave" );
  349. arr1.Insert( "Mark" );
  350. arr1.Insert( "John" );
  351. TStringArray arr2 = new TStringArray;
  352. arr2.Insert( "Sarah" );
  353. arr2.Insert( "Cate" );
  354. arr1.InsertAll(arr2);
  355. for ( int i = 0; i < arr1.Count(); i++ )
  356. {
  357. Print( arr1.Get(i) );
  358. }
  359. delete arr2;
  360. delete arr1;
  361. >> "Dave"
  362. >> "Mark"
  363. >> "John"
  364. >> "Sarah"
  365. >> "Cate"
  366. @endcode
  367. */
  368. void InsertAll(notnull array<T> from)
  369. {
  370. for ( int i = 0; i < from.Count(); i++ )
  371. {
  372. Insert( from.Get(i) );
  373. }
  374. }
  375. /*!
  376. Removes element from array. The empty position is replaced by
  377. last element, so removal is quite fast but do not retain order.
  378. \param index
  379. Index of element to be removed
  380. */
  381. proto native void Remove(int index);
  382. /*!
  383. Removes element from array, but retain all elements ordered. It's
  384. slower than Remove
  385. \param index
  386. Index of element to be removed
  387. */
  388. proto native void RemoveOrdered(int index);
  389. /*!
  390. Resizes the array to given size.
  391. If the `newSize` is lower than current Count overflowing objects are destroyed.
  392. If the `newSize` is higher than current Count missing elements are initialized to zero (null).
  393. */
  394. proto native void Resize(int newSize);
  395. /*!
  396. Resizes the array to given size internally.
  397. Is used for optimization purposes when the approx. size is known beforehand
  398. */
  399. proto native void Reserve(int newSize);
  400. /*!
  401. Swaps the contents of this and `other` arrays.
  402. Does not involve copying of the elements.
  403. */
  404. proto native void Swap(notnull array<T> other);
  405. /*!
  406. Sorts elements of array, depends on underlaying type.
  407. */
  408. proto native void Sort(bool reverse = false);
  409. /*!
  410. Copes contents of `from` array to this array.
  411. \return How many elements were copied
  412. */
  413. proto int Copy(notnull array<T> from);
  414. proto int Init(T init[]);
  415. void RemoveItem(T value)
  416. {
  417. int remove_index = Find(value);
  418. if ( remove_index >= 0 )
  419. {
  420. RemoveOrdered(remove_index);
  421. }
  422. }
  423. void RemoveItemUnOrdered(T value)
  424. {
  425. int remove_index = Find(value);
  426. if ( remove_index >= 0 )
  427. {
  428. Remove(remove_index);
  429. }
  430. }
  431. bool IsValidIndex( int index )
  432. {
  433. return ( index > -1 && index < Count() );
  434. }
  435. /*
  436. T GetChecked( int index )
  437. {
  438. if( IsValidIndex( index ) )
  439. return Get( index );
  440. else
  441. return null;
  442. }
  443. */
  444. /**
  445. \brief Print all elements in array
  446. \return \p void
  447. @code
  448. my_array.Debug();
  449. >> "One"
  450. >> "Two"
  451. >> "Three"
  452. @endcode
  453. */
  454. void Debug()
  455. {
  456. Print(string.Format("Array count: %1", Count()));
  457. for (int i = 0; i < Count(); i++)
  458. {
  459. T item = Get(i);
  460. Print(string.Format("[%1] => %2", i, item));
  461. }
  462. }
  463. /**
  464. \brief Returns a random index of array. If Count is 0, return index is -1 .
  465. \return \p int Random index of array
  466. @code
  467. Print( my_array.GetRandomIndex() );
  468. >> 2
  469. @endcode
  470. */
  471. int GetRandomIndex()
  472. {
  473. if ( Count() > 0 )
  474. {
  475. return Math.RandomInt(0, Count());
  476. }
  477. return -1;
  478. }
  479. /**
  480. \brief Returns a random element of array
  481. \return \p int Random element of array
  482. @code
  483. Print( my_array.GetRandomElement() );
  484. >> "Three"
  485. @endcode
  486. */
  487. T GetRandomElement()
  488. {
  489. return Get(GetRandomIndex());
  490. }
  491. void SwapItems(int item1_index, int item2_index)
  492. {
  493. T item1 = Get(item1_index);
  494. Set(item1_index, Get(item2_index));
  495. Set(item2_index, item1);
  496. }
  497. void InsertArray(array<T> other)
  498. {
  499. for (int i = 0; i < other.Count(); i++)
  500. {
  501. T item = other.Get(i);
  502. Insert(item);
  503. }
  504. }
  505. void Invert()
  506. {
  507. int left = 0;
  508. int right = Count() - 1;
  509. if (right > 0)
  510. {
  511. while (left < right)
  512. {
  513. T temp = Get(left);
  514. Set(left++, Get(right));
  515. Set(right--, temp);
  516. }
  517. }
  518. }
  519. /**
  520. \brief Returns a index in array moved by specific number
  521. \return \p int Moved index in this array
  522. @code
  523. Print( "Count: "+ my_array.Count() );
  524. Print( "Moved 1:"+ my_array.MoveIndex(2, 1) );
  525. Print( "Moved 3:"+ my_array.MoveIndex(2, 2) );
  526. >> "Count: 4"
  527. >> "Moved index 2 by 1: 3";
  528. >> "Moved index 2 by 2: 0";
  529. @endcode
  530. */
  531. int MoveIndex(int curr_index, int move_number)
  532. {
  533. int count = Count();
  534. int new_index = curr_index;
  535. if ( move_number > 0 )
  536. {
  537. new_index = curr_index + move_number;
  538. }
  539. if ( move_number < 0 )
  540. {
  541. new_index = curr_index - move_number;
  542. if ( new_index < 0 )
  543. {
  544. if ( new_index <= -count )
  545. {
  546. new_index = (new_index % count);
  547. }
  548. new_index = new_index + count;
  549. }
  550. }
  551. if ( new_index >= count )
  552. {
  553. new_index = (new_index % count);
  554. }
  555. // move_number is 0
  556. return new_index;
  557. }
  558. void ShuffleArray()
  559. {
  560. for (int i = 0; i < Count(); i++)
  561. {
  562. SwapItems(i,GetRandomIndex());
  563. }
  564. }
  565. /**
  566. \brief Returns an index where 2 arrays start to differ from each other
  567. \return \p int Index from where arrays differ
  568. @code
  569. array<int> arr1 = {0,1,2,3};
  570. array<int> arr2 = {0,1,3,2};
  571. int differsAt = arr1.DifferentAtPosition(arr2);
  572. Print(differsAt);
  573. >> 2
  574. @endcode
  575. */
  576. int DifferentAtPosition(array<T> pOtherArray)
  577. {
  578. if (Count() != pOtherArray.Count())
  579. {
  580. ErrorEx("arrays are not the same size");
  581. return -1;
  582. }
  583. for (int i = 0; i < pOtherArray.Count(); ++i)
  584. {
  585. if (Get(i) != pOtherArray.Get(i))
  586. {
  587. return i;
  588. }
  589. }
  590. return -1;
  591. }
  592. };
  593. //force these to compile so we can link C++ methods to them
  594. typedef array<string> TStringArray;
  595. typedef array<float> TFloatArray;
  596. typedef array<int> TIntArray;
  597. typedef array<bool> TBoolArray;
  598. typedef array<Class> TClassArray;
  599. typedef array<Managed> TManagedArray;
  600. typedef array<ref Managed> TManagedRefArray;
  601. typedef array<vector> TVectorArray;
  602. typedef array<typename> TTypenameArray;
  603. class set<Class T>
  604. {
  605. proto native int Count();
  606. proto native void Clear();
  607. /*!
  608. Tries to find the first occurance of given value in the set.
  609. \return Index of the first occurance of `value` if found, -1 otherwise
  610. */
  611. proto int Find(T value);
  612. proto T Get(int n);
  613. /*!
  614. Inserts element at the end of array.
  615. \param value
  616. Element to be inserted
  617. \return
  618. Position at which element is inserted
  619. */
  620. proto int Insert(T value);
  621. /*!
  622. Inserts element at certain position and moves all elements behind
  623. this position by one.
  624. \param value
  625. Element to be inserted
  626. \param index
  627. Position at which element is inserted. Must be less than Array::GetCardinality()
  628. \return
  629. Number of elements after insertion
  630. */
  631. proto int InsertAt(T value, int index);
  632. /*!
  633. Removes element from array, but retain all elements ordered.
  634. \param index
  635. Index of element to be removed
  636. */
  637. proto native void Remove(int index);
  638. proto int Copy(set<T> from);
  639. proto native void Swap(set<T> other);
  640. proto int Init(T init[]);
  641. void InsertSet(set<T> other)
  642. {
  643. int count = other.Count();
  644. for (int i = 0; i < count; i++)
  645. {
  646. T item = other[i];
  647. Insert(item);
  648. }
  649. }
  650. void RemoveItem(T value)
  651. {
  652. int remove_index = Find(value);
  653. if (remove_index >= 0)
  654. {
  655. Remove(remove_index);
  656. }
  657. }
  658. void RemoveItems(set<T> other)
  659. {
  660. int count = other.Count();
  661. for (int i = 0; i < count; i++)
  662. {
  663. T item = other[i];
  664. RemoveItem(item);
  665. }
  666. }
  667. void Debug()
  668. {
  669. Print(string.Format("Set count: %1", Count()));
  670. for (int i = 0; i < Count(); i++)
  671. {
  672. T item = Get(i);
  673. Print(string.Format("[%1] => %2", i, item));
  674. }
  675. }
  676. };
  677. //force these to compile so we can link C++ methods to them
  678. typedef set<string> TStringSet;
  679. typedef set<float> TFloatSet;
  680. typedef set<int> TIntSet;
  681. typedef set<Class> TClassSet;
  682. typedef set<Managed> TManagedSet;
  683. typedef set<ref Managed> TManagedRefSet;
  684. typedef set<typename> TTypenameSet;
  685. typedef int MapIterator;
  686. /**
  687. \brief Associative array template
  688. \n usage:
  689. @code
  690. autoptr map<string, int> prg_count = new map<string, int>;
  691. // fill
  692. project_locations.Insert("dayz", 10);
  693. project_locations.Insert("arma", 20);
  694. project_locations.Insert("tkom", 1);
  695. Print(project_locations.Get("arma")); // prints '20'
  696. @endcode
  697. */
  698. class map<Class TKey,Class TValue>
  699. {
  700. /*!
  701. \return
  702. The number of elements in the hashmap.
  703. */
  704. proto native int Count();
  705. /*!
  706. Clears the hash map.
  707. */
  708. proto native void Clear();
  709. /*!
  710. Search for an element with the given key.
  711. \param key
  712. The key of the element to find
  713. \return
  714. Pointer to element data if found, NULL otherwise.
  715. */
  716. proto TValue Get(TKey key);
  717. /*!
  718. Search for an element with the given key.
  719. \param key
  720. The key of the element to find
  721. \param val
  722. result is stored to val
  723. \return
  724. returns True if given key exist.
  725. */
  726. proto bool Find(TKey key, out TValue val);
  727. /*!
  728. Return the i:th element in the map.
  729. Note: This operation is O(n) complexity. Use with care!
  730. \param index
  731. The position of the element in the map
  732. \return
  733. The element on the i:th position
  734. */
  735. proto TValue GetElement(int index);
  736. /*!
  737. Return the i:th element key in the map.
  738. Note: This operation is O(n) complexity. Use with care!
  739. \param i
  740. The position of the element key in the map
  741. \return
  742. Return key of i-th element
  743. */
  744. proto TKey GetKey(int i);
  745. /*!
  746. Sets value of element with given key. If element with key not exists, it is created.
  747. Note: creating new elements is faster using Insert function.
  748. */
  749. proto void Set(TKey key, TValue value);
  750. /*!
  751. Removes element with given key.
  752. */
  753. proto void Remove(TKey key);
  754. /*!
  755. Removes i:th element with given key.
  756. Note: This operation is O(n) complexity. Use with care!
  757. \param i
  758. The position of the element key in the map
  759. */
  760. proto void RemoveElement(int i);
  761. /*!
  762. Returns if map contains element with given key.
  763. */
  764. proto bool Contains(TKey key);
  765. /*!
  766. Insert new element into hash map.
  767. \param key
  768. Key of element to be inserted.
  769. \param value
  770. Data of element to be inserted.
  771. */
  772. proto bool Insert(TKey key, TValue value);
  773. proto int Copy(map<TKey,TValue> from);
  774. array<TKey> GetKeyArray()
  775. {
  776. array<TKey> keys = new array<TKey>;
  777. for (int i = 0; i < Count(); i++)
  778. {
  779. keys.Insert( GetKey( i ) );
  780. }
  781. return keys;
  782. }
  783. array<TValue> GetValueArray()
  784. {
  785. array<TValue> elements = new array<TValue>;
  786. for (int i = 0; i < Count(); i++)
  787. {
  788. elements.Insert( GetElement( i ) );
  789. }
  790. return elements;
  791. }
  792. bool ReplaceKey(TKey old_key, TKey new_key)
  793. {
  794. if (Contains(old_key))
  795. {
  796. Set(new_key, Get(old_key));
  797. Remove(old_key);
  798. return true;
  799. }
  800. return false;
  801. }
  802. TKey GetKeyByValue(TValue value)
  803. {
  804. TKey ret;
  805. for (int i = 0; i < Count(); i++)
  806. {
  807. if (GetElement(i) == value)
  808. {
  809. ret = GetKey(i);
  810. break;
  811. }
  812. }
  813. return ret;
  814. }
  815. bool GetKeyByValueChecked(TValue value, out TKey key)
  816. {
  817. for (int i = 0; i < Count(); i++)
  818. {
  819. if (GetElement(i) == value)
  820. {
  821. key = GetKey(i);
  822. return true;
  823. }
  824. }
  825. return false;
  826. }
  827. proto native MapIterator Begin();
  828. proto native MapIterator End();
  829. proto native MapIterator Next(MapIterator it);
  830. proto TKey GetIteratorKey(MapIterator it);
  831. proto TValue GetIteratorElement(MapIterator it);
  832. };
  833. typedef map<int, float> TIntFloatMap;
  834. typedef map<int, int> TIntIntMap;
  835. typedef map<int, string> TIntStringMap;
  836. typedef map<int, Class> TIntClassMap;
  837. typedef map<int, Managed> TIntManagedMap;
  838. typedef map<int, ref Managed> TIntManagedRefMap;
  839. typedef map<int, typename> TIntTypenameMap;
  840. typedef map<int, vector> TIntVectorMap;
  841. typedef map<string, float> TStringFloatMap;
  842. typedef map<string, int> TStringIntMap;
  843. typedef map<string, string> TStringStringMap;
  844. typedef map<string, Class> TStringClassMap;
  845. typedef map<string, Managed> TStringManagedMap;
  846. typedef map<string, ref Managed> TStringManagedRefMap;
  847. typedef map<string, typename> TStringTypenameMap;
  848. typedef map<string, vector> TStringVectorMap;
  849. typedef map<Class, float> TClassFloatMap;
  850. typedef map<Class, int> TClassIntMap;
  851. typedef map<Class, string> TClassStringMap;
  852. typedef map<Class, Class> TClassClassMap;
  853. typedef map<Class, Managed> TClassManagedMap;
  854. typedef map<Class, ref Managed> TClassManagedRefMap;
  855. typedef map<Class, typename> TClassTypenameMap;
  856. typedef map<Class, vector> TClassVectorMap;
  857. typedef map<typename, float> TTypeNameFloatMap;
  858. typedef map<typename, int> TTypeNameIntMap;
  859. typedef map<typename, string> TTypeNameStringMap;
  860. typedef map<typename, Class> TTypeNameClassMap;
  861. typedef map<typename, Managed> TTypeNameManagedMap;
  862. typedef map<typename, ref Managed> TTypeNameManagedRefMap;
  863. typedef map<typename, typename> TTypeNameTypenameMap;
  864. typedef map<typename, vector> TTypeNameVectorMap;
  865. typedef map<Managed, float> TManagedFloatMap;
  866. typedef map<Managed, int> TManagedIntMap;
  867. typedef map<Managed, string> TManagedStringMap;
  868. typedef map<Managed, Class> TManagedClassMap;
  869. typedef map<Managed, Managed> TManagedManagedMap;
  870. typedef map<Managed, ref Managed> TManagedManagedRefMap;
  871. typedef map<Managed, typename> TManagedTypenameMap;
  872. typedef map<Managed, vector> TManagedVectorMap;
  873. typedef map<ref Managed, float> TManagedRefFloatMap;
  874. typedef map<ref Managed, int> TManagedRefIntMap;
  875. typedef map<ref Managed, string> TManagedRefStringMap;
  876. typedef map<ref Managed, Class> TManagedRefClassMap;
  877. typedef map<ref Managed, Managed> TManagedRefManagedMap;
  878. typedef map<ref Managed, ref Managed> TManagedRefManagedRefMap;
  879. typedef map<ref Managed, typename> TManagedRefTypenameMap;
  880. typedef map<ref Managed, vector> TManagedRefVectorMap;
  881. //@}