/src/lib/storage/low_level/string_recycling_pool.e

http://github.com/tybor/Liberty · Specman e · 157 lines · 106 code · 12 blank · 39 comment · 9 complexity · 49b0e1379abca0a0698258c74393f11c MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class STRING_RECYCLING_POOL
  5. --
  6. -- Remember that STRING_RECYCLING_ITEM is expanded. It explains that some algorithms look like they could
  7. -- be simplified. In fact they '''cannot'''.
  8. --
  9. insert
  10. PLATFORM
  11. export
  12. {STRING_RECYCLING_POOL} all
  13. end
  14. MEMORY
  15. export
  16. {STRING_RECYCLING_POOL} all
  17. end
  18. STRING_HANDLER
  19. export
  20. {STRING_RECYCLING_POOL} all
  21. end
  22. create {ANY}
  23. make
  24. feature {ANY}
  25. new: STRING
  26. -- A brand new STRING with a small default capacity.
  27. do
  28. Result := best_fit(32)
  29. ensure
  30. Result.is_empty
  31. end
  32. best_fit (capacity: INTEGER): STRING
  33. -- A STRING with a capacity at least as great as the given `capacity'.
  34. local
  35. i: INTEGER; gc: BOOLEAN
  36. item: STRING_RECYCLING_ITEM
  37. do
  38. gc := collecting
  39. collection_off
  40. if not strings.is_empty then
  41. item.set_capacity(capacity)
  42. i := items_comparator.insert_index(strings, item)
  43. if strings.valid_index(i) then
  44. from
  45. Result := strings.item(i).item
  46. until
  47. (Result /= Void and then Result.capacity >= capacity) or else i = strings.lower
  48. loop
  49. i := i - 1
  50. Result := strings.item(i).item
  51. end
  52. if Result /= Void then
  53. -- remember STRING_RECYCLING_ITEM is expanded
  54. item := strings.item(i)
  55. item.set_item(Void)
  56. strings.put(item, i)
  57. if Result.capacity < capacity then
  58. -- Rmk, 2015-12-20: wouldn't it be good to have
  59. -- the GC active already here?
  60. Result.ensure_capacity(capacity)
  61. end
  62. end
  63. end
  64. end
  65. if gc then
  66. -- restore the GC ''before'' creating Result (if need be) to let it do its work
  67. collection_on
  68. end
  69. if Result = Void then
  70. create Result.make(capacity)
  71. end
  72. ensure
  73. Result.is_empty
  74. Result.capacity >= capacity
  75. end
  76. new_twin (string: STRING): STRING
  77. -- A copy of the given `string'.
  78. require
  79. string /= Void
  80. do
  81. Result := best_fit(string.count)
  82. Result.copy(string)
  83. ensure
  84. Result /= string
  85. Result.is_equal(string)
  86. end
  87. recycle (string: STRING)
  88. -- Recycles the `string'. Note that you should not use the `string' again ''(don't keep any reference
  89. -- on it)'', but use `new', `best_fit' or `new_twin' to obtain a new STRING.
  90. local
  91. i: INTEGER
  92. item: STRING_RECYCLING_ITEM
  93. do
  94. -- I think there is no need to stop the GC
  95. string.recycle
  96. recycling_item.set_item(string)
  97. if strings.is_empty then
  98. strings.add_last(recycling_item)
  99. recycling_item := item -- a brand new one (because we need to reset the weak reference)
  100. else
  101. i := items_comparator.insert_index(strings, recycling_item)
  102. if strings.valid_index(i) and then strings.item(i).item = Void then
  103. -- remember STRING_RECYCLING_ITEM is expanded
  104. item := strings.item(i)
  105. item.set_item(string)
  106. strings.put(item, i)
  107. recycling_item.set_item(Void) -- will be reused
  108. else
  109. strings.add(item, i)
  110. recycling_item := item -- a brand new one (because we need to reset the weak reference)
  111. end
  112. end
  113. end
  114. feature {}
  115. recycling_item: STRING_RECYCLING_ITEM
  116. strings: FAST_ARRAY[STRING_RECYCLING_ITEM]
  117. -- The recycled strings
  118. make
  119. do
  120. create strings.make(0)
  121. end
  122. items_comparator: STRING_RECYCLING_ITEM_SORTER
  123. invariant
  124. items_comparator.is_sorted(strings)
  125. end -- class STRING_RECYCLING_POOL
  126. --
  127. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  128. --
  129. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  130. -- of this software and associated documentation files (the "Software"), to deal
  131. -- in the Software without restriction, including without limitation the rights
  132. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  133. -- copies of the Software, and to permit persons to whom the Software is
  134. -- furnished to do so, subject to the following conditions:
  135. --
  136. -- The above copyright notice and this permission notice shall be included in
  137. -- all copies or substantial portions of the Software.
  138. --
  139. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  140. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  141. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  142. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  143. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  144. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  145. -- THE SOFTWARE.