-
Notifications
You must be signed in to change notification settings - Fork 16
/
CAT_U.cs
107 lines (98 loc) · 2.96 KB
/
CAT_U.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace mapf;
/// <summary>
/// This class represents a union of Dictionaries, which each map <typeparamref name="K"/>
/// to a List of <typeparamref name="V"/>.
/// It is similar to Python's collections.ChainMap, but 1) read-only, 2) a combined list of V
/// elements is returned instead of just the first match.
/// </summary>
[DebuggerDisplay("count = {Count}")]
[Serializable]
public class CAT_U : ConflictAvoidanceTable
{
List<ConflictAvoidanceTable> Data;
public CAT_U()
{
this.Data = new List<ConflictAvoidanceTable>();
}
/// <summary>
/// Gets a combined list of the values mapped to the specified key in the combined dictionaries.
/// </summary>
/// <param name="key">The key to locate.</param>
/// <returns>A combined list of the values mapped to the specified key in the combined dictionaries.</returns>
/// <exception cref="System.ArgumentNullException">key is null.</exception>
/// <exception cref="System.Collections.Generic.KeyNotFoundException">key is not found.</exception>
public new IReadOnlyList<int> this[TimedMove key]
{
get
{
if (Data.Count > 1)
{
var ret = new List<int>();
foreach (ConflictAvoidanceTable cat in Data)
{
if (cat.ContainsKey(key))
ret.AddRange(cat[key]);
}
if (ret.Count == 0)
throw new KeyNotFoundException();
return ret;
}
else
{
return Data.First()[key];
}
}
}
/// <summary>
/// Returns whether the specified key exists in any of the combined dictionaries
/// </summary>
/// <param name="key">The key to look for</param>
/// <returns></returns>
public new bool ContainsKey(TimedMove key)
{
foreach (ConflictAvoidanceTable item in Data)
{
if (item.ContainsKey(key))
return true;
}
return false;
}
/// <summary>
/// Remove all dictionaries from the union. Does not clear them.
/// </summary>
public new void Clear()
{
Data.Clear();
}
/// <summary>
/// Add a dictionary to the union
/// </summary>
/// <param name="other">the dictionary to add</param>
public void Join(ConflictAvoidanceTable other)
{
Data.Add(other);
}
/// <summary>
/// Remove a dictionary from the union
/// </summary>
/// <param name="other">the dictionary to add</param>
public void Separate(ConflictAvoidanceTable other)
{
Data.Remove(other);
}
/// <summary>
/// Gets the number of keys in the Dictionary.
/// </summary>
public int Count
{
get
{
return this.Data.Sum(cat => cat.NumPlans);
}
}
}