AvaloniaPropertyValueStore.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. #nullable enable
  5. namespace Avalonia.Utilities
  6. {
  7. /// <summary>
  8. /// Stores values with <see cref="AvaloniaProperty"/> as key.
  9. /// </summary>
  10. /// <typeparam name="TValue">Stored value type.</typeparam>
  11. internal sealed class AvaloniaPropertyValueStore<TValue>
  12. {
  13. // The last item in the list is always int.MaxValue.
  14. private static readonly Entry[] s_emptyEntries = { new Entry { PropertyId = int.MaxValue, Value = default! } };
  15. private Entry[] _entries;
  16. public AvaloniaPropertyValueStore()
  17. {
  18. _entries = s_emptyEntries;
  19. }
  20. public int Count => _entries.Length - 1;
  21. public TValue this[int index] => _entries[index].Value;
  22. private (int, bool) TryFindEntry(int propertyId)
  23. {
  24. if (_entries.Length <= 12)
  25. {
  26. // For small lists, we use an optimized linear search. Since the last item in the list
  27. // is always int.MaxValue, we can skip a conditional branch in each iteration.
  28. // By unrolling the loop, we can skip another unconditional branch in each iteration.
  29. if (_entries[0].PropertyId >= propertyId)
  30. return (0, _entries[0].PropertyId == propertyId);
  31. if (_entries[1].PropertyId >= propertyId)
  32. return (1, _entries[1].PropertyId == propertyId);
  33. if (_entries[2].PropertyId >= propertyId)
  34. return (2, _entries[2].PropertyId == propertyId);
  35. if (_entries[3].PropertyId >= propertyId)
  36. return (3, _entries[3].PropertyId == propertyId);
  37. if (_entries[4].PropertyId >= propertyId)
  38. return (4, _entries[4].PropertyId == propertyId);
  39. if (_entries[5].PropertyId >= propertyId)
  40. return (5, _entries[5].PropertyId == propertyId);
  41. if (_entries[6].PropertyId >= propertyId)
  42. return (6, _entries[6].PropertyId == propertyId);
  43. if (_entries[7].PropertyId >= propertyId)
  44. return (7, _entries[7].PropertyId == propertyId);
  45. if (_entries[8].PropertyId >= propertyId)
  46. return (8, _entries[8].PropertyId == propertyId);
  47. if (_entries[9].PropertyId >= propertyId)
  48. return (9, _entries[9].PropertyId == propertyId);
  49. if (_entries[10].PropertyId >= propertyId)
  50. return (10, _entries[10].PropertyId == propertyId);
  51. }
  52. else
  53. {
  54. int low = 0;
  55. int high = _entries.Length;
  56. int id;
  57. while (high - low > 3)
  58. {
  59. int pivot = (high + low) / 2;
  60. id = _entries[pivot].PropertyId;
  61. if (propertyId == id)
  62. return (pivot, true);
  63. if (propertyId <= id)
  64. high = pivot;
  65. else
  66. low = pivot + 1;
  67. }
  68. do
  69. {
  70. id = _entries[low].PropertyId;
  71. if (id == propertyId)
  72. return (low, true);
  73. if (id > propertyId)
  74. break;
  75. ++low;
  76. }
  77. while (low < high);
  78. }
  79. return (0, false);
  80. }
  81. public bool TryGetValue(AvaloniaProperty property, [MaybeNull] out TValue value)
  82. {
  83. (int index, bool found) = TryFindEntry(property.Id);
  84. if (!found)
  85. {
  86. value = default;
  87. return false;
  88. }
  89. value = _entries[index].Value;
  90. return true;
  91. }
  92. public void AddValue(AvaloniaProperty property, TValue value)
  93. {
  94. Entry[] entries = new Entry[_entries.Length + 1];
  95. for (int i = 0; i < _entries.Length; ++i)
  96. {
  97. if (_entries[i].PropertyId > property.Id)
  98. {
  99. if (i > 0)
  100. {
  101. Array.Copy(_entries, 0, entries, 0, i);
  102. }
  103. entries[i] = new Entry { PropertyId = property.Id, Value = value };
  104. Array.Copy(_entries, i, entries, i + 1, _entries.Length - i);
  105. break;
  106. }
  107. }
  108. _entries = entries;
  109. }
  110. public void SetValue(AvaloniaProperty property, TValue value)
  111. {
  112. _entries[TryFindEntry(property.Id).Item1].Value = value;
  113. }
  114. public void Remove(AvaloniaProperty property)
  115. {
  116. var (index, found) = TryFindEntry(property.Id);
  117. if (found)
  118. {
  119. var newLength = _entries.Length - 1;
  120. // Special case - one element left means that value store is empty so we can just reuse our "empty" array.
  121. if (newLength == 1)
  122. {
  123. _entries = s_emptyEntries;
  124. return;
  125. }
  126. var entries = new Entry[newLength];
  127. int ix = 0;
  128. for (int i = 0; i < _entries.Length; ++i)
  129. {
  130. if (i != index)
  131. {
  132. entries[ix++] = _entries[i];
  133. }
  134. }
  135. _entries = entries;
  136. }
  137. }
  138. private struct Entry
  139. {
  140. internal int PropertyId;
  141. internal TValue Value;
  142. }
  143. }
  144. }