-
Notifications
You must be signed in to change notification settings - Fork 2
/
Vector.cs
116 lines (100 loc) · 4.7 KB
/
Vector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Text;
namespace SIT221_Library
{
public class Vector<T>
{
// This constant determines the default number of elements in a newly created vector.
// It is also used to extended the capacity of the existing vector
private const int DEFAULT_CAPACITY = 10;
// This array represents the internal data structure wrapped by the vector class.
// In fact, all the elements are to be stored in this private array.
// You will just write extra functionality (methods) to make the work with the array more convenient for the user.
private T[] data;
// This property represents the number of elements in the vector
public int Count { get; private set; } = 0;
// This property represents the maximum number of elements (capacity) in the vector
public int Capacity => data.Length;
// This is an overloaded constructor
public Vector(int capacity)
{
data = new T[capacity];
}
// This is the implementation of the default constructor
public Vector() : this(DEFAULT_CAPACITY) { }
// An Indexer is a special type of property that allows a class or structure to be accessed the same way as array for its internal collection.
// For example, introducing the following indexer you may address an element of the vector as vector[i] or vector[0] or ...
public T this[int index]
{
get
{
if (index >= Count || index < 0) throw new IndexOutOfRangeException();
return data[index];
}
set
{
if (index >= Count || index < 0) throw new IndexOutOfRangeException();
data[index] = value;
}
}
// This private method allows extension of the existing capacity of the vector by another 'extraCapacity' elements.
// The new capacity is equal to the existing one plus 'extraCapacity'.
// It copies the elements of 'data' (the existing array) to 'newData' (the new array), and then makes data pointing to 'newData'.
private void ExtendData(int extraCapacity)
{
T[] newData = new T[Capacity + extraCapacity];
for (int i = 0; i < Count; i++) newData[i] = data[i];
data = newData;
}
// This method adds a new element to the existing array.
// If the internal array is out of capacity, its capacity is first extended to fit the new element.
public void Add(T element)
{
if (Count == Capacity) ExtendData(DEFAULT_CAPACITY);
data[Count++] = element;
}
// This method searches for the specified object and returns the zero‐based index of the first occurrence within the entire data structure.
// This method performs a linear search; therefore, this method is an O(n) runtime complexity operation.
// If occurrence is not found, then the method returns –1.
// Note that Equals is the proper method to compare two objects for equality, you must not use operator '=' for this purpose.
public int IndexOf(T element)
{
for (var i = 0; i < Count; i++)
{
if (data[i].Equals(element)) return i;
}
return -1;
}
// TODO: Your task is to implement all the remaining methods.
// Read the instruction carefully, study the code examples from above as they should help you to write the rest of the code.
public void Insert(int index, T element)
{
if(index < 0 || index > Count) throw new IndexOutOfRangeException();
if(Count == Capacity) ExtendData(DEFAULT_CAPACITY);
for(int i = Count; i > index; i--) data[i] = data[i - 1];
data[index] = element;
Count++;
}
public void Clear()
{
data = new T[Capacity];
Count = 0;
}
public bool Contains(T element) => IndexOf(element) != -1;
public bool Remove(T element)
{
int index = IndexOf(element);
if (index is -1) return false;
RemoveAt(index);
return true;
}
public void RemoveAt(int index)
{
if(index < 0 || index >= Count) throw new IndexOutOfRangeException();
for(int i = index; i < Count - 1; i++) data[i] = data[i + 1];
Count--;
}
public override string ToString() => "[" + string.Join(", ", data[0..Count]) + "]";
}
}