src: move collections from model to a new module
authorJean Privat <jean@pryen.org>
Fri, 26 Jul 2013 01:25:23 +0000 (21:25 -0400)
committerJean Privat <jean@pryen.org>
Fri, 26 Jul 2013 01:25:23 +0000 (21:25 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/model/model.nit
src/model/model_base.nit
src/modelbuilder.nit
src/more_collections.nit [new file with mode: 0644]

index 3d6440b..58723eb 100644 (file)
@@ -32,6 +32,7 @@ module model
 import poset
 import location
 import model_base
+private import more_collections
 
 redef class Model
        # All known classes
index 834a731..b2483d9 100644 (file)
@@ -18,76 +18,7 @@ module model_base
 
 import poset
 import location
-
-# Simple way to store an HashMap[K, Array[V]]
-# FIXME: this should move to its own module
-class MultiHashMap[K: Object, V]
-       super HashMap[K, Array[V]]
-
-       # Add `v' to the array associated with `k'.
-       # If there is no array associated, then create it.
-       fun add_one(k: K, v: V)
-       do
-               if self.has_key(k) then
-                       self[k].add(v)
-               else
-                       self[k] = [v]
-               end
-       end
-
-       init do end
-end
-
-# Simple way to store an HashMap[K1, HashMap[K2, V]]
-# FIXME: this should move to its own module
-class HashMap2[K1: Object, K2: Object, V]
-       private var level1: HashMap[K1, HashMap[K2, V]] = new HashMap[K1, HashMap[K2, V]]
-       fun [](k1: K1, k2: K2): nullable V
-       do
-               var level1 = self.level1
-               if not level1.has_key(k1) then return null
-               var level2 = level1[k1]
-               if not level2.has_key(k2) then return null
-               return level2[k2]
-       end
-       fun []=(k1: K1, k2: K2, v: V)
-       do
-               var level1 = self.level1
-               var level2: HashMap[K2, V]
-               if not level1.has_key(k1) then
-                       level2 = new HashMap[K2, V]
-                       level1[k1] = level2
-               else
-                       level2 = level1[k1]
-               end
-               level2[k2] = v
-       end
-end
-
-# Simple way to store an HashMap[K1, HashMap[K2, HashMap[K3, V]]]
-# FIXME: this should move to its own module
-class HashMap3[K1: Object, K2: Object, K3: Object, V]
-       private var level1: HashMap[K1, HashMap2[K2, K3, V]] = new HashMap[K1, HashMap2[K2, K3, V]]
-       fun [](k1: K1, k2: K2, k3: K3): nullable V
-       do
-               var level1 = self.level1
-               if not level1.has_key(k1) then return null
-               var level2 = level1[k1]
-               return level2[k2, k3]
-       end
-       fun []=(k1: K1, k2: K2, k3: K3, v: V)
-       do
-               var level1 = self.level1
-               var level2: HashMap2[K2, K3, V]
-               if not level1.has_key(k1) then
-                       level2 = new HashMap2[K2, K3, V]
-                       level1[k1] = level2
-               else
-                       level2 = level1[k1]
-               end
-               level2[k2, k3] = v
-       end
-end
+private import more_collections
 
 # The container class of a Nit object-oriented model.
 # A model knows modules, classes and properties and can retrieve them.
index b0a5652..fd13faa 100644 (file)
@@ -29,6 +29,8 @@ import opts
 import toolcontext
 import phase
 
+private import more_collections
+
 ###
 
 redef class ToolContext
diff --git a/src/more_collections.nit b/src/more_collections.nit
new file mode 100644 (file)
index 0000000..2369c13
--- /dev/null
@@ -0,0 +1,93 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Highly specific, but useful, collections-related classes.
+module more_collections
+
+# Simple way to store an HashMap[K, Array[V]]
+class MultiHashMap[K: Object, V]
+       super HashMap[K, Array[V]]
+
+       # Add `v' to the array associated with `k'.
+       # If there is no array associated, then create it.
+       fun add_one(k: K, v: V)
+       do
+               if self.has_key(k) then
+                       self[k].add(v)
+               else
+                       self[k] = [v]
+               end
+       end
+
+       init do end
+end
+
+# Simple way to store an HashMap[K1, HashMap[K2, V]]
+class HashMap2[K1: Object, K2: Object, V]
+       private var level1: HashMap[K1, HashMap[K2, V]] = new HashMap[K1, HashMap[K2, V]]
+
+       # Return the value associated to the keys `k1` and `k2`.
+       # Return `null` if no such a value.
+       fun [](k1: K1, k2: K2): nullable V
+       do
+               var level1 = self.level1
+               if not level1.has_key(k1) then return null
+               var level2 = level1[k1]
+               if not level2.has_key(k2) then return null
+               return level2[k2]
+       end
+
+       # Set `v` the value associated to the keys `k1` and `k2`.
+       fun []=(k1: K1, k2: K2, v: V)
+       do
+               var level1 = self.level1
+               var level2: HashMap[K2, V]
+               if not level1.has_key(k1) then
+                       level2 = new HashMap[K2, V]
+                       level1[k1] = level2
+               else
+                       level2 = level1[k1]
+               end
+               level2[k2] = v
+       end
+end
+
+# Simple way to store an HashMap[K1, HashMap[K2, HashMap[K3, V]]]
+class HashMap3[K1: Object, K2: Object, K3: Object, V]
+       private var level1: HashMap[K1, HashMap2[K2, K3, V]] = new HashMap[K1, HashMap2[K2, K3, V]]
+
+       # Return the value associated to the keys `k1`, `k2`, and `k3`.
+       # Return `null` if no such a value.
+       fun [](k1: K1, k2: K2, k3: K3): nullable V
+       do
+               var level1 = self.level1
+               if not level1.has_key(k1) then return null
+               var level2 = level1[k1]
+               return level2[k2, k3]
+       end
+
+       # Set `v` the value associated to the keys `k1`, `k2`, and `k3`.
+       fun []=(k1: K1, k2: K2, k3: K3, v: V)
+       do
+               var level1 = self.level1
+               var level2: HashMap2[K2, K3, V]
+               if not level1.has_key(k1) then
+                       level2 = new HashMap2[K2, K3, V]
+                       level1[k1] = level2
+               else
+                       level2 = level1[k1]
+               end
+               level2[k2, k3] = v
+       end
+end