PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/Collections/BucketSet.cs

https://bitbucket.org/VahidN/interlace
C# | 144 lines | 93 code | 26 blank | 25 comment | 3 complexity | 283abdc3cf10f4cc9e0cbf4e5b929221 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. #endregion
  32. namespace Interlace.Collections
  33. {
  34. public enum BucketLumpType
  35. {
  36. IncludeItemsLessThanComparison = 1,
  37. IncludeItemsGreaterThanComparison = 2
  38. }
  39. public class BucketSet<T> : IEnumerable<Bucket<T>>
  40. {
  41. string _propertyName;
  42. List<Bucket<T>> _list;
  43. bool _hasRemainderBucket = false;
  44. BucketLumpType _lumpType;
  45. public BucketSet(string propertyName, BucketLumpType lumpType)
  46. {
  47. _list = new List<Bucket<T>>();
  48. _propertyName = propertyName;
  49. _lumpType = lumpType;
  50. }
  51. public void AddBucket(IComparable comparisonObject, object tag)
  52. {
  53. Bucket<T> bucket = new Bucket<T>(_propertyName, comparisonObject, _lumpType);
  54. bucket.Tag = tag;
  55. _list.Add(bucket);
  56. }
  57. public void AddBucket(IComparable comparisonObject)
  58. {
  59. _list.Add(new Bucket<T>(_propertyName, comparisonObject, _lumpType));
  60. }
  61. public void AddRemainderBucket(object tag)
  62. {
  63. if (_hasRemainderBucket) throw new InvalidOperationException("This bucket set already has a remainder bucket.");
  64. RemainderBucket<T> bucket = new RemainderBucket<T>();
  65. bucket.Tag = tag;
  66. _list.Add(bucket);
  67. _hasRemainderBucket = true;
  68. }
  69. public void AddRemainderBucket()
  70. {
  71. if (_hasRemainderBucket) throw new InvalidOperationException("This bucket set already has a remainder bucket.");
  72. _list.Add(new RemainderBucket<T>());
  73. _hasRemainderBucket = true;
  74. }
  75. public void FillBuckets(IEnumerable<T> items)
  76. {
  77. _list.Sort();
  78. foreach(T item in items)
  79. {
  80. foreach(Bucket<T> bucket in _list)
  81. {
  82. if (bucket.ItemFitsInBucket(item))
  83. {
  84. bucket.AddItemToBucket(item);
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. public List<Bucket<T>> Buckets
  91. {
  92. get { return _list; }
  93. }
  94. #region IEnumerable<Bucket<T>> Members
  95. public IEnumerator<Bucket<T>> GetEnumerator()
  96. {
  97. return _list.GetEnumerator();
  98. }
  99. #endregion
  100. #region IEnumerable Members
  101. IEnumerator IEnumerable.GetEnumerator()
  102. {
  103. return _list.GetEnumerator();
  104. }
  105. #endregion
  106. public Bucket<T> this[int index]
  107. {
  108. get { return _list[index]; }
  109. }
  110. public void ClearItems()
  111. {
  112. foreach(Bucket<T> bucket in _list)
  113. {
  114. bucket.Items.Clear();
  115. }
  116. }
  117. }
  118. }