corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | /* |
2 | * Copyright (c) 2006-2014, openmetaverse.org |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * - Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions are met: |
||
7 | * |
||
8 | * - Redistributions of source code must retain the above copyright notice, this |
||
9 | * list of conditions and the following disclaimer. |
||
10 | * - Neither the name of the openmetaverse.org nor the names |
||
11 | * of its contributors may be used to endorse or promote products derived from |
||
12 | * this software without specific prior written permission. |
||
13 | * |
||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
24 | * POSSIBILITY OF SUCH DAMAGE. |
||
25 | */ |
||
26 | |||
27 | using System; |
||
28 | using System.Collections.Generic; |
||
29 | using System.Text; |
||
30 | |||
31 | namespace OpenMetaverse |
||
32 | { |
||
33 | public class InventoryNodeDictionary: IComparer<UUID> |
||
34 | { |
||
35 | protected SortedDictionary<UUID, InventoryNode> SDictionary; |
||
36 | protected Dictionary<UUID, InventoryNode> Dictionary = new Dictionary<UUID, InventoryNode>(); |
||
37 | protected InventoryNode parent; |
||
38 | protected object syncRoot = new object(); |
||
39 | public int Compare(UUID id1, UUID id2) |
||
40 | { |
||
41 | InventoryNode n1 = Get(id1); |
||
42 | InventoryNode n2 = Get(id2); |
||
43 | int diff = NullCompare(n1, n2); |
||
44 | if (diff != 0) return diff; |
||
45 | if (n1 == null) return id1.CompareTo(id2); |
||
46 | DateTime t1 = n1.ModifyTime; |
||
47 | DateTime t2 = n2.ModifyTime; |
||
48 | diff = t1.CompareTo(t2); |
||
49 | if (diff != 0) return diff; |
||
50 | var d1 = n1.Data; |
||
51 | var d2 = n2.Data; |
||
52 | diff = NullCompare(d1, d2); |
||
53 | if (diff != 0) return diff; |
||
54 | if (d1 != null) |
||
55 | { |
||
56 | diff = NullCompare(d1.Name, d2.Name); |
||
57 | if (diff != 0) return diff; |
||
58 | if (d1.Name != null) |
||
59 | { |
||
60 | // both are not null.. due to NullCoimpare code |
||
61 | diff = d1.Name.CompareTo(d2.Name); |
||
62 | if (diff != 0) return diff; |
||
63 | } |
||
64 | } |
||
65 | return id1.CompareTo(id2); |
||
66 | } |
||
67 | |||
68 | private InventoryNode Get(UUID uuid) |
||
69 | { |
||
70 | InventoryNode val; |
||
71 | if (Dictionary.TryGetValue(uuid, out val)) |
||
72 | { |
||
73 | return val; |
||
74 | } |
||
75 | return null; |
||
76 | } |
||
77 | |||
78 | static int NullCompare(object o1, object o2) |
||
79 | { |
||
80 | return ReferenceEquals(o1, null).CompareTo(ReferenceEquals(o2, null)); |
||
81 | } |
||
82 | |||
83 | public InventoryNode Parent |
||
84 | { |
||
85 | get { return parent; } |
||
86 | set { parent = value; } |
||
87 | } |
||
88 | |||
89 | public object SyncRoot { get { return syncRoot; } } |
||
90 | |||
91 | public int Count { get { return Dictionary.Count; } } |
||
92 | |||
93 | public InventoryNodeDictionary(InventoryNode parentNode) |
||
94 | { |
||
95 | if (Settings.SORT_INVENTORY) SDictionary = new SortedDictionary<UUID, InventoryNode>(this); |
||
96 | parent = parentNode; |
||
97 | } |
||
98 | |||
99 | public InventoryNode this[UUID key] |
||
100 | { |
||
101 | get { return (InventoryNode)this.Dictionary[key]; } |
||
102 | set |
||
103 | { |
||
104 | value.Parent = parent; |
||
105 | lock (syncRoot) |
||
106 | { |
||
107 | Dictionary[key] = value; |
||
108 | if (Settings.SORT_INVENTORY) this.SDictionary[key] = value; |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | public ICollection<UUID> Keys |
||
114 | { |
||
115 | get |
||
116 | { |
||
117 | if (Settings.SORT_INVENTORY) return this.SDictionary.Keys; |
||
118 | return Dictionary.Keys; |
||
119 | } |
||
120 | } |
||
121 | public ICollection<InventoryNode> Values |
||
122 | { |
||
123 | get |
||
124 | { |
||
125 | if (Settings.SORT_INVENTORY) return this.SDictionary.Values; |
||
126 | return this.Dictionary.Values; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public void Add(UUID key, InventoryNode value) |
||
131 | { |
||
132 | value.Parent = parent; |
||
133 | lock (syncRoot) |
||
134 | { |
||
135 | Dictionary[key] = value; |
||
136 | if (Settings.SORT_INVENTORY) this.SDictionary.Add(key, value); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | public void Remove(UUID key) |
||
141 | { |
||
142 | lock (syncRoot) |
||
143 | { |
||
144 | this.Dictionary.Remove(key); |
||
145 | if (Settings.SORT_INVENTORY) this.SDictionary.Remove(key); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | public bool Contains(UUID key) |
||
150 | { |
||
151 | return this.Dictionary.ContainsKey(key); |
||
152 | } |
||
153 | |||
154 | internal void Sort() |
||
155 | { |
||
156 | if (Settings.SORT_INVENTORY) |
||
157 | { |
||
158 | // TODO resort SDictionary now that more data has come? |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |