Last active
October 10, 2021 00:42
-
-
Save hyrmn/387e9e8d4e2858daf5e89097396b88fb to your computer and use it in GitHub Desktop.
Ways to swap numbers (.NET 6.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
int x = 10; | |
int y = 20; | |
var tmp = x; | |
x = y; | |
y = tmp; | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
int x = 10; | |
int y = 20; | |
x = x + y; | |
y = x - y; | |
x = x - y; | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Don't do this | |
using System.Diagnostics; | |
int x = 10; | |
int y = 20; | |
x = x ^ y; | |
y = x ^ y; | |
x = x ^ y; | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
int x = 10; | |
int y = 20; | |
int[] scratch = new int[2]; | |
scratch[0] = x; | |
scratch[1] = y; | |
x = scratch[1]; | |
y = scratch[0]; | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
int x = 10; | |
int y = 20; | |
Swap(ref x, ref y); | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); | |
void Swap(ref int x, ref int y) | |
{ | |
x = x + y; | |
y = x - y; | |
x = x - y; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using obvious domain language and value based objects (int is a value) always leads to cleaner understanding | |
using System.Diagnostics; | |
int x = 10; | |
int y = 20; | |
var swappedIntegers = Swap(x, y); | |
Debug.Assert(swappedIntegers.newX == 20); | |
Debug.Assert(swappedIntegers.newY == 10); | |
(int newX, int newY) Swap(int x, int y) => (y, x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
var xAndY = new[] { 10, 20 }; | |
int? x = null; | |
int? y = null; | |
var chooser = new Random(); | |
while(x is null || y is null) | |
{ | |
//will choose 0 or 1 | |
var choice = chooser.Next(2); | |
if (xAndY[choice] == 20) x = xAndY[choice]; | |
if (xAndY[choice] == 10) y = xAndY[choice]; | |
} | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Courtesy of https://twitter.com/augustoproiete | |
using System; | |
using System.Diagnostics; | |
using System.IO; | |
int x = 10; | |
int y = 20; | |
Swap(ref x, ref y); | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); | |
static void Swap(ref int x, ref int y) | |
{ | |
var pathX = Path.GetTempFileName(); | |
var pathY = Path.GetTempFileName(); | |
File.WriteAllBytes(pathX, BitConverter.GetBytes(x)); | |
File.WriteAllBytes(pathY, BitConverter.GetBytes(y)); | |
x = BitConverter.ToInt32(File.ReadAllBytes(pathY)); | |
y = BitConverter.ToInt32(File.ReadAllBytes(pathX)); | |
File.Delete(pathX); | |
File.Delete(pathY); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Courtesy of https://twitter.com/ylorph | |
using System.Diagnostics; | |
using System.Threading; | |
int x = 10; | |
int y = 20; | |
y = Interlocked.Exchange(ref x, y); | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
int x = 10; | |
int y = 20; | |
var sequence = Enumerable.Range(Math.Min(x, y), Math.Max(x, y) - Math.Min(x, y) + 1); | |
x = sequence.Max(); | |
y = sequence.Min(); | |
Debug.Assert(x == 20); | |
Debug.Assert(y == 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//You will need to first call `dotnet add package LiteDB` | |
using LiteDB; | |
using System.Diagnostics; | |
using System.IO; | |
int x = 10; | |
int y = 20; | |
using var db = new LiteDatabase(new MemoryStream()); | |
var col = db.GetCollection<Value>(nameof(Value)); | |
col.Upsert(new Value(x)); | |
col.Upsert(new Value(y)); | |
var valY = col.FindOne(v => v.val == 10); | |
var valX = col.FindOne(v => v.val == 20); | |
Debug.Assert(valX.val == 20); | |
Debug.Assert(valY.val == 10); | |
record Value(int val); |
17_OperatorOverlordsOfTheUniverse.cs
Int x = 10;
Int y = 20;
Int.Swap(x, y);
Debug.Assert(x == 20);
Debug.Assert(y == 10);
public class Int
{
private int _value;
public static void Swap(Int x, Int y)
{
var tmp = x._value;
x._value = y._value;
y._value = tmp;
}
public static implicit operator Int (int value)
{
return new Int { _value = value };
}
public static implicit operator int(Int value)
{
return value._value;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
16_Spoopy_Swap
https://dotnetfiddle.net/ItrkEs