README.sparse_array 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. The sparse_array.c file contains an implementation of a sparse array that
  2. attempts to be both space and time efficient.
  3. The sparse array is represented using a tree structure. Each node in the
  4. tree contains a block of pointers to either the user supplied leaf values or
  5. to another node.
  6. There are a number of parameters used to define the block size:
  7. OPENSSL_SA_BLOCK_BITS Specifies the number of bits covered by each block
  8. SA_BLOCK_MAX Specifies the number of pointers in each block
  9. SA_BLOCK_MASK Specifies a bit mask to perform modulo block size
  10. SA_BLOCK_MAX_LEVELS Indicates the maximum possible height of the tree
  11. These constants are inter-related:
  12. SA_BLOCK_MAX = 2 ^ OPENSSL_SA_BLOCK_BITS
  13. SA_BLOCK_MASK = SA_BLOCK_MAX - 1
  14. SA_BLOCK_MAX_LEVELS = number of bits in size_t divided by
  15. OPENSSL_SA_BLOCK_BITS rounded up to the next multiple
  16. of OPENSSL_SA_BLOCK_BITS
  17. OPENSSL_SA_BLOCK_BITS can be defined at compile time and this overrides the
  18. built in setting.
  19. As a space and performance optimisation, the height of the tree is usually
  20. less than the maximum possible height. Only sufficient height is allocated to
  21. accommodate the largest index added to the data structure.
  22. The largest index used to add a value to the array determines the tree height:
  23. +----------------------+---------------------+
  24. | Largest Added Index | Height of Tree |
  25. +----------------------+---------------------+
  26. | SA_BLOCK_MAX - 1 | 1 |
  27. | SA_BLOCK_MAX ^ 2 - 1 | 2 |
  28. | SA_BLOCK_MAX ^ 3 - 1 | 3 |
  29. | ... | ... |
  30. | size_t max | SA_BLOCK_MAX_LEVELS |
  31. +----------------------+---------------------+
  32. The tree height is dynamically increased as needed based on additions.
  33. An empty tree is represented by a NULL root pointer. Inserting a value at
  34. index 0 results in the allocation of a top level node full of null pointers
  35. except for the single pointer to the user's data (N = SA_BLOCK_MAX for
  36. brevity):
  37. +----+
  38. |Root|
  39. |Node|
  40. +-+--+
  41. |
  42. |
  43. |
  44. v
  45. +-+-+---+---+---+---+
  46. | 0 | 1 | 2 |...|N-1|
  47. | |nil|nil|...|nil|
  48. +-+-+---+---+---+---+
  49. |
  50. |
  51. |
  52. v
  53. +-+--+
  54. |User|
  55. |Data|
  56. +----+
  57. Index 0
  58. Inserting at element 2N+1 creates a new root node and pushes down the old root
  59. node. It then creates a second second level node to hold the pointer to the
  60. user's new data:
  61. +----+
  62. |Root|
  63. |Node|
  64. +-+--+
  65. |
  66. |
  67. |
  68. v
  69. +-+-+---+---+---+---+
  70. | 0 | 1 | 2 |...|N-1|
  71. | |nil| |...|nil|
  72. +-+-+---+-+-+---+---+
  73. | |
  74. | +------------------+
  75. | |
  76. v v
  77. +-+-+---+---+---+---+ +-+-+---+---+---+---+
  78. | 0 | 1 | 2 |...|N-1| | 0 | 1 | 2 |...|N-1|
  79. |nil| |nil|...|nil| |nil| |nil|...|nil|
  80. +-+-+---+---+---+---+ +---+-+-+---+---+---+
  81. | |
  82. | |
  83. | |
  84. v v
  85. +-+--+ +-+--+
  86. |User| |User|
  87. |Data| |Data|
  88. +----+ +----+
  89. Index 0 Index 2N+1
  90. The nodes themselves are allocated in a sparse manner. Only nodes which exist
  91. along a path from the root of the tree to an added leaf will be allocated.
  92. The complexity is hidden and nodes are allocated on an as needed basis.
  93. Because the data is expected to be sparse this doesn't result in a large waste
  94. of space.
  95. Values can be removed from the sparse array by setting their index position to
  96. NULL. The data structure does not attempt to reclaim nodes or reduce the
  97. height of the tree on removal. For example, now setting index 0 to NULL would
  98. result in:
  99. +----+
  100. |Root|
  101. |Node|
  102. +-+--+
  103. |
  104. |
  105. |
  106. v
  107. +-+-+---+---+---+---+
  108. | 0 | 1 | 2 |...|N-1|
  109. | |nil| |...|nil|
  110. +-+-+---+-+-+---+---+
  111. | |
  112. | +------------------+
  113. | |
  114. v v
  115. +-+-+---+---+---+---+ +-+-+---+---+---+---+
  116. | 0 | 1 | 2 |...|N-1| | 0 | 1 | 2 |...|N-1|
  117. |nil|nil|nil|...|nil| |nil| |nil|...|nil|
  118. +---+---+---+---+---+ +---+-+-+---+---+---+
  119. |
  120. |
  121. |
  122. v
  123. +-+--+
  124. |User|
  125. |Data|
  126. +----+
  127. Index 2N+1
  128. Accesses to elements in the sparse array take O(log n) time where n is the
  129. largest element. The base of the logarithm is SA_BLOCK_MAX, so for moderately
  130. small indices (e.g. NIDs), single level (constant time) access is achievable.
  131. Space usage is O(minimum(m, n log(n)) where m is the number of elements in the
  132. array.
  133. Note: sparse arrays only include pointers to types. Thus, SPARSE_ARRAY_OF(char)
  134. can be used to store a string.