init commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public abstract class CollectionUtilities
|
||||
{
|
||||
public static void AddRange(IList to, IEnumerable range)
|
||||
{
|
||||
foreach (object item in range)
|
||||
{
|
||||
to.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CheckElementsAreOfType(IEnumerable e, Type t)
|
||||
{
|
||||
foreach (object item in e)
|
||||
{
|
||||
if (!t.IsInstanceOfType(item))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static IDictionary ReadOnly(IDictionary d)
|
||||
{
|
||||
return new UnmodifiableDictionaryProxy(d);
|
||||
}
|
||||
|
||||
public static IList ReadOnly(IList l)
|
||||
{
|
||||
return new UnmodifiableListProxy(l);
|
||||
}
|
||||
|
||||
public static ISet ReadOnly(ISet s)
|
||||
{
|
||||
return new UnmodifiableSetProxy(s);
|
||||
}
|
||||
|
||||
public static object RequireNext(IEnumerator e)
|
||||
{
|
||||
if (!e.MoveNext())
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return e.Current;
|
||||
}
|
||||
|
||||
public static string ToString(IEnumerable c)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder("[");
|
||||
IEnumerator enumerator = c.GetEnumerator();
|
||||
if (enumerator.MoveNext())
|
||||
{
|
||||
stringBuilder.Append(enumerator.Current.ToString());
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
stringBuilder.Append(", ");
|
||||
stringBuilder.Append(enumerator.Current.ToString());
|
||||
}
|
||||
}
|
||||
stringBuilder.Append(']');
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public sealed class EmptyEnumerable : IEnumerable
|
||||
{
|
||||
public static readonly IEnumerable Instance = new EmptyEnumerable();
|
||||
|
||||
private EmptyEnumerable()
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return EmptyEnumerator.Instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public sealed class EmptyEnumerator : IEnumerator
|
||||
{
|
||||
public static readonly IEnumerator Instance = new EmptyEnumerator();
|
||||
|
||||
public object Current
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new InvalidOperationException("No elements");
|
||||
}
|
||||
}
|
||||
|
||||
private EmptyEnumerator()
|
||||
{
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public sealed class EnumerableProxy : IEnumerable
|
||||
{
|
||||
private readonly IEnumerable inner;
|
||||
|
||||
public EnumerableProxy(IEnumerable inner)
|
||||
{
|
||||
if (inner == null)
|
||||
{
|
||||
throw new ArgumentNullException("inner");
|
||||
}
|
||||
this.inner = inner;
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return inner.GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public class HashSet : ISet, ICollection, IEnumerable
|
||||
{
|
||||
private readonly IDictionary impl = Platform.CreateHashtable();
|
||||
|
||||
public virtual int Count => impl.Count;
|
||||
|
||||
public virtual bool IsEmpty => impl.Count == 0;
|
||||
|
||||
public virtual bool IsFixedSize => impl.IsFixedSize;
|
||||
|
||||
public virtual bool IsReadOnly => impl.IsReadOnly;
|
||||
|
||||
public virtual bool IsSynchronized => impl.IsSynchronized;
|
||||
|
||||
public virtual object SyncRoot => impl.SyncRoot;
|
||||
|
||||
public HashSet()
|
||||
{
|
||||
}
|
||||
|
||||
public HashSet(IEnumerable s)
|
||||
{
|
||||
foreach (object item in s)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Add(object o)
|
||||
{
|
||||
impl[o] = null;
|
||||
}
|
||||
|
||||
public virtual void AddAll(IEnumerable e)
|
||||
{
|
||||
foreach (object item in e)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
impl.Clear();
|
||||
}
|
||||
|
||||
public virtual bool Contains(object o)
|
||||
{
|
||||
return impl.Contains(o);
|
||||
}
|
||||
|
||||
public virtual void CopyTo(Array array, int index)
|
||||
{
|
||||
impl.Keys.CopyTo(array, index);
|
||||
}
|
||||
|
||||
public virtual IEnumerator GetEnumerator()
|
||||
{
|
||||
return impl.Keys.GetEnumerator();
|
||||
}
|
||||
|
||||
public virtual void Remove(object o)
|
||||
{
|
||||
impl.Remove(o);
|
||||
}
|
||||
|
||||
public virtual void RemoveAll(IEnumerable e)
|
||||
{
|
||||
foreach (object item in e)
|
||||
{
|
||||
Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public interface ISet : ICollection, IEnumerable
|
||||
{
|
||||
bool IsEmpty { get; }
|
||||
|
||||
bool IsFixedSize { get; }
|
||||
|
||||
bool IsReadOnly { get; }
|
||||
|
||||
void Add(object o);
|
||||
|
||||
void AddAll(IEnumerable e);
|
||||
|
||||
void Clear();
|
||||
|
||||
bool Contains(object o);
|
||||
|
||||
void Remove(object o);
|
||||
|
||||
void RemoveAll(IEnumerable e);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public class LinkedDictionary : IDictionary, ICollection, IEnumerable
|
||||
{
|
||||
internal readonly IDictionary hash = Platform.CreateHashtable();
|
||||
|
||||
internal readonly IList keys = Platform.CreateArrayList();
|
||||
|
||||
public virtual int Count => hash.Count;
|
||||
|
||||
public virtual bool IsFixedSize => false;
|
||||
|
||||
public virtual bool IsReadOnly => false;
|
||||
|
||||
public virtual bool IsSynchronized => false;
|
||||
|
||||
public virtual object SyncRoot => false;
|
||||
|
||||
public virtual ICollection Keys => Platform.CreateArrayList(keys);
|
||||
|
||||
public virtual ICollection Values
|
||||
{
|
||||
get
|
||||
{
|
||||
IList list = Platform.CreateArrayList(keys.Count);
|
||||
foreach (object key in keys)
|
||||
{
|
||||
list.Add(hash[key]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual object this[object k]
|
||||
{
|
||||
get
|
||||
{
|
||||
return hash[k];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (!hash.Contains(k))
|
||||
{
|
||||
keys.Add(k);
|
||||
}
|
||||
hash[k] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Add(object k, object v)
|
||||
{
|
||||
hash.Add(k, v);
|
||||
keys.Add(k);
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
hash.Clear();
|
||||
keys.Clear();
|
||||
}
|
||||
|
||||
public virtual bool Contains(object k)
|
||||
{
|
||||
return hash.Contains(k);
|
||||
}
|
||||
|
||||
public virtual void CopyTo(Array array, int index)
|
||||
{
|
||||
foreach (object key in keys)
|
||||
{
|
||||
array.SetValue(hash[key], index++);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public virtual IDictionaryEnumerator GetEnumerator()
|
||||
{
|
||||
return new LinkedDictionaryEnumerator(this);
|
||||
}
|
||||
|
||||
public virtual void Remove(object k)
|
||||
{
|
||||
hash.Remove(k);
|
||||
keys.Remove(k);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
internal class LinkedDictionaryEnumerator : IDictionaryEnumerator, IEnumerator
|
||||
{
|
||||
private readonly LinkedDictionary parent;
|
||||
|
||||
private int pos = -1;
|
||||
|
||||
public virtual object Current => Entry;
|
||||
|
||||
public virtual DictionaryEntry Entry
|
||||
{
|
||||
get
|
||||
{
|
||||
object currentKey = CurrentKey;
|
||||
return new DictionaryEntry(currentKey, parent.hash[currentKey]);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual object Key => CurrentKey;
|
||||
|
||||
public virtual object Value => parent.hash[CurrentKey];
|
||||
|
||||
private object CurrentKey
|
||||
{
|
||||
get
|
||||
{
|
||||
if (pos < 0 || pos >= parent.keys.Count)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return parent.keys[pos];
|
||||
}
|
||||
}
|
||||
|
||||
internal LinkedDictionaryEnumerator(LinkedDictionary parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public virtual bool MoveNext()
|
||||
{
|
||||
if (pos >= parent.keys.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return ++pos < parent.keys.Count;
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
pos = -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public abstract class UnmodifiableDictionary : IDictionary, ICollection, IEnumerable
|
||||
{
|
||||
public abstract int Count { get; }
|
||||
|
||||
public abstract bool IsFixedSize { get; }
|
||||
|
||||
public virtual bool IsReadOnly => true;
|
||||
|
||||
public abstract bool IsSynchronized { get; }
|
||||
|
||||
public abstract object SyncRoot { get; }
|
||||
|
||||
public abstract ICollection Keys { get; }
|
||||
|
||||
public abstract ICollection Values { get; }
|
||||
|
||||
public virtual object this[object k]
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetValue(k);
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Add(object k, object v)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public abstract bool Contains(object k);
|
||||
|
||||
public abstract void CopyTo(Array array, int index);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public abstract IDictionaryEnumerator GetEnumerator();
|
||||
|
||||
public virtual void Remove(object k)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
protected abstract object GetValue(object k);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public class UnmodifiableDictionaryProxy : UnmodifiableDictionary
|
||||
{
|
||||
private readonly IDictionary d;
|
||||
|
||||
public override int Count => d.Count;
|
||||
|
||||
public override bool IsFixedSize => d.IsFixedSize;
|
||||
|
||||
public override bool IsSynchronized => d.IsSynchronized;
|
||||
|
||||
public override object SyncRoot => d.SyncRoot;
|
||||
|
||||
public override ICollection Keys => d.Keys;
|
||||
|
||||
public override ICollection Values => d.Values;
|
||||
|
||||
public UnmodifiableDictionaryProxy(IDictionary d)
|
||||
{
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
public override bool Contains(object k)
|
||||
{
|
||||
return d.Contains(k);
|
||||
}
|
||||
|
||||
public override void CopyTo(Array array, int index)
|
||||
{
|
||||
d.CopyTo(array, index);
|
||||
}
|
||||
|
||||
public override IDictionaryEnumerator GetEnumerator()
|
||||
{
|
||||
return d.GetEnumerator();
|
||||
}
|
||||
|
||||
protected override object GetValue(object k)
|
||||
{
|
||||
return d[k];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public abstract class UnmodifiableList : IList, ICollection, IEnumerable
|
||||
{
|
||||
public abstract int Count { get; }
|
||||
|
||||
public abstract bool IsFixedSize { get; }
|
||||
|
||||
public virtual bool IsReadOnly => true;
|
||||
|
||||
public abstract bool IsSynchronized { get; }
|
||||
|
||||
public abstract object SyncRoot { get; }
|
||||
|
||||
public virtual object this[int i]
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetValue(i);
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int Add(object o)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public abstract bool Contains(object o);
|
||||
|
||||
public abstract void CopyTo(Array array, int index);
|
||||
|
||||
public abstract IEnumerator GetEnumerator();
|
||||
|
||||
public abstract int IndexOf(object o);
|
||||
|
||||
public virtual void Insert(int i, object o)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public virtual void Remove(object o)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public virtual void RemoveAt(int i)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
protected abstract object GetValue(int i);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public class UnmodifiableListProxy : UnmodifiableList
|
||||
{
|
||||
private readonly IList l;
|
||||
|
||||
public override int Count => l.Count;
|
||||
|
||||
public override bool IsFixedSize => l.IsFixedSize;
|
||||
|
||||
public override bool IsSynchronized => l.IsSynchronized;
|
||||
|
||||
public override object SyncRoot => l.SyncRoot;
|
||||
|
||||
public UnmodifiableListProxy(IList l)
|
||||
{
|
||||
this.l = l;
|
||||
}
|
||||
|
||||
public override bool Contains(object o)
|
||||
{
|
||||
return l.Contains(o);
|
||||
}
|
||||
|
||||
public override void CopyTo(Array array, int index)
|
||||
{
|
||||
l.CopyTo(array, index);
|
||||
}
|
||||
|
||||
public override IEnumerator GetEnumerator()
|
||||
{
|
||||
return l.GetEnumerator();
|
||||
}
|
||||
|
||||
public override int IndexOf(object o)
|
||||
{
|
||||
return l.IndexOf(o);
|
||||
}
|
||||
|
||||
protected override object GetValue(int i)
|
||||
{
|
||||
return l[i];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public abstract class UnmodifiableSet : ISet, ICollection, IEnumerable
|
||||
{
|
||||
public abstract int Count { get; }
|
||||
|
||||
public abstract bool IsEmpty { get; }
|
||||
|
||||
public abstract bool IsFixedSize { get; }
|
||||
|
||||
public virtual bool IsReadOnly => true;
|
||||
|
||||
public abstract bool IsSynchronized { get; }
|
||||
|
||||
public abstract object SyncRoot { get; }
|
||||
|
||||
public virtual void Add(object o)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public virtual void AddAll(IEnumerable e)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public virtual void Clear()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public abstract bool Contains(object o);
|
||||
|
||||
public abstract void CopyTo(Array array, int index);
|
||||
|
||||
public abstract IEnumerator GetEnumerator();
|
||||
|
||||
public virtual void Remove(object o)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public virtual void RemoveAll(IEnumerable e)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
public class UnmodifiableSetProxy : UnmodifiableSet
|
||||
{
|
||||
private readonly ISet s;
|
||||
|
||||
public override int Count => s.Count;
|
||||
|
||||
public override bool IsEmpty => s.IsEmpty;
|
||||
|
||||
public override bool IsFixedSize => s.IsFixedSize;
|
||||
|
||||
public override bool IsSynchronized => s.IsSynchronized;
|
||||
|
||||
public override object SyncRoot => s.SyncRoot;
|
||||
|
||||
public UnmodifiableSetProxy(ISet s)
|
||||
{
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
public override bool Contains(object o)
|
||||
{
|
||||
return s.Contains(o);
|
||||
}
|
||||
|
||||
public override void CopyTo(Array array, int index)
|
||||
{
|
||||
s.CopyTo(array, index);
|
||||
}
|
||||
|
||||
public override IEnumerator GetEnumerator()
|
||||
{
|
||||
return s.GetEnumerator();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user