mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2025-12-16 14:09:00 +07:00
encapsulate dict and list
This commit is contained in:
51
Assets/IsoTools/Scripts/Internal/IsoAssocList.cs
Normal file
51
Assets/IsoTools/Scripts/Internal/IsoAssocList.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IsoTools.Internal {
|
||||
public class IsoAssocList<T> {
|
||||
IsoList<T> _list;
|
||||
Dictionary<T, int> _dict;
|
||||
IEqualityComparer<T> _comparer;
|
||||
|
||||
public IsoAssocList() {
|
||||
_list = new IsoList<T>();
|
||||
_dict = new Dictionary<T, int>();
|
||||
_comparer = EqualityComparer<T>.Default;
|
||||
}
|
||||
|
||||
public IsoAssocList(int capacity) {
|
||||
_list = new IsoList<T>(capacity);
|
||||
_dict = new Dictionary<T, int>(capacity);
|
||||
_comparer = EqualityComparer<T>.Default;
|
||||
}
|
||||
|
||||
public IsoList<T> RawList {
|
||||
get {
|
||||
return _list;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(T item) {
|
||||
if ( !_dict.ContainsKey(item) ) {
|
||||
_dict.Add(item, _list.Count);
|
||||
_list.Push(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(T item) {
|
||||
int index;
|
||||
if ( _dict.TryGetValue(item, out index) ) {
|
||||
_dict.Remove(item);
|
||||
var reordered =_list.UnorderedRemoveAt(index);
|
||||
if ( !_comparer.Equals(reordered, item) ) {
|
||||
_dict[reordered] = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
_list.Clear();
|
||||
_dict.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/IsoTools/Scripts/Internal/IsoAssocList.cs.meta
Normal file
12
Assets/IsoTools/Scripts/Internal/IsoAssocList.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da238ac7d8fe342d3a862a1c158c2469
|
||||
timeCreated: 1454613003
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user