PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/env/Lib/site-packages/wx-2.8-msw-unicode/wx/tools/Editra/src/ebmlib/histcache.py

https://bitbucket.org/beqa/nvdadependencyvirtualenvironment
Python | 262 lines | 120 code | 33 blank | 109 comment | 28 complexity | f6a378bd389ee13bc46ddf91b2dc0b4c MD5 | raw file
  1. ###############################################################################
  2. # Name: histcache.py #
  3. # Purpose: History Cache #
  4. # Author: Cody Precord <cprecord@editra.org> #
  5. # Copyright: (c) 2009 Cody Precord <staff@editra.org> #
  6. # Licence: wxWindows Licence #
  7. ###############################################################################
  8. """
  9. Editra Buisness Model Library: HistoryCache
  10. History cache that acts as a stack for managing a history list o
  11. """
  12. __author__ = "Cody Precord <cprecord@editra.org>"
  13. __cvsid__ = "$Id: histcache.py 67123 2011-03-04 00:02:35Z CJP $"
  14. __revision__ = "$Revision: 67123 $"
  15. __all__ = [ 'HistoryCache', 'HIST_CACHE_UNLIMITED',
  16. 'CycleCache']
  17. #-----------------------------------------------------------------------------#
  18. # Imports
  19. #-----------------------------------------------------------------------------#
  20. # Globals
  21. HIST_CACHE_UNLIMITED = -1
  22. #-----------------------------------------------------------------------------#
  23. class HistoryCache(object):
  24. """Data management cache.
  25. Maintains a positional list of objects that remembers the last access
  26. position in the cache.
  27. """
  28. def __init__(self, max_size=HIST_CACHE_UNLIMITED):
  29. """@param max_size: size of history cache (int)"""
  30. super(HistoryCache, self).__init__()
  31. # Attributes
  32. self._list = list()
  33. self.cpos = -1
  34. self.max_size = max_size
  35. def _Resize(self):
  36. """Adjust cache size based on max size setting"""
  37. if self.max_size != HIST_CACHE_UNLIMITED:
  38. lsize = len(self._list)
  39. if lsize:
  40. adj = self.max_size - lsize
  41. if adj < 0:
  42. self._list.pop(0)
  43. self.cpos = len(self._list) - 1
  44. def Clear(self):
  45. """Clear the history cache"""
  46. del self._list
  47. self._list = list()
  48. self.cpos = -1
  49. def GetSize(self):
  50. """Get the current size of the cache
  51. @return: int (number of items in the cache)
  52. """
  53. return len(self._list)
  54. def GetMaxSize(self):
  55. """Get the max size of the cache
  56. @return: int
  57. """
  58. return self.max_size
  59. def GetNextItem(self):
  60. """Get the next item in the history cache, moving the
  61. current position towards the end of the cache.
  62. @return: object or None if at end of list
  63. """
  64. item = None
  65. if self.cpos < len(self._list) - 1:
  66. self.cpos += 1
  67. item = self._list[self.cpos]
  68. return item
  69. def GetPreviousItem(self):
  70. """Get the previous item in the history cache, moving the
  71. current position towards the beginning of the cache.
  72. @return: object or None if at start of list
  73. """
  74. item = None
  75. if self.cpos >= 0 and len(self._list) > 0:
  76. if self.cpos == len(self._list):
  77. self.cpos -= 1
  78. item = self._list[self.cpos]
  79. self.cpos -= 1
  80. return item
  81. def HasPrevious(self):
  82. """Are there more items to the left of the current position
  83. @return: bool
  84. """
  85. llen = len(self._list)
  86. more = ((self.cpos >= 0) and llen and (self.cpos < llen))
  87. return more
  88. def HasNext(self):
  89. """Are there more items to the right of the current position
  90. @return: bool
  91. """
  92. if self.cpos == -1 and len(self._list):
  93. more = True
  94. else:
  95. more = self.cpos >= 0 and self.cpos < (len(self._list) - 1)
  96. return more
  97. def PeekNext(self):
  98. """Return the next item in the cache without modifying the
  99. currently managed position.
  100. @return: cache object
  101. """
  102. if self.HasNext():
  103. return self._list[self.cpos+1]
  104. else:
  105. return None
  106. def PeekPrevious(self):
  107. """Return the previous item in the cache without modifying the
  108. currently managed position.
  109. @return: cache object
  110. """
  111. if self.HasPrevious():
  112. return self._list[self.cpos]
  113. else:
  114. return None
  115. def PutItem(self, item):
  116. """Put an item on the top of the cache
  117. @param item: object
  118. """
  119. if self.cpos != len(self._list) - 1:
  120. self._list = self._list[:self.cpos]
  121. self._list.append(item)
  122. self.cpos += 1
  123. self._Resize()
  124. def SetMaxSize(self, max_size):
  125. """Set the maximum size of the cache
  126. @param max_size: int (HIST_CACHE_UNLIMITED for unlimited size)
  127. """
  128. assert max_size > 0 or max_size == 1, "Invalid max size"
  129. self.max_size = max_size
  130. self._Resize()
  131. #-----------------------------------------------------------------------------#
  132. class CycleCache(object):
  133. """A simple circular cache. All items are added to the end of the cache
  134. regardless of the current reference position. As items are accessed from
  135. the cache the cache reference pointer is incremented, if it passes the
  136. end it will go back to the beginning.
  137. """
  138. def __init__(self, size):
  139. """Initialize the cache.
  140. @param size: cache size
  141. """
  142. super(CycleCache, self).__init__()
  143. # Attributes
  144. self._list = list()
  145. self._cpos = -1
  146. self._size = size
  147. def __len__(self):
  148. return len(self._list)
  149. def NextIndex(self):
  150. """Get the next index in the cache
  151. @return: int
  152. """
  153. idx = self._cpos
  154. idx -= 1
  155. if abs(idx) > len(self._list):
  156. idx = -1
  157. return idx
  158. def Clear(self):
  159. """Clear the cache"""
  160. del self._list
  161. self._list = list()
  162. def GetCurrentSize(self):
  163. """Get the size of the cache
  164. @return: int
  165. """
  166. return len(self._list)
  167. def GetNext(self):
  168. """Get the next item in the cache and increment the
  169. current position.
  170. @return: object
  171. """
  172. item = None
  173. if len(self._list):
  174. item = self._list[self._cpos]
  175. self._cpos = self.NextIndex()
  176. return item
  177. def PeekNext(self):
  178. """Look the next item in the cache
  179. @return: object
  180. """
  181. item = None
  182. if abs(self._cpos) < len(self._list):
  183. item = self._list[self._cpos]
  184. return item
  185. def PeekPrev(self):
  186. """Look the next item in the cache
  187. @return: object
  188. """
  189. idx = self._cpos + 1
  190. if idx == 0:
  191. idx = -1 * len(self._list)
  192. llen = len(self._list)
  193. if llen and abs(idx) <= llen:
  194. item = self._list[idx]
  195. else:
  196. item = None
  197. return item
  198. def PutItem(self, item):
  199. """Put an item in the cache
  200. @param item: object
  201. """
  202. llen = len(self._list)
  203. if llen and (llen == self._size):
  204. del self._list[0]
  205. self._list.append(item)
  206. def Reset(self):
  207. """Reset the list reference pointer"""
  208. self._cpos = -1