123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- // Magica Cloth.
- // Copyright (c) MagicaSoft, 2020-2022.
- // https://magicasoft.jp
- using System;
- using System.Collections.Generic;
- using Unity.Collections;
- namespace MagicaCloth
- {
- /// <summary>
- /// NativeHashMapの機能拡張版
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- public class ExNativeHashMap<TKey, TValue>
- where TKey : struct, IEquatable<TKey>
- where TValue : struct
- {
- NativeHashMap<TKey, TValue> nativeHashMap;
- /// <summary>
- /// ネイティブリストの配列数
- /// ※ジョブでエラーが出ないように事前に確保しておく
- /// </summary>
- int nativeLength;
- /// <summary>
- /// 使用キーの記録
- /// </summary>
- HashSet<TKey> useKeySet = new HashSet<TKey>();
- //=========================================================================================
- public ExNativeHashMap()
- {
- nativeHashMap = new NativeHashMap<TKey, TValue>(1, Allocator.Persistent);
- nativeLength = NativeCount;
- }
- public void Dispose()
- {
- if (nativeHashMap.IsCreated)
- {
- nativeHashMap.Dispose();
- }
- }
- private int NativeCount
- {
- get
- {
- return nativeHashMap.Count();
- }
- }
- //=========================================================================================
- /// <summary>
- /// データ追加
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public void Add(TKey key, TValue value)
- {
- if (nativeHashMap.TryAdd(key, value) == false)
- {
- // すでにデータが存在するため一旦削除して再追加
- nativeHashMap.Remove(key);
- nativeHashMap.TryAdd(key, value);
- }
- useKeySet.Add(key);
- nativeLength = NativeCount;
- }
- /// <summary>
- /// データ取得
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public TValue Get(TKey key)
- {
- TValue data;
- nativeHashMap.TryGetValue(key, out data);
- return data;
- }
- /// <summary>
- /// 条件判定削除
- /// </summary>
- /// <param name="func">trueを返せば削除</param>
- public void Remove(Func<TKey, TValue, bool> func)
- {
- List<TKey> removeKey = new List<TKey>();
- foreach (TKey key in useKeySet)
- {
- TValue data;
- if (nativeHashMap.TryGetValue(key, out data))
- {
- // 削除判定
- if (func(key, data))
- {
- // 削除
- nativeHashMap.Remove(key);
- removeKey.Add(key);
- }
- }
- }
- foreach (var key in removeKey)
- useKeySet.Remove(key);
- nativeLength = NativeCount;
- }
- /// <summary>
- /// データ置き換え
- /// </summary>
- /// <param name="func">trueを返せば置換</param>
- /// <param name="rdata">引数にデータを受け取り、修正したデータを返し置換する</param>
- public void Replace(Func<TKey, TValue, bool> func, Func<TValue, TValue> datafunc)
- {
- foreach (var key in useKeySet)
- {
- TValue data;
- if (nativeHashMap.TryGetValue(key, out data))
- {
- // 置換判定
- if (func(key, data))
- {
- // 置き換え
- var newdata = datafunc(data);
- nativeHashMap.Remove(key); // 一旦削除しないと置き換えられない
- nativeHashMap.TryAdd(key, newdata);
- return;
- }
- }
- }
- }
- /// <summary>
- /// キーの削除
- /// </summary>
- /// <param name="key"></param>
- public void Remove(TKey key)
- {
- nativeHashMap.Remove(key);
- nativeLength = 0;
- useKeySet.Remove(key);
- }
- /// <summary>
- /// 実際に利用されている要素数を返す
- /// </summary>
- public int Count
- {
- get
- {
- return nativeLength;
- }
- }
- public void Clear()
- {
- nativeHashMap.Clear();
- nativeLength = 0;
- useKeySet.Clear();
- }
- /// <summary>
- /// 内部のNativeHashMapを取得する
- /// </summary>
- /// <returns></returns>
- public NativeHashMap<TKey, TValue> Map
- {
- get
- {
- return nativeHashMap;
- }
- }
- /// <summary>
- /// 使用キーセットを取得する
- /// </summary>
- public HashSet<TKey> UseKeySet
- {
- get
- {
- return useKeySet;
- }
- }
- }
- }
|