map.tex 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. \chapter{TMap}
  2. Implements container for ordered associative array with unique keys.
  3. Takes 3 arguments for specialization, first one is type of keys, second one is type of values, third
  4. one is comparator class for keys.
  5. Usage example:
  6. \lstinputlisting[language=Pascal]{mapexample.pp}
  7. Memory complexity:
  8. Size of stored base + constant overhead for each stored element (3 pointers + one boolean).
  9. Members list:
  10. \begin{longtable}{|m{10cm}|m{5cm}|}
  11. \hline
  12. Method & Complexity guarantees \\ \hline
  13. \multicolumn{2}{|m{15cm}|}{Description} \\ \hline\hline
  14. \verb!Create! & O(1) \\ \hline
  15. \multicolumn{2}{|m{15cm}|}{Constructor. Creates empty map.} \\ \hline\hline
  16. \verb!function Size(): SizeUInt! & O(1) \\ \hline
  17. \multicolumn{2}{|m{15cm}|}{Returns number of elements in map.} \\\hline\hline
  18. \verb!procedure Insert(key: TKey; value: TValue)! &
  19. O(lg N), N is number of elements in map \\ \hline
  20. \multicolumn{2}{|m{15cm}|}{Inserts key value pair into map. If key was already there, it will have
  21. new value assigned.} \\\hline\hline
  22. \verb!function InsertAndGetIterator! & $O(\lg N)$\\
  23. \verb!(key:TKey; value: TValue):TIterator! & \\ \hline
  24. \multicolumn{2}{|m{15cm}|}{Same as Insert but also returns iterator poiting to inserted element.} \\\hline\hline
  25. \verb!procedure Delete(key: TKey)! &
  26. O(lg N) \\ \hline
  27. \multicolumn{2}{|m{15cm}|}{Deletes key (and associated value) from map. If element is not in map, nothing happens.} \\\hline\hline
  28. \verb!function Find(key: T):TIterator! & O(lg N) \\\hline
  29. \multicolumn{2}{|m{15cm}|}{Searches for key in map. If value is not there returns nil. Otherwise
  30. returns iterator, which can be used for retrieving data from map.} \\\hline\hline
  31. \verb!function FindLess(key: T):TIterator! & O(lg N) \\\hline
  32. \multicolumn{2}{|m{15cm}|}{Searches for greatest element less than key in map. If such element is not there returns nil. Otherwise
  33. returns iterator, which can be used for retrieving data from map.} \\\hline\hline
  34. \verb!function FindLessEqual(key: T):TIterator! & O(lg N) \\\hline
  35. \multicolumn{2}{|m{15cm}|}{Searches for greatest element less or equal than key in map. If such element is not there returns nil. Otherwise
  36. returns iterator, which can be used for retrieving data from map.} \\\hline\hline
  37. \verb!function FindGreater(key: T):TIterator! & O(lg N) \\\hline
  38. \multicolumn{2}{|m{15cm}|}{Searches for smallest element greater than key in map. If such element is not there returns nil. Otherwise
  39. returns iterator, which can be used for retrieving data from map.} \\\hline\hline
  40. \verb!function FindGreaterEqual(key: T):TIterator! & O(lg N) \\\hline
  41. \multicolumn{2}{|m{15cm}|}{Searches for smallest element greater or equal than key in map. If such element is not there returns nil. Otherwise
  42. returns iterator, which can be used for retrieving data from map.} \\\hline\hline
  43. \verb!function Min:TIterator! & O(lg N) \\\hline
  44. \multicolumn{2}{|m{15cm}|}{Returns iterator pointing to smallest key of map. If map is empty returns
  45. nil.} \\\hline\hline
  46. \verb!function Max:TIterator! & O(lg N) \\\hline
  47. \multicolumn{2}{|m{15cm}|}{Returns iterator pointing to largest key of map. If map is empty returns
  48. nil.} \\\hline\hline
  49. \verb!function IsEmpty(): boolean! & O(1) \\ \hline
  50. \multicolumn{2}{|m{15cm}|}{Returns true when map is empty.} \\\hline
  51. \verb!property item[i: Key]: TValue; default;! & O(ln N) \\\hline
  52. \multicolumn{2}{|m{15cm}|}{Property for accessing key i in map. Can be used just by square
  53. brackets (its default property).} \\\hline\hline
  54. \end{longtable}
  55. Some methods return type TIterator, which has following methods:
  56. \begin{longtable}{|m{10cm}|m{5cm}|}
  57. \hline
  58. Method & Complexity guarantees \\ \hline
  59. \multicolumn{2}{|m{15cm}|}{Description} \\ \hline\hline
  60. \verb!function Next:boolean! & O(lg N) worst case, but traversal from smallest element to
  61. largest takes O(N) time \\\hline
  62. \multicolumn{2}{|m{15cm}|}{Moves iterator to element with smallest larger key. Returns true on
  63. success. If the iterator is already pointing on element with largest key returns false.} \\\hline\hline
  64. \verb!function Prev:boolean! & O(lg N) worst case, but traversal from largest element to
  65. smallest takes O(N) time \\\hline
  66. \multicolumn{2}{|m{15cm}|}{Moves iterator to element with largest smaller key. Returns true on
  67. success. If the iterator is already pointing on element with smallest key returns false.} \\\hline\hline
  68. \verb!property Key:TKey! & $O(1)$ \\\hline
  69. \multicolumn{2}{|m{15cm}|}{Property, which allows reading the key.} \\\hline
  70. \verb!property Value:TValue! & $O(1)$ \\\hline
  71. \multicolumn{2}{|m{15cm}|}{Property, which allows reading and writing of the value.} \\\hline
  72. \verb!property MutableValue:PValue! & $O(1)$ \\\hline
  73. \multicolumn{2}{|m{15cm}|}{Returns pointer on stored value. Usefull for accessing records and
  74. objects.} \\\hline
  75. \end{longtable}