scala array char

  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
141
142
143
import scala.collection.immutable.HashMap
class CritBitTree[T](nodes: HashMap[Array[Char], CritBitTree[T]], isExternalNode: Boolean, data: Option[T]) {
def this(isExternalNode: Boolean, data: Option[T]) = this(HashMap[Array[Char], CritBitTree[T]](), isExternalNode, data)
def add(bitString: Array[Char], value: T): CritBitTree[T] = {
if (bitString.isEmpty) {
return new CritBitTree[T](nodes, true, Option[T](value))
}
val commonPrefix = findCommonPrefix(bitString)
if (commonPrefix.isEmpty) {
val newLeaf = bitString -> new CritBitTree[T](true, Option[T](value))
return new CritBitTree[T](nodes + newLeaf, isExternalNode, data)
}
val keyVal = nodes.find(kv => kv._1.startsWith(commonPrefix)).get
val edge = keyVal._1
val trie = keyVal._2
//добавление новой вершины
if (edge == commonPrefix) {
val newLeaf = edge -> trie.add(bitString.substring(edge.length), value)
val newNodes = nodes + newLeaf
new CritBitTree[T](newNodes, isExternalNode, data)
} else {
val end = edge.substring(commonPrefix.length)
val middleVertex = new CritBitTree[T](HashMap(end -> trie), false, None)
val newLeaf = commonPrefix -> middleVertex.add(bitString.substring(commonPrefix.length), value)
val newNodes = (nodes - edge) + newLeaf
new CritBitTree[T](newNodes, isExternalNode, data)
}
}
/* def remove(bitString: Array[Char]): CritBitTree[T] = {
if (bitString.isEmpty) {
return new CritBitTree(nodes, false, None)
}
val commonPrefix = findCommonPrefix(bitString)
if (commonPrefix.isEmpty) {
return this
}
val keyVal = nodes.find(kv => kv._1.startsWith(commonPrefix)).get
val edge = keyVal._1
val trie = keyVal._2
if (edge == commonPrefix) {
val newLeaf = edge -> trie.remove(bitString.substring(edge.length))
if (newLeaf._2.size() == 0) {
val newNodes = nodes - edge
val size = countWords(newNodes)
new CritBitTree[T](newNodes, isExternalNode, size, data)
} else {
newLeaf._2.nodesNumber() match {
case 1 =>
val nextLeaf = newLeaf._2.getSingleLeaf()
if (newLeaf._2.size() == nextLeaf._2.size()) {
val leaf = (edge + nextLeaf._1) -> nextLeaf._2
val newNodes = (nodes - edge) + leaf
val size = countWords(newNodes)
new CritBitTree[T](newNodes, isExternalNode, size, data)
} else {
val newNodes = nodes + newLeaf
val size = countWords(newNodes)
new CritBitTree[T](newNodes, isExternalNode, size, data)
}
case _ =>
val newNodes = nodes + newLeaf
val size = countWords(newNodes)
new CritBitTree[T](newNodes, isExternalNode, size, data)
}
}
} else {
this
}
}*/
/*def contains(bitString: Array[Char]): Boolean = {
get(bitString) != None
}*/
/*def get(bitString: Array[Char]): Option[T] = {
if (bitString.isEmpty) {
if (isExternalNode) {
return data
} else {
return None
}
}
val commonPrefix = findCommonPrefix(bitString)
if (commonPrefix.isEmpty) {
return None
}
val keyVal = nodes.find(kv => kv._1.startsWith(commonPrefix)).get
if (keyVal._1 != commonPrefix) {
return None
}
keyVal._2.get(bitString.substring(commonPrefix.length))
}*/
def print(indent: Int = 0) {
if (isExternalNode) println(" " * indent + "$(" + data.get + ")$")
for (kv <- nodes) {
println(" " * indent + kv._1)
kv._2.print(indent + kv._1.length)
}
}
/*private def nodesNumber() = nodes.size
private def getSingleLeaf() = nodes.last*/
def findIndex(index: Int, newString: Array[Char], existPrefix: Array[Char]) {
//if (index >= newString.length || index >= existPrefix.length)
//return index
if (index < newString.length && index < existPrefix.length && newString(index) == existPrefix(index)) {
findIndex(index + 1, newString, existPrefix)
}
return index
}
private def findCommonPrefix(bitString: Array[Char]): Array[Char] = {
for (edge <- nodes.keys) {
findIndex(0, bitString, edge)
}
bitString.copyToArray(prefix, 0, )
return
}
}
object CritBitTree {
def apply[T](): CritBitTree[T] = {
new CritBitTree[T](false, None)
}
}