System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.DataStructures / ImmutableIntMap.cs
1 // 
2 // ImmutableIntMap.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2011 Alexander Chebaturkin
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // 
28
29 using System;
30 using System.Collections.Generic;
31 using System.Linq;
32
33 namespace Mono.CodeContracts.Static.DataStructures {
34         class ImmutableIntMap<T> : IImmutableIntMap<T> {
35                 private readonly Dictionary<int, T> map = new Dictionary<int, T> ();
36
37                 private ImmutableIntMap (Dictionary<int, T> map)
38                 {
39                         this.map = map;
40                 }
41
42                 #region Implementation of IImm  utableIntMap<T>
43                 public T this [int key]
44                 {
45                         get
46                         {
47                                 if (this.map == null || !this.map.ContainsKey (key))
48                                         return default(T);
49
50                                 return this.map [key];
51                         }
52                 }
53
54                 public T Any
55                 {
56                         get
57                         {
58                                 if (this.map == null)
59                                         return default(T);
60                                 return this.map.Values.First ();
61                         }
62                 }
63
64                 public IEnumerable<T> Values
65                 {
66                         get
67                         {
68                                 if (this.map == null)
69                                         return Enumerable.Empty<T> ();
70                                 return this.map.Values;
71                         }
72                 }
73
74                 public IEnumerable<int> Keys
75                 {
76                         get
77                         {
78                                 if (this.map == null)
79                                         return Enumerable.Empty<int> ();
80                                 return this.map.Keys;
81                         }
82                 }
83
84                 public int Count
85                 {
86                         get
87                         {
88                                 if (this.map == null)
89                                         return 0;
90                                 return this.map.Count;
91                         }
92                 }
93
94                 public T Lookup (int key)
95                 {
96                         if (this.map == null)
97                                 return default(T);
98                         return this.map [key];
99                 }
100
101                 public bool Contains (int key)
102                 {
103                         if (this.map == null)
104                                 return false;
105                         return this.map.ContainsKey (key);
106                 }
107
108                 public IImmutableIntMap<T> Add (int key, T value)
109                 {
110                         if (this.map == null)
111                                 return new ImmutableIntMap<T> (new Dictionary<int, T> {{key, value}});
112
113                         var newDict = new Dictionary<int, T> (this.map);
114                         newDict [key] = value;
115                         return new ImmutableIntMap<T> (newDict);
116                 }
117
118                 public IImmutableIntMap<T> Remove (int key)
119                 {
120                         if (this.map == null || !this.map.ContainsKey (key))
121                                 return this;
122
123                         var newDict = new Dictionary<int, T> (this.map);
124                         newDict.Remove (key);
125                         return new ImmutableIntMap<T> (newDict);
126                 }
127
128                 public void Visit (Action<T> action)
129                 {
130                         foreach (T value in Values)
131                                 action (value);
132                 }
133
134                 public void Visit (Action<int, T> action)
135                 {
136                         foreach (int key in Keys)
137                                 action (key, this [key]);
138                 }
139                 #endregion
140
141                 public static IImmutableIntMap<T> Empty ()
142                 {
143                         return new ImmutableIntMap<T> (null);
144                 }
145         }
146 }