Skip to content

Commit 3ba8a3b

Browse files
committedApr 8, 2016
Ordered Set
1 parent 3505179 commit 3ba8a3b

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed
 

‎OrderedSet/OrderedSet.kt

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Created by gazollajunior on 08/04/16.
3+
*/
4+
5+
class OrderedSet<T:Comparable<T>>(list:MutableList<T>){
6+
7+
var items: MutableList<T> = list
8+
9+
fun insert(element:T) {
10+
if (exists(element)) {
11+
return
12+
}
13+
for (i in 0..this.items.count() - 1){
14+
if (this.items[i] > element){
15+
this.items.add(i, element)
16+
return
17+
}
18+
}
19+
this.items.add(element)
20+
}
21+
22+
/**
23+
* Use binarySearch algorithm to find the position for the new element in array
24+
*/
25+
26+
fun findElementPosition(element:T):Int?{
27+
var rangeStart = 0
28+
var rangeEnd = this.items.count()
29+
while (rangeStart < rangeEnd) {
30+
val midIndex = rangeStart + (rangeEnd - rangeStart)/2
31+
if (this.items[midIndex] == element) {
32+
return midIndex
33+
} else if (this.items[midIndex] < element){
34+
rangeStart = midIndex + 1
35+
} else {
36+
rangeEnd = midIndex
37+
}
38+
}
39+
return null
40+
}
41+
42+
override fun toString():String = this.items.toString()
43+
44+
fun isEmpty():Boolean = this.items.isEmpty()
45+
46+
fun exists(element:T):Boolean = findElementPosition(element) != null
47+
48+
fun count():Int = this.items.count()
49+
50+
fun remove(element:T) {
51+
val position = findElementPosition(element)
52+
if (position != null) {
53+
this.items.removeAt(position)
54+
}
55+
}
56+
57+
fun removeAll() = this.items.removeAll(this.items)
58+
59+
fun max():T? {
60+
if (count() != 0) {
61+
return this.items[count() - 1]
62+
} else {
63+
return null
64+
}
65+
}
66+
fun min():T? {
67+
if (count() != 0) {
68+
return this.items[0]
69+
} else {
70+
return null
71+
}
72+
}
73+
}
74+
75+
76+
77+
fun main(args: Array<String>) {
78+
79+
80+
println("\nOriginal set:")
81+
val names = mutableListOf<String>("Demetrius")
82+
83+
var nameSet = OrderedSet<String>(names)
84+
println(nameSet)
85+
86+
87+
val n1 = "Adam"
88+
println("\nAdding ${n1} to the list:")
89+
nameSet.insert(n1)
90+
println(nameSet)
91+
92+
val n2 = "Tim"
93+
println("\nAdding ${n2} to the list:")
94+
nameSet.insert(n2)
95+
println(nameSet)
96+
97+
val n3 = "Zack"
98+
println("\nAdding ${n3} to the list:")
99+
nameSet.insert(n3)
100+
println(nameSet)
101+
102+
val n4 = "Zack"
103+
println("\nTry Add ${n4} again to the list:")
104+
nameSet.insert(n4)
105+
println(nameSet)
106+
107+
nameSet.remove(n2)
108+
println("\nRemoving ${n2} from the list:")
109+
println(nameSet)
110+
111+
nameSet.remove(n4)
112+
println("\nRemoving ${n4} from the list:")
113+
println(nameSet)
114+
115+
nameSet.remove(n1)
116+
println("\nRemoving ${n1} from the list:")
117+
println(nameSet)
118+
119+
}

‎OrderedSet/README.markdown

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Ordered Set
2+
3+
An ordered set is a common data structure that supports O(log N) lookups, insertions and removals. Ordered set is also sometimes used as an alternative to a hash map, for example in STL’s map.
4+
5+
The implementation is quite basic:
6+
7+
```kotlin
8+
class OrderedSet<T:Comparable<T>>(list:MutableList<T>){
9+
10+
var items: MutableList<T> = list
11+
12+
fun insert(element:T) {
13+
if (exists(element)) {
14+
return
15+
}
16+
for (i in 0..this.items.count() - 1){
17+
if (this.items[i] > element){
18+
this.items.add(i, element)
19+
return
20+
}
21+
}
22+
this.items.add(element)
23+
}
24+
25+
/**
26+
* Use binarySearch algorithm to find the position for the new element in array
27+
*/
28+
29+
fun findElementPosition(element:T):Int?{
30+
var rangeStart = 0
31+
var rangeEnd = this.items.count()
32+
while (rangeStart < rangeEnd) {
33+
val midIndex = rangeStart + (rangeEnd - rangeStart)/2
34+
if (this.items[midIndex] == element) {
35+
return midIndex
36+
} else if (this.items[midIndex] < element){
37+
rangeStart = midIndex + 1
38+
} else {
39+
rangeEnd = midIndex
40+
}
41+
}
42+
return null
43+
}
44+
45+
override fun toString():String = this.items.toString()
46+
47+
fun isEmpty():Boolean = this.items.isEmpty()
48+
49+
fun exists(element:T):Boolean = findElementPosition(element) != null
50+
51+
fun count():Int = this.items.count()
52+
53+
fun remove(element:T) {
54+
val position = findElementPosition(element)
55+
if (position != null) {
56+
this.items.removeAt(position)
57+
}
58+
}
59+
60+
fun removeAll() = this.items.removeAll(this.items)
61+
62+
fun max():T? {
63+
if (count() != 0) {
64+
return this.items[count() - 1]
65+
} else {
66+
return null
67+
}
68+
}
69+
fun min():T? {
70+
if (count() != 0) {
71+
return this.items[0]
72+
} else {
73+
return null
74+
}
75+
}
76+
}
77+
78+
```

0 commit comments

Comments
 (0)
Please sign in to comment.