ExNativeHashMap.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System;
  5. using System.Collections.Generic;
  6. using Unity.Collections;
  7. namespace MagicaCloth
  8. {
  9. /// <summary>
  10. /// NativeHashMapの機能拡張版
  11. /// </summary>
  12. /// <typeparam name="TKey"></typeparam>
  13. /// <typeparam name="TValue"></typeparam>
  14. public class ExNativeHashMap<TKey, TValue>
  15. where TKey : struct, IEquatable<TKey>
  16. where TValue : struct
  17. {
  18. NativeHashMap<TKey, TValue> nativeHashMap;
  19. /// <summary>
  20. /// ネイティブリストの配列数
  21. /// ※ジョブでエラーが出ないように事前に確保しておく
  22. /// </summary>
  23. int nativeLength;
  24. /// <summary>
  25. /// 使用キーの記録
  26. /// </summary>
  27. HashSet<TKey> useKeySet = new HashSet<TKey>();
  28. //=========================================================================================
  29. public ExNativeHashMap()
  30. {
  31. nativeHashMap = new NativeHashMap<TKey, TValue>(1, Allocator.Persistent);
  32. nativeLength = NativeCount;
  33. }
  34. public void Dispose()
  35. {
  36. if (nativeHashMap.IsCreated)
  37. {
  38. nativeHashMap.Dispose();
  39. }
  40. }
  41. private int NativeCount
  42. {
  43. get
  44. {
  45. return nativeHashMap.Count();
  46. }
  47. }
  48. //=========================================================================================
  49. /// <summary>
  50. /// データ追加
  51. /// </summary>
  52. /// <param name="key"></param>
  53. /// <param name="value"></param>
  54. public void Add(TKey key, TValue value)
  55. {
  56. if (nativeHashMap.TryAdd(key, value) == false)
  57. {
  58. // すでにデータが存在するため一旦削除して再追加
  59. nativeHashMap.Remove(key);
  60. nativeHashMap.TryAdd(key, value);
  61. }
  62. useKeySet.Add(key);
  63. nativeLength = NativeCount;
  64. }
  65. /// <summary>
  66. /// データ取得
  67. /// </summary>
  68. /// <param name="key"></param>
  69. /// <returns></returns>
  70. public TValue Get(TKey key)
  71. {
  72. TValue data;
  73. nativeHashMap.TryGetValue(key, out data);
  74. return data;
  75. }
  76. /// <summary>
  77. /// 条件判定削除
  78. /// </summary>
  79. /// <param name="func">trueを返せば削除</param>
  80. public void Remove(Func<TKey, TValue, bool> func)
  81. {
  82. List<TKey> removeKey = new List<TKey>();
  83. foreach (TKey key in useKeySet)
  84. {
  85. TValue data;
  86. if (nativeHashMap.TryGetValue(key, out data))
  87. {
  88. // 削除判定
  89. if (func(key, data))
  90. {
  91. // 削除
  92. nativeHashMap.Remove(key);
  93. removeKey.Add(key);
  94. }
  95. }
  96. }
  97. foreach (var key in removeKey)
  98. useKeySet.Remove(key);
  99. nativeLength = NativeCount;
  100. }
  101. /// <summary>
  102. /// データ置き換え
  103. /// </summary>
  104. /// <param name="func">trueを返せば置換</param>
  105. /// <param name="rdata">引数にデータを受け取り、修正したデータを返し置換する</param>
  106. public void Replace(Func<TKey, TValue, bool> func, Func<TValue, TValue> datafunc)
  107. {
  108. foreach (var key in useKeySet)
  109. {
  110. TValue data;
  111. if (nativeHashMap.TryGetValue(key, out data))
  112. {
  113. // 置換判定
  114. if (func(key, data))
  115. {
  116. // 置き換え
  117. var newdata = datafunc(data);
  118. nativeHashMap.Remove(key); // 一旦削除しないと置き換えられない
  119. nativeHashMap.TryAdd(key, newdata);
  120. return;
  121. }
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// キーの削除
  127. /// </summary>
  128. /// <param name="key"></param>
  129. public void Remove(TKey key)
  130. {
  131. nativeHashMap.Remove(key);
  132. nativeLength = 0;
  133. useKeySet.Remove(key);
  134. }
  135. /// <summary>
  136. /// 実際に利用されている要素数を返す
  137. /// </summary>
  138. public int Count
  139. {
  140. get
  141. {
  142. return nativeLength;
  143. }
  144. }
  145. public void Clear()
  146. {
  147. nativeHashMap.Clear();
  148. nativeLength = 0;
  149. useKeySet.Clear();
  150. }
  151. /// <summary>
  152. /// 内部のNativeHashMapを取得する
  153. /// </summary>
  154. /// <returns></returns>
  155. public NativeHashMap<TKey, TValue> Map
  156. {
  157. get
  158. {
  159. return nativeHashMap;
  160. }
  161. }
  162. /// <summary>
  163. /// 使用キーセットを取得する
  164. /// </summary>
  165. public HashSet<TKey> UseKeySet
  166. {
  167. get
  168. {
  169. return useKeySet;
  170. }
  171. }
  172. }
  173. }