encapsulate dict and list

This commit is contained in:
2016-02-05 02:13:03 +06:00
parent 8c4b0eccac
commit a4061b9af0
5 changed files with 84 additions and 45 deletions

View File

@@ -51,6 +51,7 @@
<Compile Include="Assets\IsoTools\Examples\Scripts\CubeAutoMovement.cs" />
<Compile Include="Assets\IsoTools\Examples\Scripts\IsoEchoListener.cs" />
<Compile Include="Assets\IsoTools\Examples\Scripts\PlayerController.cs" />
<Compile Include="Assets\IsoTools\Scripts\Internal\IsoAssocList.cs" />
<Compile Include="Assets\IsoTools\Scripts\Internal\IsoFakeCollider.cs" />
<Compile Include="Assets\IsoTools\Scripts\Internal\IsoFakeObject.cs" />
<Compile Include="Assets\IsoTools\Scripts\Internal\IsoFakeRigidbody.cs" />

View 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();
}
}
}

View 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:

View File

@@ -165,16 +165,15 @@ namespace IsoTools {
// ---------------------------------------------------------------------
public class InternalState {
public bool Dirty = false;
public bool Placed = false;
public Rect ScreenRect = new Rect();
public float Offset3d = 0.0f;
public IsoUtils.MinMax MinMax3d = IsoUtils.MinMax.zero;
public Vector2 MinSector = Vector2.zero;
public Vector2 MaxSector = Vector2.zero;
public Dictionary<IsoObject, int> SelfDependsD = new Dictionary<IsoObject, int>();
public IsoList<IsoObject> SelfDependsL = new IsoList<IsoObject>();
public HashSet<IsoObject> TheirDepends = new HashSet<IsoObject>();
public bool Dirty = false;
public bool Placed = false;
public Rect ScreenRect = new Rect();
public float Offset3d = 0.0f;
public IsoUtils.MinMax MinMax3d = IsoUtils.MinMax.zero;
public Vector2 MinSector = Vector2.zero;
public Vector2 MaxSector = Vector2.zero;
public IsoAssocList<IsoObject> SelfDepends = new IsoAssocList<IsoObject>(47);
public IsoAssocList<IsoObject> TheirDepends = new IsoAssocList<IsoObject>(47);
}
public InternalState Internal = new InternalState();
@@ -278,10 +277,6 @@ namespace IsoTools {
// ---------------------------------------------------------------------
void Awake() {
Internal.SelfDependsD = new Dictionary<IsoObject, int>(47);
Internal.SelfDependsL = new IsoList<IsoObject>(47);
Internal.TheirDepends = new HashSet<IsoObject>(new IsoObject[47]);
Internal.TheirDepends.Clear();
FixLastProperties();
FixIsoPosition();
}

View File

@@ -441,12 +441,7 @@ namespace IsoTools {
for ( int i = 0, e = sec.objects.Count; i < e; ++i ) {
var obj_b = sec.objects[i];
if ( obj_a != obj_b && !obj_b.Internal.Dirty && IsIsoObjectDepends(obj_a, obj_b) ) {
var a_depends_d = obj_a.Internal.SelfDependsD;
var a_depends_l = obj_a.Internal.SelfDependsL;
if ( !a_depends_d.ContainsKey(obj_b) ) {
a_depends_d.Add(obj_b, a_depends_l.Count);
a_depends_l.Push(obj_b);
}
obj_a.Internal.SelfDepends.Add(obj_b);
obj_b.Internal.TheirDepends.Add(obj_a);
}
}
@@ -459,12 +454,7 @@ namespace IsoTools {
for ( int i = 0, e = sec.objects.Count; i < e; ++i ) {
var obj_b = sec.objects[i];
if ( obj_a != obj_b && !obj_b.Internal.Dirty && IsIsoObjectDepends(obj_b, obj_a) ) {
var b_depends_d = obj_b.Internal.SelfDependsD;
var b_depends_l = obj_b.Internal.SelfDependsL;
if ( !b_depends_d.ContainsKey(obj_a) ) {
b_depends_d.Add(obj_a, b_depends_l.Count);
b_depends_l.Push(obj_a);
}
obj_b.Internal.SelfDepends.Add(obj_a);
obj_a.Internal.TheirDepends.Add(obj_b);
}
}
@@ -600,24 +590,14 @@ namespace IsoTools {
}
void ClearIsoObjectDepends(IsoObject iso_object) {
var their_depends_iter = iso_object.Internal.TheirDepends.GetEnumerator();
while ( their_depends_iter.MoveNext() ) {
var their_iso_object = their_depends_iter.Current;
if ( !their_iso_object.Internal.Dirty ) {
var their_depends_d = their_depends_iter.Current.Internal.SelfDependsD;
var their_depends_l = their_depends_iter.Current.Internal.SelfDependsL;
int index;
if ( their_depends_d.TryGetValue(iso_object, out index) ) {
their_depends_d.Remove(iso_object);
var reordered = their_depends_l.UnorderedRemoveAt(index);
if ( reordered != iso_object ) {
their_depends_d[reordered] = index;
}
}
var their_depends_l = iso_object.Internal.TheirDepends.RawList;
for ( int i = 0, e = their_depends_l.Count; i < e; ++i ) {
var their_depend = their_depends_l[i];
if ( !their_depend.Internal.Dirty ) {
their_depend.Internal.SelfDepends.Remove(iso_object);
}
}
iso_object.Internal.SelfDependsD.Clear();
iso_object.Internal.SelfDependsL.Clear();
iso_object.Internal.SelfDepends.Clear();
iso_object.Internal.TheirDepends.Clear();
}
@@ -646,9 +626,9 @@ namespace IsoTools {
return depth;
}
iso_object.Internal.Placed = true;
var depends_l = iso_object.Internal.SelfDependsL;
for ( int i = 0, e = iso_object.Internal.SelfDependsL.Count; i < e; ++i ) {
depth = RecursivePlaceIsoObject(depends_l[i], depth);
var self_depends_l = iso_object.Internal.SelfDepends.RawList;
for ( int i = 0, e = self_depends_l.Count; i < e; ++i ) {
depth = RecursivePlaceIsoObject(self_depends_l[i], depth);
}
if ( iso_object.mode == IsoObject.Mode.Mode3d ) {
var zoffset = iso_object.Internal.Offset3d;