WeakDictionary.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 WeakDictionary<K,V> where K : class
  17. {
  18. public WeakDictionary()
  19. {}
  20. List<WeakReference> _keys = new List<WeakReference>();
  21. List<V> _values = new List<V>();
  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] = value;
  47. else
  48. {
  49. _values.Add(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];
  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. }
  77. }
  78. }