FullWeakDictionary.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. namespace Xceed.Wpf.AvalonDock.Controls
  15. {
  16. class FullWeakDictionary<K,V> where K : class
  17. {
  18. public FullWeakDictionary()
  19. {}
  20. List<WeakReference> _keys = new List<WeakReference>();
  21. List<WeakReference> _values = new List<WeakReference>();
  22. public V this[K key]
  23. {
  24. get
  25. {
  26. V valueToReturn;
  27. if (!GetValue(key, out valueToReturn))
  28. throw new ArgumentException();
  29. return valueToReturn;
  30. }
  31. set
  32. {
  33. SetValue(key, value);
  34. }
  35. }
  36. public bool ContainsKey(K key)
  37. {
  38. CollectGarbage();
  39. return -1 != _keys.FindIndex(k => k.GetValueOrDefault<K>() == key);
  40. }
  41. public void SetValue(K key, V value)
  42. {
  43. CollectGarbage();
  44. int vIndex = _keys.FindIndex(k => k.GetValueOrDefault<K>() == key);
  45. if (vIndex > -1)
  46. _values[vIndex] = new WeakReference(value);
  47. else
  48. {
  49. _values.Add(new WeakReference(value));
  50. _keys.Add(new WeakReference(key));
  51. }
  52. }
  53. public bool GetValue(K key, out V value)
  54. {
  55. CollectGarbage();
  56. int vIndex = _keys.FindIndex(k => k.GetValueOrDefault<K>() == key);
  57. value = default(V);
  58. if (vIndex == -1)
  59. return false;
  60. value = _values[vIndex].GetValueOrDefault<V>();
  61. return true;
  62. }
  63. void CollectGarbage()
  64. {
  65. int vIndex = 0;
  66. do
  67. {
  68. vIndex = _keys.FindIndex(vIndex, k => !k.IsAlive);
  69. if (vIndex >= 0)
  70. {
  71. _keys.RemoveAt(vIndex);
  72. _values.RemoveAt(vIndex);
  73. }
  74. }
  75. while (vIndex >= 0);
  76. vIndex = 0;
  77. do
  78. {
  79. vIndex = _values.FindIndex(vIndex, v => !v.IsAlive);
  80. if (vIndex >= 0)
  81. {
  82. _values.RemoveAt(vIndex);
  83. _keys.RemoveAt(vIndex);
  84. }
  85. }
  86. while (vIndex >= 0);
  87. }
  88. }
  89. }