[MOAB-dev] r3085 - in MOAB/trunk: . parallel

kraftche at cae.wisc.edu kraftche at cae.wisc.edu
Thu Jul 30 18:30:35 CDT 2009


Author: kraftche
Date: 2009-07-30 18:30:35 -0500 (Thu, 30 Jul 2009)
New Revision: 3085

Modified:
   MOAB/trunk/RangeMap.hpp
   MOAB/trunk/ReadHDF5.cpp
   MOAB/trunk/WriteHDF5.cpp
   MOAB/trunk/parallel/WriteHDF5Parallel.cpp
Log:
Fix reoccuring bug using RangeMap the right way.  Rather than return
end() from insert(..) when the insertion fails, which leads to broken
code such as:
  if (idmap.end() != idmap.insert(..))
instead return std::pair<iterator,bool> where the second value indicates
the success of the insertion and the first value is the position of
insertion or the conflicting range that prevented the insertion.  This is 
more consistent with STL containers for which insertion can fail 
(std::set and std::map) and prevents the above coding mistake.


Modified: MOAB/trunk/RangeMap.hpp
===================================================================
--- MOAB/trunk/RangeMap.hpp	2009-07-30 23:23:13 UTC (rev 3084)
+++ MOAB/trunk/RangeMap.hpp	2009-07-30 23:30:35 UTC (rev 3085)
@@ -56,9 +56,11 @@
    * Insert mapping from [first_key, first_key+count) to [first_val, first_val+count) 
    *
    * Input range of keys many not overlap any other input range.  If it does overlap
-   * an existing range, end() is returned and the internal map is not changed.
+   * an existing range, the second value of the pair will be returned as false
+   * and the iterator will point to (one of) the overlapping ranges.
    */
-  inline iterator insert( KeyType first_key, ValType first_val, KeyType count );
+  inline std::pair<iterator,bool>
+  insert( KeyType first_key, ValType first_val, KeyType count );
   
   /** Find the value corresponding to the specified key.  Returns NullVal if not found */
   inline ValType find( KeyType key ) const;
@@ -110,7 +112,7 @@
 };
 
 template <typename KeyType, typename ValType, ValType NullVal> 
-inline typename RangeMap<KeyType,ValType,NullVal>::iterator
+inline std::pair<typename RangeMap<KeyType,ValType,NullVal>::iterator,bool>
 RangeMap<KeyType,ValType,NullVal>::insert( KeyType first_key, ValType first_val, KeyType count )
 {
   Range block = { first_key, count, first_val };
@@ -122,15 +124,15 @@
       if (i->begin + i->count == first_key && 
           i->value + i->count == first_val) {
         i->count += count;
-        return i;
+        return std::pair<iterator,bool>(i,true);
       }
     }
     data.push_back( block );
-    return data.end() - 1;
+    return std::pair<iterator,bool>(data.end() - 1,true);
   }
   
   if (i->begin < first_key + count)
-    return data.end();
+    return std::pair<iterator,bool>(i,false);
   
   if (i->begin == first_key + count &&
       i->value == first_val + count) {
@@ -148,7 +150,7 @@
         --i;
       }
     }
-    return i;
+    return std::pair<iterator,bool>(i,true);
   }
   
   if (i != data.begin()) {
@@ -156,12 +158,12 @@
     if (i->begin + i->count == first_key &&
         i->value + i->count == first_val) {
       i->count += count;
-      return i;
+      return std::pair<iterator,bool>(i,true);
     }
     ++i;
   }
   
-  return data.insert( i, block );
+  return std::pair<iterator,bool>(data.insert( i, block ),true);
 }
 
 

Modified: MOAB/trunk/ReadHDF5.cpp
===================================================================
--- MOAB/trunk/ReadHDF5.cpp	2009-07-30 23:23:13 UTC (rev 3084)
+++ MOAB/trunk/ReadHDF5.cpp	2009-07-30 23:30:35 UTC (rev 3085)
@@ -968,8 +968,7 @@
       memset( arrays[i], 0, count*sizeof(double) );
       arrays[i] += count;
     }
-    IDMap::iterator ins = idMap.insert( p->first, handle, count );
-    if (ins == idMap.end()) {
+    if (!idMap.insert( p->first, handle, count ).second) {
       mhdf_closeData( filePtr, data_id, &status );
       return error(MB_FAILURE);
     }
@@ -1034,8 +1033,7 @@
                                          array );
     if (MB_SUCCESS != rval) 
       break;
