diff --git a/SawCut/Bin.cs b/SawCut/Bin.cs index c8d3f9e..ea0fc30 100644 --- a/SawCut/Bin.cs +++ b/SawCut/Bin.cs @@ -7,14 +7,36 @@ namespace SawCut { public class Bin { - public List Items; + private readonly List _items; public Bin(double length) { - Items = new List(); + _items = new List(); Length = length; } + public IReadOnlyList Items => _items; + + public void AddItem(BinItem item) + { + _items.Add(item); + } + + public void AddItems(IEnumerable items) + { + _items.AddRange(items); + } + + public void RemoveItem(BinItem item) + { + _items.Remove(item); + } + + public void SortItems(Comparison comparison) + { + _items.Sort(comparison); + } + public double Spacing { get; set; } public double Length { get; set; } diff --git a/SawCut/Nesting/AdvancedFitEngine.cs b/SawCut/Nesting/AdvancedFitEngine.cs index e1f1f61..fd23bcf 100644 --- a/SawCut/Nesting/AdvancedFitEngine.cs +++ b/SawCut/Nesting/AdvancedFitEngine.cs @@ -31,17 +31,17 @@ namespace SawCut.Nesting Items = items.OrderByDescending(i => i.Length).ToList(); - var result = new Result - { - ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList() - }; + var result = new Result(); + var itemsTooLarge = Items.Where(i => i.Length > StockLength).ToList(); + result.AddItemsNotUsed(itemsTooLarge); - Items.RemoveAll(item => result.ItemsNotUsed.Contains(item)); + Items.RemoveAll(item => itemsTooLarge.Contains(item)); CreateBins(); - result.ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList(); - result.Bins = Bins; + var finalItemsTooLarge = Items.Where(i => i.Length > StockLength).ToList(); + result.AddItemsNotUsed(finalItemsTooLarge); + result.AddBins(Bins); foreach (var bin in result.Bins) { @@ -51,7 +51,7 @@ namespace SawCut.Nesting } } - result.ItemsNotUsed.AddRange(Items); + result.AddItemsNotUsed(Items); return result; } @@ -70,8 +70,8 @@ namespace SawCut.Nesting while (TryImprovePacking(bin)) { } - - bin.Items.Sort((a, b) => + + bin.SortItems((a, b) => { int comparison = b.Length.CompareTo(a.Length); return comparison != 0 ? comparison : a.Length.CompareTo(b.Length); @@ -105,7 +105,7 @@ namespace SawCut.Nesting { if (bin.RemainingLength >= Items[i].Length) { - bin.Items.Add(Items[i]); + bin.AddItem(Items[i]); Items.RemoveAt(i); i--; } @@ -130,7 +130,7 @@ namespace SawCut.Nesting foreach (var item in originalBin.Items) { var newItem = Items.FirstOrDefault(a => a.Length == item.Length); - newBin.Items.Add(newItem); + newBin.AddItem(newItem); Items.Remove(newItem); } @@ -167,7 +167,7 @@ namespace SawCut.Nesting { var minRemainingLength = bin.RemainingLength; var firstItem = group.Items.FirstOrDefault(); - bin.Items.Remove(firstItem); + bin.RemoveItem(firstItem); for (int i = 0; i < Items.Count; i++) { @@ -178,7 +178,7 @@ namespace SawCut.Nesting var bin2 = new Bin(bin.RemainingLength); bin2.Spacing = bin.Spacing; - bin2.Items.Add(item1); + bin2.AddItem(item1); for (int j = i + 1; j < Items.Count; j++) { @@ -190,13 +190,13 @@ namespace SawCut.Nesting if (item2.Length > bin2.RemainingLength) continue; - bin2.Items.Add(item2); + bin2.AddItem(item2); } if (bin2.RemainingLength < minRemainingLength) { Items.Add(firstItem); - bin.Items.AddRange(bin2.Items); + bin.AddItems(bin2.Items); foreach (var item in bin2.Items) { @@ -208,13 +208,13 @@ namespace SawCut.Nesting } } - bin.Items.Add(firstItem); + bin.AddItem(firstItem); } return false; } - private List GroupItemsByLength(List items) + private List GroupItemsByLength(IEnumerable items) { var groups = new List(); var groupMap = new Dictionary(); diff --git a/SawCut/Nesting/BestFitEngine.cs b/SawCut/Nesting/BestFitEngine.cs index 2ae4500..a60e0f1 100644 --- a/SawCut/Nesting/BestFitEngine.cs +++ b/SawCut/Nesting/BestFitEngine.cs @@ -23,16 +23,18 @@ namespace SawCut.Nesting Items = items.OrderByDescending(i => i.Length).ToList(); var result = new Result(); - result.ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList(); + var itemsTooLarge = Items.Where(i => i.Length > StockLength).ToList(); + result.AddItemsNotUsed(itemsTooLarge); - foreach (var item in result.ItemsNotUsed) + foreach (var item in itemsTooLarge) { Items.Remove(item); } - result.Bins = GetBins(); + var bins = GetBins(); + result.AddBins(bins); - foreach (var bin in result.Bins) + foreach (var bin in bins) { foreach (var item in bin.Items) { @@ -40,7 +42,7 @@ namespace SawCut.Nesting } } - result.ItemsNotUsed.AddRange(Items); + result.AddItemsNotUsed(Items); return result; } @@ -66,7 +68,7 @@ namespace SawCut.Nesting } if (best_bin != null) - best_bin.Items.Add(item); + best_bin.AddItem(item); } diff --git a/SawCut/Nesting/MultiBinEngine.cs b/SawCut/Nesting/MultiBinEngine.cs index 4cbac29..836c37e 100644 --- a/SawCut/Nesting/MultiBinEngine.cs +++ b/SawCut/Nesting/MultiBinEngine.cs @@ -29,11 +29,11 @@ namespace SawCut.Nesting e.Spacing = Spacing; var r = e.Pack(remainingItems); - result.Bins.AddRange(r.Bins); - remainingItems = r.ItemsNotUsed; + result.AddBins(r.Bins); + remainingItems = r.ItemsNotUsed.ToList(); } - result.ItemsNotUsed = remainingItems; + result.AddItemsNotUsed(remainingItems); return result; } diff --git a/SawCut/Nesting/Result.cs b/SawCut/Nesting/Result.cs index 54b2e93..949593c 100644 --- a/SawCut/Nesting/Result.cs +++ b/SawCut/Nesting/Result.cs @@ -4,14 +4,37 @@ namespace SawCut.Nesting { public class Result { + private readonly List _itemsNotUsed; + private readonly List _bins; + public Result() { - ItemsNotUsed = new List(); - Bins = new List(); + _itemsNotUsed = new List(); + _bins = new List(); } - public List ItemsNotUsed { get; set; } + public IReadOnlyList ItemsNotUsed => _itemsNotUsed; - public List Bins { get; set; } + public IReadOnlyList Bins => _bins; + + public void AddItemNotUsed(BinItem item) + { + _itemsNotUsed.Add(item); + } + + public void AddItemsNotUsed(IEnumerable items) + { + _itemsNotUsed.AddRange(items); + } + + public void AddBin(Bin bin) + { + _bins.Add(bin); + } + + public void AddBins(IEnumerable bins) + { + _bins.AddRange(bins); + } } } \ No newline at end of file