Skip to content

Commit 73feb87

Browse files
ClémentClément
authored andcommitted
Adding CList_ICollection source code.
1 parent 26b7d48 commit 73feb87

File tree

3 files changed

+223
-3
lines changed

3 files changed

+223
-3
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections;
4+
5+
public class CList<T>:ICollection<T>
6+
{
7+
/*
8+
* https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.icollection-1?view=net-9.0
9+
* A class realizing the ICollection interface must have the following properties:
10+
* - A `Count` property that gets the number of elements contained in the ICollection<T>.
11+
* - A `IsReadOnly` property that gets a value indicating whether the ICollection<T> is read-only.
12+
* and the following methods:
13+
* - Add(T): Adds an item to the ICollection<T>.
14+
* - Clear(): Removes all items from the ICollection<T>.
15+
* Contains(T): Determines whether the ICollection<T> contains a specific value.
16+
* CopyTo(T[], Int32): Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
17+
* GetEnumerator(): Returns an enumerator that iterates through a collection. (Inherited from IEnumerable)
18+
* Remove(T): Removes the first occurrence of a specific object from the ICollection<T>.
19+
*/
20+
21+
/* Same implementation of Cell as usual.*/
22+
private Cell first;
23+
24+
public CList()
25+
{
26+
first = null;
27+
}
28+
29+
private class Cell
30+
{
31+
public T Data { get; set; }
32+
public Cell Next { get; set; }
33+
34+
public Cell(T dataP = default(T), Cell nextP =null) // We provide default values
35+
{
36+
Data = dataP;
37+
Next = nextP;
38+
}
39+
}
40+
41+
/* Done with Cell.*/
42+
43+
// Empty
44+
public bool IsEmpty() { return first == null; }
45+
46+
// Add is simply "AddF", slightly revisited.
47+
public void Add(T value)
48+
{
49+
if (isReadonly) { throw new InvalidOperationException("List is read-only."); }
50+
Cell cCell = first;
51+
if (cCell != null)
52+
{
53+
while (cCell.Next != null)
54+
// As long as the cCell Cell has a neighbour…
55+
{
56+
cCell = cCell.Next;
57+
// We move the cCell cell to this neighbour.
58+
}
59+
cCell.Next = new Cell(value, null);
60+
}
61+
else
62+
{
63+
first = new Cell(value, null);
64+
}
65+
}
66+
67+
public void Clear()
68+
{
69+
first = null;
70+
}
71+
72+
public bool Contains(T value)
73+
{
74+
bool found = false;
75+
Cell cCell = first;
76+
while (cCell != null && !found)
77+
{
78+
if (cCell.Data.Equals(value)) { found = true; }
79+
cCell = cCell.Next;
80+
}
81+
return found;
82+
}
83+
84+
// Copies the elements of the ICollection to an Array, starting at a particular Array index.
85+
public void CopyTo(T[] array, int arrayIndex)
86+
{
87+
if (array == null)
88+
throw new ArgumentNullException("The array cannot be null.");
89+
if (arrayIndex < 0)
90+
throw new ArgumentOutOfRangeException("The starting array index cannot be negative.");
91+
if (Count > array.Length - arrayIndex)
92+
throw new ArgumentException("The destination array has fewer elements than the collection.");
93+
94+
Cell cCell = first;
95+
int i = 0; // keeping track of how many elements were copied.
96+
while (cCell != null)
97+
{
98+
array[i + arrayIndex] = cCell.Data;
99+
i++;
100+
cCell = cCell.Next;
101+
}
102+
}
103+
104+
public bool Remove(T value)
105+
{
106+
if (isReadonly) { throw new InvalidOperationException("List is read-only"); }
107+
bool removed = false;
108+
if (!IsEmpty())
109+
{
110+
if (first.Data.Equals(value)) { first = first.Next; removed = true; }
111+
else
112+
{
113+
Cell cCell = first;
114+
while (cCell.Next != null)
115+
{
116+
if (cCell.Next.Data.Equals(value)) { cCell.Next = cCell.Next.Next; removed = true; }
117+
}
118+
}
119+
}
120+
return removed;
121+
}
122+
123+
public int Count
124+
{
125+
get
126+
{
127+
int size = 0;
128+
Cell cCell = first;
129+
while (cCell != null)
130+
{
131+
cCell = cCell.Next;
132+
size++;
133+
}
134+
return size;
135+
}
136+
}
137+
138+
public bool isReadonly = false;
139+
public bool IsReadOnly
140+
{
141+
get { return isReadonly; }
142+
set { isReadonly = value; }
143+
}
144+
145+
public IEnumerator<T> GetEnumerator()
146+
{
147+
Cell cCell = first;
148+
while (cCell != null)
149+
{
150+
yield return cCell.Data;
151+
cCell = cCell.Next;
152+
}
153+
}
154+
155+
IEnumerator IEnumerable.GetEnumerator()
156+
{
157+
return this.GetEnumerator(); // call the generic version of the method
158+
}
159+
160+
/* We are done realizing the ICollection class. */
161+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
/* Simple example. */
8+
CList<int> myList1 = new CList<int>();
9+
Console.WriteLine(myList1);
10+
myList1.Add(1);
11+
myList1.Add(5);
12+
myList1.Add(2);
13+
myList1.Add(2);
14+
myList1.Add(1);
15+
myList1.Add(1);
16+
myList1.Add(3);
17+
myList1.Add(4);
18+
myList1.Add(5);
19+
myList1.Add(5);
20+
myList1.Add(5);
21+
myList1.Add(2);
22+
myList1.Add(2);
23+
Console.WriteLine(myList1);
24+
25+
// A benefit of realizing the ICollection interface
26+
// is that we can iterate over elements of lists now:
27+
foreach (var item in myList1)
28+
{
29+
Console.WriteLine(item);
30+
}
31+
32+
Console.WriteLine("Our list is read-only:" + myList1.IsReadOnly + ".");
33+
myList1.IsReadOnly = true;
34+
Console.WriteLine("Our list is read-only:" + myList1.IsReadOnly + ".");
35+
try
36+
{
37+
myList1.Add(12);
38+
}
39+
catch(Exception ex) { Console.WriteLine(ex.Message); }
40+
try
41+
{
42+
myList1.Remove(1);
43+
}
44+
catch (Exception ex) { Console.WriteLine(ex.Message); }
45+
46+
int[] array = new int[15];
47+
try
48+
{
49+
myList1.CopyTo(array, 6);
50+
} catch(Exception ex) { Console.WriteLine(ex.Message); }
51+
52+
array[0] = -10;
53+
myList1.CopyTo(array, 1);
54+
Console.WriteLine("The array contains:");
55+
foreach (int i in array){ Console.Write(i + " "); }
56+
57+
}
58+
59+
}

source/lectures/data/CList_as_ICollection.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ tags:
55

66
# List as ICollection
77

8-
Another way of implementing lists is to make our class realize the [ICollection abstract class](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.icollection-1?view=net-9.0):
8+
Another way of implementing lists is to make our class realize the [ICollection interface](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.icollection-1?view=net-9.0):
99

1010

1111
!include diag/cla/ICollection.md
1212

1313
This requires implementing a series of properties and methods:
1414

1515

16-
```{download="./code/projects/CList_Icollection.zip"}
17-
!include`snippetStart="/* Done with Cell.*/", snippetEnd="/* We are done realizing the ICollection class. */"` code/projects/CList_Icollection/CList_Icollection.cs
16+
```{download="./code/projects/CList_ICollection.zip"}
17+
!include`snippetStart="/* Done with Cell.*/", snippetEnd="/* We are done realizing the ICollection class. */"` code/projects/CList_ICollection/CList_ICollection/CList.cs
1818
```
1919

2020

0 commit comments

Comments
 (0)