Created
September 25, 2020 12:58
-
-
Save sangkeon/00989d52da98eb9c4c776143150b9657 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package boundedset | |
class BoundedSet[T](capacity: Int) { | |
private var data = List[T]() | |
def add(elem:T) = { | |
if(elem == null ) { | |
throw new NullPointerException() | |
} | |
data = data.filterNot(_ == elem) | |
data = if (data.size == capacity) data.tail else data | |
data = data.appended(elem) | |
} | |
def contains(elem: T) : Boolean = { | |
data.contains(elem) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment