* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Runtime.Serialization / System.Xml / XmlDictionary.cs
1 #if NET_2_0
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5
6 namespace System.Xml
7 {
8         public class XmlDictionary : IXmlDictionary
9         {
10                 internal class EmptyDictionary : XmlDictionary
11                 {
12                         public static readonly EmptyDictionary Instance =
13                                 new EmptyDictionary ();
14
15                         public EmptyDictionary ()
16                                 : base (1)
17                         {
18                         }
19                 }
20
21                 static XmlDictionary empty = new XmlDictionary (true);
22
23                 public static XmlDictionary Empty {
24                         get { return empty; }
25                 }
26
27                 readonly bool is_readonly;
28                 Dictionary<string, XmlDictionaryString> dict;
29                 List<XmlDictionaryString> list;
30
31                 public XmlDictionary ()
32                 {
33                         dict = new Dictionary<string, XmlDictionaryString> ();
34                         list = new List<XmlDictionaryString> ();
35                 }
36
37                 public XmlDictionary (int capacity)
38                 {
39                         dict = new Dictionary<string, XmlDictionaryString> (capacity);
40                         list = new List<XmlDictionaryString> (capacity);
41                 }
42
43                 // for static empty.
44                 private XmlDictionary (bool isReadOnly)
45                         : this (1)
46                 {
47                         is_readonly = isReadOnly;
48                 }
49
50                 public virtual XmlDictionaryString Add (string value)
51                 {
52                         if (is_readonly)
53                                 throw new InvalidOperationException ();
54                         XmlDictionaryString ret;
55                         if (dict.TryGetValue (value, out ret))
56                                 return ret;
57                         ret = new XmlDictionaryString (this, value, dict.Count);
58                         dict.Add (value, ret);
59                         list.Add (ret);
60                         return ret;
61                 }
62
63                 public bool TryLookup (int key, out XmlDictionaryString result)
64                 {
65                         if (key < 0 || dict.Count >= key) {
66                                 result = null;
67                                 return false;
68                         }
69                         result = list [key];
70                         return true;
71                 }
72
73                 public bool TryLookup (string value, out XmlDictionaryString result)
74                 {
75                         if (value == null)
76                                 throw new ArgumentNullException ();
77                         result = dict [value];
78                         return result != null;
79                 }
80
81                 public bool TryLookup (XmlDictionaryString value,
82                         out XmlDictionaryString result)
83                 {
84                         if (value == null)
85                                 throw new ArgumentNullException ();
86                         if (value.Dictionary != this) {
87                                 result = null;
88                                 return false;
89                         }
90                         for (int i = 0; i < list.Count; i++) {
91                                 if (object.ReferenceEquals (list [i], value)) {
92                                         result = value;
93                                         return true;
94                                 }
95                         }
96                         result = null;
97                         return false;
98                 }
99         }
100 }
101 #endif