-    IDMap::iterator ins = idMap.insert( p->first, handle, count );
-    if (ins == idMap.end()) 
+    if (!idMap.insert( p->first, handle, count ).second) 
       { rval = MB_FAILURE; break; }
 
     mhdf_readConnectivityWithOpt( data_id, p->first - first_id, count, handleType, array, indepIO, &status );
@@ -1145,8 +1143,7 @@
         iter += node_per_elem;
         continue;
       }
-      IDMap::iterator ins = idMap.insert( start_id + i, h++, 1 );
-      if (ins == idMap.end()) 
+      if (!idMap.insert( start_id + i, h++, 1 ).second) 
         return error(MB_FAILURE);
         
       long* const end = iter + node_per_elem;
@@ -1236,8 +1233,7 @@
       MBErrorCode rval = mb->create_element( type, conn, len, handle );
       if (MB_SUCCESS != rval)
         return error(rval);
-      IDMap::iterator ins = idMap.insert( file_id, handle, 1 );
-      if (ins == idMap.end()) 
+      if (!idMap.insert( file_id, handle, 1 ).second) 
         return error(MB_FAILURE);
       return MB_SUCCESS;
     }
@@ -2068,8 +2064,7 @@
     MBEntityHandle h = start_handle;
     for (p = file_ids.const_pair_begin(); p != file_ids.const_pair_end(); ++p) {
       long count = p->second - p->first + 1;
-      IDMap::iterator ins = idMap.insert( p->first, h, count );
-      if (ins == idMap.end()) 
+      if (!idMap.insert( p->first, h, count ).second) 
         return error(MB_FAILURE);
       h += count;
     }

Modified: MOAB/trunk/WriteHDF5.cpp
===================================================================
--- MOAB/trunk/WriteHDF5.cpp	2009-07-30 23:23:13 UTC (rev 3084)
+++ MOAB/trunk/WriteHDF5.cpp	2009-07-30 23:30:35 UTC (rev 3085)
@@ -251,8 +251,7 @@
       (unsigned long)id,
       (unsigned long)(id+n-1));
 #endif
-    RangeMap<MBEntityHandle,id_t>::iterator it = idMap.insert( pi->first, id, n );
-    if (idMap.end() == it)
+    if (!idMap.insert( pi->first, id, n ).second)
       return MB_FAILURE;
     id += n;
   }

Modified: MOAB/trunk/parallel/WriteHDF5Parallel.cpp
===================================================================
--- MOAB/trunk/parallel/WriteHDF5Parallel.cpp	2009-07-30 23:23:13 UTC (rev 3084)
+++ MOAB/trunk/parallel/WriteHDF5Parallel.cpp	2009-07-30 23:30:35 UTC (rev 3085)
@@ -1350,7 +1350,6 @@
     if (p == val_id_map.end())
       val_id_map[data.all_values[i]] = offset++;
   }
-  RangeMap<MBEntityHandle,id_t>::iterator insp;
   MBRange::const_iterator riter = data.range.begin();
   for (size_t i = 0; i < data.local_values.size(); ++i, ++riter)
   {
@@ -1358,8 +1357,7 @@
     assert( p != val_id_map.end() );
     long id = p->second;
     
-    insp = idMap.insert( *riter, id, 1 );
-    if (idMap.end() == insp) {
+    if (!idMap.insert( *riter, id, 1 ).second) {
       for (unsigned x = 0; x < myPcomm->size(); ++x) {
         MPI_Barrier( myPcomm->proc_config().proc_comm() );      
         if (x != myPcomm->rank()) continue;   
@@ -2121,7 +2119,6 @@
   }
   
     // store file IDs for remote entities
-  RangeMap<MBEntityHandle,id_t>::iterator insp;
   for (proc_iter p = interfaceMesh.begin(); p != interfaceMesh.end(); ++p) {
     if (p->first == myPcomm->proc_config().proc_rank())
       continue;
@@ -2141,8 +2138,7 @@
          return MB_FAILURE;
       }
       else {
-        insp = idMap.insert( *i, *j, 1 );
-        if (insp == idMap.end()) {
+        if (!idMap.insert( *i, *j, 1 ).second) {
           iFace->tag_delete( file_id_tag );
           return MB_FAILURE;
         }



More information about the moab-dev mailing list