Remove debug statements from KCal backends.
[beagle.git] / Util / SemWeb / UriMap.cs
blob4f9c32bbcfc843ac317e469450a4f2fcda3dd7ef
1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
5 namespace SemWeb.Util {
6 internal class UriMap : IDictionary {
7 private static char[] splitchars = { '\\', '/', '#' };
9 Node Root;
10 int count;
12 public UriMap() {
13 Clear();
16 bool ICollection.IsSynchronized { get { return false; } }
17 object ICollection.SyncRoot { get { return null; } }
19 bool IDictionary.IsFixedSize { get { return false; } }
20 bool IDictionary.IsReadOnly { get { return false; } }
22 object IDictionary.this[object key] {
23 get {
24 return this[(string)key];
26 set {
27 this[(string)key] = value;
31 void IDictionary.Add(object key, object value) {
32 this[(string)key] = value;
35 bool IDictionary.Contains(object key) {
36 return Contains((string)key);
39 ICollection IDictionary.Keys { get { throw new NotImplementedException(); } }
40 ICollection IDictionary.Values { get { throw new NotImplementedException(); } }
41 IDictionaryEnumerator IDictionary.GetEnumerator() { throw new NotImplementedException(); }
43 void IDictionary.Remove(object key) {
44 Remove((string)key);
47 void ICollection.CopyTo(Array dest, int index) { throw new NotImplementedException(); }
49 IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
51 public object this[string uri] {
52 get {
53 Node node = FindNode(uri, false);
54 if (node == null) return null;
55 return node.Value;
57 set {
58 Node node = FindNode(uri, true);
59 node.Value = value;
63 public void Clear() {
64 Root = new Node();
65 count = 0;
68 public bool Contains(string uri) {
69 Node node = FindNode(uri, false);
70 return node != null;
73 public int Count { get { return count; } }
75 Node FindNode(string uri, bool create) {
76 Node node = Root;
77 int pos; string piece;
78 NextPieceStart(out pos);
79 while ((piece = NextPiece(uri, ref pos)) != null) {
80 bool created;
81 node = node.FindChild(piece, create, out created);
82 if (created) count++;
83 if (node == null) return null;
86 return node;
89 public void Remove(string uri) {
90 Node node = Root;
92 int pos;
93 NextPieceStart(out pos);
94 string piece = NextPiece(uri, ref pos);
95 string next = NextPiece(uri, ref pos);
96 while (piece != null) {
97 if (next != null) {
98 bool created;
99 node = node.FindChild(piece, false, out created);
100 if (node == null) return;
101 } else {
102 if (node.RemoveChild(piece))
103 count--;
105 piece = next;
106 next = NextPiece(uri, ref pos);
110 void NextPieceStart(out int position) {
111 position = 0;
114 string NextPiece(string uri, ref int position) {
115 if (position == uri.Length) return null;
117 // Get the next stopping position
118 int look = position;
119 if (position == 0 && uri.Length > 15) look = 15;
120 int next = uri.IndexOfAny(splitchars, look);
121 if (next == -1) {
122 string r = uri.Substring(position);
123 position = uri.Length;
124 return r;
127 // Grab sequences of the same character in a row.
128 while (next != uri.Length-1 && uri[next] == uri[next+1])
129 next++;
131 string ret = uri.Substring(position, next-position+1);
132 position = next+1;
134 return ret;
137 private class Node {
138 public IDictionary Children;
139 public object Value;
141 public Node FindChild(string name, bool create, out bool created) {
142 if (Children == null && !create) {
143 created = false;
144 return null;
147 if (Children == null)
148 Children = new HybridDictionary();
150 Node ret = (Node)Children[name];
151 if (ret != null || !create) {
152 created = false;
153 return ret;
154 } else {
155 created = true;
156 ret = new Node();
157 Children[name] = ret;
158 return ret;
162 public bool RemoveChild(string name) {
163 if (Children != null && Children.Contains(name)) {
164 Children.Remove(name);
165 return true;
167 return false;