|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.IO; |
|
19 using System.Collections.Generic; |
|
20 using SymbianUtils.Range; |
|
21 |
|
22 namespace SymbianUtils.Range |
|
23 { |
|
24 public class AddressRangeCollection : IComparer<AddressRange> |
|
25 { |
|
26 #region Constructors |
|
27 public AddressRangeCollection() |
|
28 { |
|
29 |
|
30 } |
|
31 |
|
32 public AddressRangeCollection( IEnumerable<AddressRange> aList ) |
|
33 { |
|
34 foreach ( AddressRange entry in aList ) |
|
35 { |
|
36 Add( entry ); |
|
37 } |
|
38 } |
|
39 #endregion |
|
40 |
|
41 #region API |
|
42 public void Add( AddressRange aRange ) |
|
43 { |
|
44 int pos = iRange.BinarySearch( aRange, this ); |
|
45 |
|
46 // If not already added... |
|
47 if ( pos < 0 ) |
|
48 { |
|
49 pos = ~pos; |
|
50 |
|
51 int prevItemPos = pos - 1; |
|
52 if ( prevItemPos >= 0 ) |
|
53 { |
|
54 AddressRange last = iRange[ pos - 1 ]; |
|
55 if ( last.Max + 1 == aRange.Min ) |
|
56 { |
|
57 last.UpdateMax( aRange.Max ); |
|
58 } |
|
59 else |
|
60 { |
|
61 iRange.Insert( pos, aRange ); |
|
62 } |
|
63 } |
|
64 else |
|
65 { |
|
66 iRange.Insert( pos, aRange ); |
|
67 } |
|
68 } |
|
69 } |
|
70 |
|
71 public bool Contains( uint aValue ) |
|
72 { |
|
73 bool ret = false; |
|
74 // |
|
75 AddressRange temp = new AddressRange( aValue, aValue ); |
|
76 int pos = NearestIndexOf( temp ); |
|
77 if ( pos >= 0 ) |
|
78 { |
|
79 ret = true; |
|
80 } |
|
81 // |
|
82 return ret; |
|
83 } |
|
84 |
|
85 public void Clear() |
|
86 { |
|
87 iRange.Clear(); |
|
88 } |
|
89 #endregion |
|
90 |
|
91 #region Properties |
|
92 public int Count |
|
93 { |
|
94 get { return iRange.Count; } |
|
95 } |
|
96 |
|
97 public AddressRange this[ int aIndex ] |
|
98 { |
|
99 get { return iRange[ aIndex ]; } |
|
100 } |
|
101 |
|
102 public AddressRange RangeFirst |
|
103 { |
|
104 get |
|
105 { |
|
106 AddressRange ret = null; |
|
107 // |
|
108 if ( iRange.Count > 0 ) |
|
109 { |
|
110 ret = iRange[ 0 ]; |
|
111 } |
|
112 // |
|
113 return ret; |
|
114 } |
|
115 } |
|
116 |
|
117 public AddressRange RangeLast |
|
118 { |
|
119 get |
|
120 { |
|
121 AddressRange ret = null; |
|
122 // |
|
123 int count = iRange.Count; |
|
124 if ( count > 0 ) |
|
125 { |
|
126 ret = iRange[ count - 1 ]; |
|
127 } |
|
128 // |
|
129 return ret; |
|
130 } |
|
131 } |
|
132 #endregion |
|
133 |
|
134 #region Internal methods |
|
135 private int NearestIndexOf( AddressRange aRange ) |
|
136 { |
|
137 int pos = iRange.BinarySearch( aRange, this ); |
|
138 return pos; |
|
139 } |
|
140 #endregion |
|
141 |
|
142 #region From IComparer<AddressRange> |
|
143 public int Compare( AddressRange aLeft, AddressRange aRight ) |
|
144 { |
|
145 int ret = -1; |
|
146 // |
|
147 if ( aLeft.Min > aRight.Max ) |
|
148 { |
|
149 ret = 1; |
|
150 } |
|
151 else if ( aLeft.Contains( aRight ) || aRight.Contains( aLeft ) ) |
|
152 { |
|
153 ret = 0; |
|
154 } |
|
155 // |
|
156 return ret; |
|
157 } |
|
158 #endregion |
|
159 |
|
160 #region Data members |
|
161 private List<AddressRange> iRange = new List<AddressRange>(); |
|
162 #endregion |
|
163 } |
|
164 } |