Support building without TR1.

Change-Id: Ib59e201198e1ff2621626ab80e6b6f2156f0d1d1
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 628cab7..f8af88c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -413,17 +413,27 @@
   ADD_DEFINITIONS(-DCERES_NO_THREADS)
 ENDIF (${BUILD_ANDROID})
 
-# Use the std namespace for the hash<> and related templates. This may vary by
-# system.
-IF (MSVC)
-  # This is known to work with Visual Studio 2010 Express.
-  ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_START=namespace std {\"")
-  ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_END=}\"")
-ELSE (MSVC)
-  # This is known to work with recent versions of Linux and Mac OS X.
-  ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_START=namespace std { namespace tr1 {\"")
-  ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_END=}}\"")
-ENDIF (MSVC)
+OPTION(DISABLE_TR1
+       "Don't use TR1. This replaces some hash tables with sets. Slower."
+       OFF)
+
+IF (${DISABLE_TR1})
+  MESSAGE("-- Replacing unordered_map/set with map/set (warning: slower!)")
+  ADD_DEFINITIONS(-DCERES_NO_TR1)
+ELSE (${DISABLE_TR1})
+  MESSAGE("-- Using normal TR1 unordered_map/set")
+  # Use the std namespace for the hash<> and related templates. This may vary by
+  # system.
+  IF (MSVC)
+    # This is known to work with Visual Studio 2010 Express.
+    ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_START=namespace std {\"")
+    ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_END=}\"")
+  ELSE (MSVC)
+    # This is known to work with recent versions of Linux and Mac OS X.
+    ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_START=namespace std { namespace tr1 {\"")
+    ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_END=}}\"")
+  ENDIF (MSVC)
+ENDIF (${DISABLE_TR1})
 
 INCLUDE_DIRECTORIES(
   include
diff --git a/internal/ceres/collections_port.h b/internal/ceres/collections_port.h
index 88032a5..a356cc0 100644
--- a/internal/ceres/collections_port.h
+++ b/internal/ceres/collections_port.h
@@ -33,17 +33,41 @@
 #ifndef CERES_INTERNAL_COLLECTIONS_PORT_H_
 #define CERES_INTERNAL_COLLECTIONS_PORT_H_
 
-#if defined(_MSC_VER) && _MSC_VER <= 1600
-#include <unordered_map>
-#include <unordered_set>
+#if defined(CERES_NO_TR1)
+#  include <map>
+#  include <set>
 #else
-#include <tr1/unordered_map>
-#include <tr1/unordered_set>
+#  if defined(_MSC_VER) && _MSC_VER <= 1600
+#    include <unordered_map>
+#    include <unordered_set>
+#  else
+#    include <tr1/unordered_map>
+#    include <tr1/unordered_set>
+#  endif
 #endif
 #include <utility>
 #include "ceres/integral_types.h"
 #include "ceres/internal/port.h"
 
+// Some systems don't have access to TR1. In that case, substitute the hash
+// map/set with normal map/set. The price to pay is slightly slower speed for
+// some operations.
+#if defined(CERES_NO_TR1)
+
+namespace ceres {
+namespace internal {
+
+template<typename K, typename V>
+struct HashMap : map<K, V> {};
+
+template<typename K>
+struct HashSet : set<K> {};
+
+}  // namespace internal
+}  // namespace ceres
+
+#else
+
 namespace ceres {
 namespace internal {
 
@@ -138,4 +162,6 @@
 
 CERES_HASH_NAMESPACE_END
 
+#endif  // CERES_NO_TR1
+
 #endif  // CERES_INTERNAL_COLLECTIONS_PORT_H_
diff --git a/internal/ceres/schur_ordering.cc b/internal/ceres/schur_ordering.cc
index 2fe1080..0654724 100644
--- a/internal/ceres/schur_ordering.cc
+++ b/internal/ceres/schur_ordering.cc
@@ -39,6 +39,8 @@
 #include "ceres/residual_block.h"
 #include "glog/logging.h"
 
+#if !defined(CERES_NO_TR1)
+
 CERES_HASH_NAMESPACE_START
 
 // Allow us to hash pointers as if they were int's
@@ -50,6 +52,8 @@
 
 CERES_HASH_NAMESPACE_END
 
+#endif
+
 namespace ceres {
 namespace internal {