Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2498 | - | 1 | |
2 | namespace GMap.NET.Internals |
||
3 | { |
||
4 | using System.Collections.Generic; |
||
5 | using System.Threading; |
||
6 | using System.Diagnostics; |
||
7 | using System; |
||
8 | |||
9 | /// <summary> |
||
10 | /// matrix for tiles |
||
11 | /// </summary> |
||
12 | internal class TileMatrix : IDisposable |
||
13 | { |
||
14 | List<Dictionary<GPoint, Tile>> Levels = new List<Dictionary<GPoint, Tile>>(33); |
||
15 | FastReaderWriterLock Lock = new FastReaderWriterLock(); |
||
16 | |||
17 | public TileMatrix() |
||
18 | { |
||
19 | for(int i = 0; i < Levels.Capacity; i++) |
||
20 | { |
||
21 | Levels.Add(new Dictionary<GPoint, Tile>(55, new GPointComparer())); |
||
22 | } |
||
23 | } |
||
24 | |||
25 | public void ClearAllLevels() |
||
26 | { |
||
27 | Lock.AcquireWriterLock(); |
||
28 | try |
||
29 | { |
||
30 | foreach(var matrix in Levels) |
||
31 | { |
||
32 | foreach(var t in matrix) |
||
33 | { |
||
34 | t.Value.Dispose(); |
||
35 | } |
||
36 | matrix.Clear(); |
||
37 | } |
||
38 | } |
||
39 | finally |
||
40 | { |
||
41 | Lock.ReleaseWriterLock(); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | public void ClearLevel(int zoom) |
||
46 | { |
||
47 | Lock.AcquireWriterLock(); |
||
48 | try |
||
49 | { |
||
50 | if(zoom < Levels.Count) |
||
51 | { |
||
52 | var l = Levels[zoom]; |
||
53 | |||
54 | foreach(var t in l) |
||
55 | { |
||
56 | t.Value.Dispose(); |
||
57 | } |
||
58 | |||
59 | l.Clear(); |
||
60 | } |
||
61 | } |
||
62 | finally |
||
63 | { |
||
64 | Lock.ReleaseWriterLock(); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | List<KeyValuePair<GPoint, Tile>> tmp = new List<KeyValuePair<GPoint, Tile>>(44); |
||
69 | |||
70 | public void ClearLevelAndPointsNotIn(int zoom, List<DrawTile> list) |
||
71 | { |
||
72 | Lock.AcquireWriterLock(); |
||
73 | try |
||
74 | { |
||
75 | if(zoom < Levels.Count) |
||
76 | { |
||
77 | var l = Levels[zoom]; |
||
78 | |||
79 | tmp.Clear(); |
||
80 | |||
81 | foreach(var t in l) |
||
82 | { |
||
83 | if(!list.Exists(p => p.PosXY == t.Key)) |
||
84 | { |
||
85 | tmp.Add(t); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | foreach(var r in tmp) |
||
90 | { |
||
91 | l.Remove(r.Key); |
||
92 | r.Value.Dispose(); |
||
93 | } |
||
94 | |||
95 | tmp.Clear(); |
||
96 | } |
||
97 | } |
||
98 | finally |
||
99 | { |
||
100 | Lock.ReleaseWriterLock(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | public void ClearLevelsBelove(int zoom) |
||
105 | { |
||
106 | Lock.AcquireWriterLock(); |
||
107 | try |
||
108 | { |
||
109 | if(zoom - 1 < Levels.Count) |
||
110 | { |
||
111 | for(int i = zoom - 1; i >= 0; i--) |
||
112 | { |
||
113 | var l = Levels[i]; |
||
114 | |||
115 | foreach(var t in l) |
||
116 | { |
||
117 | t.Value.Dispose(); |
||
118 | } |
||
119 | |||
120 | l.Clear(); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | finally |
||
125 | { |
||
126 | Lock.ReleaseWriterLock(); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public void ClearLevelsAbove(int zoom) |
||
131 | { |
||
132 | Lock.AcquireWriterLock(); |
||
133 | try |
||
134 | { |
||
135 | if(zoom + 1 < Levels.Count) |
||
136 | { |
||
137 | for(int i = zoom + 1; i < Levels.Count; i++) |
||
138 | { |
||
139 | var l = Levels[i]; |
||
140 | |||
141 | foreach(var t in l) |
||
142 | { |
||
143 | t.Value.Dispose(); |
||
144 | } |
||
145 | |||
146 | l.Clear(); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | finally |
||
151 | { |
||
152 | Lock.ReleaseWriterLock(); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | public void EnterReadLock() |
||
157 | { |
||
158 | Lock.AcquireReaderLock(); |
||
159 | } |
||
160 | |||
161 | public void LeaveReadLock() |
||
162 | { |
||
163 | Lock.ReleaseReaderLock(); |
||
164 | } |
||
165 | |||
166 | public Tile GetTileWithNoLock(int zoom, GPoint p) |
||
167 | { |
||
168 | Tile ret = Tile.Empty; |
||
169 | |||
170 | //if(zoom < Levels.Count) |
||
171 | { |
||
172 | Levels[zoom].TryGetValue(p, out ret); |
||
173 | } |
||
174 | |||
175 | return ret; |
||
176 | } |
||
177 | |||
178 | public Tile GetTileWithReadLock(int zoom, GPoint p) |
||
179 | { |
||
180 | Tile ret = Tile.Empty; |
||
181 | |||
182 | Lock.AcquireReaderLock(); |
||
183 | try |
||
184 | { |
||
185 | ret = GetTileWithNoLock(zoom, p); |
||
186 | } |
||
187 | finally |
||
188 | { |
||
189 | Lock.ReleaseReaderLock(); |
||
190 | } |
||
191 | |||
192 | return ret; |
||
193 | } |
||
194 | |||
195 | public void SetTile(Tile t) |
||
196 | { |
||
197 | Lock.AcquireWriterLock(); |
||
198 | try |
||
199 | { |
||
200 | if(t.Zoom < Levels.Count) |
||
201 | { |
||
202 | Levels[t.Zoom][t.Pos] = t; |
||
203 | } |
||
204 | } |
||
205 | finally |
||
206 | { |
||
207 | Lock.ReleaseWriterLock(); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | #region IDisposable Members |
||
212 | |||
213 | ~TileMatrix() |
||
214 | { |
||
215 | Dispose(false); |
||
216 | } |
||
217 | |||
218 | void Dispose(bool disposing) |
||
219 | { |
||
220 | if(Lock != null) |
||
221 | { |
||
222 | if(disposing) |
||
223 | { |
||
224 | ClearAllLevels(); |
||
225 | } |
||
226 | |||
227 | Levels.Clear(); |
||
228 | Levels = null; |
||
229 | |||
230 | tmp.Clear(); |
||
231 | tmp = null; |
||
232 | |||
233 | Lock.Dispose(); |
||
234 | Lock = null; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | public void Dispose() |
||
239 | { |
||
240 | this.Dispose(true); |
||
241 | GC.SuppressFinalize(this); |
||
242 | } |
||
243 | |||
244 | #endregion |
||
245 | } |
||
246 | } |