HashSet tot AssocList

This commit is contained in:
2016-09-02 18:05:57 +07:00
parent f56be1fbe9
commit 711bc09355
6 changed files with 209 additions and 7 deletions

View File

@@ -45,7 +45,9 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfAssocList.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfAttributes.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfList.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfSettings.cs" />
<Compile Include="Assets\FlashTools\Scripts\SwfAsset.cs" />
<Compile Include="Assets\FlashTools\Scripts\SwfClip.cs" />

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
namespace FlashTools.Internal {
public class SwfAssocList<T> {
SwfList<T> _list;
Dictionary<T, int> _dict;
IEqualityComparer<T> _comparer;
public SwfAssocList() {
_list = new SwfList<T>();
_dict = new Dictionary<T, int>();
_comparer = EqualityComparer<T>.Default;
}
public SwfAssocList(int capacity) {
_list = new SwfList<T>(capacity);
_dict = new Dictionary<T, int>(capacity);
_comparer = EqualityComparer<T>.Default;
}
public T this[int index] {
get {
return _list[index];
}
}
public int this[T item] {
get {
return _dict[item];
}
}
public int Count {
get {
return _list.Count;
}
}
public bool Contains(T value) {
return _dict.ContainsKey(value);
}
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: dbb6cc9af87db40aaa429b450fec1de9
timeCreated: 1472813600
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,110 @@
using System;
namespace FlashTools.Internal {
public class SwfList<T> {
T[] _data;
int _size;
static readonly T[] _emptyData = new T[0];
const int _defaultCapacity = 4;
public SwfList() {
_data = _emptyData;
_size = 0;
}
public SwfList(int capacity) {
if ( capacity < 0 ) {
throw new ArgumentOutOfRangeException(
"capacity", "capacity must be >= 0");
} else if ( capacity == 0 ) {
_data = _emptyData;
_size = 0;
} else {
_data = new T[capacity];
_size = 0;
}
}
public void Push(T value) {
if ( _size == _data.Length ) {
var new_capacity = _size == 0
? _defaultCapacity : _size * 2;
var new_data = new T[new_capacity];
Array.Copy(_data, new_data, _size);
_data = new_data;
}
_data[_size++] = value;
}
public T Pop() {
if ( _size == 0 ) {
throw new InvalidOperationException("empty list");
}
var last = _data[--_size];
_data[_size] = default(T);
return last;
}
public T Peek() {
if ( _size == 0 ) {
throw new InvalidOperationException("empty list");
}
return _data[_size - 1];
}
public void Clear() {
Array.Clear(_data, 0, _size);
_size = 0;
}
public T UnorderedRemoveAt(int index) {
if ( (uint)index >= (uint)_size ) {
throw new IndexOutOfRangeException();
}
var last = _data[_size - 1];
_data[index] = last;
_data[--_size] = default(T);
return last;
}
public T this[int index] {
get {
if ( (uint)index >= (uint)_size ) {
throw new IndexOutOfRangeException();
}
return _data[index];
}
set {
if ( (uint)index >= (uint)_size ) {
throw new IndexOutOfRangeException();
}
_data[index] = value;
}
}
public int Count {
get { return _size; }
}
public int Capacity {
get { return _data.Length; }
set {
if ( value < _size ) {
throw new ArgumentOutOfRangeException("capacity");
}
if ( value != _data.Length ) {
if ( value > 0 ) {
var new_data = new T[value];
if ( _size > 0 ) {
Array.Copy(_data, new_data, _size);
}
_data = new_data;
} else {
_data = _emptyData;
}
}
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c73b0d62df5ee4a9cb8babe2dbf4a004
timeCreated: 1472813804
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,12 +1,12 @@
using UnityEngine;
using System.Collections.Generic;
using FlashTools.Internal;
namespace FlashTools {
[ExecuteInEditMode, DisallowMultipleComponent]
public class SwfManager : MonoBehaviour {
HashSet<SwfClip> _clips = new HashSet<SwfClip>();
HashSet<SwfClipController> _controllers = new HashSet<SwfClipController>();
SwfAssocList<SwfClip> _clips = new SwfAssocList<SwfClip>();
SwfAssocList<SwfClipController> _controllers = new SwfAssocList<SwfClipController>();
// ---------------------------------------------------------------------
//
@@ -91,10 +91,9 @@ namespace FlashTools {
}
void UpdateControllers() {
var dt = Time.deltaTime;
var iter = _controllers.GetEnumerator();
while ( iter.MoveNext() ) {
iter.Current.InternalUpdate(dt);
var dt = Time.deltaTime;
for ( int i = 0, e = _controllers.Count; i < e; ++i ) {
_controllers[i].InternalUpdate(dt);
}
}