From a58a1f85fd1039e8f1ed839cee98aff9ccf7b34d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Christophe=20Beaupr=C3=A9?= Date: Sun, 21 May 2017 19:20:58 -0400 Subject: [PATCH 1/1] coloring: Only generate strictly positive IDs MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This will allow to reserve negative IDs (including 0) for special purposes. Signed-off-by: Jean-Christophe Beaupré --- src/compiler/coloring.nit | 5 ++++- src/compiler/test_coloring.nit | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/compiler/test_coloring.nit diff --git a/src/compiler/coloring.nit b/src/compiler/coloring.nit index 9b3748b..80f29ec 100644 --- a/src/compiler/coloring.nit +++ b/src/compiler/coloring.nit @@ -178,6 +178,9 @@ class POSetColorer[E: Object] var is_colored = false # Resulting ids + # + # All ids are strictly positive (`>= 1`). + # # REQUIRE: is_colored fun ids: Map[E, Int] do assert is_colored @@ -223,7 +226,7 @@ class POSetColorer[E: Object] ids_cache.clear var elements = new HashSet[E].from(poset_cache.to_a) for e in poset_cache.linearize(elements) do - ids_cache[e] = ids_cache.length + ids_cache[e] = ids_cache.length + 1 end end diff --git a/src/compiler/test_coloring.nit b/src/compiler/test_coloring.nit new file mode 100644 index 0000000..867afcf --- /dev/null +++ b/src/compiler/test_coloring.nit @@ -0,0 +1,32 @@ +# 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. + +module test_coloring is test_suite + +import test_suite +import coloring + +class TestPOSetColorer + super TestSuite + + fun test_ids_strictly_positive do + var poset = new POSet[String] + poset.add_node "A" + + var colorer = new POSetColorer[String] + colorer.colorize(poset) + + assert colorer.ids["A"] > 0 + end +end -- 1.7.9.5