Update copyright header. Normalize line endings to LF.
This commit is contained in:
@@ -1,40 +1,40 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
/// <summary>
|
||||
/// A thread-safe action queue, suitable for passing units of work between threads.
|
||||
/// </summary>
|
||||
public class ActionQueue
|
||||
{
|
||||
object syncRoot = new object();
|
||||
Action actions = () => { };
|
||||
|
||||
public void Add(Action a)
|
||||
{
|
||||
lock (syncRoot)
|
||||
actions += a;
|
||||
}
|
||||
|
||||
public void PerformActions()
|
||||
{
|
||||
Action a;
|
||||
lock (syncRoot)
|
||||
{
|
||||
a = actions;
|
||||
actions = () => { };
|
||||
}
|
||||
a();
|
||||
}
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
/// <summary>
|
||||
/// A thread-safe action queue, suitable for passing units of work between threads.
|
||||
/// </summary>
|
||||
public class ActionQueue
|
||||
{
|
||||
object syncRoot = new object();
|
||||
Action actions = () => { };
|
||||
|
||||
public void Add(Action a)
|
||||
{
|
||||
lock (syncRoot)
|
||||
actions += a;
|
||||
}
|
||||
|
||||
public void PerformActions()
|
||||
{
|
||||
Action a;
|
||||
lock (syncRoot)
|
||||
{
|
||||
a = actions;
|
||||
actions = () => { };
|
||||
}
|
||||
a();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public class Cache<T, U> : IEnumerable<KeyValuePair<T, U>>
|
||||
{
|
||||
Dictionary<T, U> hax = new Dictionary<T, U>();
|
||||
Func<T,U> loader;
|
||||
|
||||
public Cache(Func<T,U> loader)
|
||||
{
|
||||
if (loader == null)
|
||||
throw new ArgumentNullException();
|
||||
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
public U this[T key]
|
||||
{
|
||||
get
|
||||
{
|
||||
U result;
|
||||
if (!hax.TryGetValue(key, out result))
|
||||
hax.Add(key, result = loader(key));
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<T, U>> GetEnumerator() { return hax.GetEnumerator(); }
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
|
||||
|
||||
public IEnumerable<T> Keys { get { return hax.Keys; } }
|
||||
public IEnumerable<U> Values { get { return hax.Values; } }
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public class Cache<T, U> : IEnumerable<KeyValuePair<T, U>>
|
||||
{
|
||||
Dictionary<T, U> hax = new Dictionary<T, U>();
|
||||
Func<T,U> loader;
|
||||
|
||||
public Cache(Func<T,U> loader)
|
||||
{
|
||||
if (loader == null)
|
||||
throw new ArgumentNullException();
|
||||
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
public U this[T key]
|
||||
{
|
||||
get
|
||||
{
|
||||
U result;
|
||||
if (!hax.TryGetValue(key, out result))
|
||||
hax.Add(key, result = loader(key));
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<T, U>> GetEnumerator() { return hax.GetEnumerator(); }
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
|
||||
|
||||
public IEnumerable<T> Keys { get { return hax.Keys; } }
|
||||
public IEnumerable<U> Values { get { return hax.Values; } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
public class DisposableAction : IDisposable
|
||||
{
|
||||
public DisposableAction(Action a) { this.a = a; }
|
||||
|
||||
Action a;
|
||||
bool disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed) return;
|
||||
disposed = true;
|
||||
a();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~DisposableAction()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
public class DisposableAction : IDisposable
|
||||
{
|
||||
public DisposableAction(Action a) { this.a = a; }
|
||||
|
||||
Action a;
|
||||
bool disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed) return;
|
||||
disposed = true;
|
||||
a();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~DisposableAction()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public class Lazy<T>
|
||||
{
|
||||
Func<T> p;
|
||||
T value;
|
||||
|
||||
public Lazy(Func<T> p)
|
||||
{
|
||||
if (p == null)
|
||||
throw new ArgumentNullException();
|
||||
|
||||
this.p = p;
|
||||
}
|
||||
|
||||
public T Value
|
||||
{
|
||||
get
|
||||
{
|
||||
if (p == null)
|
||||
return value;
|
||||
|
||||
value = p();
|
||||
p = null;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Lazy
|
||||
{
|
||||
public static Lazy<T> New<T>(Func<T> p) { return new Lazy<T>(p); }
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public class Lazy<T>
|
||||
{
|
||||
Func<T> p;
|
||||
T value;
|
||||
|
||||
public Lazy(Func<T> p)
|
||||
{
|
||||
if (p == null)
|
||||
throw new ArgumentNullException();
|
||||
|
||||
this.p = p;
|
||||
}
|
||||
|
||||
public T Value
|
||||
{
|
||||
get
|
||||
{
|
||||
if (p == null)
|
||||
return value;
|
||||
|
||||
value = p();
|
||||
p = null;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Lazy
|
||||
{
|
||||
public static Lazy<T> New<T>(Func<T> p) { return new Lazy<T>(p); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public struct Pair<T, U>
|
||||
{
|
||||
public T First;
|
||||
public U Second;
|
||||
|
||||
public Pair(T first, U second)
|
||||
{
|
||||
First = first;
|
||||
Second = second;
|
||||
}
|
||||
|
||||
static IEqualityComparer<T> tc = EqualityComparer<T>.Default;
|
||||
static IEqualityComparer<U> uc = EqualityComparer<U>.Default;
|
||||
|
||||
public static bool operator ==(Pair<T, U> a, Pair<T, U> b)
|
||||
{
|
||||
return tc.Equals(a.First, b.First) && uc.Equals(a.Second, b.Second);
|
||||
}
|
||||
|
||||
public static bool operator !=(Pair<T, U> a, Pair<T, U> b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is Pair<T, U>))
|
||||
return false;
|
||||
|
||||
return (Pair<T, U>)obj == this;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return First.GetHashCode() ^ Second.GetHashCode();
|
||||
}
|
||||
|
||||
public Pair<T, U> WithFirst(T t) { return new Pair<T, U>(t, Second); }
|
||||
public Pair<T, U> WithSecond(U u) { return new Pair<T, U>(First, u); }
|
||||
|
||||
public static T AsFirst(Pair<T, U> p) { return p.First; }
|
||||
public static U AsSecond(Pair<T, U> p) { return p.Second; }
|
||||
|
||||
public Pair<U, T> Swap() { return Pair.New(Second, First); }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "({0},{1})".F(First, Second);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Pair
|
||||
{
|
||||
public static Pair<T, U> New<T, U>(T t, U u) { return new Pair<T, U>(t, u); }
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public struct Pair<T, U>
|
||||
{
|
||||
public T First;
|
||||
public U Second;
|
||||
|
||||
public Pair(T first, U second)
|
||||
{
|
||||
First = first;
|
||||
Second = second;
|
||||
}
|
||||
|
||||
static IEqualityComparer<T> tc = EqualityComparer<T>.Default;
|
||||
static IEqualityComparer<U> uc = EqualityComparer<U>.Default;
|
||||
|
||||
public static bool operator ==(Pair<T, U> a, Pair<T, U> b)
|
||||
{
|
||||
return tc.Equals(a.First, b.First) && uc.Equals(a.Second, b.Second);
|
||||
}
|
||||
|
||||
public static bool operator !=(Pair<T, U> a, Pair<T, U> b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is Pair<T, U>))
|
||||
return false;
|
||||
|
||||
return (Pair<T, U>)obj == this;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return First.GetHashCode() ^ Second.GetHashCode();
|
||||
}
|
||||
|
||||
public Pair<T, U> WithFirst(T t) { return new Pair<T, U>(t, Second); }
|
||||
public Pair<T, U> WithSecond(U u) { return new Pair<T, U>(First, u); }
|
||||
|
||||
public static T AsFirst(Pair<T, U> p) { return p.First; }
|
||||
public static U AsSecond(Pair<T, U> p) { return p.Second; }
|
||||
|
||||
public Pair<U, T> Swap() { return Pair.New(Second, First); }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "({0},{1})".F(First, Second);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Pair
|
||||
{
|
||||
public static Pair<T, U> New<T, U>(T t, U u) { return new Pair<T, U>(t, u); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,103 +1,103 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public class PriorityQueue<T>
|
||||
where T : IComparable<T>
|
||||
{
|
||||
List<T[]> items = new List<T[]>();
|
||||
int level, index;
|
||||
|
||||
public PriorityQueue()
|
||||
{
|
||||
items.Add(new T[1]);
|
||||
}
|
||||
|
||||
public void Add(T item)
|
||||
{
|
||||
int addLevel = level;
|
||||
int addIndex = index;
|
||||
|
||||
while (addLevel >= 1 && Above(addLevel, addIndex).CompareTo(item) > 0)
|
||||
{
|
||||
items[addLevel][addIndex] = Above(addLevel, addIndex);
|
||||
--addLevel;
|
||||
addIndex >>= 1;
|
||||
}
|
||||
|
||||
items[addLevel][addIndex] = item;
|
||||
|
||||
if (++index >= (1 << level))
|
||||
{
|
||||
index = 0;
|
||||
if (items.Count <= ++level)
|
||||
items.Add(new T[1 << level]);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Empty { get { return (level == 0); } }
|
||||
|
||||
T At(int level, int index) { return items[level][index]; }
|
||||
T Above(int level, int index) { return items[level - 1][index >> 1]; }
|
||||
|
||||
T Last()
|
||||
{
|
||||
int lastLevel = level;
|
||||
int lastIndex = index;
|
||||
|
||||
if (--lastIndex < 0)
|
||||
lastIndex = (1 << --lastLevel) - 1;
|
||||
|
||||
return At(lastLevel, lastIndex);
|
||||
}
|
||||
|
||||
public T Pop()
|
||||
{
|
||||
if (level == 0 && index == 0)
|
||||
throw new InvalidOperationException("Attempting to pop empty PriorityQueue");
|
||||
|
||||
T ret = At(0, 0);
|
||||
BubbleInto(0, 0, Last());
|
||||
if (--index < 0)
|
||||
index = (1 << --level) - 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void BubbleInto(int intoLevel, int intoIndex, T val)
|
||||
{
|
||||
int downLevel = intoLevel + 1;
|
||||
int downIndex = intoIndex << 1;
|
||||
|
||||
if (downLevel > level || (downLevel == level && downIndex >= index))
|
||||
{
|
||||
items[intoLevel][intoIndex] = val;
|
||||
return;
|
||||
}
|
||||
|
||||
if (downLevel <= level && downIndex < index - 1 &&
|
||||
At(downLevel, downIndex).CompareTo(At(downLevel, downIndex + 1)) >= 0)
|
||||
++downIndex;
|
||||
|
||||
if (val.CompareTo(At(downLevel, downIndex)) <= 0)
|
||||
{
|
||||
items[intoLevel][intoIndex] = val;
|
||||
return;
|
||||
}
|
||||
|
||||
items[intoLevel][intoIndex] = At(downLevel, downIndex);
|
||||
BubbleInto(downLevel, downIndex, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.FileFormats
|
||||
{
|
||||
public class PriorityQueue<T>
|
||||
where T : IComparable<T>
|
||||
{
|
||||
List<T[]> items = new List<T[]>();
|
||||
int level, index;
|
||||
|
||||
public PriorityQueue()
|
||||
{
|
||||
items.Add(new T[1]);
|
||||
}
|
||||
|
||||
public void Add(T item)
|
||||
{
|
||||
int addLevel = level;
|
||||
int addIndex = index;
|
||||
|
||||
while (addLevel >= 1 && Above(addLevel, addIndex).CompareTo(item) > 0)
|
||||
{
|
||||
items[addLevel][addIndex] = Above(addLevel, addIndex);
|
||||
--addLevel;
|
||||
addIndex >>= 1;
|
||||
}
|
||||
|
||||
items[addLevel][addIndex] = item;
|
||||
|
||||
if (++index >= (1 << level))
|
||||
{
|
||||
index = 0;
|
||||
if (items.Count <= ++level)
|
||||
items.Add(new T[1 << level]);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Empty { get { return (level == 0); } }
|
||||
|
||||
T At(int level, int index) { return items[level][index]; }
|
||||
T Above(int level, int index) { return items[level - 1][index >> 1]; }
|
||||
|
||||
T Last()
|
||||
{
|
||||
int lastLevel = level;
|
||||
int lastIndex = index;
|
||||
|
||||
if (--lastIndex < 0)
|
||||
lastIndex = (1 << --lastLevel) - 1;
|
||||
|
||||
return At(lastLevel, lastIndex);
|
||||
}
|
||||
|
||||
public T Pop()
|
||||
{
|
||||
if (level == 0 && index == 0)
|
||||
throw new InvalidOperationException("Attempting to pop empty PriorityQueue");
|
||||
|
||||
T ret = At(0, 0);
|
||||
BubbleInto(0, 0, Last());
|
||||
if (--index < 0)
|
||||
index = (1 << --level) - 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void BubbleInto(int intoLevel, int intoIndex, T val)
|
||||
{
|
||||
int downLevel = intoLevel + 1;
|
||||
int downIndex = intoIndex << 1;
|
||||
|
||||
if (downLevel > level || (downLevel == level && downIndex >= index))
|
||||
{
|
||||
items[intoLevel][intoIndex] = val;
|
||||
return;
|
||||
}
|
||||
|
||||
if (downLevel <= level && downIndex < index - 1 &&
|
||||
At(downLevel, downIndex).CompareTo(At(downLevel, downIndex + 1)) >= 0)
|
||||
++downIndex;
|
||||
|
||||
if (val.CompareTo(At(downLevel, downIndex)) <= 0)
|
||||
{
|
||||
items[intoLevel][intoIndex] = val;
|
||||
return;
|
||||
}
|
||||
|
||||
items[intoLevel][intoIndex] = At(downLevel, downIndex);
|
||||
BubbleInto(downLevel, downIndex, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,66 +1,66 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.Collections
|
||||
{
|
||||
public class Set<T> : IEnumerable<T>
|
||||
{
|
||||
Dictionary<T, bool> data = new Dictionary<T, bool>();
|
||||
|
||||
public void Add( T obj )
|
||||
{
|
||||
data.Add( obj, false );
|
||||
if( OnAdd != null )
|
||||
OnAdd( obj );
|
||||
}
|
||||
|
||||
public void Remove( T obj )
|
||||
{
|
||||
data.Remove( obj );
|
||||
if( OnRemove != null )
|
||||
OnRemove( obj );
|
||||
}
|
||||
|
||||
public event Action<T> OnAdd;
|
||||
public event Action<T> OnRemove;
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return data.Keys.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public class CachedView<T,U> : Set<U>
|
||||
{
|
||||
public CachedView( Set<T> set, Func<T, bool> include, Func<T, U> store )
|
||||
: this( set, include, x => new[] { store( x ) } )
|
||||
{
|
||||
}
|
||||
|
||||
public CachedView( Set<T> set, Func<T,bool> include, Func<T,IEnumerable<U>> store )
|
||||
{
|
||||
foreach( var t in set )
|
||||
if( include( t ) )
|
||||
store( t ).Do( x => Add( x ) );
|
||||
|
||||
set.OnAdd += obj => { if( include( obj ) ) store( obj ).Do( x => Add( x ) ); };
|
||||
set.OnRemove += obj => { if( include( obj ) ) store( obj ).Do( x => Remove( x ) ); };
|
||||
}
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.Collections
|
||||
{
|
||||
public class Set<T> : IEnumerable<T>
|
||||
{
|
||||
Dictionary<T, bool> data = new Dictionary<T, bool>();
|
||||
|
||||
public void Add( T obj )
|
||||
{
|
||||
data.Add( obj, false );
|
||||
if( OnAdd != null )
|
||||
OnAdd( obj );
|
||||
}
|
||||
|
||||
public void Remove( T obj )
|
||||
{
|
||||
data.Remove( obj );
|
||||
if( OnRemove != null )
|
||||
OnRemove( obj );
|
||||
}
|
||||
|
||||
public event Action<T> OnAdd;
|
||||
public event Action<T> OnRemove;
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return data.Keys.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public class CachedView<T,U> : Set<U>
|
||||
{
|
||||
public CachedView( Set<T> set, Func<T, bool> include, Func<T, U> store )
|
||||
: this( set, include, x => new[] { store( x ) } )
|
||||
{
|
||||
}
|
||||
|
||||
public CachedView( Set<T> set, Func<T,bool> include, Func<T,IEnumerable<U>> store )
|
||||
{
|
||||
foreach( var t in set )
|
||||
if( include( t ) )
|
||||
store( t ).Do( x => Add( x ) );
|
||||
|
||||
set.OnAdd += obj => { if( include( obj ) ) store( obj ).Do( x => Add( x ) ); };
|
||||
set.OnRemove += obj => { if( include( obj ) ) store( obj ).Do( x => Remove( x ) ); };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,104 +1,104 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct float2
|
||||
{
|
||||
public float X, Y;
|
||||
|
||||
public float2(float x, float y) { X = x; Y = y; }
|
||||
public float2(PointF p) { X = p.X; Y = p.Y; }
|
||||
public float2(Point p) { X = p.X; Y = p.Y; }
|
||||
public float2(Size p) { X = p.Width; Y = p.Height; }
|
||||
public float2(SizeF p) { X = p.Width; Y = p.Height; }
|
||||
|
||||
public PointF ToPointF() { return new PointF(X, Y); }
|
||||
public SizeF ToSizeF() { return new SizeF(X, Y); }
|
||||
|
||||
public static implicit operator float2(int2 src) { return new float2(src.X, src.Y); }
|
||||
|
||||
public static float2 operator +(float2 a, float2 b) { return new float2(a.X + b.X, a.Y + b.Y); }
|
||||
public static float2 operator -(float2 a, float2 b) { return new float2(a.X - b.X, a.Y - b.Y); }
|
||||
|
||||
public static float2 operator -(float2 a) { return new float2(-a.X, -a.Y); }
|
||||
|
||||
public static float Lerp(float a, float b, float t) { return a + t * (b - a); }
|
||||
|
||||
public static float2 Lerp(float2 a, float2 b, float t)
|
||||
{
|
||||
return new float2(
|
||||
Lerp(a.X, b.X, t),
|
||||
Lerp(a.Y, b.Y, t));
|
||||
}
|
||||
|
||||
public static float2 Lerp(float2 a, float2 b, float2 t)
|
||||
{
|
||||
return new float2(
|
||||
Lerp(a.X, b.X, t.X),
|
||||
Lerp(a.Y, b.Y, t.Y));
|
||||
}
|
||||
|
||||
public static float2 FromAngle(float a) { return new float2((float)Math.Sin(a), (float)Math.Cos(a)); }
|
||||
|
||||
static float Constrain(float x, float a, float b) { return x < a ? a : x > b ? b : x; }
|
||||
|
||||
public float2 Constrain(float2 min, float2 max)
|
||||
{
|
||||
return new float2(
|
||||
Constrain(X, min.X, max.X),
|
||||
Constrain(Y, min.Y, max.Y));
|
||||
}
|
||||
|
||||
public static float2 operator *(float a, float2 b) { return new float2(a * b.X, a * b.Y); }
|
||||
public static float2 operator *(float2 b, float a) { return new float2(a * b.X, a * b.Y); }
|
||||
public static float2 operator *( float2 a, float2 b ) { return new float2( a.X * b.X, a.Y * b.Y ); }
|
||||
public static float2 operator /( float2 a, float2 b ) { return new float2( a.X / b.X, a.Y / b.Y ); }
|
||||
|
||||
public static bool operator ==(float2 me, float2 other) { return (me.X == other.X && me.Y == other.Y); }
|
||||
public static bool operator !=(float2 me, float2 other) { return !(me == other); }
|
||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
float2 o = (float2)obj;
|
||||
return o == this;
|
||||
}
|
||||
|
||||
public static readonly float2 Zero = new float2(0, 0);
|
||||
|
||||
public static bool WithinEpsilon(float2 a, float2 b, float e)
|
||||
{
|
||||
float2 d = a - b;
|
||||
return Math.Abs(d.X) < e && Math.Abs(d.Y) < e;
|
||||
}
|
||||
|
||||
public float2 Sign() { return new float2(Math.Sign(X), Math.Sign(Y)); }
|
||||
public static float Dot(float2 a, float2 b) { return a.X * b.X + a.Y * b.Y; }
|
||||
public float2 Round() { return new float2((float)Math.Round(X), (float)Math.Round(Y)); }
|
||||
|
||||
public override string ToString() { return string.Format("({0},{1})", X, Y); }
|
||||
public int2 ToInt2() { return new int2((int)X, (int)Y); }
|
||||
|
||||
public static float2 Max(float2 a, float2 b) { return new float2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }
|
||||
public static float2 Min(float2 a, float2 b) { return new float2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }
|
||||
|
||||
public float LengthSquared { get { return X * X + Y * Y; } }
|
||||
public float Length { get { return (float)Math.Sqrt(LengthSquared); } }
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct float2
|
||||
{
|
||||
public float X, Y;
|
||||
|
||||
public float2(float x, float y) { X = x; Y = y; }
|
||||
public float2(PointF p) { X = p.X; Y = p.Y; }
|
||||
public float2(Point p) { X = p.X; Y = p.Y; }
|
||||
public float2(Size p) { X = p.Width; Y = p.Height; }
|
||||
public float2(SizeF p) { X = p.Width; Y = p.Height; }
|
||||
|
||||
public PointF ToPointF() { return new PointF(X, Y); }
|
||||
public SizeF ToSizeF() { return new SizeF(X, Y); }
|
||||
|
||||
public static implicit operator float2(int2 src) { return new float2(src.X, src.Y); }
|
||||
|
||||
public static float2 operator +(float2 a, float2 b) { return new float2(a.X + b.X, a.Y + b.Y); }
|
||||
public static float2 operator -(float2 a, float2 b) { return new float2(a.X - b.X, a.Y - b.Y); }
|
||||
|
||||
public static float2 operator -(float2 a) { return new float2(-a.X, -a.Y); }
|
||||
|
||||
public static float Lerp(float a, float b, float t) { return a + t * (b - a); }
|
||||
|
||||
public static float2 Lerp(float2 a, float2 b, float t)
|
||||
{
|
||||
return new float2(
|
||||
Lerp(a.X, b.X, t),
|
||||
Lerp(a.Y, b.Y, t));
|
||||
}
|
||||
|
||||
public static float2 Lerp(float2 a, float2 b, float2 t)
|
||||
{
|
||||
return new float2(
|
||||
Lerp(a.X, b.X, t.X),
|
||||
Lerp(a.Y, b.Y, t.Y));
|
||||
}
|
||||
|
||||
public static float2 FromAngle(float a) { return new float2((float)Math.Sin(a), (float)Math.Cos(a)); }
|
||||
|
||||
static float Constrain(float x, float a, float b) { return x < a ? a : x > b ? b : x; }
|
||||
|
||||
public float2 Constrain(float2 min, float2 max)
|
||||
{
|
||||
return new float2(
|
||||
Constrain(X, min.X, max.X),
|
||||
Constrain(Y, min.Y, max.Y));
|
||||
}
|
||||
|
||||
public static float2 operator *(float a, float2 b) { return new float2(a * b.X, a * b.Y); }
|
||||
public static float2 operator *(float2 b, float a) { return new float2(a * b.X, a * b.Y); }
|
||||
public static float2 operator *( float2 a, float2 b ) { return new float2( a.X * b.X, a.Y * b.Y ); }
|
||||
public static float2 operator /( float2 a, float2 b ) { return new float2( a.X / b.X, a.Y / b.Y ); }
|
||||
|
||||
public static bool operator ==(float2 me, float2 other) { return (me.X == other.X && me.Y == other.Y); }
|
||||
public static bool operator !=(float2 me, float2 other) { return !(me == other); }
|
||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
float2 o = (float2)obj;
|
||||
return o == this;
|
||||
}
|
||||
|
||||
public static readonly float2 Zero = new float2(0, 0);
|
||||
|
||||
public static bool WithinEpsilon(float2 a, float2 b, float e)
|
||||
{
|
||||
float2 d = a - b;
|
||||
return Math.Abs(d.X) < e && Math.Abs(d.Y) < e;
|
||||
}
|
||||
|
||||
public float2 Sign() { return new float2(Math.Sign(X), Math.Sign(Y)); }
|
||||
public static float Dot(float2 a, float2 b) { return a.X * b.X + a.Y * b.Y; }
|
||||
public float2 Round() { return new float2((float)Math.Round(X), (float)Math.Round(Y)); }
|
||||
|
||||
public override string ToString() { return string.Format("({0},{1})", X, Y); }
|
||||
public int2 ToInt2() { return new int2((int)X, (int)Y); }
|
||||
|
||||
public static float2 Max(float2 a, float2 b) { return new float2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }
|
||||
public static float2 Min(float2 a, float2 b) { return new float2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }
|
||||
|
||||
public float LengthSquared { get { return X * X + Y * Y; } }
|
||||
public float Length { get { return (float)Math.Sqrt(LengthSquared); } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,85 +1,85 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see LICENSE.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
public struct int2
|
||||
{
|
||||
public int X,Y;
|
||||
|
||||
public int2( int x, int y ) { this.X = x; this.Y = y; }
|
||||
public int2( Point p ) { X = p.X; Y = p.Y; }
|
||||
public int2( Size p ) { X = p.Width; Y = p.Height; }
|
||||
|
||||
public static int2 operator +(int2 a, int2 b) { return new int2(a.X + b.X, a.Y + b.Y); }
|
||||
public static int2 operator -(int2 a, int2 b) { return new int2(a.X - b.X, a.Y - b.Y); }
|
||||
public static int2 operator *(int a, int2 b) { return new int2(a * b.X, a * b.Y); }
|
||||
public static int2 operator *(int2 b, int a) { return new int2(a * b.X, a * b.Y); }
|
||||
public static int2 operator /(int2 a, int b) { return new int2(a.X / b, a.Y / b); }
|
||||
|
||||
public static int2 operator -(int2 a) { return new int2(-a.X, -a.Y); }
|
||||
|
||||
public static bool operator ==(int2 me, int2 other) { return (me.X == other.X && me.Y == other.Y); }
|
||||
public static bool operator !=(int2 me, int2 other) { return !(me == other); }
|
||||
|
||||
public int2 Sign() { return new int2(Math.Sign(X), Math.Sign(Y)); }
|
||||
public int2 Abs() { return new int2( Math.Abs( X ), Math.Abs( Y ) ); }
|
||||
public int LengthSquared { get { return X * X + Y * Y; } }
|
||||
public int Length { get { return (int)Math.Sqrt(LengthSquared); } }
|
||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
|
||||
|
||||
public static int2 Max(int2 a, int2 b) { return new int2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }
|
||||
public static int2 Min(int2 a, int2 b) { return new int2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
int2 o = (int2)obj;
|
||||
return o == this;
|
||||
}
|
||||
|
||||
public static readonly int2 Zero = new int2(0, 0);
|
||||
public Point ToPoint() { return new Point(X, Y); }
|
||||
public PointF ToPointF() { return new PointF(X, Y); }
|
||||
public float2 ToFloat2() { return new float2(X, Y); }
|
||||
|
||||
public override string ToString() { return string.Format("{0},{1}", X, Y); }
|
||||
|
||||
// Change endianness of a uint32
|
||||
public static uint Swap(uint orig)
|
||||
{
|
||||
return (uint)((orig & 0xff000000) >> 24) | ((orig & 0x00ff0000) >> 8) | ((orig & 0x0000ff00) << 8) | ((orig & 0x000000ff) << 24);
|
||||
}
|
||||
|
||||
public static int Lerp( int a, int b, int mul, int div )
|
||||
{
|
||||
return a + ( b - a ) * mul / div;
|
||||
}
|
||||
|
||||
public static int2 Lerp( int2 a, int2 b, int mul, int div )
|
||||
{
|
||||
return a + ( b - a ) * mul / div;
|
||||
}
|
||||
|
||||
public int2 Clamp(Rectangle r)
|
||||
{
|
||||
return new int2(Math.Min(r.Right, Math.Max(X, r.Left)),
|
||||
Math.Min(r.Bottom, Math.Max(Y, r.Top)));
|
||||
}
|
||||
|
||||
public static int Dot(int2 a, int2 b) { return a.X * b.X + a.Y * b.Y; }
|
||||
|
||||
}
|
||||
}
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenRA
|
||||
{
|
||||
public struct int2
|
||||
{
|
||||
public int X,Y;
|
||||
|
||||
public int2( int x, int y ) { this.X = x; this.Y = y; }
|
||||
public int2( Point p ) { X = p.X; Y = p.Y; }
|
||||
public int2( Size p ) { X = p.Width; Y = p.Height; }
|
||||
|
||||
public static int2 operator +(int2 a, int2 b) { return new int2(a.X + b.X, a.Y + b.Y); }
|
||||
public static int2 operator -(int2 a, int2 b) { return new int2(a.X - b.X, a.Y - b.Y); }
|
||||
public static int2 operator *(int a, int2 b) { return new int2(a * b.X, a * b.Y); }
|
||||
public static int2 operator *(int2 b, int a) { return new int2(a * b.X, a * b.Y); }
|
||||
public static int2 operator /(int2 a, int b) { return new int2(a.X / b, a.Y / b); }
|
||||
|
||||
public static int2 operator -(int2 a) { return new int2(-a.X, -a.Y); }
|
||||
|
||||
public static bool operator ==(int2 me, int2 other) { return (me.X == other.X && me.Y == other.Y); }
|
||||
public static bool operator !=(int2 me, int2 other) { return !(me == other); }
|
||||
|
||||
public int2 Sign() { return new int2(Math.Sign(X), Math.Sign(Y)); }
|
||||
public int2 Abs() { return new int2( Math.Abs( X ), Math.Abs( Y ) ); }
|
||||
public int LengthSquared { get { return X * X + Y * Y; } }
|
||||
public int Length { get { return (int)Math.Sqrt(LengthSquared); } }
|
||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
|
||||
|
||||
public static int2 Max(int2 a, int2 b) { return new int2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }
|
||||
public static int2 Min(int2 a, int2 b) { return new int2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
int2 o = (int2)obj;
|
||||
return o == this;
|
||||
}
|
||||
|
||||
public static readonly int2 Zero = new int2(0, 0);
|
||||
public Point ToPoint() { return new Point(X, Y); }
|
||||
public PointF ToPointF() { return new PointF(X, Y); }
|
||||
public float2 ToFloat2() { return new float2(X, Y); }
|
||||
|
||||
public override string ToString() { return string.Format("{0},{1}", X, Y); }
|
||||
|
||||
// Change endianness of a uint32
|
||||
public static uint Swap(uint orig)
|
||||
{
|
||||
return (uint)((orig & 0xff000000) >> 24) | ((orig & 0x00ff0000) >> 8) | ((orig & 0x0000ff00) << 8) | ((orig & 0x000000ff) << 24);
|
||||
}
|
||||
|
||||
public static int Lerp( int a, int b, int mul, int div )
|
||||
{
|
||||
return a + ( b - a ) * mul / div;
|
||||
}
|
||||
|
||||
public static int2 Lerp( int2 a, int2 b, int mul, int div )
|
||||
{
|
||||
return a + ( b - a ) * mul / div;
|
||||
}
|
||||
|
||||
public int2 Clamp(Rectangle r)
|
||||
{
|
||||
return new int2(Math.Min(r.Right, Math.Max(X, r.Left)),
|
||||
Math.Min(r.Bottom, Math.Max(Y, r.Top)));
|
||||
}
|
||||
|
||||
public static int Dot(int2 a, int2 b) { return a.X * b.X + a.Y * b.Y; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user