forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnorderedMap.cs
More file actions
87 lines (75 loc) · 1.86 KB
/
UnorderedMap.cs
File metadata and controls
87 lines (75 loc) · 1.86 KB
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
using System.Collections.Generic;
namespace Tensorflow.Util
{
public class UnorderedMap<Tk, Tv> : Dictionary<Tk, Tv>
{
/// <summary>
/// Avoid null when accessing not existed element
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public new Tv this[Tk key]
{
get
{
if (!ContainsKey(key))
Add(key, default);
return base[key];
}
set
{
base[key] = value;
}
}
public Tv SetDefault(Tk key, Tv default_value)
{
if(TryGetValue(key, out var res))
{
return res;
}
else
{
base[key] = default_value;
return base[key];
}
}
public void push_back(Tk key, Tv value)
=> this[key] = value;
public void emplace(Tk key, Tv value)
=> this[key] = value;
public bool find(Tk key)
=> ContainsKey(key);
public void erase(Tk key)
=> Remove(key);
public bool find(Tk key, out Tv value)
{
if (ContainsKey(key))
{
value = this[key];
return true;
}
else
{
value = default(Tv);
return false;
}
}
}
public class UnorderedMapEnumerable<Tk, Tv> : UnorderedMap<Tk, Tv>
where Tv : new()
{
public new Tv this[Tk key]
{
get
{
if (!ContainsKey(key))
Add(key, new Tv());
return base[key];
}
set
{
base[key] = value;
}
}
}
}