Last active
August 15, 2017 06:03
-
-
Save basp1/adef332913f88cb0d30c50657b7a316d to your computer and use it in GitHub Desktop.
Intlist class
This file contains hidden or 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.Generic; | |
using System.Diagnostics; | |
namespace Matlib | |
{ | |
public class Intlist | |
{ | |
readonly static int NIL = -1; | |
readonly static int EMPTY = -2; | |
int[] data; | |
int ip; | |
int count; | |
int size; | |
public Intlist(int size) | |
{ | |
this.size = size; | |
this.ip = NIL; | |
this.data = new int[size]; | |
for (int i = 0; i < size; i++) | |
data[i] = EMPTY; | |
Reset(); | |
} | |
public void Reset() | |
{ | |
int i = ip; | |
int p = ip; | |
while (NIL != i) | |
{ | |
p = i; | |
i = data[i]; | |
data[p] = EMPTY; | |
} | |
if (NIL != p) | |
{ | |
data[p] = EMPTY; | |
} | |
ip = NIL; | |
count = 0; | |
} | |
public void Clear() | |
{ | |
while (count > 0) | |
{ | |
Pop(); | |
} | |
} | |
public int Count { get { return count; } } | |
public int Size { get { return size; } } | |
public bool Empty { get { return 0 == count; } } | |
public bool Contains(int value) | |
{ | |
Debug.Assert(value >= 0); | |
Debug.Assert(value < size); | |
return EMPTY != data[value]; | |
} | |
public void Push(int value) | |
{ | |
if (Contains(value)) | |
{ | |
return; | |
} | |
data[value] = ip; | |
ip = value; | |
count += 1; | |
} | |
public int Pop() | |
{ | |
Debug.Assert(ip >= 0); | |
int value = ip; | |
ip = data[ip]; | |
data[value] = EMPTY; | |
count -= 1; | |
return value; | |
} | |
public List<int> Move() | |
{ | |
List<int> list = new List<int>(count); | |
Move(ref list); | |
return list; | |
} | |
public void Move(ref List<int> list) | |
{ | |
list.Clear(); | |
list.Capacity = count; | |
while (count > 0) | |
{ | |
list.Add(Pop()); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 Matlib; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace Matlib.Tests | |
{ | |
[TestClass] | |
public class IntlistTests | |
{ | |
[TestMethod] | |
public void IntlistPush() | |
{ | |
var il = new Intlist(10); | |
il.Push(1); | |
il.Push(7); | |
il.Push(3); | |
il.Push(2); | |
Assert.AreEqual(4, il.Count); | |
} | |
[TestMethod] | |
public void IntlistContains() | |
{ | |
var il = new Intlist(10); | |
il.Push(1); | |
il.Push(7); | |
il.Push(3); | |
il.Push(2); | |
Assert.IsTrue(il.Contains(1)); | |
Assert.IsTrue(il.Contains(2)); | |
Assert.IsTrue(il.Contains(3)); | |
Assert.IsTrue(il.Contains(7)); | |
Assert.IsTrue(!il.Contains(0)); | |
Assert.IsTrue(!il.Contains(4)); | |
} | |
[TestMethod] | |
public void IntlistPop() | |
{ | |
var il = new Intlist(10); | |
il.Push(1); | |
il.Push(7); | |
il.Push(3); | |
il.Push(2); | |
Assert.AreEqual(2, il.Pop()); | |
Assert.AreEqual(3, il.Pop()); | |
Assert.AreEqual(7, il.Pop()); | |
Assert.AreEqual(1, il.Pop()); | |
} | |
[TestMethod] | |
public void IntlistMove() | |
{ | |
var il = new Intlist(10); | |
il.Push(1); | |
il.Push(7); | |
il.Push(3); | |
il.Push(2); | |
Assert.AreEqual(4, il.Count); | |
var list = il.Move(); | |
Assert.AreEqual(0, il.Count); | |
Assert.AreEqual(2, list[0]); | |
Assert.AreEqual(3, list[1]); | |
Assert.AreEqual(7, list[2]); | |
Assert.AreEqual(1, list[3]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment