1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2019 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using KeePassLib.Interfaces;
namespace KeePassLib.Collections
{
/// <summary>
/// List of objects that implement <c>IDeepCloneable</c>,
/// and cannot be <c>null</c>.
/// </summary>
/// <typeparam name="T">Type specifier.</typeparam>
public sealed class PwObjectList<T> : IEnumerable<T>
where T : class, IDeepCloneable<T>
{
private List<T> m_vObjects = new List<T>();
/// <summary>
/// Get number of objects in this list.
/// </summary>
public uint UCount
{
get { return (uint)m_vObjects.Count; }
}
/// <summary>
/// Construct a new list of objects.
/// </summary>
public PwObjectList()
{
}
IEnumerator IEnumerable.GetEnumerator()
{
return m_vObjects.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return m_vObjects.GetEnumerator();
}
public void Clear()
{
// Do not destroy contained objects!
m_vObjects.Clear();
}
/// <summary>
/// Clone the current <c>PwObjectList</c>, including all
/// stored objects (deep copy).
/// </summary>
/// <returns>New <c>PwObjectList</c>.</returns>
public PwObjectList<T> CloneDeep()
{
PwObjectList<T> pl = new PwObjectList<T>();
foreach(T po in m_vObjects)
pl.Add(po.CloneDeep());
return pl;
}
public PwObjectList<T> CloneShallow()
{
PwObjectList<T> tNew = new PwObjectList<T>();
foreach(T po in m_vObjects) tNew.Add(po);
return tNew;
}
public List<T> CloneShallowToList()
{
PwObjectList<T> tNew = CloneShallow();
return tNew.m_vObjects;
}
/// <summary>
/// Add an object to this list.
/// </summary>
/// <param name="pwObject">Object to be added.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the input
/// parameter is <c>null</c>.</exception>
public void Add(T pwObject)
{
Debug.Assert(pwObject != null);
if(pwObject == null) throw new ArgumentNullException("pwObject");
m_vObjects.Add(pwObject);
}
public void Add(PwObjectList<T> vObjects)
{
Debug.Assert(vObjects != null);
if(vObjects == null) throw new ArgumentNullException("vObjects");
foreach(T po in vObjects)
{
m_vObjects.Add(po);
}
}
public void Add(List<T> vObjects)
{
Debug.Assert(vObjects != null);
if(vObjects == null) throw new ArgumentNullException("vObjects");
foreach(T po in vObjects)
{
m_vObjects.Add(po);
}
}
public void Insert(uint uIndex, T pwObject)
{
Debug.Assert(pwObject != null);
if(pwObject == null) throw new ArgumentNullException("pwObject");
m_vObjects.Insert((int)uIndex, pwObject);
}
/// <summary>
/// Get an object of the list.
/// </summary>
/// <param name="uIndex">Index of the object to get. Must be valid, otherwise an
/// exception is thrown.</param>
/// <returns>Reference to an existing <c>T</c> object. Is never <c>null</c>.</returns>
public T GetAt(uint uIndex)
{
Debug.Assert(uIndex < m_vObjects.Count);
if(uIndex >= m_vObjects.Count) throw new ArgumentOutOfRangeException("uIndex");
return m_vObjects[(int)uIndex];
}
public void SetAt(uint uIndex, T pwObject)
{
Debug.Assert(pwObject != null);
if(pwObject == null) throw new ArgumentNullException("pwObject");
if(uIndex >= (uint)m_vObjects.Count)
throw new ArgumentOutOfRangeException("uIndex");
m_vObjects[(int)uIndex] = pwObject;
}
/// <summary>
/// Get a range of objects.
/// </summary>
/// <param name="uStartIndexIncl">Index of the first object to be
/// returned (inclusive).</param>
/// <param name="uEndIndexIncl">Index of the last object to be
/// returned (inclusive).</param>
/// <returns></returns>
public List<T> GetRange(uint uStartIndexIncl, uint uEndIndexIncl)
{
if(uStartIndexIncl >= (uint)m_vObjects.Count)
throw new ArgumentOutOfRangeException("uStartIndexIncl");
if(uEndIndexIncl >= (uint)m_vObjects.Count)
throw new ArgumentOutOfRangeException("uEndIndexIncl");
if(uStartIndexIncl > uEndIndexIncl)
throw new ArgumentException();
List<T> list = new List<T>((int)(uEndIndexIncl - uStartIndexIncl) + 1);
for(uint u = uStartIndexIncl; u <= uEndIndexIncl; ++u)
{
list.Add(m_vObjects[(int)u]);
}
return list;
}
public int IndexOf(T pwReference)
{
Debug.Assert(pwReference != null); if(pwReference == null) throw new ArgumentNullException("pwReference");
return m_vObjects.IndexOf(pwReference);
}
/// <summary>
/// Delete an object of this list. The object to be deleted is identified
/// by a reference handle.
/// </summary>
/// <param name="pwReference">Reference of the object to be deleted.</param>
/// <returns>Returns <c>true</c> if the object was deleted, <c>false</c> if
/// the object wasn't found in this list.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if the input
/// parameter is <c>null</c>.</exception>
public bool Remove(T pwReference)
{
Debug.Assert(pwReference != null); if(pwReference == null) throw new ArgumentNullException("pwReference");
return m_vObjects.Remove(pwReference);
}
public void RemoveAt(uint uIndex)
{
m_vObjects.RemoveAt((int)uIndex);
}
/// <summary>
/// Move an object up or down.
/// </summary>
/// <param name="tObject">The object to be moved.</param>
/// <param name="bUp">Move one up. If <c>false</c>, move one down.</param>
public void MoveOne(T tObject, bool bUp)
{
Debug.Assert(tObject != null);
if(tObject == null) throw new ArgumentNullException("tObject");
int nCount = m_vObjects.Count;
if(nCount <= 1) return;
int nIndex = m_vObjects.IndexOf(tObject);
if(nIndex < 0) { Debug.Assert(false); return; }
if(bUp && (nIndex > 0)) // No assert for top item
{
T tTemp = m_vObjects[nIndex - 1];
m_vObjects[nIndex - 1] = m_vObjects[nIndex];
m_vObjects[nIndex] = tTemp;
}
else if(!bUp && (nIndex != (nCount - 1))) // No assert for bottom item
{
T tTemp = m_vObjects[nIndex + 1];
m_vObjects[nIndex + 1] = m_vObjects[nIndex];
m_vObjects[nIndex] = tTemp;
}
}
public void MoveOne(T[] vObjects, bool bUp)
{
Debug.Assert(vObjects != null);
if(vObjects == null) throw new ArgumentNullException("vObjects");
List<int> lIndices = new List<int>();
foreach(T t in vObjects)
{
if(t == null) { Debug.Assert(false); continue; }
int p = IndexOf(t);
if(p >= 0) lIndices.Add(p);
else { Debug.Assert(false); }
}
MoveOne(lIndices.ToArray(), bUp);
}
public void MoveOne(int[] vIndices, bool bUp)
{
Debug.Assert(vIndices != null);
if(vIndices == null) throw new ArgumentNullException("vIndices");
int n = m_vObjects.Count;
if(n <= 1) return; // No moving possible
int m = vIndices.Length;
if(m == 0) return; // Nothing to move
int[] v = new int[m];
Array.Copy(vIndices, v, m);
Array.Sort<int>(v);
if((bUp && (v[0] <= 0)) || (!bUp && (v[m - 1] >= (n - 1))))
return; // Moving as a block is not possible
int iStart = (bUp ? 0 : (m - 1));
int iExcl = (bUp ? m : -1);
int iStep = (bUp ? 1 : -1);
for(int i = iStart; i != iExcl; i += iStep)
{
int p = v[i];
if((p < 0) || (p >= n)) { Debug.Assert(false); continue; }
T t = m_vObjects[p];
if(bUp)
{
Debug.Assert(p > 0);
m_vObjects.RemoveAt(p);
m_vObjects.Insert(p - 1, t);
}
else // Down
{
Debug.Assert(p < (n - 1));
m_vObjects.RemoveAt(p);
m_vObjects.Insert(p + 1, t);
}
}
}
/// <summary>
/// Move some of the objects in this list to the top/bottom.
/// </summary>
/// <param name="vObjects">List of objects to be moved.</param>
/// <param name="bTop">Move to top. If <c>false</c>, move to bottom.</param>
public void MoveTopBottom(T[] vObjects, bool bTop)
{
Debug.Assert(vObjects != null);
if(vObjects == null) throw new ArgumentNullException("vObjects");
if(vObjects.Length == 0) return;
int nCount = m_vObjects.Count;
foreach(T t in vObjects) m_vObjects.Remove(t);
if(bTop)
{
int nPos = 0;
foreach(T t in vObjects)
{
m_vObjects.Insert(nPos, t);
++nPos;
}
}
else // Move to bottom
{
foreach(T t in vObjects) m_vObjects.Add(t);
}
Debug.Assert(nCount == m_vObjects.Count);
if(nCount != m_vObjects.Count)
throw new ArgumentException("At least one of the T objects in the vObjects list doesn't exist!");
}
public void Sort(IComparer<T> tComparer)
{
if(tComparer == null) throw new ArgumentNullException("tComparer");
m_vObjects.Sort(tComparer);
}
public void Sort(Comparison<T> tComparison)
{
if(tComparison == null) throw new ArgumentNullException("tComparison");
m_vObjects.Sort(tComparison);
}
public static PwObjectList<T> FromArray(T[] tArray)
{
if(tArray == null) throw new ArgumentNullException("tArray");
PwObjectList<T> l = new PwObjectList<T>();
foreach(T t in tArray) { l.Add(t); }
return l;
}
public static PwObjectList<T> FromList(List<T> tList)
{
if(tList == null) throw new ArgumentNullException("tList");
PwObjectList<T> l = new PwObjectList<T>();
l.Add(tList);
return l;
}
}
}
|