/// <summary> /// Solves the backpacking problem, and prints out information /// about the solution. /// </summary> public void Run() { Solve(TheBackPack.WeightCapacityLeft); TheBackPack.PrintContent(); System.Console.WriteLine(); TheVault.PrintContent(); }
public static void AlsoNeedHelp() { using (RwLck lck = TheVault.Lock()) { IllegalWrapper wrapper = default; wrapper.SetLockedResource(in lck); lck.AppendLine("Goodbye cruel world!"); Console.WriteLine(wrapper.WrappedLockedResource.ToString()); } }
public override void Solve(double capacityLeft) { string description = PickNextItemFromVault(capacityLeft); if (description != string.Empty) { BackPackItem item = TheVault.RemoveItem(description); TheBackPack.AddItem(item); Solve(TheBackPack.WeightCapacityLeft); } }
/// <summary> /// Naive solver implementation (but it's recursive!) /// </summary> public override void Solve(double capacityLeft) { // Keep adding the first element from the vault, until // the first element cannot fit into the backpack... if (TheVault.Items[0].Weight <= capacityLeft) { BackPackItem item = TheVault.RemoveItem(TheVault.Items[0].Description); TheBackPack.AddItem(item); Solve(TheBackPack.WeightCapacityLeft); } }
//internal static void ShowMoreDetailedBug92Fix() //{ // //protected resources are now decorated with the NoCopy attribute. // //If a ref struct contains a field of the same type as the protected resource, // //it is now prohibited to allow these two to overlap in scope. // //ref struct wrapper with containing non-static field of type StringBuilderRwLockedResource // RwLockWrapper wrapperCopy=default; // { // using (var lck = TheVault.Lock()) // { // lck.AppendLine("We don't need no education ... "); // //Bug 92 fix -- uncommenting the following line triggers compilation error // //bug 92 fix -- a ref struct that has a non-static field of the same type as a locked // //bug 92 fix -- resource object, may not appear in the same scope as the locked // //bug 92 fix -- resource object, hence, following line will cause compilation error. // using (wrapperCopy = new RwLockWrapper(in lck, "Foobar")) // { // Console.WriteLine(wrapperCopy.ToString()); // } // } // } // //the locked resource has been returned to vault and this line causes unsynchronized access // Console.WriteLine(wrapperCopy.ToString()); //} //internal static void ShowMoreDetailedBug92AnotherVariation() //{ // //protected resources are now decorated with the NoCopy attribute. // //If a ref struct contains a field of the same type as the protected resource, // //it is now prohibited to allow these two to overlap in scope. // //ref struct wrapper with containing non-static field of type StringBuilderRwLockedResource // string fizz; // { // using (var lck = TheVault.Lock()) // { // lck.AppendLine("We don't need no education ... "); // //Bug 92 fix -- uncommenting the following line triggers compilation error // //bug 92 fix -- a ref struct that has a non-static field of the same type as a locked // //bug 92 fix -- resource object, may not appear in the same scope as the locked // //bug 92 fix -- resource object, hence, following line will cause compilation error. // var wrapperCopy = new RwLockWrapper(in lck, "Foobar"); // fizz = wrapperCopy.ToString(); // } // } // //the locked resource has been returned to vault and this line causes unsynchronized access // Console.WriteLine(fizz); //} internal static void Bug92StillAProblem() { using var lck = TheVault.Lock(); lck.AppendLine("We don't need no education ... "); //Bug 92 fix -- uncommenting the following line triggers compilation error //bug 92 fix -- a ref struct that has a non-static field of the same type as a locked //bug 92 fix -- resource object, may not appear in the same scope as the locked //bug 92 fix -- resource object, hence, following line will cause compilation error. //using RwLockWrapper wrapperCopy = new RwLockWrapper(in lck, "Foobar"); //Console.WriteLine(wrapperCopy.ToString()); }
public static void HelpMe() { using RwLck lck = TheVault.Lock(); //bad IllegalWrapper wrapper = IllegalWrapper.CreateCopyInSneakyWay(in lck, DateTime.Now); //also bad wrapper.SetLockedResource(in lck); lck.AppendLine("Hi mom!"); Console.WriteLine(wrapper.WrappedLockedResource.ToString()); }
public override void Solve(double capacityLeft) { //sort by weight List <BackPackItem> items = new List <BackPackItem>(); foreach (BackPackItem item in TheVault.Items) { items.Add(item); } items = items.OrderBy(o => o.Weight).ToList(); items.Reverse(); if (items[0].Weight <= capacityLeft) { BackPackItem item = TheVault.RemoveItem(items[0].Description); //vault remove item TheBackPack.AddItem(items[0]); //backpack add item Solve(TheBackPack.WeightCapacityLeft); //recursive } }
public override void Solve(double capacityLeft) { BackPackItem currentItem = TheVault.Items[0]; foreach (BackPackItem item in TheVault.Items) { if ((item.Value / item.Weight) > (currentItem.Value / currentItem.Weight)) { currentItem = item; } } if (currentItem.Weight <= capacityLeft) { TheVault.RemoveItem(currentItem.Description); TheBackPack.AddItem(currentItem); Solve(TheBackPack.WeightCapacityLeft); } else //most valuable item (by ratio) weighs more than we have room for { List <BackPackItem> lowWeightItems = new List <BackPackItem>(); foreach (BackPackItem item in TheVault.Items) { if (item.Weight < capacityLeft) { lowWeightItems.Add(item); } } lowWeightItems = lowWeightItems.OrderBy(o => o.Value).ToList(); if (lowWeightItems.Count > 0) { if (lowWeightItems[0].Weight <= capacityLeft) { TheVault.RemoveItem(lowWeightItems[0].Description); TheBackPack.AddItem(lowWeightItems[0]); Solve(TheBackPack.WeightCapacityLeft); } } } }
/// <summary> /// Locked resource objects should never be passed by value ... takes too long and /// is dangerous /// </summary> internal static void ShowPassByValueNowForbidden() { using (RoLock lck = TheVault.RoLock()) { //Pass by value... bad ... now will not compile //Extensions.BadPrint3(lck); //passed by mutable reference ... now will not compile //Extensions.BadPrint4(ref lck); //passed by write-mandatory mutable reference ... now will not compile //Extensions.BadPrint5(out lck); //this is ok -- passed with 'in' Extensions.OkPrint(in lck); //you will always have to use in parameter... this won't compile //Extensions.OkPrint(lck); //this is fine too ... passed by 'in' //Extensions.OkPrint("Toodles", in lck); //it still catches non-in passing if name-colon syntax used ... now will not compile //Extensions.OkPrint(roLock: lck, text: "tiddly winks"); //of course if you use 'in' with the name colon it's ok Extensions.OkPrint(roLock: in lck, text: "tiddly winks"); //Still works if extension method ... //Can't pass by value to extension method ... will no longer compile //lck.BadPrint(); //can't pass by mutable reference to extension method //lck.BadPrint2(); //will no longer compile //This is the correct way for extension method! //extension method with signature: //"void OkPrintToo(this in RoLock roLock);" lck.OkPrintToo(); } { //all same problems detected if using declaration using RoLock lck = TheVault.RoLock(); //Pass by value... bad ... now will not compile //Extensions.BadPrint3(lck); //passed by mutable reference ... now will not compile //Extensions.BadPrint4(ref lck); //passed by write-mandatory mutable reference ... now will not compile //Extensions.BadPrint5(out lck); //this is ok -- passed with 'in' Extensions.OkPrint(in lck); //you will always have to use in parameter... this won't compile //Extensions.OkPrint(lck); //this is fine too ... passed by 'in' Extensions.OkPrint("Toodles", in lck); //it still catches non-in passing if name-colon syntax used ... now will not compile //Extensions.OkPrint(roLock: lck, text: "tiddly winks"); //of course if you use 'in' with the name colon it's ok Extensions.OkPrint(roLock: in lck, text: "tiddly winks"); //Still works if extension method ... //Can't pass by value to extension method ... will no longer compile //lck.BadPrint(); //can't pass by mutable reference to extension method //lck.BadPrint2(); //will no longer compile //This is the correct way for extension method! //extension method with signature: //"void OkPrintToo(this in RoLock roLock);" lck.OkPrintToo(); } }
/// <summary> /// Solves the backpacking problem, and prints out information /// about the solution. /// </summary> public void Run() { Solve(TheBackPack.WeightCapacityLeft); TheBackPack.PrintContent(); TheVault.PrintContent(); }