Created
May 30, 2017 12:25
-
-
Save KristupasSavickas/37ff9980f24c67dcae67d041ece78c67 to your computer and use it in GitHub Desktop.
HDDLinkedList
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.Collections; | |
using System.IO; | |
using System.Text; | |
namespace Utilities.LinkedList | |
{ | |
public class HDDLinkedList : IEnumerable, IDisposable | |
{ | |
readonly FileStream stream; | |
readonly BinaryWriter writer; | |
readonly BinaryReader reader; | |
public HDDNode First { get; set; } | |
public HDDLinkedList(string file) | |
{ | |
stream = new FileStream(file, FileMode.Create, FileAccess.ReadWrite); | |
writer = new BinaryWriter(stream, Encoding.ASCII); | |
reader = new BinaryReader(stream, Encoding.ASCII); | |
First = null; | |
} | |
public void AddLast(int value) | |
{ | |
int index = (int)stream.Length; | |
if (First == null) | |
{ | |
First = new HDDNode(stream, writer, reader, index, value); | |
return; | |
} | |
var walk = First; | |
while (walk.Next != null) | |
{ | |
walk = walk.Next; | |
} | |
walk.Next = new HDDNode(stream, writer, reader, index, value); | |
} | |
public void Swap(HDDNode node1, HDDNode node2) | |
{ | |
var temp = node1.Index; | |
node1.Index = node2.Index; | |
node2.Index = node1.Index; | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
var walk = First; | |
while (walk != null) | |
{ | |
yield return walk.Value; | |
walk = walk.Next; | |
} | |
} | |
public void Dispose() | |
{ | |
reader?.Dispose(); | |
writer?.Dispose(); | |
stream?.Dispose(); | |
} | |
} | |
} |
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.IO; | |
namespace Utilities.LinkedList | |
{ | |
public class HDDNode | |
{ | |
readonly FileStream stream; | |
readonly BinaryWriter writer; | |
readonly BinaryReader reader; | |
public HDDNode Next { get; set; } | |
public int Index { get; set; } | |
public int Value | |
{ | |
get | |
{ | |
stream.Position = Index; | |
return reader.ReadInt32(); | |
} | |
set | |
{ | |
stream.Position = Index; | |
writer.Write(value); | |
} | |
} | |
public HDDNode(FileStream stream, BinaryWriter writer, BinaryReader reader, int index, int value) | |
{ | |
this.stream = stream; | |
this.writer = writer; | |
this.reader = reader; | |
Index = index; | |
Value = value; | |
Next = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment