Skip to content

Commit 23a3795

Browse files
ClémentClément
authored andcommitted
Tidy up source code.
1 parent e3d14a7 commit 23a3795

2 files changed

Lines changed: 224 additions & 223 deletions

File tree

source/code/projects/AList/AList/AList.cs

Lines changed: 162 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -4,195 +4,191 @@
44

55
public class AList<T> : ICollection<T>
66
{
7-
// Our implementation of list is so that
8-
// the alist array is always of the "right
9-
// size", so we don't need to keep track
10-
// of a separate counter for the size of the
11-
// list. This is more memory-
12-
// intensive (because arrays get created
13-
// frequently), but lighter to implement.
14-
private T[] alist;
15-
16-
public AList()
7+
// Our implementation of list is so that
8+
// the alist array is always of the "right
9+
// size", so we don't need to keep track
10+
// of a separate counter for the size of the
11+
// list. This is more memory-
12+
// intensive (because arrays get created
13+
// frequently), but lighter to implement.
14+
private T[] alist;
15+
16+
public AList()
17+
{
18+
alist = new T[0];
19+
}
20+
21+
public bool IsEmpty()
22+
{
23+
return alist.Length == 0;
24+
}
25+
26+
public void Clear()
27+
{
28+
alist = new T[0];
29+
}
30+
31+
// Attribute and property for the
32+
// isReadOnly required by ICollection.
33+
public bool isReadonly = false;
34+
public bool IsReadOnly
35+
{
36+
get { return isReadonly; }
37+
set { isReadonly = value; }
38+
}
39+
40+
// Add is adding "to the left".
41+
public void Add(T value)
42+
{
43+
if (isReadonly)
1744
{
18-
alist = new T[0];
45+
throw new InvalidOperationException(
46+
"List is read-only, you cannot add to it."
47+
);
1948
}
20-
21-
public bool IsEmpty()
49+
else
2250
{
23-
return alist.Length == 0;
51+
// We create a larger array
52+
// using the length of the existing
53+
// array.
54+
T[] newlist = new T[alist.Length + 1];
55+
// We insert the new value "to the left".
56+
newlist[0] = value;
57+
// We copy the remaining values,
58+
// shifting them by one.
59+
for (int i = 0; i < alist.Length; i++)
60+
newlist[i + 1] = alist[i];
61+
// Finally, our new array becomes our
62+
// alist attribute.
63+
alist = newlist;
2464
}
25-
26-
public void Clear()
65+
}
66+
67+
// Remove the first node containing the value
68+
// if it exists and returns true,
69+
// returns false otherwise.
70+
public bool Remove(T value)
71+
{
72+
if (isReadonly)
2773
{
28-
alist = new T[0];
74+
throw new InvalidOperationException(
75+
"List is read-only, you cannot remove from it."
76+
);
2977
}
30-
31-
// Attribute and property for the
32-
// isReadOnly required by ICollection.
33-
public bool isReadonly = false;
34-
public bool IsReadOnly
78+
bool removed = false;
79+
if (!IsEmpty())
3580
{
36-
get { return isReadonly; }
37-
set { isReadonly = value; }
38-
}
39-
40-
// Add is adding "to the left".
41-
public void Add(T value)
42-
{
43-
if (isReadonly)
44-
{
45-
throw new InvalidOperationException(
46-
"List is read-only, you cannot add to it."
47-
);
48-
}
49-
else
81+
int i = 0;
82+
while (i < alist.Length && !removed)
83+
{
84+
if (alist[i].Equals(value))
5085
{
51-
// We create a larger array
52-
// using the length of the existing
53-
// array.
54-
T[] newlist = new T[alist.Length + 1];
55-
// We insert the new value "to the left".
56-
newlist[0] = value;
57-
// We copy the remaining values,
58-
// shifting them by one.
59-
for (int i = 0; i < alist.Length; i++)
60-
newlist[i + 1] = alist[i];
61-
// Finally, our new array becomes our
62-
// alist attribute.
63-
alist = newlist;
86+
removed = true;
87+
88+
// We create the array that will replace
89+
// the current one.
90+
91+
T[] newList = new T[alist.Length - 1];
92+
// We first copy the values
93+
// before the value we found:
94+
for (int j = 0; j < i; j++)
95+
newList[j] = alist[j];
96+
// We now copy the values *after*
97+
// the value we found.
98+
for (int j = i; j < newList.Length; j++)
99+
newList[j] = alist[j + 1];
100+
alist = newList;
64101
}
102+
i++;
103+
}
65104
}
66-
67-
68-
// Remove the first node containing the value
69-
// if it exists and returns true,
70-
// returns false otherwise.
71-
public bool Remove(T value)
72-
{
73-
if (isReadonly)
74-
{
75-
throw new InvalidOperationException(
76-
"List is read-only, you cannot remove from it."
77-
);
78-
}
79-
bool removed = false;
80-
if (!IsEmpty())
81-
{
82-
int i = 0;
83-
while (i < alist.Length && !removed)
84-
{
85-
if (alist[i].Equals(value))
86-
{
87-
removed = true;
88-
89-
// We create the array that will replace
90-
// the current one.
91-
92-
T[] newList = new T[alist.Length - 1];
93-
// We first copy the values
94-
// before the value we found:
95-
for (int j = 0; j < i; j++)
96-
newList[j] = alist[j];
97-
// We now copy the values *after*
98-
// the value we found.
99-
for (int j = i; j < newList.Length; j++)
100-
newList[j] = alist[j + 1];
101-
alist = newList;
102-
}
103-
i++;
104-
}
105-
}
106-
return removed;
107-
}
108-
109-
public bool Contains(T value)
105+
return removed;
106+
}
107+
108+
public bool Contains(T value)
109+
{
110+
// We start by assuming we did not find
111+
// the target value.
112+
bool found = false;
113+
// We go through the array.
114+
for (int i = 0; i < alist.Length; i++)
110115
{
111-
// We start by assuming we did not find
112-
// the target value.
113-
bool found = false;
114-
// We go through the array.
115-
for (int i = 0; i < alist.Length; i++)
116-
{
117-
if (alist[i].Equals(value)) found = true;
118-
}
119-
return found;
116+
if (alist[i].Equals(value))
117+
found = true;
120118
}
119+
return found;
120+
}
121121

122-
123-
// Adding "to the right" (as the last value).
124-
public void AddR(T value)
122+
// Adding "to the right" (as the last value).
123+
public void AddR(T value)
124+
{
125+
if (isReadonly)
125126
{
126-
if (isReadonly)
127-
{
128-
throw new InvalidOperationException(
129-
"List is read-only, you cannot add to it."
130-
);
131-
}
132-
else
133-
{
134-
// We resize the array using
135-
// built-in method.
136-
Array.Resize(ref alist, alist.Length + 1);
137-
// We insert the new value "at the end".
138-
alist[alist.Length - 1] = value;
139-
}
127+
throw new InvalidOperationException(
128+
"List is read-only, you cannot add to it."
129+
);
140130
}
141-
142-
// Count property simply returns
143-
// the length of the alist array
144-
// if it is not null.
145-
public int Count
131+
else
146132
{
147-
get
148-
{
149-
return alist.Length;
150-
}
133+
// We resize the array using
134+
// built-in method.
135+
Array.Resize(ref alist, alist.Length + 1);
136+
// We insert the new value "at the end".
137+
alist[alist.Length - 1] = value;
151138
}
152-
153-
// Copies the elements of the ICollection to an Array, starting at a particular Array index.
154-
public void CopyTo(T[] array, int arrayIndex)
139+
}
140+
141+
// Count property simply returns
142+
// the length of the alist array
143+
// if it is not null.
144+
public int Count
145+
{
146+
get { return alist.Length; }
147+
}
148+
149+
// Copies the elements of the ICollection to an Array, starting at a particular Array index.
150+
public void CopyTo(T[] array, int arrayIndex)
151+
{
152+
if (array == null)
153+
// The array into which we copy
154+
// cannot be null.
155+
throw new ArgumentNullException();
156+
if (arrayIndex < 0)
157+
// The index must be strictly positive.
158+
throw new ArgumentOutOfRangeException();
159+
if (Count > array.Length - arrayIndex)
160+
// If there is not enough room in the
161+
// array given as argument to copy
162+
// our list, we throw an exception.
163+
throw new ArgumentException(
164+
$"The destination array does not have enough space ({array.Length}) to copy the list ({Count}) starting at the given index ({arrayIndex})."
165+
);
166+
// If none of the exceptions were triggered,
167+
// we can copy the values at the right index.
168+
for (int i = 0; i < alist.Length; i++)
155169
{
156-
if (array == null)
157-
// The array into which we copy
158-
// cannot be null.
159-
throw new ArgumentNullException();
160-
if (arrayIndex < 0)
161-
// The index must be strictly positive.
162-
throw new ArgumentOutOfRangeException();
163-
if (Count > array.Length - arrayIndex)
164-
// If there is not enough room in the
165-
// array given as argument to copy
166-
// our list, we throw an exception.
167-
throw new ArgumentException(
168-
$"The destination array does not have enough space ({array.Length}) to copy the list ({Count}) starting at the given index ({arrayIndex})."
169-
);
170-
// If none of the exceptions were triggered,
171-
// we can copy the values at the right index.
172-
for (int i = 0; i < alist.Length; i++)
173-
{
174-
array[i + arrayIndex] = alist[i];
175-
}
170+
array[i + arrayIndex] = alist[i];
176171
}
172+
}
177173

178-
/* The following is "bureaucracy" to realize the
179-
* ICollection interface. */
174+
/* The following is "bureaucracy" to realize the
175+
* ICollection interface. */
180176

181-
public IEnumerator<T> GetEnumerator()
177+
public IEnumerator<T> GetEnumerator()
178+
{
179+
if (!IsEmpty())
182180
{
183-
if (!IsEmpty())
184-
{
185-
for (int i = 0; i < alist.Length; i++)
186-
{
187-
yield return alist[i];
188-
}
189-
}
181+
for (int i = 0; i < alist.Length; i++)
182+
{
183+
yield return alist[i];
184+
}
190185
}
186+
}
191187

192-
IEnumerator IEnumerable.GetEnumerator()
193-
{
194-
return this.GetEnumerator(); // call the generic version of the method
195-
}
188+
IEnumerator IEnumerable.GetEnumerator()
189+
{
190+
return this.GetEnumerator(); // call the generic version of the method
191+
}
196192

197-
/* We are done realizing the ICollection interface. */
193+
/* We are done realizing the ICollection interface. */
198194
}

0 commit comments

Comments
 (0)