protected TempHashMap <MapEntry <K, V>, K, V> CreateCopy() { SmartCopyMap <K, V> This = this; // Copy existing data in FULLY NEW STRUCTURE MapEntry <K, V>[] table = this.table; TempHashMap <MapEntry <K, V>, K, V> backupMap = CreateEmptyInstance(); if (AutoCleanupNullValue) { for (int a = table.Length; a-- > 0;) { MapEntry <K, V> entry = table[a]; while (entry != null) { K key = entry.Key; if (key != null) { V value = entry.Value; WeakReference valueAsRef = value as WeakReference; if (valueAsRef.Target != null) { // Only copy the entry if the value content is still valid backupMap.Put(CloneKey(key), CloneValue(value)); } } entry = entry.NextEntryReal; } } } else { for (int a = table.Length; a-- > 0;) { MapEntry <K, V> entry = table[a]; while (entry != null) { K key = entry.Key; if (key != null) { V value = entry.Value; backupMap.Put(CloneKey(key), CloneValue(value)); } entry = GetNextEntry(entry); } } } return(backupMap); }
public override V Put(K key, V value) { Object writeLock = GetWriteLock(); lock (writeLock) { TempHashMap <MapEntry <K, V>, K, V> backupMap = CreateCopy(); // Write new data in the copied structure V existingValue = backupMap.Put(key, value); SaveCopy(backupMap); return(existingValue); } }