6 using System.Collections.Generic;
10 public class Map : IDisposable {
11 private readonly Dictionary<object, object> map =
new Dictionary<object, object>();
13 private readonly ArrayList keys =
new ArrayList();
18 internal Map CreateItem() =>
new Map {cached =
true};
22 public Map Remove(
object key,
bool dispose =
true) {
23 if ((key == null) || !map.ContainsKey(key))
return this;
26 if (dispose) (map[key] as IDisposable)?.
Dispose();
33 public object First =>
Count > 0 ? keys[index.Start()] : null;
36 public object Next => index.Reached(
Count - 1) ? null : keys[index.Next()];
41 public object this[
int i] => keys[i];
44 public object[]
Keys => keys.ToArray();
53 public Map Sort(Comparison<object> comparison) {
54 keys.
Sort(Comparer<object>.Create(comparison));
59 public Map this[
object key] {
68 public Map Add(
object key,
object value = null) {
69 if (key == null)
return this;
71 if (!map.ContainsKey(key)) keys.
Add(key);
90 if (cached) {
Cache<Map>.Dispose(
this); }
else { DeactivateItem(
this); }
93 internal static void DeactivateItem(
Map item) {
94 for (var i = 0; i < item.
Count; i++) (item.map[item.keys[i]] as IDisposable)?.Dispose();
109 var dictionary =
new Dictionary<string, Tv>();
111 for (var key =
First; key != null; key =
Next) dictionary[key.ToString()] =
this[key].Value as Tv;
118 (builder ?? (builder =
new List<string>())).
Clear();
120 for (var key =
First; key != null; key =
Next) builder.Add($
"{key}={this[key].Value}");
122 return string.Join(separator:
", ", value: builder.ToArray());
125 private List<string> builder;
int Count
Number of entries in the map
static Map Instance
//#TBD#//
object Key
Last key searched for
A stack of counters for garbage-free iterations
bool Found
Was the last search successful?
Map Add(object key, object value=null)
Add a map or set entry
object [] Keys
Array of keys in entry/sorted order
object First
Retrieve the value for the first key entered/sorted
override void Dispose()
Send back to recycling
object Next
Retrieve the value for the second key entered/sorted
Map Sort(Comparison< object > comparison)
Sort keys using a comparator
Map Remove(object key, bool dispose=true)
Remove an entry, optionally calling Dispose()
override string ToString()
Map Sort()
Sort keys ascending alphabetic
virtual void Dispose()
Remove all entries - calling Dispose() on each one
static new CounterFifo Instance
Retrieve a new (cached) instance
Dictionary< string, Tv > ToDictionary< Tv >()
//#TBD#//
object Value
Last value found (null for failure)