-
Notifications
You must be signed in to change notification settings - Fork 108
/
DynamicArray.kt
140 lines (115 loc) · 3.06 KB
/
DynamicArray.kt
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package structures
import java.lang.IllegalStateException
/**
*
* Dynamic array or array list is a random access, variable-size list data structure
*
* that allows elements to be added or removed, in Java this is java.util.ArrayList
*
* P.S. Kotlin lists use under the hood java.util.ArrayList on the JVM
*
* Example:
*
* 1) val numbers = listOf(1, 2, 3) // java.util.ArrayList
*
* 2) val symbols = mutableListOf('a', 'b', 'c') // also java.util.ArrayList
*
*/
class DynamicArray<T>(private var capacity: Int = 10) {
private var data = arrayOfNulls<Any>(capacity)
private var size = 0
/**
* Complexity:
* worst time - O(n) because increaseSize() is called
* best time - O(1)
* average time - O(1)
*/
fun add(value: T) {
if (size <= data.size - 1) {
data[size] = value
} else {
increaseSize()
data[size] = value
}
size += 1
}
/**
* Complexity:
* worst time: O(n)
* best time: O(n)
* average time: O(n)
*/
fun remove(value: T) : Boolean {
var foundedIndex = -1
for (i in data.indices) {
if (data[i] == value) {
foundedIndex = i
break
}
}
if (foundedIndex == -1) return false
for (i in foundedIndex until data.size - 1) {
data[i] = data[i + 1]
}
size -= 1
return true
}
/**
* Complexity:
* worst time: O(n)
* best time: O(1)
* average time: O(n)
*/
fun contains(value: T): Boolean {
for (i in data.indices) {
if (data[i] == value) {
return true
}
}
return false
}
/**
* Complexity:
* worst time: O(1)
* best time: O(1)
* average time: O(1)
*/
fun set(index: Int, value: T): T? {
if (index !in 0 until size) throw IllegalStateException("The index $index is out of bounds!")
val oldValue = data[index]
data[index] = value
return oldValue as? T
}
/**
* Complexity:
* worst time: O(1)
* best time: O(1)
* average time: O(1)
*/
fun get(index: Int) : T {
if (index !in data.indices) throw IllegalStateException("The index $index is out of bounds!")
return data[index] as T
}
override fun toString(): String {
val builder = StringBuilder()
builder.append("capacity: $capacity\n")
builder.append("size: $size\n")
builder.append("elements: ")
for (i in 0 until size - 1) {
builder.append("${data[i]}, ")
}
builder.append(data[size - 1])
return builder.toString()
}
private fun increaseSize() {
capacity = (capacity * INCREASE_SIZE_COEFFICIENT).toInt()
val newArray = arrayOfNulls<Any>(capacity)
for (i in data.indices) {
newArray[i] = data[i]
}
data = newArray
}
companion object {
private const val INCREASE_SIZE_COEFFICIENT = 1.5f
}
}