map.tex 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. Some methods return type TMSet.PNode. Usefull fields are Data.Key, Data.Value, for retrieving
  8. actual Key and Value from node. This node can be also used for navigation between elements by methods of set class.
  9. You can also change value in node (but not key).
  10. (But don't do anything else with it, you can lose data integrity.)
  11. Memory complexity:
  12. Size of stored base + constant overhead for each stored element (3 pointers + one boolean).
  13. Members list:
  14. \begin{longtable}{|m{10cm}|m{5cm}|}
  15. \hline
  16. Method & Complexity guarantees \\ \hline
  17. \multicolumn{2}{|m{15cm}|}{Description} \\ \hline\hline
  18. \verb!Create! & O(1) \\ \hline
  19. \multicolumn{2}{|m{15cm}|}{Constructor. Creates empty map.} \\ \hline\hline
  20. \verb!function Size(): SizeUInt! & O(1) \\ \hline
  21. \multicolumn{2}{|m{15cm}|}{Returns number of elements in map.} \\\hline\hline
  22. \verb!procedure Insert(key: TKey; value: TValue)! &
  23. O(lg N), N is number of elements in map \\ \hline
  24. \multicolumn{2}{|m{15cm}|}{Inserts key value pair into map. If key was already there, it will have
  25. new value assigned.} \\\hline\hline
  26. \verb!procedure Delete(key: TKey)! &
  27. O(lg N) \\ \hline
  28. \multicolumn{2}{|m{15cm}|}{Deletes key (and associated value) from map. If element is not in map, nothing happens.} \\\hline\hline
  29. \verb!function Find(key: T):TMSet.PNode! & O(lg N) \\\hline
  30. \multicolumn{2}{|m{15cm}|}{Searches for key in map. If value is not there returns nil. Otherwise
  31. returns pointer to tree node (type TMSet.PNode), which can be used for retrieving data from map.} \\\hline\hline
  32. \verb!function FindLess(key: T):TMSet.PNode! & O(lg N) \\\hline
  33. \multicolumn{2}{|m{15cm}|}{Searches for greatest element less than key in map. If such element is not there returns nil. Otherwise
  34. returns pointer to tree node (type TMSet.PNode), which can be used for retrieving data from map.} \\\hline\hline
  35. \verb!function FindLessEqual(key: T):TMSet.PNode! & O(lg N) \\\hline
  36. \multicolumn{2}{|m{15cm}|}{Searches for greatest element less or equal than key in map. If such element is not there returns nil. Otherwise
  37. returns pointer to tree node (type TMSet.PNode), which can be used for retrieving data from map.} \\\hline\hline
  38. \verb!function FindGreater(key: T):TMSet.PNode! & O(lg N) \\\hline
  39. \multicolumn{2}{|m{15cm}|}{Searches for smallest element greater than key in map. If such element is not there returns nil. Otherwise
  40. returns pointer to tree node (type TMSet.PNode), which can be used for retrieving data from map.} \\\hline\hline
  41. \verb!function FindGreaterEqual(key: T):TMSet.PNode! & O(lg N) \\\hline
  42. \multicolumn{2}{|m{15cm}|}{Searches for smallest element greater or equal than key in map. If such element is not there returns nil. Otherwise
  43. returns pointer to tree node (type TMSet.PNode), which can be used for retrieving data from map.} \\\hline\hline
  44. \verb!function Min:TMSet.PNode! & O(lg N) \\\hline
  45. \multicolumn{2}{|m{15cm}|}{Returns node containing smallest key of map. If map is empty returns
  46. nil.} \\\hline\hline
  47. \verb!function Max:TMSet.PNode! & O(lg N) \\\hline
  48. \multicolumn{2}{|m{15cm}|}{Returns node containing largest key of map. If map is empty returns
  49. nil.} \\\hline\hline
  50. \verb!function Next(x:TMSet.PNode):TMSet.PNode! & O(lg N) worst case, but traversal from smallest element to
  51. largest takes O(N) time \\\hline
  52. \multicolumn{2}{|m{15cm}|}{Returns successor of x. If x is largest key of map, returns nil.} \\\hline\hline
  53. \verb!function Prev(x:TMSet.PNode):TMSet.PNode! & O(lg N) worst case, but traversal from largest element to
  54. smallest takes O(N) time \\\hline
  55. \multicolumn{2}{|m{15cm}|}{Returns predecessor of x. If x is smallest key of map, returns nil.} \\\hline\hline
  56. \verb!function IsEmpty(): boolean! & O(1) \\ \hline
  57. \multicolumn{2}{|m{15cm}|}{Returns true when map is empty.} \\\hline
  58. \verb!function GetValue(key:TKey):TValue! & O(lg N) \\\hline
  59. \multicolumn{2}{|m{15cm}|}{Returns value associated with key. Is key isn't in map crashes.} \\\hline
  60. \verb!property item[i: Key]: TValue; default;! & O(ln N) \\\hline
  61. \multicolumn{2}{|m{15cm}|}{Property for accessing key i in map. Can be used just by square
  62. brackets (its default property).} \\\hline\hline
  63. \end{longtable}