optimizations to IntPairMap.java
[sbp.git] / src / edu / berkeley / sbp / util / IntegerTopology.java
index ae99c08..55ac082 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp.util;
 import java.io.*;
 import java.util.*;
@@ -7,7 +9,7 @@ import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.util.*;
 
 /** implementation of <tt>Topology</tt> for any class for which there is a mapping to the <tt>int</tt>s */
-public class IntegerTopology<V> implements Topology<V> {
+public class IntegerTopology<V> implements Topology<V>, Serializable {
 
     private final Range.Set rs;
     private final Functor<V,Integer> f;
@@ -15,6 +17,7 @@ public class IntegerTopology<V> implements Topology<V> {
     private int toInt(V v) { return f==null?((IntegerMappable)v).toInt():f.invoke(v); }
 
     public Range.Set getRanges()         { return new Range.Set(rs); }
+    public Functor<V,Integer> functor() { return f; }
 
     public IntegerTopology(Functor<V,Integer> f)               { this(f, new Range.Set()); }
     public IntegerTopology(Functor<V,Integer> f, V a)          { this(f, f==null?((IntegerMappable)a).toInt():f.invoke(a)); }
@@ -23,8 +26,11 @@ public class IntegerTopology<V> implements Topology<V> {
     public IntegerTopology(Functor<V,Integer> f, int a)        { this(f, a, a); }
     public IntegerTopology(Functor<V,Integer> f, int a, int b) { this(f, new Range(a, b)); }
     public IntegerTopology(Functor<V,Integer> f, Range r)      { this(f, new Range.Set(r)); }
-    public IntegerTopology(Functor<V,Integer> f, Range.Set rs) { this.rs = rs; this.f = f; }
-
+    public IntegerTopology(Functor<V,Integer> f, Range.Set rs) {
+        this.rs = rs;
+        this.f = f==null?(this instanceof Functor ? (Functor)this : null):f;
+    }
+    
     public Topology<V> empty()              { return new IntegerTopology<V>(f);   }
         
     public boolean          contains(V v)              { return rs.contains(toInt(v)); }