Created
December 19, 2023 15:19
-
-
Save swisstackle/f69ce28002b06809c01ddab009a8bf0b to your computer and use it in GitHub Desktop.
Self sorting cells.
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; | |
namespace CellviewAlgos | |
{ | |
public class Cell | |
{ | |
public string Name { get; set; } | |
public int Value { get; set; } | |
public int Position { get; set; } | |
private bool cantBeMoved { get; set; } | |
private bool cantMove { get; set; } | |
public bool CantBeMoved() { return cantBeMoved; } | |
public bool CantMove() { return cantMove; } | |
public List<Cell> cells { get; set; } | |
public Cell(bool _cantBeMoved, bool _cantMove, string name, int position, int value) | |
{ | |
cantBeMoved = _cantBeMoved; | |
cantMove = _cantMove; | |
this.Position = position; | |
this.Name = name; | |
this.Value = value; | |
} | |
private SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1); | |
public async Task<bool> AttemptSetPosition(int position) | |
{ | |
await semaphoreSlim.WaitAsync(); | |
try | |
{ | |
if (!CantBeMoved()) | |
{ | |
Position = position; | |
return true; | |
} | |
return false; | |
} | |
finally | |
{ | |
semaphoreSlim.Release(); | |
} | |
} | |
public async Task Start(List<Cell> cells) | |
{ | |
while (true) // Consider adding a condition to break this loop | |
{ | |
var left_cells = cells.Where(e => e.Position < this.Position); | |
var right_cells = cells.Where(e => e.Position > this.Position); | |
Cell left_cell = null; | |
Cell right_cell = null; | |
if (left_cells.Any()) | |
{ | |
left_cell = left_cells.Aggregate((max, current) => max.Position > current.Position ? max : current); | |
//Console.WriteLine($"Left Cell of {this.Name} is {left_cell.Name}"); | |
} | |
if (right_cells.Any()) | |
{ | |
right_cell = right_cells.Aggregate( | |
(min, current) => min.Position < current.Position ? min : current); | |
//Console.WriteLine($"Right Cell of {this.Name} is {right_cell.Name}"); | |
} | |
if (left_cell != null && left_cell.Value > this.Value && !CantMove()) | |
{ | |
// Operations for left cell | |
Console.Write($"{this.Name} wants to swap with {left_cell?.Name}"); | |
var temp = left_cell.Position; | |
var success = await left_cell.AttemptSetPosition(this.Position); | |
if (success) | |
{ | |
this.Position = temp; | |
Console.WriteLine(" and did successfully."); | |
} | |
else | |
{ | |
Console.WriteLine(" but couldn't."); | |
} | |
} | |
else if (right_cell != null && right_cell.Value < this.Value && !CantMove()) | |
{ | |
// Operations for right cell | |
Console.Write($"{this.Name} wants to swap with {right_cell?.Name}"); | |
var temp = right_cell.Position; | |
var success = await right_cell.AttemptSetPosition(this.Position); | |
if (success) | |
{ | |
this.Position = temp; | |
Console.WriteLine(" and did successfully."); | |
} | |
else | |
{ | |
Console.WriteLine(" but couldn't."); | |
} | |
} | |
else | |
{ | |
Console.WriteLine(""); | |
} | |
// Rest of your code | |
var cell1 = cells.Where(e => e.Name == "cell_1").FirstOrDefault()?.Position; | |
var cell2 = cells.Where(e => e.Name == "cell_2").FirstOrDefault()?.Position; | |
var cell3 = cells.Where(e => e.Name == "cell_0").FirstOrDefault()?.Position; | |
var cell4 = cells.Where(e => e.Name == "cell_3").FirstOrDefault()?.Position; | |
Console.WriteLine($"Cell_1 : {cell1} / Cell_2 : {cell2} / Cell_0 : {cell3} / cell_3 : {cell4}"); | |
} | |
} | |
} | |
public class CellViewSort | |
{ | |
public static async Task Main() | |
{ | |
var cell2 = new Cell(false,true, "cell_2", 3, 2); | |
var cell1 = new Cell(false, false, "cell_1", 2, 1); | |
var cell0 = new Cell(false, false, "cell_0", 1, 0); | |
var cell4 = new Cell(false, false, "cell_3", 0, 3); | |
var cellList = new List<Cell> { cell1, cell2, cell0, cell4 }; | |
var task1 = Task.Run(() => cell4.Start(cellList)); | |
var task2 = Task.Run(() => cell2.Start(cellList)); | |
var task3 = Task.Run(() => cell0.Start(cellList)); | |
var task4 = Task.Run(() => cell1.Start(cellList)); | |
await Task.WhenAll(task1, task2, task3, task4); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment