/src/yolk-cache-discrete_keys.adb

http://github.com/ThomasLocke/yolk · Ada · 306 lines · 174 code · 51 blank · 81 comment · 3 complexity · 407b4b4db231b45005b0ab7693c8422c MD5 · raw file

  1. -------------------------------------------------------------------------------
  2. -- --
  3. -- Copyright (C) 2010-, Thomas ¸cke --
  4. -- --
  5. -- This library is free software; you can redistribute it and/or modify --
  6. -- it under terms of the GNU General Public License as published by the --
  7. -- Free Software Foundation; either version 3, or (at your option) any --
  8. -- later version. This library is distributed in the hope that it will be --
  9. -- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of --
  10. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
  11. -- --
  12. -- As a special exception under Section 7 of GPL version 3, you are --
  13. -- granted additional permissions described in the GCC Runtime Library --
  14. -- Exception, version 3.1, as published by the Free Software Foundation. --
  15. -- --
  16. -- You should have received a copy of the GNU General Public License and --
  17. -- a copy of the GCC Runtime Library Exception along with this program; --
  18. -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
  19. -- <http://www.gnu.org/licenses/>. --
  20. -- --
  21. -------------------------------------------------------------------------------
  22. with Ada.Calendar;
  23. with Ada.Containers.Hashed_Maps;
  24. package body Yolk.Cache.Discrete_Keys is
  25. use Ada.Containers;
  26. type Element_Container is
  27. record
  28. Added_Timestamp : Ada.Calendar.Time;
  29. Element : Element_Type;
  30. end record;
  31. function Equivalent_Keys
  32. (Left : in Key_Type;
  33. Right : in Key_Type)
  34. return Boolean;
  35. -- Used by the Element_Map to determine equivalence between values.
  36. function Key_Hash
  37. (Key : in Key_Type)
  38. return Hash_Type;
  39. -- Used by Element_Map to hash keys.
  40. package Element_Map is new Hashed_Maps
  41. (Key_Type => Key_Type,
  42. Element_Type => Element_Container,
  43. Hash => Key_Hash,
  44. Equivalent_Keys => Equivalent_Keys);
  45. Null_Container : Element_Container;
  46. pragma Unmodified (Null_Container);
  47. protected P_Element_List is
  48. procedure Cleanup;
  49. -- ????
  50. procedure Clear;
  51. -- ????
  52. procedure Clear
  53. (Key : in Key_Type);
  54. -- ????
  55. function Is_Valid
  56. (Key : in Key_Type)
  57. return Boolean;
  58. -- ????
  59. function Length
  60. return Natural;
  61. -- ????
  62. procedure Read
  63. (Key : in Key_Type;
  64. Valid : out Boolean;
  65. Value : out Element_Type);
  66. -- ????
  67. procedure Write
  68. (Key : in Key_Type;
  69. Value : in Element_Type);
  70. -- ????
  71. private
  72. Element_List : Element_Map.Map;
  73. end P_Element_List;
  74. ----------------------
  75. -- P_Element_List --
  76. ----------------------
  77. protected body P_Element_List is
  78. ---------------
  79. -- Cleanup --
  80. ---------------
  81. procedure Cleanup
  82. is
  83. use Ada.Calendar;
  84. use Element_Map;
  85. Cursor : Element_Map.Cursor := Element_List.First;
  86. Now : constant Time := Clock;
  87. begin
  88. while Has_Element (Cursor) loop
  89. if (Now - Element (Cursor).Added_Timestamp) >= Max_Element_Age then
  90. Element_List.Delete (Position => Cursor);
  91. end if;
  92. Next (Cursor);
  93. end loop;
  94. end Cleanup;
  95. -------------
  96. -- Clear --
  97. -------------
  98. procedure Clear
  99. is
  100. begin
  101. Element_List.Clear;
  102. end Clear;
  103. -------------
  104. -- Clear --
  105. -------------
  106. procedure Clear
  107. (Key : in Key_Type)
  108. is
  109. begin
  110. Element_List.Exclude (Key => Key);
  111. end Clear;
  112. ----------------
  113. -- Is_Valid - -
  114. ----------------
  115. function Is_Valid
  116. (Key : in Key_Type)
  117. return Boolean
  118. is
  119. use Ada.Calendar;
  120. begin
  121. return (Element_List.Contains (Key => Key)) and then
  122. (Clock - Element_List.Element (Key => Key).Added_Timestamp <
  123. Max_Element_Age);
  124. end Is_Valid;
  125. --------------
  126. -- Length --
  127. --------------
  128. function Length
  129. return Natural
  130. is
  131. begin
  132. return Natural (Element_List.Length);
  133. end Length;
  134. ------------
  135. -- Read --
  136. ------------
  137. procedure Read
  138. (Key : in Key_Type;
  139. Valid : out Boolean;
  140. Value : out Element_Type)
  141. is
  142. use Ada.Calendar;
  143. begin
  144. Valid := Is_Valid (Key => Key);
  145. if Valid then
  146. Value := Element_List.Element (Key => Key).Element;
  147. else
  148. Clear (Key => Key);
  149. Value := Null_Container.Element;
  150. end if;
  151. end Read;
  152. -------------
  153. -- Write --
  154. -------------
  155. procedure Write
  156. (Key : in Key_Type;
  157. Value : in Element_Type)
  158. is
  159. begin
  160. Element_List.Include
  161. (Key => Key,
  162. New_Item => (Added_Timestamp => Ada.Calendar.Clock,
  163. Element => Value));
  164. end Write;
  165. end P_Element_List;
  166. ---------------
  167. -- Cleanup --
  168. ---------------
  169. procedure Cleanup
  170. is
  171. begin
  172. P_Element_List.Cleanup;
  173. end Cleanup;
  174. -------------
  175. -- Clear --
  176. -------------
  177. procedure Clear
  178. is
  179. begin
  180. P_Element_List.Clear;
  181. end Clear;
  182. -------------
  183. -- Clear --
  184. -------------
  185. procedure Clear
  186. (Key : in Key_Type)
  187. is
  188. begin
  189. P_Element_List.Clear (Key => Key);
  190. end Clear;
  191. -----------------------
  192. -- Equivalent_Keys --
  193. -----------------------
  194. function Equivalent_Keys
  195. (Left : in Key_Type;
  196. Right : in Key_Type)
  197. return Boolean
  198. is
  199. begin
  200. return Left = Right;
  201. end Equivalent_Keys;
  202. ----------------
  203. -- Is_Valid --
  204. ----------------
  205. function Is_Valid
  206. (Key : in Key_Type)
  207. return Boolean
  208. is
  209. begin
  210. return P_Element_List.Is_Valid (Key => Key);
  211. end Is_Valid;
  212. ----------------
  213. -- Key_Hash --
  214. ----------------
  215. function Key_Hash
  216. (Key : in Key_Type)
  217. return Hash_Type
  218. is
  219. begin
  220. return Hash_Type (Key_Type'Pos (Key));
  221. end Key_Hash;
  222. --------------
  223. -- Length --
  224. --------------
  225. function Length
  226. return Natural
  227. is
  228. begin
  229. return P_Element_List.Length;
  230. end Length;
  231. ------------
  232. -- Read --
  233. ------------
  234. procedure Read
  235. (Key : in Key_Type;
  236. Is_Valid : out Boolean;
  237. Value : out Element_Type)
  238. is
  239. begin
  240. P_Element_List.Read (Key => Key,
  241. Valid => Is_Valid,
  242. Value => Value);
  243. end Read;
  244. -------------
  245. -- Write --
  246. -------------
  247. procedure Write
  248. (Key : in Key_Type;
  249. Value : in Element_Type)
  250. is
  251. begin
  252. P_Element_List.Write (Key => Key,
  253. Value => Value);
  254. end Write;
  255. end Yolk.Cache.Discrete_Keys;