[MOAB-dev] r1374 - in MOAB/trunk: . test/perf tools/mbperf

kraftche at mcs.anl.gov kraftche at mcs.anl.gov
Fri Nov 9 16:28:33 CST 2007


Author: kraftche
Date: 2007-11-09 16:28:33 -0600 (Fri, 09 Nov 2007)
New Revision: 1374

Added:
   MOAB/trunk/ElementSequence.hpp
   MOAB/trunk/ScdElementData.cpp
   MOAB/trunk/ScdElementData.hpp
   MOAB/trunk/ScdVertexData.cpp
   MOAB/trunk/ScdVertexData.hpp
   MOAB/trunk/SequenceData.cpp
   MOAB/trunk/SequenceData.hpp
   MOAB/trunk/SequenceManager.cpp
   MOAB/trunk/SequenceManager.hpp
   MOAB/trunk/StructuredElementSeq.cpp
   MOAB/trunk/StructuredElementSeq.hpp
   MOAB/trunk/TestTypeSequenceManager.cpp
   MOAB/trunk/TestUtil.hpp
   MOAB/trunk/TypeSequenceManager.cpp
   MOAB/trunk/TypeSequenceManager.hpp
   MOAB/trunk/UnstructuredElemSeq.cpp
   MOAB/trunk/UnstructuredElemSeq.hpp
   MOAB/trunk/VertexSequence.cpp
   MOAB/trunk/VertexSequence.hpp
Removed:
   MOAB/trunk/EntitySequenceManager.cpp
   MOAB/trunk/EntitySequenceManager.hpp
   MOAB/trunk/PolyEntitySequence.cpp
   MOAB/trunk/PolyEntitySequence.hpp
   MOAB/trunk/ScdElementSeq.cpp
   MOAB/trunk/ScdElementSeq.hpp
   MOAB/trunk/ScdVertexSeq.cpp
   MOAB/trunk/ScdVertexSeq.hpp
Modified:
   MOAB/trunk/EntitySequence.cpp
   MOAB/trunk/EntitySequence.hpp
   MOAB/trunk/HigherOrderFactory.cpp
   MOAB/trunk/HigherOrderFactory.hpp
   MOAB/trunk/MBCore.cpp
   MOAB/trunk/MBCore.hpp
   MOAB/trunk/MBRangeSeqIntersectIter.cpp
   MOAB/trunk/MBRangeSeqIntersectIter.hpp
   MOAB/trunk/MBReadUtil.cpp
   MOAB/trunk/MBReadUtil.hpp
   MOAB/trunk/MBReadUtilIface.hpp
   MOAB/trunk/MBTest.cpp
   MOAB/trunk/MBWriteUtil.cpp
   MOAB/trunk/Makefile.am
   MOAB/trunk/MeshSetSequence.cpp
   MOAB/trunk/MeshSetSequence.hpp
   MOAB/trunk/ReadHDF5.cpp
   MOAB/trunk/ReadVtk.cpp
   MOAB/trunk/ReadVtk.hpp
   MOAB/trunk/WriteHDF5.cpp
   MOAB/trunk/WriteHDF5.hpp
   MOAB/trunk/scdseq_test.cpp
   MOAB/trunk/test/perf/perf.cpp
   MOAB/trunk/tools/mbperf/mbperf.cpp
Log:
Rewrite EntitySequence data structure and logic:

 o Represent holes in blocks of data by separating the EntitySequence
   instance from the SequenceData instance.  
   
 o Separate entity-type specific logic and type-independent logic.
   The latter is implemented in TypeSequenceManager.  The SequenceManager
   class implements the former and gains the latter through the
   contained array of TypeSequenceManager instances (one for each 
   MBEntityType).
   
 o Represent MBPOLYGON and MBPOLYHEDRON elements using the same fixed-
   length connectivity data structure that is used for other unstructred
   mesh elements.  Create multiple sequences and group into sequences
   by connectivity length.

 o Update file readers and writers for change to poly element 
   representation.  .H5M file format modified to use similar data
   organization in the file format.  Support for reading older-format
   poly data is retained.
   
 
 


Added: MOAB/trunk/ElementSequence.hpp
===================================================================
--- MOAB/trunk/ElementSequence.hpp	                        (rev 0)
+++ MOAB/trunk/ElementSequence.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,75 @@
+#ifndef ELEMENT_SEQUENCE_HPP
+#define ELEMENT_SEQUENCE_HPP
+
+#include "EntitySequence.hpp"
+#include "SequenceData.hpp"
+#include "MBCN.hpp"
+
+class ElementSequence : public EntitySequence
+{
+public:
+  
+  ElementSequence( MBEntityHandle start,
+                   MBEntityID count,
+                   unsigned int nodes_per_element,
+                   SequenceData* data )
+    : EntitySequence( start, count, data ), 
+      nodesPerElement(nodes_per_element)
+    {}
+                   
+  virtual ~ElementSequence() {}
+  
+  inline unsigned int nodes_per_element() const { return nodesPerElement; }
+  
+  virtual MBErrorCode get_connectivity( MBEntityHandle handle,
+                                        std::vector<MBEntityHandle>& connect,
+                                        bool topological = false ) const = 0;
+  
+  virtual MBErrorCode get_connectivity( MBEntityHandle handle,
+                                        MBEntityHandle const*& connect,
+                                        int &connect_length,
+                                        bool topological = false,
+                                        std::vector<MBEntityHandle>* storage = 0
+                                       ) const = 0;
+
+  virtual MBErrorCode set_connectivity( MBEntityHandle handle,
+                                        MBEntityHandle const* connect,
+                                        int connect_length ) = 0;
+
+  inline MBEntityHandle const* get_connectivity_array() const;
+  
+  virtual MBEntityHandle* get_connectivity_array() = 0;
+  
+  inline bool has_mid_edge_nodes() const;
+  inline bool has_mid_face_nodes() const;
+  inline bool has_mid_volume_nodes() const;
+
+protected:
+
+  ElementSequence( ElementSequence& split_from, MBEntityHandle here )
+    : EntitySequence( split_from, here ),
+      nodesPerElement( split_from.nodesPerElement )
+    {}
+
+private:
+  
+  unsigned nodesPerElement;
+};
+
+inline MBEntityHandle const*
+ElementSequence::get_connectivity_array() const
+  { return const_cast<ElementSequence*>(this)->get_connectivity_array(); }
+
+inline bool
+ElementSequence::has_mid_edge_nodes() const
+  { return MBCN::HasMidEdgeNodes( type(), nodes_per_element() ); }
+
+inline bool
+ElementSequence::has_mid_face_nodes() const
+  { return MBCN::HasMidFaceNodes( type(), nodes_per_element() ); }
+
+inline bool
+ElementSequence::has_mid_volume_nodes() const
+  { return MBCN::HasMidRegionNodes( type(), nodes_per_element() ); }
+  
+#endif

Modified: MOAB/trunk/EntitySequence.cpp
===================================================================
--- MOAB/trunk/EntitySequence.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/EntitySequence.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,816 +1,73 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-
-#ifdef WIN32
-#ifdef _DEBUG
-// turn off warnings that say they debugging identifier has been truncated
-// this warning comes up when using some STL containers
-#pragma warning(disable : 4786)
-#endif
-#endif
-
-
 #include "EntitySequence.hpp"
-#include "EntitySequenceManager.hpp"
-#include "MBCN.hpp"
-#include <assert.h>
-#include "AEntityFactory.hpp" 
-#include "MBCore.hpp"
-#include <set>
+#include "SequenceData.hpp"
 
-using namespace std;
-
-//Constructor
-MBEntitySequence::MBEntitySequence(EntitySequenceManager* seq_manager,
-  MBEntityHandle start_handle, MBEntityID num_entities )
-  : mSequenceManager(seq_manager)
+bool EntitySequence::using_entire_data() const 
 {
-  assert(MB_START_ID <= ID_FROM_HANDLE(start_handle));
-  assert(TYPE_FROM_HANDLE(start_handle) < MBMAXTYPE);
-
-  // set first entity handle in sequence
-  mStartEntityHandle = start_handle;
-
-  // set number of entity handles in sequence
-  mNumAllocated = num_entities;
-
-  // no entities created yet
-  mNumEntities = 0;
-
-  mLastDeletedIndex = -1;
+  return start_handle() == data()->start_handle()
+        && end_handle() == data()->  end_handle();
 }
 
-MBEntitySequence::~MBEntitySequence()
-{ }
+int EntitySequence::values_per_entity() const
+  { return 0; }
 
-VertexEntitySequence::VertexEntitySequence(EntitySequenceManager* seq_manager,
-                                           MBEntityHandle start_handle, 
-                                           MBEntityID num_entities,
-                                           bool all_handles_used)
-: MBEntitySequence(seq_manager, start_handle, num_entities)
+MBErrorCode EntitySequence::pop_back( MBEntityID count )
 {
-  // we are assuming this through most of this class, so make
-  // sure it's true. - j.k. 2007-03-09
-  assert(sizeof(MBEntityID) <= sizeof(double));
-
-  seq_manager->entity_sequence_created(this);
-
-  // allocate the arrays
-  mCoords = new double* [3];
-  mCoords[0] = new double[num_entities];
-  mCoords[1] = new double[num_entities];
-  mCoords[2] = new double[num_entities];
+  MBEntityHandle new_end = endHandle - count;
+  if (new_end < startHandle)
+    return MB_FAILURE;
   
-  mFreeEntities.clear();
-  if(all_handles_used)
-  {
-    mNumEntities = num_entities;
-    mFirstFreeIndex = -1;
-  }
-  else
-  {
-    seq_manager->notify_not_full(this);
-    mFreeEntities.resize(mNumAllocated, true);
-    mNumEntities = 0;
-    mFirstFreeIndex = 0;
-    for(MBEntityID i=0; i<num_entities; i++)
-    {
-      reinterpret_cast<MBEntityID&>(mCoords[0][i]) = i+1;
-    }
-    reinterpret_cast<MBEntityID&>(mCoords[0][num_entities-1]) = -1;
-  }
+  endHandle = new_end;
+  return MB_SUCCESS;
 }
 
-VertexEntitySequence::~VertexEntitySequence()
+MBErrorCode EntitySequence::pop_front( MBEntityID count )
 {
-  mSequenceManager->entity_sequence_deleted(this);
-  delete [] mCoords[0];
-  delete [] mCoords[1];
-  delete [] mCoords[2];
-  delete [] mCoords;
-}
-
-
-MBEntityHandle VertexEntitySequence::get_unused_handle()
-{
-  if(mFirstFreeIndex == -1)
-    return 0;
-
-  MBEntityHandle new_handle = mStartEntityHandle + mFirstFreeIndex;
-  mFreeEntities[mFirstFreeIndex] = false;
-
-  mFirstFreeIndex = reinterpret_cast<MBEntityID&>(mCoords[0][mFirstFreeIndex]);
-
-  mNumEntities++;
-
-  if(mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_full(this);
-    std::vector<bool> empty;
-    mFreeEntities.swap(empty);
-  }
-
-  if( mLastDeletedIndex == (MBEntityID)( new_handle - mStartEntityHandle ))
-    mLastDeletedIndex = -1;
-
-  return new_handle;
-}
-
-MBErrorCode VertexEntitySequence::allocate_handle(MBEntityHandle handle) 
-{
-  return MB_FAILURE;
-}
-
-void VertexEntitySequence::free_handle(MBEntityHandle handle)
-{
-
-  if(!is_valid_entity(handle))
-    return;
-
-  if(mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_not_full(this);
-    mFreeEntities.resize( mNumAllocated, false );
-  }
-
-  mFreeEntities[handle - mStartEntityHandle] = true;
-
-  MBEntityID prev_free_handle = -1;
-  const MBEntityID handle_index = handle - mStartEntityHandle;
+  MBEntityHandle new_start = startHandle + count;
+  if (new_start > endHandle)
+    return MB_FAILURE;
   
-  MBEntityID index = mFirstFreeIndex;
-  if( mLastDeletedIndex != -1 && handle_index > mLastDeletedIndex)
-    index = mLastDeletedIndex;
-
-  mLastDeletedIndex = handle_index;
-
-  for(;
-      (index != -1) && (index < handle_index); 
-      index = reinterpret_cast<MBEntityID&>(mCoords[0][index]) )
-  {
-    prev_free_handle = index;
-  }
-
-  // was full and now has one free handle
-  if(prev_free_handle == -1 && mFirstFreeIndex == -1)
-  {
-    mFirstFreeIndex = handle_index;
-    reinterpret_cast<MBEntityID&>(mCoords[0][handle_index]) = -1;
-  }
-  // insert before all free handles
-  else if(prev_free_handle == -1)
-  {
-    reinterpret_cast<MBEntityID&>(mCoords[0][handle_index]) = mFirstFreeIndex;
-    mFirstFreeIndex = handle_index;
-  }
-  // insert in middle or end
-  else
-  {
-    reinterpret_cast<MBEntityID&>(mCoords[0][handle_index]) = 
-      reinterpret_cast<MBEntityID&>(mCoords[0][prev_free_handle]);
-    reinterpret_cast<MBEntityID&>(mCoords[0][prev_free_handle]) = handle_index;
-  }
-
-  mNumEntities--;
-
+  startHandle = new_start;
+  return MB_SUCCESS;
 }
 
-void VertexEntitySequence::get_entities(MBRange& entities) const
-{
-  MBRange::iterator iter = entities.insert(mStartEntityHandle, mStartEntityHandle+mNumAllocated-1);
-  
-  for(MBEntityID index = mFirstFreeIndex; (index != -1); 
-      index = reinterpret_cast<MBEntityID&>(mCoords[0][index]))
-  {
-    for(; *iter != (mStartEntityHandle+index); ++iter);
-    entities.erase(iter);
-  }
 
-}
-
-MBEntityID VertexEntitySequence::get_next_free_index( MBEntityID prev_free_index ) const
+MBErrorCode EntitySequence::prepend_entities( MBEntityID count )
 {
-  if (prev_free_index < 0)
-    return mFirstFreeIndex;
-  return reinterpret_cast<MBEntityID&>(mCoords[0][prev_free_index]);
-}
-
-void VertexEntitySequence::get_memory_use( unsigned long& used,
-                                           unsigned long& allocated) const
-{
-  unsigned long per_ent = get_memory_use((MBEntityHandle)0);
-  allocated = sizeof(*this) + mFreeEntities.capacity()/8 + per_ent*number_allocated();
-  used = per_ent * number_entities();
-}
-
-unsigned long VertexEntitySequence::get_memory_use( MBEntityHandle ) const
-{
-  return 3 * sizeof(double);
-}
-
-MBEntityID ElementEntitySequence::get_next_free_index( MBEntityID prev_free_index ) const
-{
-  if (prev_free_index < 0)
-    return mFirstFreeIndex;
-  return reinterpret_cast<MBEntityID&>(mElements[prev_free_index*mNodesPerElement]);
-}
-
-ElementEntitySequence::ElementEntitySequence(EntitySequenceManager* seq_manager,
-                        MBEntityHandle start_handle, MBEntityID num_entities,
-                        int nodes_per_element, bool all_handles_used,
-                                             bool allocate_connect)
-: MBEntitySequence(seq_manager, start_handle, num_entities)
-{
-  // this should never fail unless something when seriously wrong 
-  // during the configure phase, but make sure anyway because we're
-  // doing reintepret_casts
-  assert(sizeof(MBEntityHandle)>=sizeof(MBEntityID));
-
-  assert(nodes_per_element < 28);
-  if (allocate_connect) {
-    mElements = new MBEntityHandle[num_entities*nodes_per_element];
-    memset(mElements, 0, sizeof(MBEntityHandle)*num_entities*nodes_per_element);
-  }
-  else {
-    mElements = NULL;
-  }
+  MBEntityHandle new_start = startHandle - count;
+  if (new_start < data()->start_handle())
+    return MB_FAILURE;
   
-  mNodesPerElement = nodes_per_element;
-
-  seq_manager->entity_sequence_created(this);
-
-  mFreeEntities.clear();
-  if(all_handles_used)
-  {
-    mNumEntities = num_entities;
-    mFirstFreeIndex = -1;
-  }
-  else
-  {
-    seq_manager->notify_not_full(this);
-    mFreeEntities.resize( mNumAllocated, true );
-    mNumEntities = 0;
-    mFirstFreeIndex = 0;
-    if (nodes_per_element)
-    {
-      MBEntityID max = (num_entities-1)*nodes_per_element;
-      for(MBEntityID i=0; i<max ; i+=nodes_per_element)
-      {
-        mElements[i] = (i/nodes_per_element)+1;
-      }
-      reinterpret_cast<MBEntityID&>(mElements[max]) = -1;
-    }
-  }
+  startHandle = new_start;
+  return MB_SUCCESS;
 }
-
-
-ElementEntitySequence::~ElementEntitySequence()
+ 
+MBErrorCode EntitySequence::append_entities( MBEntityID count )
 {
-  mSequenceManager->entity_sequence_deleted(this);
-  if (NULL != mElements)
-    delete [] mElements;
-}
-
-MBEntityHandle ElementEntitySequence::get_unused_handle()
-{
-  if(mFirstFreeIndex == -1)
-    return 0;
-
-  mFreeEntities[mFirstFreeIndex] = false;
-  MBEntityHandle new_handle = mStartEntityHandle + mFirstFreeIndex;
-
-  mFirstFreeIndex = reinterpret_cast<MBEntityID&>(mElements[mFirstFreeIndex*mNodesPerElement]);
-
-  mNumEntities++;
-
-  if(mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_full(this);
-    std::vector<bool> empty;
-    mFreeEntities.swap(empty);
-  }
+  MBEntityHandle new_end = endHandle + count;
+  if (new_end > data()->end_handle())
+    return MB_FAILURE;
   
-  if( mLastDeletedIndex == (MBEntityID)( new_handle - mStartEntityHandle ))
-    mLastDeletedIndex = -1;
-
-  return new_handle;
+  endHandle = new_end;
+  return MB_SUCCESS;
 }
 
-MBErrorCode ElementEntitySequence::allocate_handle(MBEntityHandle handle) 
+MBErrorCode EntitySequence::merge( EntitySequence& other )
 {
-  return MB_FAILURE;
-}
-
-void ElementEntitySequence::free_handle(MBEntityHandle handle)
-{
-  if(!is_valid_entity(handle))
-    return;
-
-  if(mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_not_full(this);
-    mFreeEntities.resize( mNumAllocated, false );
+  if (sequenceData != other.sequenceData)
+    return MB_FAILURE;
+  if (end_handle() + 1 == other.start_handle()) {
+    endHandle = other.end_handle();
+    other.startHandle = other.end_handle()+1;
   }
-
-  mFreeEntities[handle-mStartEntityHandle] = true;
-  
-  MBEntityID prev_free_index = -1;
-  const MBEntityID handle_index = handle - mStartEntityHandle;
-
-  MBEntityID index = mFirstFreeIndex;
-  if( mLastDeletedIndex != -1 && handle_index > mLastDeletedIndex)
-    index = mLastDeletedIndex;
-
-  mLastDeletedIndex = handle_index;
-  
-  for(;
-      (index != -1) && (index < handle_index); 
-      index = reinterpret_cast<MBEntityID&>(mElements[index*mNodesPerElement]) )
-  {
-    prev_free_index = index;
+  else if (start_handle() == other.end_handle() + 1) {
+    startHandle = other.start_handle();
+    other.endHandle = other.start_handle()-1;
   }
-  
-  memset(&mElements[handle_index*mNodesPerElement], 0, sizeof(MBEntityHandle)*mNodesPerElement);
-
-  // was full and now has one free handle
-  if(prev_free_index == -1 && mFirstFreeIndex == -1)
-  {
-    mFirstFreeIndex = handle_index;
-    reinterpret_cast<MBEntityID&>(mElements[handle_index*mNodesPerElement]) = -1;
-  }
-  // insert before all free handles
-  else if(prev_free_index == -1)
-  {
-    reinterpret_cast<MBEntityID&>(mElements[handle_index*mNodesPerElement]) = mFirstFreeIndex;
-    mFirstFreeIndex = handle_index;
-  }
-  // insert in middle or end
   else
-  {
-    mElements[handle_index*mNodesPerElement] = mElements[prev_free_index*mNodesPerElement];
-    reinterpret_cast<MBEntityID&>(mElements[prev_free_index*mNodesPerElement]) = handle_index;
-  }
-
-  mNumEntities--;
-
-}
-
-
-
-void ElementEntitySequence::get_entities(MBRange& entities) const
-{
-  MBRange::iterator iter = entities.insert(mStartEntityHandle, mStartEntityHandle+mNumAllocated-1);
-  
-  for(MBEntityID index = mFirstFreeIndex; index != -1;
-      index = reinterpret_cast<MBEntityID&>(mElements[index*mNodesPerElement]) )
-  {
-    for(; *iter != (mStartEntityHandle+index); ++iter);
-    entities.erase(iter);
-  }
-}
-
-
-
-MBErrorCode ElementEntitySequence::split(MBEntityHandle split_location, 
-    MBEntitySequence*& new_sequence)
-{
-  // only split (begin, end)
-  assert(mStartEntityHandle < split_location);
-  assert(get_end_handle() >= split_location);
-
-  // make a new sequence
-  const bool all_used = mFreeEntities.empty();
-  ElementEntitySequence* seq = new ElementEntitySequence( mSequenceManager, split_location, 
-      get_end_handle() - split_location + 1 , mNodesPerElement, all_used);
-  new_sequence = seq;
-
-  // copy data into new sequence
-  memcpy(seq->mElements, &mElements[mNodesPerElement*(split_location - mStartEntityHandle)],
-         seq->mNumAllocated*mNodesPerElement*sizeof(MBEntityHandle));
-
-  // make a new shorter array for this sequence and copy data over 
-  mNumAllocated = split_location - mStartEntityHandle;
-  MBEntityHandle* tmp = new MBEntityHandle[mNumAllocated*mNodesPerElement];
-  memcpy(tmp, mElements, mNumAllocated*mNodesPerElement*sizeof(MBEntityHandle));
-  delete [] mElements;
-  mElements = tmp;
-
-  if (all_used) {
-    mNumEntities = split_location - get_start_handle();
-    assert( mFirstFreeIndex == -1 );
-    assert( mLastDeletedIndex == -1 );
-    assert( seq->mFirstFreeIndex == -1 );
-    assert( seq->mLastDeletedIndex == -1 );
-    assert( mNumEntities == mNumAllocated );
-    assert( seq->mNumEntities == seq->mNumAllocated );
-    return MB_SUCCESS;
-  }
-
-  //copy free handles over too
-  std::copy(mFreeEntities.begin()+(split_location-mStartEntityHandle),
-            mFreeEntities.end(), seq->mFreeEntities.begin());
-
-  // shrink capacity to what we need
-  mFreeEntities.resize(mNumAllocated);
-  std::vector<bool>(mFreeEntities).swap(mFreeEntities);
-
-
-  // need to recompute : mNumEntities, mFirstFreeIndex for both sequences
-  
-  mNumEntities = 0;
-  mFirstFreeIndex = -1;
-  
-  std::vector<bool>::reverse_iterator iter = mFreeEntities.rbegin();
-  std::vector<bool>::reverse_iterator end_iter = mFreeEntities.rend();
-  MBEntityID index = mFreeEntities.size() - 1;
-  MBEntityID last_index = -1;
-
-  for(; iter != end_iter; )
-  {
-    if(*iter == true)
-    {
-      reinterpret_cast<MBEntityID&>(mElements[index*mNodesPerElement]) = last_index;
-      last_index = index;
-    }
-    else
-    {
-      ++mNumEntities;
-    }
-
-    ++iter;
-    --index;
-  }
-  mFirstFreeIndex = last_index;
-
-  if(mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_full(this);
-    std::vector<bool> empty;
-    mFreeEntities.swap( empty );
-  }
-  else
-    mSequenceManager->notify_not_full(this);
-  
-  
-  seq->mNumEntities = 0;
-  seq->mFirstFreeIndex = -1;
-  
-  iter = seq->mFreeEntities.rbegin();
-  end_iter = seq->mFreeEntities.rend();
-  index = seq->mFreeEntities.size() - 1;
-  last_index = -1;
-
-  for(; iter != end_iter; )
-  {
-    if(*iter == true)
-    {
-      reinterpret_cast<MBEntityID&>(seq->mElements[index*mNodesPerElement]) = last_index;
-      last_index = index;
-    }
-    else
-    {
-      ++seq->mNumEntities;
-    }
-
-    ++iter;
-    --index;
-  }
-  seq->mFirstFreeIndex = last_index;
-  
-  if(seq->mNumEntities == seq->mNumAllocated) {
-    mSequenceManager->notify_full(seq);
-    std::vector<bool> empty;
-    seq->mFreeEntities.swap( empty );
-  }
-  else
-    mSequenceManager->notify_not_full(seq);
-
+    return MB_FAILURE;
   return MB_SUCCESS;
 }
 
-
-
-MBErrorCode ElementEntitySequence::convert_realloc(bool& mid_edge_nodes,
-    bool& mid_face_nodes, bool& mid_volume_nodes, MBCore* MB, 
-    MBTag delete_mark_bit )
-{
-
-  MBEntityType this_type = get_type();
-
-  // figure out how many nodes per element we'll end up with
-  int num_corner_nodes = MBCN::VerticesPerEntity(this_type);
-  int new_nodes_per_element = num_corner_nodes;
-  if(mid_edge_nodes)
-    new_nodes_per_element += MBCN::mConnectivityMap[this_type][0].num_sub_elements;
-  if(mid_face_nodes)
-    new_nodes_per_element += MBCN::mConnectivityMap[this_type][1].num_sub_elements;
-  if(mid_volume_nodes)
-    new_nodes_per_element++;
-
-  if(new_nodes_per_element == mNodesPerElement)
-    return MB_SUCCESS;
-
-  // make the new array
-  MBEntityHandle* new_array = new MBEntityHandle[mNumAllocated*new_nodes_per_element];
-  memset(new_array, 0, sizeof(MBEntityHandle)*mNumAllocated*new_nodes_per_element);
-
-  // copy the corner nodes
-  MBEntityHandle* old_end_handle = mElements + mNodesPerElement*mNumAllocated;
-  MBEntityHandle* old_iter = mElements;
-  MBEntityHandle* new_iter = new_array;
-  int amount_to_copy = num_corner_nodes*sizeof(MBEntityHandle);
-  for(; old_iter < old_end_handle; )
-  {
-    memcpy(new_iter, old_iter, amount_to_copy);
-    old_iter += mNodesPerElement;
-    new_iter += new_nodes_per_element;
-  }
-
-  // copy mid edge nodes if they existed and we want to keep them
-  if(mid_edge_nodes && has_mid_edge_nodes())
-  {
-    amount_to_copy = MBCN::mConnectivityMap[this_type][0].num_sub_elements * sizeof(MBEntityHandle);
-    old_end_handle = mElements + mNodesPerElement*mNumAllocated;
-    old_iter = mElements + MBCN::VerticesPerEntity(this_type);
-    new_iter = new_array + MBCN::VerticesPerEntity(this_type);
-    for(; old_iter < old_end_handle; )
-    {
-      memcpy(new_iter, old_iter, amount_to_copy);
-      old_iter += mNodesPerElement;
-      new_iter += new_nodes_per_element;
-    }
-  }
-  // need to delete them
-  else if(has_mid_edge_nodes())
-  {
-    int number_to_delete = MBCN::mConnectivityMap[this_type][0].num_sub_elements;
-    old_end_handle = mElements + mNodesPerElement*mNumAllocated;
-    old_iter = mElements + MBCN::VerticesPerEntity(this_type);
-
-    std::set<MBEntityHandle> nodes_processed; 
-    for(; old_iter < old_end_handle; )
-    {
-      //tag each node-to-delete with parent element entity handle and number 
-      //of elements it is found on
-      for(int i=0; i<number_to_delete; i++)
-      {
-        //see if node has been processed yet
-        if( old_iter[i] && nodes_processed.insert(old_iter[i]).second )
-        {
-          //determines if node should be deleted or not
-          //(makes sure it's not on other entities besides those in this sequence) 
-          if( tag_for_deletion( old_iter - mElements, MB ) )
-          {
-            //tag node as deletable
-            unsigned char bit = 0x1;
-            MB->tag_set_data(delete_mark_bit, &(old_iter[i]), 1, &bit);
-          }
-        }
-      }
-      old_iter += mNodesPerElement;
-    }
-  }
-
-  // copy mid face nodes if they existed and we want to keep them
-  if(mid_face_nodes && has_mid_face_nodes())
-  {
-    amount_to_copy = MBCN::mConnectivityMap[this_type][1].num_sub_elements * sizeof(MBEntityHandle);
-    old_end_handle = mElements + mNodesPerElement*mNumAllocated;
-    old_iter = mElements + MBCN::VerticesPerEntity(this_type);
-    if(has_mid_edge_nodes())
-      old_iter += MBCN::mConnectivityMap[this_type][0].num_sub_elements;
-    new_iter = new_array + MBCN::VerticesPerEntity(this_type);
-    if(mid_edge_nodes)
-      new_iter += MBCN::mConnectivityMap[this_type][0].num_sub_elements;
-
-    for(; old_iter < old_end_handle; )
-    {
-      memcpy(new_iter, old_iter, amount_to_copy);
-      old_iter += mNodesPerElement;
-      new_iter += new_nodes_per_element;
-    }
-  }
-  // need to delete them
-  else if(has_mid_face_nodes())
-  {
-    int number_to_delete = MBCN::mConnectivityMap[this_type][1].num_sub_elements;
-    old_end_handle = mElements + mNodesPerElement*mNumAllocated;
-    old_iter = mElements + MBCN::VerticesPerEntity(this_type);
-    if(has_mid_edge_nodes())
-      old_iter+=MBCN::mConnectivityMap[this_type][0].num_sub_elements;
-
-    std::set<MBEntityHandle> nodes_processed; 
-    for(; old_iter < old_end_handle; )
-    {
-      //tag each node-to-delete with parent element entity handle and number 
-      //of elements it is found on
-      for(int i=0; i<number_to_delete; i++)
-      {
-        //see if node has been processed yet
-        if( old_iter[i] && nodes_processed.insert(old_iter[i]).second )
-        {
-          //determines if node should be deleted or not
-          //(makes sure it's not on other entities besides those in this sequence) 
-          if( tag_for_deletion( old_iter - mElements, MB ) )
-          {
-            //tag node as deletable
-            unsigned char bit = 0x1;
-            MB->tag_set_data(delete_mark_bit, &(old_iter[i]), 1, &bit);
-          }
-        }
-      }
-      old_iter += mNodesPerElement;
-    }
-
-  }
-
-  // copy mid volume nodes if they existed and we want to keep them
-  if(mid_volume_nodes && has_mid_volume_nodes())
-  {
-    old_end_handle = mElements + mNodesPerElement*mNumAllocated;
-    old_iter = mElements + MBCN::VerticesPerEntity(this_type);
-    if(has_mid_edge_nodes())
-      old_iter += MBCN::mConnectivityMap[this_type][0].num_sub_elements;
-    if(has_mid_face_nodes())
-      old_iter += MBCN::mConnectivityMap[this_type][1].num_sub_elements;
-    new_iter = new_array + (new_nodes_per_element - 1);
-   
-    for(; old_iter < old_end_handle; )
-    {
-      *new_iter = *old_iter;
-      old_iter += mNodesPerElement;
-      new_iter += new_nodes_per_element;
-    }
-  }
-  // need to delete them
-  else if(has_mid_volume_nodes())
-  {
-    old_end_handle = mElements + mNodesPerElement*mNumAllocated;
-    old_iter = mElements + MBCN::VerticesPerEntity(this_type);
-    if(has_mid_edge_nodes())
-      old_iter += MBCN::mConnectivityMap[this_type][0].num_sub_elements;
-    if(has_mid_face_nodes())
-      old_iter += MBCN::mConnectivityMap[this_type][1].num_sub_elements;
-   
-    for(; old_iter < old_end_handle; )
-    {
-      if( *old_iter != 0 && tag_for_deletion( old_iter - mElements, MB ) )
-      {
-        //tag node as deletable
-        unsigned char bit = 0x1;
-        MB->tag_set_data(delete_mark_bit, &(*old_iter), 1, &bit);
-      }
-      old_iter += mNodesPerElement;
-    }
-
-  }
-
-  // did we actually allocate new space for these?
-  if(mid_edge_nodes)
-    mid_edge_nodes = !has_mid_edge_nodes();
-  if(mid_face_nodes)
-    mid_face_nodes = !has_mid_face_nodes();
-  if(mid_volume_nodes)
-    mid_volume_nodes = !has_mid_volume_nodes();
-  
-  // swap arrays
-  delete [] mElements;
-  mElements = new_array;
-
-  mNodesPerElement = new_nodes_per_element;
-  
-  return MB_SUCCESS;
-}
-
-bool ElementEntitySequence::has_mid_edge_nodes() const
-{
-  return MBCN::HasMidEdgeNodes(get_type(), mNodesPerElement);
-}
-
-bool ElementEntitySequence::has_mid_face_nodes() const
-{
-  return MBCN::HasMidFaceNodes(get_type(), mNodesPerElement);
-}
-
-bool ElementEntitySequence::has_mid_volume_nodes() const
-{
-  return MBCN::HasMidRegionNodes(get_type(), mNodesPerElement);
-}
-
-bool ElementEntitySequence::tag_for_deletion( MBEntityID node_index, 
-                                              MBCore *MB ) 
-{
-  //get type of this sequence
-  MBEntityType this_type = get_type();
-
-  //find 'parent' element (in this sequence)
-  const MBEntityHandle elem_index = node_index / mNodesPerElement;
-  const int            conn_index = node_index % mNodesPerElement;
-  const MBEntityHandle parent_handle = elem_index + mStartEntityHandle;
-
-  //get dimension of 'parent' element
-  int this_dimension = MB->dimension_from_handle( parent_handle );
-
-  //tells us if higher order node is on 
-  int dimension, side_number; 
-  MBCN::HONodeParent( this_type, mNodesPerElement,
-                      conn_index, dimension, side_number );  
-
-  //it MUST be a higher-order node
-  bool delete_node = false;
-
-  assert( dimension != -1 );
-  assert( side_number != -1 );
-
-  //could be a mid-volume/face/edge node on a hex/face/edge respectively
-  //if so...delete it bc/ no one else owns it too
-  std::vector<MBEntityHandle> connectivity;
-  if( dimension == this_dimension && side_number == 0 )
-    delete_node = true;
-  else //the node could also be on a lower order entity of 'tmp_entity' 
-  {
-    //get 'side' of 'parent_handle' that node is on 
-    MBEntityHandle target_entity = 0;
-    MB->side_element( parent_handle, dimension, side_number, target_entity );
-
-    if( target_entity )
-    {
-      AEntityFactory *a_fact = MB->a_entity_factory();
-      MBEntityHandle low_meshset;
-      int dum;
-      low_meshset = CREATE_HANDLE(MBENTITYSET, 0, dum);
-
-      //just get corner nodes of target_entity
-      connectivity.clear();
-      MB->get_connectivity(&( target_entity), 1, connectivity, true  );
-
-      //for each node, get all common adjacencies of nodes in 'parent_handle' 
-      std::vector<MBEntityHandle> adj_list_1, adj_list_2, adj_entities;
-      a_fact->get_adjacencies(connectivity[0], adj_list_1);
-
-      // remove meshsets
-      adj_list_1.erase(std::remove_if(adj_list_1.begin(), adj_list_1.end(), 
-           std::bind2nd(std::greater<MBEntityHandle>(),low_meshset)), adj_list_1.end());
-
-      size_t i; 
-      for( i=1; i<connectivity.size(); i++)
-      {
-        adj_list_2.clear();
-        a_fact->get_adjacencies(connectivity[i], adj_list_2);
-
-        // remove meshsets
-        adj_list_2.erase(std::remove_if(adj_list_2.begin(), adj_list_2.end(), 
-             std::bind2nd(std::greater<MBEntityHandle>(),low_meshset)), adj_list_2.end());
-       
-        //intersect the 2 lists 
-        adj_entities.clear();
-        std::set_intersection(adj_list_1.begin(), adj_list_1.end(), 
-                              adj_list_2.begin(), adj_list_2.end(), 
-                              std::back_inserter< std::vector<MBEntityHandle> >(adj_entities));
-        adj_list_1.clear();
-        adj_list_1 = adj_entities;
-      } 
-
-      assert( adj_entities.size() );  //has to have at least one adjacency 
-
-      //see if node is in other elements, not in this sequence...if so, delete it 
-      for( i=0; i<adj_entities.size(); i++)
-      {
-        if( adj_entities[i] >= get_start_handle() &&
-            adj_entities[i] <= get_end_handle() )
-        {
-          delete_node = false;
-          break;
-        }
-        else 
-          delete_node = true;
-      }             
-    }
-    else //there is no lower order entity that also contains node 
-      delete_node = true;
-  }
-
-  return delete_node;
-
-}
-
-void ElementEntitySequence::get_memory_use( unsigned long& used,
-                                            unsigned long& allocated) const
-{
-  unsigned long per_ent = get_memory_use((MBEntityHandle)0);
-  allocated = sizeof(*this) + mFreeEntities.capacity()/8 + per_ent*number_allocated();
-  used = per_ent * number_entities();
-}
-
-unsigned long ElementEntitySequence::get_memory_use( MBEntityHandle ) const
-{
-  return sizeof(MBEntityHandle) * nodes_per_element();
-}
+unsigned long EntitySequence::get_per_entity_memory_use( MBEntityHandle,
+                                                         MBEntityHandle ) const
+  { return 0; }

Modified: MOAB/trunk/EntitySequence.hpp
===================================================================
--- MOAB/trunk/EntitySequence.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/EntitySequence.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,352 +1,126 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
+#ifndef ENTITY_SEQUENCE_HPP
+#define ENTITY_SEQUENCE_HPP
 
-/*!
- *  \class   MBEntitySequence
- *  \authors Karl Merkley & Corey Ernst
- *  \date    3/27/02
- *  \brief   The MBEntitySequence represents a contiguous range of 
- *           mesh entities of a single type.  MBEntitySequence manages
- *           the internal ids of those entities using a starting id and 
- *           the number of entities in the range.  All MBEntitySequences 
- *           for a given MBEntity type are stored in an stl Map.  When a 
- *           MBHandle references an entity in mdb, mdb finds the Entity-
- *           Sequence associated with the MBHandle from the stl Map.  
- *          
- */ 
-
-#ifndef MB_ENTITY_SEQUENCE_HPP
-#define MB_ENTITY_SEQUENCE_HPP
-
-#ifndef IS_BUILDING_MB
-#error "EntitySequence.hpp isn't supposed to be included into an application"
-#endif
-
-#include "MBForward.hpp"
+#include "MBTypes.h"
 #include "MBInternals.hpp"
-#include "MBCN.hpp"
-#include <set>
-#include <assert.h>
 
-class EntitySequenceManager;
-class MBCore;
+class SequenceData;
 
-class MBEntitySequence
-{
-public:
+class EntitySequence {
+private:
+  MBEntityHandle startHandle, endHandle;
+  SequenceData* sequenceData;
 
-  //! Constructor-- takes a start handle, and number of entities
-  //!  if all_handles_used is true, then there is no room to create
-  //!  new entities
-  MBEntitySequence( EntitySequenceManager* manager, 
-                    MBEntityHandle start_handle, 
-                    MBEntityID num_entities);
+protected:
 
-  //! Destructor
-  virtual ~MBEntitySequence();
+  EntitySequence( MBEntityHandle h )
+    : startHandle(h), endHandle(h) {}
 
-  //! gets starting MBEntityHandle (mStartEntityHandle) 
-  //! note: this doesn't account for unused entity handles
-  MBEntityHandle get_start_handle() const { return mStartEntityHandle; }
- 
-  //! gets the end of allocated entities, does not account for holes 
-  MBEntityHandle get_end_handle() const { return mStartEntityHandle + mNumAllocated - 1; }
-
-  //! get type of elements in EntitySequence
-  MBEntityType get_type() const { return TYPE_FROM_HANDLE(mStartEntityHandle); }
-
-  //! returns number of MBEntityHandles in MBEntitySequence
-  MBEntityID number_entities() const {return mNumEntities;}
-
-  //! returns the number of entities space was allocated for
-  MBEntityID number_allocated() const { return mNumAllocated; }
-  
-  inline bool is_valid_entity(MBEntityHandle entity) const;
-
-  //! get an unused handle
-  virtual MBEntityHandle get_unused_handle() = 0;
-
-  //! free an entity handle
-  virtual void free_handle(MBEntityHandle handle) = 0;
-
-  //! get entities in this range, will not add unused entities
-  virtual void get_entities(MBRange& entities) const = 0;
-
-  //! split this sequence range [begin, end] into
-  //! [begin, split_location-1] and [split_location, end]
-  //! returns the new_entitiy sequence which holds [split_location, end]
-  //! returns success
-  virtual MBErrorCode split(MBEntityHandle split_location, MBEntitySequence*& new_sequence)
+  EntitySequence( EntitySequence& split_from, MBEntityHandle here )
+    : startHandle( here ),
+      endHandle( split_from.endHandle ),
+      sequenceData( split_from.sequenceData )
   {
-    split_location = split_location;
-    new_sequence = NULL;
-    return MB_FAILURE;
+    split_from.endHandle = here - 1;
   }
-  
-  virtual MBEntityID get_next_free_index( MBEntityID prev_free_index ) const = 0;
 
-  virtual void get_memory_use( unsigned long& used,
-                               unsigned long& allocated ) const = 0;
-  virtual unsigned long get_memory_use( MBEntityHandle handle ) const = 0;
+  SequenceData* create_data_subset( MBEntityHandle start_handle,
+                                    MBEntityHandle end_handle,
+                                    int num_sequence_arrays,
+                                    unsigned const* bytes_per_element ) const;
 
-protected:
+  MBErrorCode prepend_entities( MBEntityID count );
+  MBErrorCode append_entities( MBEntityID count );
 
-    //! allocate the handle passed in, effectively creating the entity
-  virtual MBErrorCode allocate_handle(MBEntityHandle handle) = 0;
-  
-  EntitySequenceManager* mSequenceManager;
-
-  //!id to 1st element in EntitySequence
-  MBEntityHandle       mStartEntityHandle;
-
-  //!number of Entities in EntitySequence 
-  MBEntityID           mNumEntities;
-  
-  //!the number of entities that space has been allocated for
-  MBEntityID            mNumAllocated;
-
-  //! the head to the list of unused handles, -1 is no free handles
-  MBEntityID            mFirstFreeIndex;
-
-  //! the last entity deleted, for speed when deleting entities sequentially
-  //! -1 is means no last index
-  MBEntityID            mLastDeletedIndex;
-
-  //! a list of whether entities are free or not
-  std::vector<bool>     mFreeEntities;
-
-};
-
-
-//! entity sequence for vertex type entities
-class VertexEntitySequence : public MBEntitySequence
-{
 public:
-  //! constructor
-  VertexEntitySequence(EntitySequenceManager* seq_manager,
-        MBEntityHandle start_handle, MBEntityID num_entities, bool all_handles_used);
-  //! destructor
-  virtual ~VertexEntitySequence();
 
-  MBEntityHandle get_unused_handle();
-  virtual void free_handle(MBEntityHandle handle);
+  EntitySequence( MBEntityHandle start, MBEntityID count, SequenceData* data )
+    : startHandle(start), endHandle( start + count - 1 ), sequenceData( data )
+    {}
 
-  MBErrorCode get_coordinates(MBEntityHandle entity,
-                               double& x, double& y, double& z) const;
-  
-  MBErrorCode get_coordinates(MBEntityHandle entity,
-                               double* xyz) const;
-  
-  MBErrorCode get_coordinates_ref(MBEntityHandle entity,
-                               const double*& x, const double*& y, const double*& z) const;
-  
-  MBErrorCode set_coordinates(MBEntityHandle entity,
-                               const double& x,
-                               const double& y,
-                               const double& z);
-  
-  MBErrorCode get_coordinate_arrays(double*& x, double*& y, double*& z);
+  virtual ~EntitySequence() {}
 
-  virtual void get_entities(MBRange& entities) const;
-  
-  virtual MBEntityID get_next_free_index( MBEntityID prev_free_index ) const;
+  MBEntityType type() const
+    { return TYPE_FROM_HANDLE(start_handle()); }
 
-  virtual void get_memory_use( unsigned long& used, unsigned long& allocated ) const;
-  virtual unsigned long get_memory_use( MBEntityHandle handle ) const;
-
-protected:
-    //! allocate the handle passed in, effectively creating the entity
-  virtual MBErrorCode allocate_handle(MBEntityHandle handle);
+  MBEntityHandle start_handle() const
+    { return startHandle; }
   
-private:
-
-  // coordinate arrays x,y,z
-  double** mCoords;
+  MBEntityHandle end_handle() const
+    { return endHandle; }
   
-};
-
-
-class ElementEntitySequence: public MBEntitySequence
-{
-public:
-  ElementEntitySequence(EntitySequenceManager* seq_manager, 
-                        MBEntityHandle start_handle, 
-                        MBEntityID num_entities,
-                        int nodes_per_element, bool all_handles_used,
-                        bool allocate_connect = true);
-  virtual ~ElementEntitySequence();
-
-  virtual MBEntityHandle get_unused_handle();
-  virtual void free_handle(MBEntityHandle handle);
-
-  virtual unsigned int nodes_per_element() const { return mNodesPerElement; }
-
-  virtual MBErrorCode get_connectivity(MBEntityHandle entity, 
-                                       std::vector<MBEntityHandle>& connectivity,
-                                       const bool topological_connectivity = false) const;
-  virtual MBErrorCode get_connectivity(MBEntityHandle entity, 
-                                        const MBEntityHandle*& connectivity,
-                                        int &num_vertices,
-                                       const bool topological_connectivity = false,
-                                       std::vector<MBEntityHandle>* storage = 0) const;
-
-  virtual MBErrorCode set_connectivity(MBEntityHandle entity, const MBEntityHandle *conn,
-                                const int num_vertices);
-
-  virtual MBErrorCode get_connectivity_array(MBEntityHandle*& conn_array);
+  SequenceData* data() const
+    { return sequenceData; }
+    
+  void data( SequenceData* ptr )
+    { sequenceData = ptr; }
   
-  virtual void get_entities(MBRange& entities) const;
+  MBEntityID size() const
+    { return endHandle - startHandle + 1; }
+    
+    /**\brief True if SequenceData has no holes and is used only 
+     *        by this EntitySequence */
+  bool using_entire_data() const;
   
-  virtual MBEntityID get_next_free_index( MBEntityID prev_free_index ) const;
+    /**\brief Integer value used in finding appropriate SequenceData
+     *
+     * This value is matched to input values by TypeSequenceManager to
+     * determine if an available, unused portino of a SequenceData can
+     * be used for a specific entity allocation.  For example, it is
+     * used to find a SequenceData with the appropriate number of vertices
+     * per element when allocating elements.  The default value is zero.
+     */
+  virtual int values_per_entity() const;
   
-  virtual MBErrorCode split(MBEntityHandle split_location, MBEntitySequence*& new_sequence);
+    /**\brief Split this sequence into two consecutive sequences
+     *
+     * Split this sequence into two sequences.
+     *\param here New sequences should be [start_handle(),here) & [here,end_handle()]
+     *\return New sequence containing [here,end_handle()]
+     */
+  virtual EntitySequence* split( MBEntityHandle here ) = 0;
 
-  // reallocated the sequence to hold extra/less nodes, pass in what you want, and will return whether it needed
-  // reallocate space for those nodes
-  virtual MBErrorCode convert_realloc(bool& mid_edge_nodes, bool& mid_face_nodes, bool& mid_volume_nodes, 
-      MBCore* MB, MBTag bit_delete_mark );
+    /**\brief Merge this sequence with another
+     *
+     * Combine two adjacent sequences.  Sequence handle blocks must be
+     * consective and sequences must share a common SequenceData.
+     */
+  virtual MBErrorCode merge( EntitySequence& other );
   
-  virtual bool has_mid_edge_nodes() const;
-  virtual bool has_mid_face_nodes() const;
-  virtual bool has_mid_volume_nodes() const;
-
-  virtual void get_memory_use( unsigned long& used, unsigned long& allocated ) const;
-  virtual unsigned long get_memory_use( MBEntityHandle handle ) const;
-
-protected:
+    /**\brief Erase entities in range: (end_handle()-count, end_handle()] */
+  virtual MBErrorCode pop_back( MBEntityID count );
   
-    //! allocate the handle passed in, effectively creating the entity
-  virtual MBErrorCode allocate_handle(MBEntityHandle handle);
+    /**\brief Erase entities in range: [start_handle(), start_handle()+count) */
+  virtual MBErrorCode pop_front( MBEntityID count );
   
-  unsigned short mNodesPerElement;
+    /**\brief Create a new SequenceData that is a copy of a subset of 
+    *         the one referenced by this sequence.
+    *
+    * Create a new SequenceData that is a copy of a subset of the 
+    * SequenceData referenced by this EntitySequence.  Do not make any
+    * changes to this EntitySequence or the current SequenceData.
+    */
+  virtual SequenceData* create_data_subset( MBEntityHandle start_handle,
+                                            MBEntityHandle end_handle ) const = 0;
 
-  MBEntityHandle* mElements;
-
-    /** Check if HO node should be deleted when removed from element.
-     *\param node_index Location of node in connectivity array of this sequence
+    /**\brief Get memory characteristcs that are the same for all entities
+     *
+     * Get charactersitic constant memory use for all entities in sequence.
+     *\param bytes_per_entity The total bytes consumed for each entity in
+     *                        the underlying SequenceData.  It is assumed
+     *                        that the same amount of memory is consumed
+     *                        for unused portions of the SequenceData.
+     *\param size_of_sequence The size of the leaf subclass of this class
      */
-  bool tag_for_deletion( MBEntityID node_index, MBCore *MB );
-
+  virtual void get_const_memory_use( unsigned long& bytes_per_entity,
+                                     unsigned long& size_of_sequence ) const = 0;
+    /**\brief Get portion of memory use that varies per entity
+     *
+     *\return Any per-entity memory use not accounted for in the results
+     *        of get_const_memory_use.
+     */
+  virtual unsigned long get_per_entity_memory_use( MBEntityHandle first,
+                                                   MBEntityHandle last ) const;
 };
 
-
-inline bool MBEntitySequence::is_valid_entity(MBEntityHandle entity) const
-{
-  return mFreeEntities.empty() || !mFreeEntities[entity-mStartEntityHandle];
-}
-
-
-
-inline MBErrorCode VertexEntitySequence::get_coordinates(MBEntityHandle handle, 
-    double& x, double& y, double& z) const
-{
-  unsigned int index = handle - mStartEntityHandle;
-  x = mCoords[0][index];
-  y = mCoords[1][index];
-  z = mCoords[2][index];
-  return MB_SUCCESS;
-}
-
-inline MBErrorCode VertexEntitySequence::get_coordinates(MBEntityHandle handle, 
-    double* xyz) const
-{
-  unsigned int index = handle - mStartEntityHandle;
-  xyz[0] = mCoords[0][index];
-  xyz[1] = mCoords[1][index];
-  xyz[2] = mCoords[2][index];
-  return MB_SUCCESS;
-}
-
-inline MBErrorCode VertexEntitySequence::get_coordinates_ref(MBEntityHandle handle, 
-    const double*& x, const double*& y, const double*& z) const
-{
-  unsigned int index = handle - mStartEntityHandle;
-  x = &mCoords[0][index];
-  y = &mCoords[1][index];
-  z = &mCoords[2][index];
-  return MB_SUCCESS;
-}
-
-inline MBErrorCode VertexEntitySequence::set_coordinates(MBEntityHandle handle, 
-    const double& x, const double& y, const double& z)
-{
-  unsigned int index = handle - mStartEntityHandle;
-  mCoords[0][index] = x;
-  mCoords[1][index] = y;
-  mCoords[2][index] = z;
-  return MB_SUCCESS;
-}
-
-inline MBErrorCode VertexEntitySequence::get_coordinate_arrays(double*& x, double*& y, double*& z)
-{
-  x = mCoords[0];
-  y = mCoords[1];
-  z = mCoords[2]; 
-  return MB_SUCCESS;
-}
-
-inline MBErrorCode ElementEntitySequence::get_connectivity(MBEntityHandle entity,
-                                                           const MBEntityHandle*& conn,
-                                                           int &num_vertices,
-                                                           const bool topological_connectivity,
-                                                           std::vector<MBEntityHandle>* ) const
-{
-  num_vertices = mNodesPerElement;
-  int index = entity - mStartEntityHandle;
-  conn = mElements+index*mNodesPerElement;
-  num_vertices = (topological_connectivity ? MBCN::VerticesPerEntity(TYPE_FROM_HANDLE(entity))
-              : mNodesPerElement);
-  return MB_SUCCESS;
-}
-
-inline MBErrorCode ElementEntitySequence::get_connectivity(MBEntityHandle entity,
-                                                           std::vector<MBEntityHandle> &conn,
-                                                           const bool topological_connectivity) const
-{
-  int numv = (topological_connectivity ? MBCN::VerticesPerEntity(TYPE_FROM_HANDLE(entity))
-              : mNodesPerElement);
-  conn.reserve(numv);
-  int index = entity - mStartEntityHandle;
-  MBErrorCode result = MB_SUCCESS;
-  if (!is_valid_entity(entity)) result = MB_FAILURE;
-  else
-    conn.insert(conn.end(), mElements+index*mNodesPerElement, 
-                mElements+index*mNodesPerElement+numv);
-  return result;
-}
-
-inline MBErrorCode ElementEntitySequence::set_connectivity(MBEntityHandle entity, 
-                                                            const MBEntityHandle *conn,
-                                                            const int num_vertices)
-{
-  if(num_vertices != mNodesPerElement)
-    return MB_FAILURE;
-
-  MBEntityHandle* iter = &mElements[(entity - mStartEntityHandle) * mNodesPerElement];
-  std::copy(conn, (conn+num_vertices), iter);
-  return MB_SUCCESS;
-}
-
-
-inline MBErrorCode ElementEntitySequence::get_connectivity_array(MBEntityHandle*& conn_array)
-{
-  conn_array = mElements;
-  return MB_SUCCESS;
-}
-
-
-
 #endif
-

Deleted: MOAB/trunk/EntitySequenceManager.cpp
===================================================================
--- MOAB/trunk/EntitySequenceManager.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/EntitySequenceManager.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,787 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-
-#ifdef WIN32
-#ifdef _DEBUG
-// turn off warnings that say they debugging identifier has been truncated
-// this warning comes up when using some STL containers
-#pragma warning(disable : 4786)
-#endif
-#endif
-
-
-#include "PolyEntitySequence.hpp"
-#include "EntitySequence.hpp"
-#include "EntitySequenceManager.hpp"
-#include "MBRange.hpp"
-#include "ScdElementSeq.hpp"
-#include "ScdVertexSeq.hpp"
-#include "MeshSetSequence.hpp"
-#include "MBHandleUtils.hpp"
-#include <assert.h>
-#include <algorithm>
-
-
-EntitySequenceManager::EntitySequenceManager( const MBHandleUtils &handle_utils)
-  : handleUtils(handle_utils)
-{
-  memset(mLastAccessed, 0, MBMAXTYPE*sizeof(void*));
-}
-
-EntitySequenceManager::~EntitySequenceManager()
-{
-  // delete the entity sequences
-  delete_all();
-}
-
-void EntitySequenceManager::entity_sequence_created(MBEntitySequence* seq)
-{
-  mSequenceMap[seq->get_type()].insert(
-    std::pair<MBEntityHandle, MBEntitySequence*> (seq->get_start_handle(), seq));
-}
-
-void EntitySequenceManager::entity_sequence_deleted(MBEntitySequence* seq)
-{
-  mSequenceMap[seq->get_type()].erase(seq->get_start_handle());
-  mPartlyFullSequenceMap[seq->get_type()].erase(seq->get_start_handle());  
-}
-
-void EntitySequenceManager::notify_full(MBEntitySequence* seq)
-{
-  mPartlyFullSequenceMap[seq->get_type()].erase(seq->get_start_handle());
-}
-
-void EntitySequenceManager::notify_not_full(MBEntitySequence* seq)
-{
-  mPartlyFullSequenceMap[seq->get_type()].insert(
-    std::pair<MBEntityHandle, MBEntitySequence*>(seq->get_start_handle(), seq));
-}
-
-  //! create a structured sequence of vertices or elements
-MBErrorCode EntitySequenceManager::create_scd_sequence(const int imin, const int jmin, const int kmin,
-                                                        const int imax, const int jmax, const int kmax,
-                                                        const MBEntityType type,
-                                                        const MBEntityID hint_start_id,
-                                                        MBEntityHandle &start_handle,
-                                                        MBEntitySequence *&seq) 
-{
-  int this_dim = MBCN::Dimension(type);
-
-    // use > instead of != in the following assert to also catch cases where imin > imax, etc.
-  assert((this_dim < 3 || kmax > kmin) &&
-         (this_dim < 2 || jmax > jmin) &&
-         (this_dim < 1 || imax > imin));
-
-    // compute # entities; not as easy as it would appear...
-  MBEntityID num_ent;
-  if (MBVERTEX == type)
-    num_ent = (MBEntityID)(imax-imin+1)*(MBEntityID)(jmax-jmin+1)*(MBEntityID)(kmax-kmin+1);
-  else {
-    num_ent = (imax-imin) *
-      (this_dim >= 2 ? (jmax-jmin) : 1) *
-      (this_dim >= 3 ? (kmax-kmin) : 1);
-  }
-  
-    // get a start handle
-  MBErrorCode rval = get_start_handle(hint_start_id, handleUtils.proc_rank(), type, num_ent, start_handle);
-  if (MB_SUCCESS != rval) return rval;
-  
-  if (MBVERTEX == type)
-      // create a new structured vertex sequence
-    seq = new ScdVertexSeq(this, start_handle, 
-                           imin, jmin, kmin, imax, jmax, kmax);
-  else
-    seq = new ScdElementSeq(this, start_handle, 
-                           imin, jmin, kmin, imax, jmax, kmax);
-    
-  if (NULL == seq) return MB_FAILURE;
-
-  else return MB_SUCCESS;
-}
-
-
-  //! create a structured sequence of vertices or elements
-MBErrorCode EntitySequenceManager::create_scd_sequence(const HomCoord &coord_min,
-                                                       const HomCoord &coord_max,
-                                                       const MBEntityType type,
-                                                       const MBEntityID hint_start_id,
-                                                       MBEntityHandle &start_handle,
-                                                       MBEntitySequence *&seq) 
-{
-  return create_scd_sequence(coord_min.i(), coord_min.j(), coord_min.k(), 
-                             coord_max.i(), coord_max.j(), coord_max.k(), 
-                             type, hint_start_id,
-                             start_handle, seq);
-}
-
-MBErrorCode EntitySequenceManager::create_meshset_sequence( MBEntityID num_ent,
-                                                            MBEntityID hint_start_id,
-                                                            int hint_start_proc,
-                                                            const unsigned* flags,
-                                                            MBEntitySequence *&seq )
-{
-  MBEntityHandle start_handle;
-  MBErrorCode rval = get_start_handle(hint_start_id, hint_start_proc, MBENTITYSET, num_ent, start_handle);
-  if (MB_SUCCESS != rval)
-    return rval;
-  
-  seq = new MeshSetSequence( this, start_handle, num_ent, flags );
-  return MB_SUCCESS;
-}
-
-
-/*!
-  creates an entity sequence based on number of entities and type.
-  uses the hint_start as the start id for the entity handles if possible
-  returns the actual start handle and the entity sequence pointer
-*/
-MBErrorCode EntitySequenceManager::create_entity_sequence( MBEntityType type, MBEntityID num_ent, int num_nodes,
-                                                           MBEntityID hint_start, int hint_start_proc, 
-                                                           MBEntityHandle& start_handle, 
-                                                            MBEntitySequence*& seq)
-{
-  MBErrorCode rval = get_start_handle(hint_start, hint_start_proc, type, num_ent, start_handle);
-  if (MB_SUCCESS != rval)
-    return rval;
-  
-  // actually create the sequence
-  return private_create_entity_sequence( start_handle, num_ent, num_nodes, true, seq);
-}
-
-MBErrorCode EntitySequenceManager::get_start_handle( MBEntityID hint_start, 
-                                                     int proc, 
-                                                     MBEntityType type,
-                                                     MBEntityID num_ent,
-                                                     MBEntityHandle& start_hint_handle) 
-{
-  if (hint_start < MB_START_ID)
-    return find_free_handles( proc, type, num_ent, start_hint_handle );
-
-  // Create handles from input parameters
-  int dum = 0;
-  start_hint_handle = CREATE_HANDLE(type, handleUtils.create_id(hint_start, proc), dum);
-  MBEntityHandle end_hint_handle = start_hint_handle + num_ent - 1;
-  MBEntityHandle last_handle = CREATE_HANDLE( type, handleUtils.last_id(proc), dum );
-  
-  // Check if the handle type can accomodate the requested number of handles
-  if (end_hint_handle > last_handle)
-    return find_free_handles( proc, type, num_ent, start_hint_handle );
-
-  // Find the first entity sequence with a handle greater than requested handle.
-  SeqMap::iterator iter = mSequenceMap[type].upper_bound(start_hint_handle);
-  
-  // Check that the requested handle range is before the beginning
-  // of the next sequence.
-  if (iter != mSequenceMap[type].end() && iter->first <= end_hint_handle)
-    return find_free_handles( proc, type, num_ent, start_hint_handle );
-  
-  // If there is no previous sequence, then done.
-  if (iter == mSequenceMap[type].begin())
-    return MB_SUCCESS;
-  // Otherwise make sure the start of the requested range is
-  // after the previous sequence.
-  --iter;
-  if (iter->second->get_end_handle() < start_hint_handle)
-    return MB_SUCCESS;
-  else
-    return find_free_handles( proc, type, num_ent, start_hint_handle );
-}
-
-MBErrorCode EntitySequenceManager::find_free_handles( int proc,
-                                                      MBEntityType type,
-                                                      MBEntityID num_ent,
-                                                      MBEntityHandle& handle_out )
-{
-  int dum = 0;
-
-    // get first and largest possible handle for specified proc and type
-  MBEntityHandle last_handle = CREATE_HANDLE( type, handleUtils.last_id(proc), dum );
-  handle_out = CREATE_HANDLE( type, handleUtils.first_id(proc), dum );
-
-    // check that handle space is large enough to accomodate requested
-    // number of entities
-  if (last_handle - handle_out + 1 < (MBEntityHandle)num_ent)
-    return MB_MEMORY_ALLOCATION_FAILED;
-
-    // get the first entity sequence for the NEXT rank (processor id)
-  SeqMap::iterator iter = mSequenceMap[type].upper_bound( last_handle );
-  
-    // If map is empty or all sequences have larger processor ID
-  if (iter == mSequenceMap[type].begin()) 
-    return MB_SUCCESS;
-  
-    // decrement to get first sequence with same (or smaller) 
-    // rank (processor ID)
-  --iter;
-  
-    // If previous sequence is for previous processor, then there
-    // are currently no handles allocated for this type and proc.
-    // Return the first handle.
-  if (handleUtils.rank_from_handle(iter->second->get_end_handle()) < (unsigned)proc)
-    return MB_SUCCESS;
-
-    // Otherwise try the handle after those currently allocated
-    // for this type and processor.
-  handle_out = iter->second->get_end_handle() + 1;
-    // Check if enough IDs at end of range
-  if (last_handle - handle_out + 1 >= (MBEntityHandle)num_ent)
-    return MB_SUCCESS;
-
-    // Not enough available handles at the end of the range of handles
-    // for the specified entity type and processor.  Scan the entire
-    // range of handles looking for a large enough hole before we give
-    // up entirely.
-  for (;;) {
-    MBEntityHandle last_start = iter->second->get_start_handle();
-      // If this is the first sequence for this processor
-    if (iter == mSequenceMap[type].begin() ||
-        handleUtils.rank_from_handle((--iter)->second->get_end_handle()) < (unsigned)proc) {
-      MBEntityHandle first_handle = CREATE_HANDLE( type, handleUtils.first_id(proc), dum );
-      if (first_handle - last_start > (MBEntityHandle)num_ent) {
-        handle_out = last_start - num_ent;
-        return MB_SUCCESS;
-      }
-      break;
-    }
-
-      // check if we fit in the current range
-    if (last_start - iter->second->get_end_handle() - 1 > (MBEntityHandle)num_ent) {
-      handle_out = iter->second->get_end_handle() + 1;
-      return MB_SUCCESS;
-    }
-  }
-  return MB_MEMORY_ALLOCATION_FAILED;
-}
-
-MBErrorCode EntitySequenceManager::private_create_entity_sequence(MBEntityHandle start,
-                                                           MBEntityID num_ent, int num_nodes,
-                                                           bool full,
-                                                           MBEntitySequence *& seq)
-{
-  MBEntityType type = TYPE_FROM_HANDLE(start);
-  if(type == MBVERTEX)
-    seq = new VertexEntitySequence(this, start, num_ent, full);
-  else if(type == MBPOLYGON || type == MBPOLYHEDRON)
-    seq = new PolyEntitySequence(this, start, num_ent, num_nodes, full);
-  else
-    seq = new ElementEntitySequence(this, start, num_ent, num_nodes, full);
-  
-  return MB_SUCCESS;
-}
-
-
-MBErrorCode EntitySequenceManager::create_vertex( const unsigned processor_id,
-                                                  const double coords[3], 
-                                                  MBEntityHandle& handle )
-{
-  VertexEntitySequence* seq = 0;
-
-  // see if there is an existing sequence that can take this new vertex
-  SeqMap& seq_map = mPartlyFullSequenceMap[MBVERTEX];
-  for (SeqMap::iterator i = seq_map.begin(); i != seq_map.end(); ++i) {
-    if (handleUtils.rank_from_handle(i->second->get_start_handle()) == processor_id) {
-      seq = static_cast<VertexEntitySequence*>(i->second);
-      break;
-    }
-  }
-
-  if (!seq) {
-    MBErrorCode rval = get_start_handle( MB_START_ID, processor_id, MBVERTEX, 4096, handle );
-    if (MB_SUCCESS != rval)
-      return rval;
-    seq = new VertexEntitySequence( this, handle, 4096, false );
-  }
-
-  handle = seq->get_unused_handle();
-  seq->set_coordinates(handle, coords[0], coords[1], coords[2]);
-  
-  return MB_SUCCESS;
-}
-
-
-MBErrorCode EntitySequenceManager::create_element( MBEntityType type, 
-                                                   const unsigned processor_id,
-                                                   const MBEntityHandle *conn, 
-                                                   const unsigned num_vertices,
-                                                   MBEntityHandle& handle )
-{
-  MBErrorCode rval;
-  const bool poly = (MBPOLYGON == type || MBPOLYHEDRON == type);
-  const unsigned connlen = poly ? 0 : num_vertices;
-  
-  ElementEntitySequence* seq = 0;
-  SeqMap& seq_map = mPartlyFullSequenceMap[type];
-  for (SeqMap::iterator i = seq_map.begin(); i != seq_map.end(); ++i) {
-    ElementEntitySequence* tseq = reinterpret_cast<ElementEntitySequence*>(i->second);
-    if (tseq->nodes_per_element() == connlen && 
-        handleUtils.rank_from_handle( tseq->get_start_handle() ) == processor_id) {
-      seq = tseq;
-      break;
-    }
-  }
-  
-  if (poly) {
-    PolyEntitySequence* pseq = reinterpret_cast<PolyEntitySequence*>(seq);
-    if (!pseq) {
-      rval = get_start_handle( MB_START_ID, processor_id, type, 4096, handle );
-      if (MB_SUCCESS != rval)
-        return rval;
-      pseq = new PolyEntitySequence(this, handle, 4096, 0, false);
-    }
-    handle = pseq->get_unused_handle();
-    return pseq->add_entity(conn, num_vertices, handle);
-  }
-  
-  if (!seq) {
-    rval = get_start_handle( MB_START_ID, processor_id, type, 4096, handle );
-    if (MB_SUCCESS != rval)
-      return rval;
-    seq = new ElementEntitySequence(this, handle, 4096, num_vertices, false);
-  } 
-
-  handle = seq->get_unused_handle();
-  return seq->set_connectivity(handle, conn, num_vertices);
-}
-
-MBErrorCode EntitySequenceManager::create_mesh_set( unsigned proc_id,
-                                                    unsigned flags,
-                                                    MBEntityHandle& h )
-{
-  MeshSetSequence* seq = 0;
-
-  // see if there is an existing sequence that can take this new vertex
-  SeqMap& seq_map = mPartlyFullSequenceMap[MBENTITYSET];
-  for (SeqMap::iterator i = seq_map.begin(); i != seq_map.end(); ++i) {
-    if (handleUtils.rank_from_handle(i->second->get_start_handle()) == proc_id) {
-      seq = static_cast<MeshSetSequence*>(i->second);
-      break;
-    }
-  }
-
-  if (!seq) {
-    MBErrorCode rval = get_start_handle( MB_START_ID, proc_id, MBENTITYSET, 4096, h );
-    if (MB_SUCCESS != rval)
-      return rval;
-    seq = new MeshSetSequence( this, h, 4096, (const unsigned*)0 );
-  }
-
-  h = seq->add_meshset( flags );
-  return MB_SUCCESS;
-}
-
-//MBErrorCode EntitySequenceManager::allocate_handle( MBEntityHandle handle, unsigned flags ) 
-//{
-//  return MB_FAILURE;
-//}
-
-MBErrorCode EntitySequenceManager::allocate_mesh_set( MBEntityHandle handle, unsigned flags )
-{
-  std::map<MBEntityHandle, MBEntitySequence*>::iterator seq_itr;
-  seq_itr = mSequenceMap[MBENTITYSET].lower_bound( handle );
-  if (seq_itr != mSequenceMap[MBENTITYSET].begin())
-    --seq_itr;
-  
-  for (; seq_itr != mSequenceMap[MBENTITYSET].end() && 
-         seq_itr->second->get_end_handle() < handle;
-         ++seq_itr );
-  
-  MeshSetSequence* seq;
-  if (seq_itr != mSequenceMap[MBENTITYSET].end() && 
-      seq_itr->second->get_start_handle() <= handle)
-    seq = reinterpret_cast<MeshSetSequence*>(seq_itr->second);
-  else {
-    MBEntityID new_seq_size = 4096;
-    if (seq_itr != mSequenceMap[MBENTITYSET].end()) {
-      MBEntityID diff = seq_itr->second->get_start_handle() - handle;
-      if (diff < new_seq_size)
-        new_seq_size = diff;
-    }
-    seq = new MeshSetSequence( this, handle, new_seq_size, (const unsigned*)0 );
-  }
-  
-  return seq->add_meshset( handle, flags );
-}
-
-
-MBErrorCode EntitySequenceManager::get_entities(MBEntityType type, MBRange &entities) const
-{
-  
-  //index into the static sequence map to get the sequences according to type only 
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator beg_seq, end_seq;
-  beg_seq = mSequenceMap[type].begin();
-  end_seq = mSequenceMap[type].end();
-
-  //for each sequence, get all the entity handles it contains
-
-  for (; beg_seq != end_seq; ++beg_seq)
-  {
-    const MBEntitySequence* tmp_seq = beg_seq->second;
-    tmp_seq->get_entities(entities);
-  }
-
-  return MB_SUCCESS;
-}
-
-MBErrorCode EntitySequenceManager::get_number_entities(MBEntityType type, MBEntityID& num_entities) const 
-{
-  num_entities = 0;
-  //index into the static sequence map to get the sequences according to type only 
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator beg_seq, end_seq;
-  beg_seq = mSequenceMap[type].begin();
-  end_seq = mSequenceMap[type].end();
-
-  //for each sequence, get all the entity handles it contains
-
-  for (; beg_seq != end_seq; ++beg_seq)
-  {
-    num_entities += beg_seq->second->number_entities();
-  }
-  return MB_SUCCESS;
-}
-
-
-void EntitySequenceManager::delete_all()
-{
-  for(MBEntityType i = MBVERTEX; i<MBMAXTYPE; i++)
-  {
-    while(!mSequenceMap[i].empty())
-      delete mSequenceMap[i].begin()->second;
-    mSequenceMap[i].clear();
-  } 
-}
-
-
-MBErrorCode EntitySequenceManager::find( MBEntityHandle entity_handle,
-                                      MBEntitySequence*& sequence ) const
-{
-  MBEntityType ent_type = TYPE_FROM_HANDLE(entity_handle);
-
-  // check to see if the sequence is cached
-  sequence = mLastAccessed[ent_type];
-  if (sequence && sequence->get_start_handle() <= entity_handle &&
-     sequence->get_end_handle() >= entity_handle)
-  {
-    return MB_SUCCESS;
-  }
-  
-  sequence = 0;
-
-  if ( !mSequenceMap[ent_type].empty() )
-  {
-    if (ID_FROM_HANDLE(entity_handle) < MB_START_ID) 
-      return MB_INDEX_OUT_OF_RANGE;
-
-    // create an iterator to look for which sequence entity handle will be found in
-    // using lower bounds function of map
-    std::map<MBEntityHandle, MBEntitySequence*>::const_iterator iter =
-      mSequenceMap[ent_type].upper_bound(entity_handle);
-    if (iter == mSequenceMap[ent_type].begin())
-      return MB_ENTITY_NOT_FOUND;
-    --iter;
-
-    // ensure that the entity is bounded by this sequence.  
-    // upper_bound will indicate that this sequence can hold this entity
-    // but doesn't guarantee that it does!
-    MBEntitySequence* seq = iter->second; 
-    if ( (entity_handle >= seq->get_start_handle()) &&
-         (entity_handle <= seq->get_end_handle()))
-    {
-      sequence = seq;
-      mLastAccessed[ent_type] = seq;
-      return MB_SUCCESS; 
-    }
-    else
-      return MB_ENTITY_NOT_FOUND;
-  }
-
-  return MB_ENTITY_NOT_FOUND;
-}
-
-
-
-MBErrorCode EntitySequenceManager::delete_entity( MBEntityHandle entity )
-{
-  MBEntitySequence* seq;
-  find(entity, seq);
-  if(seq != NULL)
-  {
-    seq->free_handle(entity);
-    // leave the sequences around for a while if it is empty
-    
-    return MB_SUCCESS;
-  }
-  return MB_FAILURE;
-}
-
-void EntitySequenceManager::get_memory_use( unsigned long& entity,
-                                            unsigned long& total) const
-{
-  total = entity = 0;
-  unsigned long used, allocated;
-  for (MBEntityType type = MBVERTEX; type < MBMAXTYPE; ++type) {
-    get_memory_use( type, used, allocated );
-    total += allocated;
-    entity += used;
-  }
-}
-
-void EntitySequenceManager::get_memory_use( MBEntityType type,
-                                            unsigned long& entity,
-                                            unsigned long& total ) const
-{
-  entity = total = 0;
-  unsigned long used, allocated;
-  for (SeqMap::const_iterator i = mSequenceMap[type].begin(); i != mSequenceMap[type].end(); ++i) {
-    i->second->get_memory_use( used, allocated );
-    entity += used;
-    total += allocated;
-  }
-}
-  
-
-MBErrorCode EntitySequenceManager::get_memory_use( 
-                                     const MBRange& entities,
-                                     unsigned long& min_per_entity,
-                                     unsigned long& amortized ) const
-{
-  min_per_entity = 0;
-  amortized =0;
-  if (entities.empty())
-    return MB_SUCCESS;
-  
-  MBRange::iterator e, i = entities.begin();
-  for (unsigned type = TYPE_FROM_HANDLE( *i ); type < MBMAXTYPE; ++type) {
-    if (i == entities.end())
-      break;
-    
-    const SeqMap& map = mSequenceMap[type];
-    if (map.empty())
-      continue;
-      
-    for (SeqMap::const_iterator j = map.begin(); j != map.end(); ++j)
-    {
-      MBEntitySequence* seq = j->second;
-      if (seq->get_end_handle() < *i)
-        continue;
-      
-      if (*i < seq->get_start_handle())
-        return MB_ENTITY_NOT_FOUND;
-      
-      e = entities.upper_bound( i, entities.end(), seq->get_end_handle() );
-      MBEntityID count = 0;
-      for (; i != e; ++i) {
-        min_per_entity += seq->get_memory_use( *i );
-        ++count;
-      }
-      
-      unsigned long used, allocated;
-      seq->get_memory_use( used, allocated );
-      amortized += (unsigned long)( (double)count * allocated / seq->number_entities() );
-    }
-  }
-        
-  return MB_SUCCESS;
-}
-
-
-#ifdef TEST
-
-#include <iostream>
-
-#define check( A ) \
-  do {  \
-    if ((A) == MB_SUCCESS) break; \
-    std::cerr << "Error at " << __FILE__ << ":" << __LINE__ << std::endl; \
-    return 1; \
-  } while(false)
-  
-#define ensure( A ) \
-  do { \
-    if ((A)) break; \
-    std::cerr << "Failed test at " << __FILE__ << ":" << __LINE__ << std::endl; \
-    std::cerr << "  " #A << std::endl; \
-    return 1; \
-  } while(false)
-    
-
-int main()
-{
-  EntitySequenceManager manager( MBHandleUtils(0,1) );
-  MBEntitySequence* seq;
-  
-  // create some sequences
-  const unsigned NV[] = { 100, 5, 1000 };
-  MBEntityHandle vh[3];
-  MBEntitySequence* vs[3];
-  MBEntityHandle th;
-  MBEntitySequence *ts;
-  const MBEntityID TRI_START_ID = 3;
-  const unsigned NT = 640;
-  check(manager.create_entity_sequence( MBVERTEX, NV[0], 0, 35, 0, vh[0], vs[0] ));
-  check(manager.create_entity_sequence( MBVERTEX, NV[1], 0, 8, 0, vh[1], vs[1] ));
-  check(manager.create_entity_sequence( MBVERTEX, NV[2], 0, 1, 0, vh[2], vs[2] ));
-  check(manager.create_entity_sequence( MBTRI, NT, 3, TRI_START_ID, 0, th, ts ));
-  
-  // check that returned entity sequences are valid
-  ensure( ID_FROM_HANDLE(vh[0]) > 0 && TYPE_FROM_HANDLE(vh[0]) == MBVERTEX );
-  ensure( ID_FROM_HANDLE(vh[0]) > 0 && TYPE_FROM_HANDLE(vh[1]) == MBVERTEX );
-  ensure( ID_FROM_HANDLE(vh[0]) > 0 && TYPE_FROM_HANDLE(vh[2]) == MBVERTEX );
-  ensure( ID_FROM_HANDLE( th  ) > 0 && TYPE_FROM_HANDLE( th  ) == MBTRI    );
-  ensure( ID_FROM_HANDLE(th) == TRI_START_ID );
-  ensure( vs[0] );
-  ensure( vs[1] );  
-  ensure( vs[2] );  
-  ensure( ts );  
-  ensure( vs[0]->get_type() == MBVERTEX );
-  ensure( vs[1]->get_type() == MBVERTEX );
-  ensure( vs[2]->get_type() == MBVERTEX );
-  ensure( ts   ->get_type() == MBTRI    );
-  ensure( vs[0]->get_start_handle() == vh[0] );
-  ensure( vs[1]->get_start_handle() == vh[1] );
-  ensure( vs[2]->get_start_handle() == vh[2] );
-  ensure( ts   ->get_start_handle() == th    );
-  ensure( vs[0]->get_end_handle() - vs[0]->get_start_handle() == NV[0] - 1 );
-  ensure( vs[1]->get_end_handle() - vs[1]->get_start_handle() == NV[1] - 1 );
-  ensure( vs[2]->get_end_handle() - vs[2]->get_start_handle() == NV[2] - 1 );
-  ensure( ts   ->get_end_handle() - ts   ->get_start_handle() == NT    - 1 );
-  
-  // construct ranges
-  MBRange vertices;
-  vertices.insert( vs[0]->get_start_handle(), vs[0]->get_end_handle() );
-  vertices.insert( vs[1]->get_start_handle(), vs[1]->get_end_handle() );
-  vertices.insert( vs[2]->get_start_handle(), vs[2]->get_end_handle() );
-  MBRange tris( ts->get_start_handle(), ts->get_end_handle() );
-  
-  // check find sequence given first
-  check( manager.find( vs[0]->get_start_handle(), seq ) );
-  ensure( seq == vs[0] );
-  check( manager.find( vs[1]->get_start_handle(), seq ) );
-  ensure( seq == vs[1] );
-  check( manager.find( vs[2]->get_start_handle(), seq ) );
-  ensure( seq == vs[2] );
-  check( manager.find( ts   ->get_start_handle(), seq ) );
-  ensure( seq == ts    );
-  
-  // check find sequence given last
-  check( manager.find( vs[0]->get_end_handle(), seq ) );
-  ensure( seq == vs[0] );
-  check( manager.find( vs[1]->get_end_handle(), seq ) );
-  ensure( seq == vs[1] );
-  check( manager.find( vs[2]->get_end_handle(), seq ) );
-  ensure( seq == vs[2] );
-  check( manager.find( ts   ->get_end_handle(), seq ) );
-  ensure( seq == ts    );
-  
-  // check find of invalid handle
-  MBEntityHandle badhandle = (MBEntityHandle)(ts->get_end_handle() + 1);
-  ensure( manager.find( badhandle, seq ) == MB_ENTITY_NOT_FOUND );
-  
-  // check find of invalid type
-  int junk;
-  badhandle = CREATE_HANDLE( MBTET, 1, junk );
-  ensure( manager.find( badhandle, seq ) == MB_ENTITY_NOT_FOUND );
-  
-  // check get_entities
-  MBRange chkverts, chktris, chkhexes;
-  check( manager.get_entities( MBVERTEX, chkverts ) );
-  check( manager.get_entities( MBTRI   , chktris  ) );
-  check( manager.get_entities( MBHEX   , chkhexes ) );
-  ensure( chkverts.size() == vertices.size() && 
-          std::equal(chkverts.begin(), chkverts.end(), vertices.begin() ) );
-  ensure( chktris.size() == tris.size() && 
-          std::equal(chktris.begin(), chktris.end(), tris.begin() ) );
-  ensure( chkhexes.empty()     );
-  
-  // check get_number_entities
-  MBEntityID num_vtx, num_tri, num_hex;
-  check( manager.get_number_entities( MBVERTEX, num_vtx ) );
-  check( manager.get_number_entities( MBTRI,    num_tri ) );
-  check( manager.get_number_entities( MBHEX,    num_hex ) );
-  ensure( num_vtx >= 0 &&  (unsigned)num_vtx == chkverts.size() );
-  ensure( num_tri >= 0 &&  (unsigned)num_tri == chktris .size() );
-  ensure( num_hex == 0 );
-  
-  // check allocation of meshsets with caller-specified handles
-  EntitySequenceManager setman( MBHandleUtils(0,1) );
-  MBEntityHandle start_handle = CREATE_HANDLE( MBENTITYSET, 1, junk );
-  // ID space: {}
-  check( setman.allocate_mesh_set( start_handle + 2, 0 ) );
-  // get current sequence
-  MBEntitySequence* b_seq_ptr;
-  check( setman.find( start_handle + 2, b_seq_ptr ) );
-  MeshSetSequence* seq_ptr = dynamic_cast<MeshSetSequence*>(b_seq_ptr);
-  check( seq_ptr->is_valid() );
-  ensure( seq_ptr->number_entities() == 1 );
-  // ID space: {3}
-  check( setman.allocate_mesh_set( start_handle + 4, 0 ) );
-  check( seq_ptr->is_valid() );
-  ensure( seq_ptr->number_entities() == 2 );
-  // ID space: {3,5}
-  check( setman.allocate_mesh_set( start_handle + 5, 0 ) );
-  check( seq_ptr->is_valid() );
-  ensure( seq_ptr->number_entities() == 3 );
-  // ID space: {3,5,6}
-  check( setman.allocate_mesh_set( start_handle + 3, 0 ) );
-  check( seq_ptr->is_valid() );
-  ensure( seq_ptr->number_entities() == 4 );
-  // ID space: {3-6}
-  // create new sequence after current one, with a single ID whole between them
-  MBEntityHandle offset_handle = seq_ptr->get_end_handle();
-  check( setman.allocate_mesh_set( offset_handle + 2, 0 ) );
-  // now fill in that whole (with a 1-entity sequence?)
-  check( setman.allocate_mesh_set( offset_handle + 1, 0 ) );
-  // make sure all sequences are valid
-  MBEntityHandle prev_handle = 0;
-  const std::map<MBEntityHandle, MBEntitySequence*>& map = *setman.entity_map( MBENTITYSET );
-  for (std::map<MBEntityHandle, MBEntitySequence*>::const_iterator mi = map.begin();
-       mi != map.end(); ++mi) {
-    seq_ptr = dynamic_cast<MeshSetSequence*>(mi->second);
-    check( seq_ptr->is_valid());
-    ensure( seq_ptr->get_start_handle() == mi->first );
-    ensure( seq_ptr->get_start_handle() > prev_handle );
-    ensure( seq_ptr->number_entities() <= seq_ptr->number_allocated() );
-    ensure( seq_ptr->number_entities() > 0 );
-    prev_handle = mi->second->get_end_handle();
-  }
-  // make sure attempt to re-allocate existing set fails
-  ensure( MB_ALREADY_ALLOCATED == setman.allocate_mesh_set( start_handle + 2, 0 ) );
-  ensure( MB_ALREADY_ALLOCATED == setman.allocate_mesh_set( start_handle + 3, 0 ) );
-  ensure( MB_ALREADY_ALLOCATED == setman.allocate_mesh_set( start_handle + 4, 0 ) );
-  ensure( MB_ALREADY_ALLOCATED == setman.allocate_mesh_set( start_handle + 5, 0 ) );
-  // some consistency checks
-  MBEntityID count;
-  check( setman.get_number_entities( MBENTITYSET, count ) );
-  ensure( 6 == count );
-  MBRange setlist;
-  check( setman.get_entities( MBENTITYSET, setlist ) );
-  MBRange setlist2;
-  setlist2.insert( start_handle + 2, start_handle + 5 );
-  setlist2.insert( offset_handle + 1, offset_handle + 2 );
-  if (setlist != setlist2) {
-    std::cout << "Expected:\n";
-    setlist2.print( std::cout );
-    std::cout << "Actual:\n";
-    setlist.print( std::cout );
-  }
-  ensure( setlist2 == setlist );
-  
-  
-  return 0;
-}
-
-#endif
-
-

Deleted: MOAB/trunk/EntitySequenceManager.hpp
===================================================================
--- MOAB/trunk/EntitySequenceManager.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/EntitySequenceManager.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,195 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-/*!
- *  \class   MBEntitySequenceManager
- *  \authors Karl Merkley & Corey Ernst
- *  \date    3/27/02
- *  \brief   The MBEntitySequence represents a contiguous range of 
- *           mesh entities of a single type.  MBEntitySequence manages
- *           the internal ids of those entities using a starting id and 
- *           the number of entities in the range.  All MBEntitySequences 
- *           for a given MBEntity type are stored in an stl Map.  When a 
- *           MBHandle references an entity in mdb, mdb finds the Entity-
- *           Sequence associated with the MBHandle from the stl Map.  
- *           All enquires for any data associated with the MBEntity are
- *           directed through the MBEntitySequence object to which the
- *           MBEntity's MBHandle refers.  A MBEntitySequence knows 
- *           about all DenseTags defined for that MBEntitySequence, and
- *           contains one TagRange for each of these DenseTags.  The 
- *           MBEntitySequence can query for and return any of the DenseTag
- *           data from the TagRanges it manages. 
- *          
- */ 
-
-#ifndef ENTITY_SEQUENCE_MANAGER_HPP
-#define ENTITY_SEQUENCE_MANAGER_HPP
-
-#ifndef IS_BUILDING_MB
-#error "EntitySequenceManager.hpp isn't supposed to be included into an application"
-#endif
-
-#include "MBForward.hpp"
-#include "MBHandleUtils.hpp"
-#include <map>
-
-class MBEntitySequence;
-class HomCoord;
-
-//! class for managing entity sequences
-class EntitySequenceManager
-{
-public:
-
-  //! constructor
-  EntitySequenceManager( const MBHandleUtils &handle_utils);
-
-  //! destructor
-  ~EntitySequenceManager();
-
-  MBErrorCode create_scd_sequence(const int imin, const int jmin, const int kmin,
-                                   const int imax, const int jmax, const int kmax,
-                                   const MBEntityType type,
-                                   const MBEntityID hint_start_id,
-                                   MBEntityHandle &start,
-                                   MBEntitySequence *&seq);
-  
-  MBErrorCode create_scd_sequence(const HomCoord &coord_min,
-                                   const HomCoord &coord_max,
-                                   const MBEntityType type,
-                                   const MBEntityID hint_start_id,
-                                   MBEntityHandle &start,
-                                   MBEntitySequence *&seq);
-  
-  //! creates an entity sequence that will fit number of entities.  uses hint_start
-  //! for the start handle if possible
-  //! returns the start handle and a pointer to the entity sequence.
-  MBErrorCode create_entity_sequence(MBEntityType type, MBEntityID num_ent, int num_nodes_per, 
-                                     MBEntityID hint_start_id, int hint_start_proc,
-                                     MBEntityHandle& start, MBEntitySequence *&);
-
-  //! Create entity sequence for mesh sets.  If flags is NULL, 
-  //! sequence will be created with all handles unused.  If
-  //! flags is not NULL, it should be an array of num_ent values.
-  MBErrorCode create_meshset_sequence( MBEntityID num_ent,
-                                       MBEntityID hint_start_id,
-                                       int hint_start_proc,
-                                       const unsigned* flags,
-                                       MBEntitySequence *&seq );
-                                       
-  //! finds the specific MBEntitySequence in which MBEntityHandle resides
-  MBErrorCode find( MBEntityHandle     entity_handle,
-                     MBEntitySequence*& sequence ) const;
-
-  //! get entities from the entity sequences according to type 
-  MBErrorCode get_entities(MBEntityType,
-                            MBRange &entities) const;
-  
-  //! get entities from the entity sequences according to type 
-  MBErrorCode get_number_entities(MBEntityType, MBEntityID& entities) const;
-
-  //! deletes an entity from the database
-  MBErrorCode delete_entity( MBEntityHandle entity );
-
-  //! creates a vertex in the database
-  MBErrorCode create_vertex( const unsigned processor_id,
-                             const double coords[3], 
-                             MBEntityHandle& vertex );
-
-  //! creates a vertex in the database with specified id
-  MBErrorCode create_vertex( const unsigned processor_id,
-                             const unsigned entity_id,
-                             const double coords[3], 
-                             MBEntityHandle& vertex ) {return MB_FAILURE;}
-
-  //! creates an element in the database
-  MBErrorCode create_element( MBEntityType type, 
-                              const unsigned processor_id,
-                              const MBEntityHandle *conn_array,
-                              const unsigned num_vertices,
-                              MBEntityHandle& element);
-
-  //! creates an element in the database with specified id
-  MBErrorCode create_element( MBEntityType type, 
-                              const unsigned processor_id,
-                              const unsigned entity_id,
-                              const MBEntityHandle *conn_array,
-                              const unsigned num_vertices,
-                              MBEntityHandle& element) {return MB_FAILURE;}
-
-  MBErrorCode create_mesh_set( unsigned proc_id,
-                               unsigned flags,
-                               MBEntityHandle& h );
-                               
-  MBErrorCode allocate_mesh_set( MBEntityHandle handle, unsigned flags );
-
-  //! return a const reference to the map of sequences
-  const std::map<MBEntityHandle, MBEntitySequence*>*
-    entity_map( MBEntityType type ) const { return &(mSequenceMap[type]); }
-
-  void entity_sequence_created(MBEntitySequence* seq);
-  void entity_sequence_deleted(MBEntitySequence* seq);
-  void notify_full(MBEntitySequence* seq);
-  void notify_not_full(MBEntitySequence* seq);
-  
-  void get_memory_use( unsigned long& total_entity_storage,
-                       unsigned long& total_storage ) const;
-  void get_memory_use( MBEntityType type,
-                       unsigned long& total_entity_storage,
-                       unsigned long& total_storage ) const;
-  MBErrorCode get_memory_use( const MBRange& entities,
-                              unsigned long& total_entity_storage,
-                              unsigned long& total_amortized_storage ) const;
-
-private:
-  
-  //! creates an entity sequence with a start handle and number of entities
-  MBErrorCode private_create_entity_sequence(MBEntityHandle start,
-                                      MBEntityID num_ent, int num_nodes,
-                                      bool full, MBEntitySequence *&);
-
-  void delete_all();
-
-  //! last instance of entity sequence that was accessed
-  //! each entity type keeps its own cache
-  mutable MBEntitySequence* mLastAccessed[MBMAXTYPE];
-  
-  //! map of all the EntitySequences that are created 
-  //! each entity type has its own map, and each processor
-  //! ID has it's own map.
-  typedef std::map< MBEntityHandle, MBEntitySequence* > SeqMap;
-  SeqMap mSequenceMap[MBMAXTYPE];
-  SeqMap mPartlyFullSequenceMap[MBMAXTYPE];
-
-    //! get a valid start handle for this type and a hinted-at start id
-  MBErrorCode get_start_handle( MBEntityID hint_start, 
-                                int hint_proc, 
-                                MBEntityType type, 
-                                MBEntityID num_ent,
-                                MBEntityHandle& start_handle );
-                                
-    //! helper function for get_start_handle, if the requested entity ID
-    //! is not available, find any available range of handles
-  MBErrorCode find_free_handles( int proc, 
-                                 MBEntityType type, 
-                                 MBEntityID num_ent,
-                                 MBEntityHandle& start_handle );
-  
-  const MBHandleUtils handleUtils;
-};
-
-#endif
-
-

Modified: MOAB/trunk/HigherOrderFactory.cpp
===================================================================
--- MOAB/trunk/HigherOrderFactory.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/HigherOrderFactory.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -23,8 +23,9 @@
 #endif
 
 #include "HigherOrderFactory.hpp"
-#include "EntitySequenceManager.hpp"
-#include "EntitySequence.hpp"
+#include "SequenceManager.hpp"
+#include "UnstructuredElemSeq.hpp"
+#include "VertexSequence.hpp"
 #include "AEntityFactory.hpp"
 #include "MBCore.hpp"
 #include "MBCN.hpp"
@@ -65,8 +66,10 @@
   //mMapInitialized = true;
 }
 
-MBErrorCode HigherOrderFactory::convert(const MBEntityHandle meshset, const bool mid_edge_nodes, 
-                                         const bool mid_face_nodes, const bool mid_volume_nodes)
+MBErrorCode HigherOrderFactory::convert( const MBEntityHandle meshset, 
+                                         const bool mid_edge_nodes, 
+                                         const bool mid_face_nodes, 
+                                         const bool mid_volume_nodes )
 {
 
   // TODO --  add some more code to prevent from splitting of entity sequences when we don't need to.
@@ -79,167 +82,116 @@
   // find out what entity sequences we need to convert 
   // and where, if necessary, to split them
 
-  EntitySequenceManager* seq_manager = mMB->sequence_manager();
-
-  std::vector< std::pair<ElementEntitySequence*,MBRange> > sequence_range_pairs;
-
-  // get all the sequences that have these entities
-  // could speed this up a bit by using a MBRange::pair_iterator
-  for(MBRange::iterator p_iter = entities.begin(); p_iter != entities.end(); ++p_iter)
-  {
-    if(TYPE_FROM_HANDLE(*p_iter) == MBVERTEX || TYPE_FROM_HANDLE(*p_iter) >= MBENTITYSET)
-      continue;
-
-    MBEntitySequence* seq = NULL;
-    MBErrorCode rval = seq_manager->find(*p_iter, seq);
-    if(MB_SUCCESS == rval)
-    {
-      if(sequence_range_pairs.empty() || seq != sequence_range_pairs.rbegin()->first)
-      {
-        sequence_range_pairs.push_back(
-            std::pair<ElementEntitySequence*,MBRange>( static_cast<ElementEntitySequence*>(seq), MBRange() )
-            );
-      }
-      sequence_range_pairs.rbegin()->second.insert(*p_iter);
+  SequenceManager* seq_manager = mMB->sequence_manager();
+  MBRange::const_pair_iterator p_iter;
+  for (p_iter = entities.const_pair_begin(); p_iter != entities.const_pair_end(); ++p_iter) {
+    
+    MBEntityHandle h = p_iter->first;
+    while (h <= p_iter->second) {
+      
+      EntitySequence* seq;
+      MBErrorCode rval = seq_manager->find( h, seq );
+      if (MB_SUCCESS != rval)
+        return rval;
+      
+      if (seq->type() == MBVERTEX || seq->type() >= MBENTITYSET)
+        return MB_TYPE_OUT_OF_RANGE;
+      
+        // make sequence is not structured mesh
+      ElementSequence* elemseq = static_cast<ElementSequence*>(seq);
+      if (NULL == elemseq->get_connectivity_array())
+        return MB_NOT_IMPLEMENTED;
+      
+      MBEntityHandle last = p_iter->second;
+      if (last > seq->end_handle())
+        last = seq->end_handle();
+      
+      rval = convert_sequence( elemseq, h, last, 
+                               mid_edge_nodes,
+                               mid_face_nodes,
+                               mid_volume_nodes );
+      if (MB_SUCCESS != rval)
+        return rval;
+        
+      h = last + 1;
     }
   }
-
-  std::vector<ElementEntitySequence*> sequences_to_process;
   
-
-  // iterate through each entity sequence and split up entity sequences where we need to
-  // put results into sequences list to process
-  std::vector< std::pair<ElementEntitySequence*, MBRange> >::iterator s_iter;
-  for(s_iter = sequence_range_pairs.begin(); s_iter != sequence_range_pairs.end(); ++s_iter)
-  {
-    // find all the entities we weren't told to convert
-    MBRange holes;
-
-    // we have a hole at the beginning of the sequence
-    if(*(s_iter->second.begin()) != s_iter->first->get_start_handle())
-    {
-      holes.insert(s_iter->first->get_start_handle(), *(s_iter->second.begin())-1);
-    }
-
-    //we have a hole at the end of the sequence
-    if(*(s_iter->second.rbegin()) != s_iter->first->get_end_handle())
-    {
-      holes.insert(*(s_iter->second.rbegin())+1, s_iter->first->get_end_handle());
-    }
-    // any holes in the middle
-    MBRange::pair_iterator r_iter = s_iter->second.begin();
-    MBRange::pair_iterator prev_r_iter = r_iter;
-    r_iter++;
-    for(; r_iter != s_iter->second.end(); )
-    {
-      holes.insert(prev_r_iter->second+1, r_iter->first-1);
-      ++r_iter;
-      ++prev_r_iter;
-    }
-
-    //we have a hole at the end of the sequence
-    if(*(s_iter->second.rbegin()) != s_iter->first->get_end_handle())
-    {
-      holes.insert(*(s_iter->second.rbegin())+1, s_iter->first->get_end_handle());
-    }
-
-    // remove entities from the holes list that are unallocated entities
-    for(MBRange::iterator rm_iter = holes.begin(); rm_iter != holes.end(); )
-    {
-      if(!s_iter->first->is_valid_entity(*rm_iter))
-        rm_iter = holes.erase(rm_iter);
-      else
-        ++rm_iter;
-    }
-
-    // no holes, convert the whole sequence
-    if(holes.empty())
-    {
-      sequences_to_process.push_back(s_iter->first);
-    }
-    else
-    {
-      MBEntitySequence* new_seq=NULL;
-      // now we know where we need to split
-
-      r_iter = holes.begin();
-
-      //split at beginning
-      if(r_iter->first == s_iter->first->get_start_handle())
-      {
-        s_iter->first->split(r_iter->second+1, new_seq);
-        s_iter->first = static_cast<ElementEntitySequence*>(new_seq);
-        sequences_to_process.push_back(static_cast<ElementEntitySequence*>(new_seq));
-      }
-      else
-      {
-        sequences_to_process.push_back(s_iter->first);
-        s_iter->first->split(r_iter->first, new_seq);
-        s_iter->first = static_cast<ElementEntitySequence*>(new_seq);
-        if(r_iter->second != new_seq->get_end_handle())
-        {
-          s_iter->first->split(r_iter->second+1, new_seq);
-          s_iter->first = static_cast<ElementEntitySequence*>(new_seq);
-          sequences_to_process.push_back(static_cast<ElementEntitySequence*>(new_seq));
-        }
-      }
-
-      // split in middle
-      ++r_iter;
-      MBRange::pair_iterator next_r_iter = r_iter;
-      ++next_r_iter;
-      for( ; r_iter != holes.end(); )
-      {
-        s_iter->first->split(r_iter->first, new_seq);
-        s_iter->first = static_cast<ElementEntitySequence*>(new_seq);
-        if( (next_r_iter != holes.end()) || (next_r_iter == holes.end() && r_iter->second != new_seq->get_end_handle()) )
-        {
-          s_iter->first->split(r_iter->second+1, new_seq);
-          s_iter->first = static_cast<ElementEntitySequence*>(new_seq);
-          sequences_to_process.push_back(static_cast<ElementEntitySequence*>(new_seq));
-        }
-        ++r_iter;
-        ++next_r_iter;
-      }
-
-    }
-  }
-
-  // now convert the entity sequences
-  for(std::vector<ElementEntitySequence*>::iterator ziter = sequences_to_process.begin(); 
-      ziter != sequences_to_process.end(); ++ziter)
-  {
-    convert_sequence(*ziter, mid_edge_nodes, mid_face_nodes, mid_volume_nodes);
-  }
-
-
   return MB_SUCCESS;
-}
+} 
 
 
-MBErrorCode HigherOrderFactory::convert_sequence(ElementEntitySequence* seq, const bool mid_edge_nodes, 
-                                                  const bool mid_face_nodes, const bool mid_volume_nodes)
+MBErrorCode HigherOrderFactory::convert_sequence( ElementSequence* seq, 
+                                                  MBEntityHandle start,
+                                                  MBEntityHandle end,
+                                                  bool mid_edge_nodes, 
+                                                  bool mid_face_nodes, 
+                                                  bool mid_volume_nodes)
 {
 
   MBErrorCode status = MB_SUCCESS;
 
-  bool temp_mid_edge = mid_edge_nodes;
-  bool temp_mid_face = mid_face_nodes;
-  bool temp_mid_volume = mid_volume_nodes;
-  
   // lets make sure parameters are ok before we continue
-  MBEntityType this_type = seq->get_type();
-  if(this_type == MBEDGE)
-    temp_mid_face = temp_mid_volume = false;
-  if(this_type == MBTRI || this_type == MBQUAD)
-    temp_mid_volume = false;
+  switch (seq->type()) {
+    default: return MB_TYPE_OUT_OF_RANGE; 
+    case MBEDGE:
+      mid_face_nodes = false;
+    case MBTRI:
+    case MBQUAD:
+      mid_volume_nodes = false;
+    case MBTET:
+    case MBHEX:
+    case MBPRISM:
+    case MBPYRAMID:
+    case MBKNIFE:
+      break;
+  }
 
+    // calculate number of nodes in target configuration
+  unsigned nodes_per_elem = MBCN::VerticesPerEntity( seq->type() );
+  if (mid_edge_nodes)
+    nodes_per_elem += (seq->type() == MBEDGE) ? 1 : MBCN::NumSubEntities( seq->type(), 1 );
+  if (mid_face_nodes)
+    nodes_per_elem += (MBCN::Dimension(seq->type()) == 2) ? 1 : MBCN::NumSubEntities( seq->type(), 2 );
+  if (mid_volume_nodes) 
+    nodes_per_elem += 1;
+  
+  if (nodes_per_elem == seq->nodes_per_element())
+    return MB_SUCCESS;
+  
   MBTag deletable_nodes;
-  mMB->tag_create("", 1, MB_TAG_BIT, deletable_nodes, NULL);
+  status = mMB->tag_create("", 1, MB_TAG_BIT, deletable_nodes, NULL);
+  if (MB_SUCCESS != status)
+    return status;
+  
+  UnstructuredElemSeq* new_seq = new UnstructuredElemSeq( start,
+                                                          end - start + 1,
+                                                          nodes_per_elem,
+                                                          end - start + 1 );
+  
+  copy_corner_nodes( seq, new_seq );
 
-  // will return if we need to create them
-  seq->convert_realloc(temp_mid_edge, temp_mid_face, temp_mid_volume, mMB, deletable_nodes);
+  if (seq->has_mid_edge_nodes() && mid_edge_nodes) 
+    status = copy_mid_edge_nodes( seq, new_seq );
+  else if (seq->has_mid_edge_nodes() && !mid_edge_nodes)
+    status = remove_mid_edge_nodes( seq, start, end, deletable_nodes );
+  if (MB_SUCCESS != status)
+    return status;
 
+  if (seq->has_mid_face_nodes() && mid_face_nodes) 
+    status = copy_mid_face_nodes( seq, new_seq );
+  else if (seq->has_mid_face_nodes() && !mid_face_nodes)
+    status = remove_mid_face_nodes( seq, start, end, deletable_nodes );
+  if (MB_SUCCESS != status)
+    return status;
+ 
+  if (seq->has_mid_volume_nodes() && mid_volume_nodes) 
+    status = copy_mid_volume_nodes( seq, new_seq );
+  else if (seq->has_mid_volume_nodes() && !mid_volume_nodes)
+    status = remove_mid_volume_nodes( seq, start, end, deletable_nodes );
+  if (MB_SUCCESS != status)
+    return status;
+
   // gather nodes that were marked
   MBRange nodes;
   mMB->get_entities_by_type_and_tag(0, MBVERTEX, &deletable_nodes, NULL, 1, nodes);
@@ -261,27 +213,45 @@
     }
   }
   
+  const bool create_midedge = !seq->has_mid_edge_nodes() && mid_edge_nodes;
+  const bool create_midface = !seq->has_mid_face_nodes() && mid_face_nodes;
+  const bool create_midvolm = !seq->has_mid_volume_nodes() && mid_volume_nodes;
+  
   mMB->tag_delete(deletable_nodes);
 
-  // create mid edge nodes if necessary
-  if(temp_mid_edge)
-    status = add_mid_edge_nodes(seq);
-  // create mid face nodes if necessary
-  if(temp_mid_face)
-    status = add_mid_face_nodes(seq);
-  // create mid volume nodes if necessary
-  if(temp_mid_volume)
-    status = add_mid_volume_nodes(seq);
+  status = mMB->sequence_manager()->replace_subsequence( new_seq, mMB->tag_server() );
+  if (MB_SUCCESS != status) {
+    SequenceData* data = new_seq->data();
+    delete new_seq;
+    delete data;
+    return status;
+  }
 
+  if (create_midedge) {
+    status = add_mid_edge_nodes( new_seq );
+    if (MB_SUCCESS != status)
+      return status;
+  }
+  if (create_midface) {
+    status = add_mid_face_nodes( new_seq );
+    if (MB_SUCCESS != status)
+      return status;
+  }
+  if (create_midvolm) {
+    status = add_mid_volume_nodes( new_seq );
+    if (MB_SUCCESS != status)
+      return status;
+  }
+  
   return status;
 
 }
 
 
-MBErrorCode HigherOrderFactory::add_mid_volume_nodes(ElementEntitySequence* seq)
+MBErrorCode HigherOrderFactory::add_mid_volume_nodes(ElementSequence* seq)
 {
-  MBEntityType this_type = seq->get_type();
-  EntitySequenceManager* seq_manager = mMB->sequence_manager();
+  MBEntityType this_type = seq->type();
+  SequenceManager* seq_manager = mMB->sequence_manager();
 
   // find out where in the connectivity list to add these new mid volume nodes
   int edge_factor = seq->has_mid_edge_nodes() ? 1 : 0;
@@ -292,29 +262,21 @@
   new_node_index += edge_factor * MBCN::mConnectivityMap[this_type][0].num_sub_elements;
   new_node_index += face_factor * MBCN::mConnectivityMap[this_type][1].num_sub_elements;
 
-  MBEntityHandle* element = NULL;
-  seq->get_connectivity_array(element);
-  MBEntityHandle curr_handle = seq->get_start_handle();
+  MBEntityHandle* element = seq->get_connectivity_array();
+  MBEntityHandle curr_handle = seq->start_handle();
   int nodes_per_element = seq->nodes_per_element();
-  MBEntityHandle* end_element = element + nodes_per_element * (seq->number_allocated());
+  MBEntityHandle* end_element = element + nodes_per_element * (seq->size());
 
   // iterate over the elements
   for(; element < end_element; element+=nodes_per_element)
   {
-    // this element isn't being used, skip it
-    if(!seq->is_valid_entity(curr_handle))
-    {
-      curr_handle++;
-      continue;
-    }
-
     // find the centroid of this element
     double tmp_coords[3], sum_coords[3] = {0,0,0};
-    MBEntitySequence* seq=NULL;
+    EntitySequence* seq=NULL;
     for(int i=0; i<num_corner_nodes; i++)
     {
       seq_manager->find(element[i], seq);
-      static_cast<VertexEntitySequence*>(seq)->get_coordinates(
+      static_cast<VertexSequence*>(seq)->get_coordinates(
           element[i], tmp_coords[0], tmp_coords[1], tmp_coords[2]
           );
       sum_coords[0] += tmp_coords[0];
@@ -338,10 +300,10 @@
 }
 
 
-MBErrorCode HigherOrderFactory::add_mid_face_nodes(ElementEntitySequence* seq)
+MBErrorCode HigherOrderFactory::add_mid_face_nodes(ElementSequence* seq)
 {
-  MBEntityType this_type = seq->get_type();
-  EntitySequenceManager* seq_manager = mMB->sequence_manager();
+  MBEntityType this_type = seq->type();
+  SequenceManager* seq_manager = mMB->sequence_manager();
   int num_vertices = MBCN::VerticesPerEntity(this_type);
   int num_edges = MBCN::mConnectivityMap[this_type][0].num_sub_elements;
   num_edges = seq->has_mid_edge_nodes() ? num_edges : 0;
@@ -349,11 +311,10 @@
 
   const MBCN::ConnMap& entity_faces = MBCN::mConnectivityMap[this_type][1];
 
-  MBEntityHandle* element = NULL;
-  seq->get_connectivity_array(element);
-  MBEntityHandle curr_handle = seq->get_start_handle();
+  MBEntityHandle* element = seq->get_connectivity_array();
+  MBEntityHandle curr_handle = seq->start_handle();
   int nodes_per_element = seq->nodes_per_element();
-  MBEntityHandle* end_element = element + nodes_per_element * (seq->number_allocated());
+  MBEntityHandle* end_element = element + nodes_per_element * (seq->size());
 
   MBEntityHandle tmp_face_conn[4];  // max face nodes = 4
   std::vector<MBEntityHandle> adjacent_entities(4);
@@ -363,13 +324,6 @@
   // iterate over the elements
   for(; element < end_element; element+=nodes_per_element)
   {
-    // this element isn't being used, skip it
-    if(!seq->is_valid_entity(curr_handle))
-    {
-      curr_handle++;
-      continue;
-    }
-
     // for each edge in this entity
     for(int i=0; i<num_faces; i++)
     {
@@ -394,13 +348,13 @@
       // create a node
       else
       {
-        MBEntitySequence* tmp_sequence = NULL;
+        EntitySequence* tmp_sequence = NULL;
         double sum_coords[3] = {0,0,0};
         int max_nodes = entity_faces.num_nodes_per_sub_element[i];
         for(int k=0; k<max_nodes; k++)
         {
           seq_manager->find(tmp_face_conn[k], tmp_sequence);
-          static_cast<VertexEntitySequence*>(tmp_sequence)->get_coordinates( 
+          static_cast<VertexSequence*>(tmp_sequence)->get_coordinates( 
               tmp_face_conn[k], tmp_coords[0], tmp_coords[1], tmp_coords[2]);
           sum_coords[0] += tmp_coords[0];
           sum_coords[1] += tmp_coords[1];
@@ -427,11 +381,11 @@
 }
 
 
-MBErrorCode HigherOrderFactory::add_mid_edge_nodes(ElementEntitySequence* seq)
+MBErrorCode HigherOrderFactory::add_mid_edge_nodes(ElementSequence* seq)
 {
   // for each node, need to see if it was already created.
-  MBEntityType this_type = seq->get_type();
-  EntitySequenceManager* seq_manager = mMB->sequence_manager();
+  MBEntityType this_type = seq->type();
+  SequenceManager* seq_manager = mMB->sequence_manager();
 
   // offset by number of corner nodes
   int num_vertices = MBCN::VerticesPerEntity(this_type);
@@ -439,12 +393,10 @@
 
   const MBCN::ConnMap& entity_edges = MBCN::mConnectivityMap[this_type][0];
   
-
-  MBEntityHandle* element = NULL;
-  seq->get_connectivity_array(element);
-  MBEntityHandle curr_handle = seq->get_start_handle();
+  MBEntityHandle* element = seq->get_connectivity_array();
+  MBEntityHandle curr_handle = seq->start_handle();
   int nodes_per_element = seq->nodes_per_element();
-  MBEntityHandle* end_element = element + nodes_per_element * (seq->number_allocated());
+  MBEntityHandle* end_element = element + nodes_per_element * (seq->size());
 
   MBEntityHandle tmp_edge_conn[2];
   std::vector<MBEntityHandle> adjacent_entities(32);
@@ -454,13 +406,6 @@
   // iterate over the elements
   for(; element < end_element; element+=nodes_per_element)
   {
-    // this element isn't being used, skip it
-    if(!seq->is_valid_entity(curr_handle))
-    {
-      curr_handle++;
-      continue;
-    }
-
     // for each edge in this entity
     for(int i=0; i<num_edges; i++)
     {
@@ -481,16 +426,16 @@
       // create a node
       else
       {
-        MBEntitySequence* tmp_sequence = NULL;
+        EntitySequence* tmp_sequence = NULL;
         double sum_coords[3] = {0,0,0};
         seq_manager->find(tmp_edge_conn[0], tmp_sequence);
-        static_cast<VertexEntitySequence*>(tmp_sequence)->get_coordinates( 
+        static_cast<VertexSequence*>(tmp_sequence)->get_coordinates( 
             tmp_edge_conn[0], tmp_coords[0], tmp_coords[1], tmp_coords[2]);
         sum_coords[0] += tmp_coords[0];
         sum_coords[1] += tmp_coords[1];
         sum_coords[2] += tmp_coords[2];
         seq_manager->find(tmp_edge_conn[1], tmp_sequence);
-        static_cast<VertexEntitySequence*>(tmp_sequence)->get_coordinates( 
+        static_cast<VertexSequence*>(tmp_sequence)->get_coordinates( 
             tmp_edge_conn[1], tmp_coords[0], tmp_coords[1], tmp_coords[2]);
         sum_coords[0] = (sum_coords[0] + tmp_coords[0]) /2;
         sum_coords[1] = (sum_coords[1] + tmp_coords[1]) /2;
@@ -540,6 +485,10 @@
       iter != adj_entities.end(); )
   {
     MBEntityType this_type = TYPE_FROM_HANDLE(*iter);
+    if (this_type == MBENTITYSET) {
+      ++iter;
+      continue;
+    }
     mMB->get_connectivity(*iter, conn, conn_size);
     // if this entity has mid edge nodes
     if(MBCN::HasMidEdgeNodes(this_type, conn_size))
@@ -598,6 +547,10 @@
       iter != adj_entities.end(); )
   {
     MBEntityType this_type = TYPE_FROM_HANDLE(*iter);
+    if (this_type == MBENTITYSET) {
+      ++iter;
+      continue;
+    }
     const MBCN::ConnMap& entity_faces = MBCN::mConnectivityMap[this_type][1];
     mMB->get_connectivity(*iter, conn, conn_size);
     int offset = MBCN::VerticesPerEntity(this_type);
@@ -669,3 +622,286 @@
   return true;
 }
 
+MBErrorCode 
+HigherOrderFactory::copy_corner_nodes( ElementSequence* src, ElementSequence* dst )
+{
+  unsigned num_corners = MBCN::VerticesPerEntity( src->type() );
+  return copy_nodes( src, dst, num_corners, 0, 0 );
+}
+
+MBErrorCode 
+HigherOrderFactory::copy_mid_edge_nodes( ElementSequence* src, ElementSequence* dst )
+{
+  if (!src->has_mid_edge_nodes() || !dst->has_mid_edge_nodes())
+    return MB_FAILURE;
+  
+  unsigned num_corners = MBCN::VerticesPerEntity( src->type() );
+  unsigned num_edges = (src->type() == MBEDGE) ? 1 : MBCN::NumSubEntities( src->type(), 1 );
+  return copy_nodes( src, dst, num_edges, num_corners, num_corners );
+}
+
+MBErrorCode 
+HigherOrderFactory::copy_mid_face_nodes( ElementSequence* src, ElementSequence* dst )
+{
+  if (!src->has_mid_face_nodes() || !dst->has_mid_face_nodes())
+    return MB_FAILURE;
+  
+  unsigned src_offset = MBCN::VerticesPerEntity( src->type() );
+  unsigned dst_offset = src_offset;
+  if (src->has_mid_edge_nodes())
+    src_offset += MBCN::NumSubEntities( src->type(), 1 );
+  if (dst->has_mid_edge_nodes())
+    dst_offset += MBCN::NumSubEntities( dst->type(), 1 );
+  unsigned num_faces = (MBCN::Dimension(src->type()) == 2) ? 1 : MBCN::NumSubEntities( src->type(), 2 );
+  return copy_nodes( src, dst, num_faces, src_offset, dst_offset );
+}
+
+
+MBErrorCode 
+HigherOrderFactory::copy_mid_volume_nodes( ElementSequence* src, ElementSequence* dst )
+{
+  if (!src->has_mid_volume_nodes() || !dst->has_mid_volume_nodes())
+    return MB_FAILURE;
+  
+  unsigned src_offset = MBCN::VerticesPerEntity( src->type() );
+  unsigned dst_offset = src_offset;
+  if (src->has_mid_edge_nodes())
+    src_offset += MBCN::NumSubEntities( src->type(), 1 );
+  if (dst->has_mid_edge_nodes())
+    dst_offset += MBCN::NumSubEntities( dst->type(), 1 );
+  if (src->has_mid_face_nodes())
+    src_offset += MBCN::NumSubEntities( src->type(), 2 );
+  if (dst->has_mid_face_nodes())
+    dst_offset += MBCN::NumSubEntities( dst->type(), 2 );
+  return copy_nodes( src, dst, 1, src_offset, dst_offset );
+}
+
+MBErrorCode 
+HigherOrderFactory::copy_nodes( ElementSequence* src,
+                                ElementSequence* dst,
+                                unsigned nodes_per_elem,
+                                unsigned src_offset,
+                                unsigned dst_offset )
+{
+  if (src->type() != dst->type())
+    return MB_FAILURE;
+
+  unsigned src_stride = src->nodes_per_element();
+  unsigned dst_stride = dst->nodes_per_element();
+  MBEntityHandle* src_conn = src->get_connectivity_array();
+  MBEntityHandle* dst_conn = dst->get_connectivity_array();
+  if (!src_conn || !dst_conn)
+    return MB_FAILURE;
+  
+  if (dst->start_handle() < src->start_handle() ||
+      dst->end_handle()   > src->end_handle())
+    return MB_FAILURE;
+  
+  src_conn += (dst->start_handle() - src->start_handle()) * src_stride;
+  MBEntityID count = dst->size();
+  for (MBEntityID i = 0; i < count; ++i) {
+    for (unsigned j = 0; j < nodes_per_elem; ++j)
+      dst_conn[j+dst_offset] = src_conn[j+src_offset];
+    src_conn += src_stride; 
+    dst_conn += dst_stride;
+  }
+  
+  return MB_SUCCESS;
+}
+
+MBErrorCode 
+HigherOrderFactory::remove_mid_edge_nodes( ElementSequence* seq, 
+                                           MBEntityHandle start,
+                                           MBEntityHandle end,
+                                           MBTag deletable_nodes )
+{
+  int count;
+  int offset;
+  if (seq->type() == MBEDGE) {
+    count = 1;
+    offset = 2;
+  }
+  else {
+    count = MBCN::NumSubEntities( seq->type(), 1 );
+    offset = MBCN::VerticesPerEntity( seq->type() );
+  }
+  
+  return remove_ho_nodes( seq, start, end, count, offset, deletable_nodes );
+}
+
+
+MBErrorCode 
+HigherOrderFactory::remove_mid_face_nodes( ElementSequence* seq, 
+                                           MBEntityHandle start,
+                                           MBEntityHandle end,
+                                           MBTag deletable_nodes )
+{
+  int count;
+  if (MBCN::Dimension(seq->type()) == 2)
+    count = 1;
+  else 
+    count = MBCN::NumSubEntities( seq->type(), 2 );
+  int offset = MBCN::VerticesPerEntity( seq->type() );
+  if (seq->has_mid_edge_nodes())
+    offset += MBCN::NumSubEntities( seq->type(), 1 );
+  
+  return remove_ho_nodes( seq, start, end, count, offset, deletable_nodes );
+}
+
+MBErrorCode 
+HigherOrderFactory::remove_mid_volume_nodes( ElementSequence* seq, 
+                                             MBEntityHandle start,
+                                             MBEntityHandle end,
+                                             MBTag deletable_nodes )
+{
+  int offset = MBCN::VerticesPerEntity( seq->type() );
+  if (seq->has_mid_edge_nodes())
+    offset += MBCN::NumSubEntities( seq->type(), 1 );
+  if (seq->has_mid_face_nodes())
+    offset += MBCN::NumSubEntities( seq->type(), 2 );
+  
+  return remove_ho_nodes( seq, start, end, 1, offset, deletable_nodes );
+}
+
+// Code mostly copied from old EntitySequence.cpp
+// (ElementEntitySequence::convert_realloc & 
+//  ElementEntitySequence::tag_for_deletion).
+// Copyright from old EntitySequence.cpp:
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ * 
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+MBErrorCode 
+HigherOrderFactory::remove_ho_nodes( ElementSequence* seq,
+                                     MBEntityHandle start,
+                                     MBEntityHandle end,
+                                     int nodes_per_elem,
+                                     int elem_conn_offset,
+                                     MBTag deletable_nodes )
+{
+  if (start < seq->start_handle() || end > seq->end_handle())
+    return MB_ENTITY_NOT_FOUND;
+  MBEntityHandle* array = seq->get_connectivity_array();
+  if (!array)
+    return MB_NOT_IMPLEMENTED;
+   
+  std::set<MBEntityHandle> nodes_processed;
+  for (MBEntityHandle i = start; i <= end; ++i) {  // for each element
+    for (int j = 0; j < nodes_per_elem; ++j) {  // for each HO node to remove
+      const MBEntityID elem = (i - seq->start_handle()); // element index
+      const int conn_idx = j + elem_conn_offset;
+      const MBEntityID index = elem * seq->nodes_per_element() + conn_idx;
+      if (array[index] && nodes_processed.insert( array[index] ).second) {
+        if (tag_for_deletion( i, conn_idx, seq )) {
+          unsigned char bit = 0x1;
+          mMB->tag_set_data( deletable_nodes, &(array[index]), 1, &bit );
+        }
+      }
+    }
+  }
+  
+  return MB_SUCCESS;
+}
+
+bool
+HigherOrderFactory::tag_for_deletion( MBEntityHandle parent_handle,
+                                      int conn_index,
+                                      ElementSequence* seq )
+{
+  //get type of this sequence
+  MBEntityType this_type = seq->type();
+
+  //get dimension of 'parent' element
+  int this_dimension = mMB->dimension_from_handle( parent_handle );
+
+  //tells us if higher order node is on 
+  int dimension, side_number; 
+  MBCN::HONodeParent( this_type, seq->nodes_per_element(),
+                      conn_index, dimension, side_number );  
+
+  //it MUST be a higher-order node
+  bool delete_node = false;
+
+  assert( dimension != -1 );
+  assert( side_number != -1 );
+
+  //could be a mid-volume/face/edge node on a hex/face/edge respectively
+  //if so...delete it bc/ no one else owns it too
+  std::vector<MBEntityHandle> connectivity;
+  if( dimension == this_dimension && side_number == 0 )
+    delete_node = true;
+  else //the node could also be on a lower order entity of 'tmp_entity' 
+  {
+    //get 'side' of 'parent_handle' that node is on 
+    MBEntityHandle target_entity = 0;
+    mMB->side_element( parent_handle, dimension, side_number, target_entity );
+
+    if( target_entity )
+    {
+      AEntityFactory *a_fact = mMB->a_entity_factory();
+      MBEntityHandle low_meshset;
+      int dum;
+      low_meshset = CREATE_HANDLE(MBENTITYSET, 0, dum);
+
+      //just get corner nodes of target_entity
+      connectivity.clear();
+      mMB->get_connectivity(&( target_entity), 1, connectivity, true  );
+
+      //for each node, get all common adjacencies of nodes in 'parent_handle' 
+      std::vector<MBEntityHandle> adj_list_1, adj_list_2, adj_entities;
+      a_fact->get_adjacencies(connectivity[0], adj_list_1);
+
+      // remove meshsets
+      adj_list_1.erase(std::remove_if(adj_list_1.begin(), adj_list_1.end(), 
+           std::bind2nd(std::greater<MBEntityHandle>(),low_meshset)), adj_list_1.end());
+
+      size_t i; 
+      for( i=1; i<connectivity.size(); i++)
+      {
+        adj_list_2.clear();
+        a_fact->get_adjacencies(connectivity[i], adj_list_2);
+
+        // remove meshsets
+        adj_list_2.erase(std::remove_if(adj_list_2.begin(), adj_list_2.end(), 
+             std::bind2nd(std::greater<MBEntityHandle>(),low_meshset)), adj_list_2.end());
+       
+        //intersect the 2 lists 
+        adj_entities.clear();
+        std::set_intersection(adj_list_1.begin(), adj_list_1.end(), 
+                              adj_list_2.begin(), adj_list_2.end(), 
+                              std::back_inserter< std::vector<MBEntityHandle> >(adj_entities));
+        adj_list_1.clear();
+        adj_list_1 = adj_entities;
+      } 
+
+      assert( adj_entities.size() );  //has to have at least one adjacency 
+
+      //see if node is in other elements, not in this sequence...if so, delete it 
+      for( i=0; i<adj_entities.size(); i++)
+      {
+        if( adj_entities[i] >= seq->start_handle() &&
+            adj_entities[i] <= seq->end_handle() )
+        {
+          delete_node = false;
+          break;
+        }
+        else 
+          delete_node = true;
+      }             
+    }
+    else //there is no lower order entity that also contains node 
+      delete_node = true;
+  }
+
+  return delete_node;
+}

Modified: MOAB/trunk/HigherOrderFactory.hpp
===================================================================
--- MOAB/trunk/HigherOrderFactory.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/HigherOrderFactory.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -29,7 +29,7 @@
 #endif
 
 #include "MBInterface.hpp"
-class ElementEntitySequence;
+class ElementSequence;
 class MBCore;
 
 class HigherOrderFactory
@@ -52,11 +52,15 @@
   MBCore* mMB;
   MBInterface::HONodeAddedRemoved* mHONodeAddedRemoved;
 
-  MBErrorCode convert_sequence(ElementEntitySequence*, const bool mid_edge_nodes, 
-                                const bool mid_face_nodes, const bool mid_volume_nodes);
-  MBErrorCode add_mid_edge_nodes(ElementEntitySequence*);
-  MBErrorCode add_mid_face_nodes(ElementEntitySequence*);
-  MBErrorCode add_mid_volume_nodes(ElementEntitySequence*);
+  MBErrorCode convert_sequence(ElementSequence* sequence, 
+                               MBEntityHandle sequence_subset_start,
+                               MBEntityHandle sequence_subset_end,
+                               bool mid_edge_nodes, 
+                               bool mid_face_nodes,
+                               bool mid_volume_nodes);
+  MBErrorCode add_mid_edge_nodes(ElementSequence*);
+  MBErrorCode add_mid_face_nodes(ElementSequence*);
+  MBErrorCode add_mid_volume_nodes(ElementSequence*);
 
   //! returns the handle of the first center node found between the two corner nodes.
   //! returns zero if none found
@@ -74,6 +78,38 @@
   bool add_center_node(MBEntityType type, MBEntityHandle* element_conn, int conn_size, 
       MBEntityHandle corner_node1, MBEntityHandle corner_node2, MBEntityHandle center_node);
 
+
+  MBErrorCode copy_corner_nodes( ElementSequence* src, ElementSequence* dst );
+  MBErrorCode copy_mid_edge_nodes( ElementSequence* src, ElementSequence* dst ); 
+  MBErrorCode copy_mid_face_nodes( ElementSequence* src, ElementSequence* dst ); 
+  MBErrorCode copy_mid_volume_nodes( ElementSequence* src, ElementSequence* dst ); 
+  MBErrorCode copy_nodes( ElementSequence* src, 
+                          ElementSequence* dst,
+                          unsigned nodes_per_elem_to_copy,
+                          unsigned src_conn_offset,
+                          unsigned dst_conn_offset ); 
+
+  MBErrorCode remove_mid_edge_nodes( ElementSequence* seq, 
+                                     MBEntityHandle start,
+                                     MBEntityHandle stop,
+                                     MBTag deletable_ndoes );
+  MBErrorCode remove_mid_face_nodes( ElementSequence* seq, 
+                                     MBEntityHandle start,
+                                     MBEntityHandle stop,
+                                     MBTag deletable_ndoes );
+  MBErrorCode remove_mid_volume_nodes( ElementSequence* seq, 
+                                       MBEntityHandle start,
+                                       MBEntityHandle stop,
+                                       MBTag deletable_ndoes );
+  MBErrorCode remove_ho_nodes( ElementSequence* sequence,
+                               MBEntityHandle subset_start_handle,
+                               MBEntityHandle subset_end_handle,
+                               int nodes_per_elem_to_remove,
+                               int elem_conn_offset_to_remove,
+                               MBTag deletable_nodes );
+  bool tag_for_deletion( MBEntityHandle element_with_node,
+                         int node_index_in_elem_connectivity,
+                         ElementSequence* sequence );
 };
 
 

Modified: MOAB/trunk/MBCore.cpp
===================================================================
--- MOAB/trunk/MBCore.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBCore.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -28,13 +28,15 @@
 #include "MBCore.hpp"
 #include "TagServer.hpp"
 #include "MeshSetSequence.hpp"
+#include "ElementSequence.hpp"
+#include "VertexSequence.hpp"
 #include "assert.h"
 #include "AEntityFactory.hpp"
 #include "MBReadUtil.hpp"
 #include "MBWriteUtil.hpp"
 #include "MBCN.hpp"
 #include "HigherOrderFactory.hpp"
-#include "EntitySequenceManager.hpp"
+#include "SequenceManager.hpp"
 #include "MBError.hpp"
 #include "MBReaderWriterSet.hpp"
 #include "MBReaderIface.hpp"
@@ -88,13 +90,11 @@
   "MB_FAILURE",
 };
 
-static inline MBMeshSet* get_mesh_set( const EntitySequenceManager* sm,
+static inline MBMeshSet* get_mesh_set( const SequenceManager* sm,
                                        MBEntityHandle h )
 {
-  MBEntitySequence* seq;
-  if (MBENTITYSET != TYPE_FROM_HANDLE(h) ||
-      MB_SUCCESS != sm->find( h, seq ) || 
-      !seq->is_valid_entity(h))
+  EntitySequence* seq;
+  if (MBENTITYSET != TYPE_FROM_HANDLE(h) || MB_SUCCESS != sm->find( h, seq ))
     return 0;
   return reinterpret_cast<MeshSetSequence*>(seq)->get_set(h);
 }
@@ -142,7 +142,7 @@
   if (!tagServer)
     return MB_MEMORY_ALLOCATION_FAILED;
   
-  sequenceManager = new EntitySequenceManager( handleUtils );
+  sequenceManager = new SequenceManager( handleUtils );
   if (!sequenceManager)
     return MB_MEMORY_ALLOCATION_FAILED;
 
@@ -308,11 +308,8 @@
   handle = CREATE_HANDLE(type, id, err);
 
     //check to see if handle exists 
-  MBEntitySequence *dummy_seq = 0;
+  EntitySequence *dummy_seq = 0;
   MBErrorCode error_code = sequence_manager()->find(handle, dummy_seq);
-  if(error_code == MB_SUCCESS && type != MBVERTEX)
-    error_code = dummy_seq->is_valid_entity(handle) ? MB_SUCCESS : MB_ENTITY_NOT_FOUND;
-
   return error_code; 
 }
 
@@ -490,7 +487,7 @@
   
   if (sequenceManager)
     delete sequenceManager;
-  sequenceManager = new EntitySequenceManager( handleUtils );
+  sequenceManager = new SequenceManager( handleUtils );
 
   return result;
 }
@@ -560,67 +557,45 @@
 
 MBErrorCode  MBCore::get_coords(const MBRange& entities, double *coords) const
 {
-
-  int node_index = 0;
-
-  // lets get ready to iterate the entity sequences and copy data
-  // we'll iterate the map in the sequence manager and
-  // the entities range at the same time
-
-  MBRange::const_iterator range_iter = entities.begin();
-  MBRange::const_iterator range_iter_end = entities.end();
-
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter
-    = sequence_manager()->entity_map(MBVERTEX)->begin();
+  const TypeSequenceManager& vert_data = sequence_manager()->entity_map( MBVERTEX );
+  TypeSequenceManager::const_iterator seq_iter;
   
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter_end
-    = sequence_manager()->entity_map(MBVERTEX)->end();
+  MBRange::const_pair_iterator i = entities.const_pair_begin();
+  MBEntityHandle first = i->first;
+  while (i != entities.const_pair_end()) {
+    
+    seq_iter = vert_data.lower_bound( first );
+    if (seq_iter == vert_data.end() || first < (*seq_iter)->start_handle())
+      return MB_ENTITY_NOT_FOUND;
+    const VertexSequence* vseq = reinterpret_cast<const VertexSequence*>(*seq_iter);
 
-  // lets find the entity sequence which holds the first entity
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter_lookahead = seq_iter;
-  seq_iter_lookahead++;
-  for( ; seq_iter_lookahead != seq_iter_end && 
-      seq_iter_lookahead->second->get_start_handle() < *range_iter; )
-  {
-    ++seq_iter;
-    ++seq_iter_lookahead;
-  }
-
-  // a look ahead iterator
-  MBRange::const_iterator range_iter_lookahead = range_iter;
-
-  // our main loop
-  for(; range_iter != range_iter_end && seq_iter != seq_iter_end; /* ++ is handled in loop*/ )
-  {
-    // find a range that fits in the current entity sequence
-    for(; range_iter_lookahead != range_iter_end && 
-        *range_iter_lookahead <= seq_iter->second->get_end_handle(); 
-        ++range_iter_lookahead)
-    {}
-
-    double* coord_array[3];
-    static_cast<VertexEntitySequence*>(seq_iter->second)->get_coordinate_arrays(
-        coord_array[0], coord_array[1], coord_array[2]);
-    MBEntityHandle start_ent = seq_iter->second->get_start_handle();
-
-    // for each of the entities in this entity sequence, copy data
-    for(MBRange::const_iterator tmp_iter = range_iter; 
-        tmp_iter != range_iter_lookahead;
-        ++tmp_iter)
-    {
-      coords[node_index] = coord_array[0][*tmp_iter - start_ent];
-      node_index++;
-      coords[node_index] = coord_array[1][*tmp_iter - start_ent];
-      node_index++;
-      coords[node_index] = coord_array[2][*tmp_iter - start_ent];
-      node_index++;
+    MBEntityID offset = first - vseq->start_handle();
+    MBEntityID count;
+    if (i->second <= vseq->end_handle()) {
+      count = i->second - first + 1;
+      ++i;
+      if (i != entities.const_pair_end())
+        first = i->first;
     }
-
-    // go to the next entity sequence
-    ++seq_iter;
-    // start with the next entities
-    range_iter = range_iter_lookahead;
+    else {
+      count = vseq->end_handle() - first + 1;
+      first = vseq->end_handle()+1;
+    }
+    
+    double const *x, *y, *z;
+    MBErrorCode rval = vseq->get_coordinate_arrays( x, y, z );
+    if (MB_SUCCESS != rval)
+      return rval;
+    x += offset;
+    y += offset;
+    z += offset;
+    for (MBEntityID j = 0; j < count; ++j) {
+      *coords = *x; ++coords; ++x;
+      *coords = *y; ++coords; ++y;
+      *coords = *z; ++coords; ++z;
+    }
   }
+  
   return MB_SUCCESS;
 }
 
@@ -630,63 +605,43 @@
                                 double *y_coords,
                                 double *z_coords ) const
 {
-    // declare some iterators
-  MBRange::const_iterator iter = entities.begin();
-  const MBRange::const_iterator end = entities.end();
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter
-    = sequence_manager()->entity_map(MBVERTEX)->begin();
-  const std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_end
-    = sequence_manager()->entity_map(MBVERTEX)->end();
-
-    // if no vertices in database, give up now
-  if (seq_iter == seq_end)
-    return MB_ENTITY_NOT_FOUND;
-
-    // loop over all entities in range
-  while(iter != end) {
-      // find sequence for vertex at *iter
-    while (seq_iter->second->get_end_handle() < *iter)
-       if (++seq_iter == seq_end)
-        return MB_ENTITY_NOT_FOUND;
-    if (*iter < seq_iter->second->get_start_handle())
+  const TypeSequenceManager& vert_data = sequence_manager()->entity_map( MBVERTEX );
+  TypeSequenceManager::const_iterator seq_iter;
+  
+  MBRange::const_pair_iterator i = entities.const_pair_begin();
+  MBEntityHandle first = i->first;
+  while (i != entities.const_pair_end()) {
+    
+    seq_iter = vert_data.lower_bound( first );
+    if (seq_iter == vert_data.end() || first < (*seq_iter)->start_handle())
       return MB_ENTITY_NOT_FOUND;
+    const VertexSequence* vseq = reinterpret_cast<const VertexSequence*>(*seq_iter);
 
-      // get data for sequence
-    VertexEntitySequence* seq = static_cast<VertexEntitySequence*>(seq_iter->second);
-    const MBEntityHandle start_ent = seq->get_start_handle();
-    const MBEntityHandle end_ent = seq->get_end_handle();
-    double *seq_x, *seq_y, *seq_z;
-    seq->get_coordinate_arrays( seq_x, seq_y, seq_z );
-
-      // for each entity in range and sequence 
-    while (iter != end && *iter <= end_ent) {
+    MBEntityID offset = first - vseq->start_handle();
+    MBEntityID count;
+    if (i->second <= vseq->end_handle()) {
+      count = i->second - first + 1;
+      ++i;
+      if (i != entities.const_pair_end())
+        first = i->first;
+    }
+    else {
+      count = vseq->end_handle() - first + 1;
+      first = vseq->end_handle()+1;
+    }
     
-        // get block of consecutive handles
-      const MBEntityHandle range_start = *iter;
-      iter = iter.end_of_block();
-      if (*iter > end_ent) // trim block to those in the sequence
-        iter -= *iter - end_ent;
-       
-      const MBEntityID offset = range_start - start_ent;
-      const MBEntityID count = *iter - range_start + 1;
-      ++iter; // done with iter for this iteration, advance to next handle
-      
-        // copy coordinate data
-      if (x_coords) {
-        memcpy( x_coords, seq_x + offset, sizeof(double)*count );
-        x_coords += count;
-      }
-      if (y_coords) {
-        memcpy( y_coords, seq_y + offset, sizeof(double)*count );
-        y_coords += count;
-      }
-      if (z_coords) {
-        memcpy( z_coords, seq_z + offset, sizeof(double)*count );
-        z_coords += count;
-      }
-    }
+    double const *x, *y, *z;
+    MBErrorCode rval = vseq->get_coordinate_arrays( x, y, z );
+    if (MB_SUCCESS != rval)
+      return rval;
+    memcpy( x_coords, x + offset, count * sizeof(double ) );
+    memcpy( y_coords, y + offset, count * sizeof(double ) );
+    memcpy( z_coords, z + offset, count * sizeof(double ) );
+    x_coords += count;
+    y_coords += count;
+    z_coords += count;
   }
-
+  
   return MB_SUCCESS;
 }
 
@@ -695,7 +650,7 @@
                                   double *coords) const
 {
   MBErrorCode status;
-  MBEntitySequence* seq;
+  EntitySequence* seq;
   const MBEntityHandle* const end = entities + num_entities;
 
   for(const MBEntityHandle* iter = entities; iter != end; ++iter)
@@ -704,10 +659,10 @@
       return MB_TYPE_OUT_OF_RANGE;
 
     status = sequence_manager()->find(*iter, seq);
-    if(status != MB_SUCCESS || !seq->is_valid_entity(*iter) )
+    if(status != MB_SUCCESS )
       return MB_ENTITY_NOT_FOUND;
     
-    static_cast<VertexEntitySequence*>(seq)->get_coordinates(*iter, coords);
+    static_cast<VertexSequence*>(seq)->get_coordinates(*iter, coords);
     coords += 3;
   }
 
@@ -723,13 +678,13 @@
 
   if ( TYPE_FROM_HANDLE(entity_handle) == MBVERTEX )
   {
-    MBEntitySequence* seq = 0;
+    EntitySequence* seq = 0;
     status = sequence_manager()->find(entity_handle, seq);
 
-    if (seq == 0 || status != MB_SUCCESS || !seq->is_valid_entity(entity_handle) )
+    if (seq == 0 || status != MB_SUCCESS)
       return MB_ENTITY_NOT_FOUND;
 
-    status = static_cast<VertexEntitySequence*>(seq)->get_coordinates_ref(entity_handle, 
+    status = static_cast<VertexSequence*>(seq)->get_coordinates_ref(entity_handle, 
                                                                           x, y, z);
 
   }
@@ -751,11 +706,11 @@
   for (i = 0; i < num_entities; i++) {
     if ( TYPE_FROM_HANDLE(entity_handles[i]) == MBVERTEX )
     {
-      MBEntitySequence* seq = 0;
+      EntitySequence* seq = 0;
       status = sequence_manager()->find(entity_handles[i], seq);
 
       if (seq != 0 && status == MB_SUCCESS) {
-        status = static_cast<VertexEntitySequence*>(seq)->set_coordinates(entity_handles[i], coords[j], coords[j+1], coords[j+2]);
+        status = static_cast<VertexSequence*>(seq)->set_coordinates(entity_handles[i], coords[j], coords[j+1], coords[j+2]);
         j += 3;
       }
     }
@@ -779,11 +734,11 @@
   for (MBRange::iterator rit = entity_handles.begin(); rit != entity_handles.end(); rit++) {
     if ( TYPE_FROM_HANDLE(*rit) == MBVERTEX )
     {
-      MBEntitySequence* seq = 0;
+      EntitySequence* seq = 0;
       status = sequence_manager()->find(*rit, seq);
 
       if (seq != 0 && status == MB_SUCCESS) {
-        status = static_cast<VertexEntitySequence*>(seq)->set_coordinates(*rit, coords[j], coords[j+1], coords[j+2]);
+        status = static_cast<VertexSequence*>(seq)->set_coordinates(*rit, coords[j], coords[j+1], coords[j+2]);
         j += 3;
       }
     }
@@ -871,12 +826,12 @@
 
   MBErrorCode result = MB_SUCCESS, temp_result;
   for (i = 0; i < num_handles; i++) {
-    MBEntitySequence* seq = 0;
+    EntitySequence* seq = 0;
 
       // We know that connectivity is stored in an EntitySequence so jump straight
       // to the entity sequence
     temp_result = sequence_manager()->find(entity_handles[i], seq);
-    if (seq == NULL || !seq->is_valid_entity(entity_handles[i])) {
+    if (seq == NULL) {
       result = MB_ENTITY_NOT_FOUND;
       continue;
     }
@@ -885,7 +840,7 @@
       continue;
     }
 
-    ElementEntitySequence *elem_seq = static_cast<ElementEntitySequence*>(seq);
+    ElementSequence *elem_seq = static_cast<ElementSequence*>(seq);
       // let's be smart about this...
     temp_result = elem_seq->get_connectivity(entity_handles[i], connectivity,
                                              topological_connectivity);
@@ -905,7 +860,6 @@
                                      bool topological_connectivity,
                                      std::vector<MBEntityHandle>* storage) const
 {
-
   MBErrorCode status;
 
     // Make sure the entity should have a connectivity.
@@ -919,18 +873,19 @@
     return MB_FAILURE;
   }
   
-  MBEntitySequence* seq = 0;
+  EntitySequence* seq = 0;
 
     // We know that connectivity is stored in an EntitySequence so jump straight
     // to the entity sequence
   status = sequence_manager()->find(entity_handle, seq);
-  if (seq == 0 || status != MB_SUCCESS || !seq->is_valid_entity(entity_handle)) 
+  if (seq == 0 || status != MB_SUCCESS) 
     return MB_ENTITY_NOT_FOUND;
 
-  return static_cast<ElementEntitySequence*>(seq)->get_connectivity(entity_handle, connectivity,
-                                                                    number_nodes,
-                                                                    topological_connectivity,
-                                                                    storage);
+  return static_cast<ElementSequence*>(seq)->get_connectivity(entity_handle, 
+                                                              connectivity,
+                                                              number_nodes,
+                                                              topological_connectivity,
+                                                              storage);
 }
 
 //! set the connectivity for element handles.  For non-element handles, return an error
@@ -944,25 +899,25 @@
     // WARNING: This is very dependent on the ordering of the MBEntityType enum
   MBEntityType type = TYPE_FROM_HANDLE(entity_handle);
   
-  MBEntitySequence* seq = 0;
+  EntitySequence* seq = 0;
 
   if (type < MBVERTEX || type > MBENTITYSET)
     return MB_TYPE_OUT_OF_RANGE;
   
   status = sequence_manager()->find(entity_handle, seq);
-  if (seq == 0 || status != MB_SUCCESS || !seq->is_valid_entity(entity_handle))
+  if (seq == 0 || status != MB_SUCCESS)
     return (status != MB_SUCCESS ? status : MB_ENTITY_NOT_FOUND);
 
   const MBEntityHandle* old_conn;
   int len;
-  status = static_cast<ElementEntitySequence*>(seq)->get_connectivity(entity_handle, old_conn, len);
+  status = static_cast<ElementSequence*>(seq)->get_connectivity(entity_handle, old_conn, len);
   if (status != MB_SUCCESS) return status;
 
   aEntityFactory->notify_change_connectivity(
     entity_handle, old_conn, connect, num_connect);
   
-  status = static_cast<ElementEntitySequence*>(seq)->set_connectivity(entity_handle, 
-                                                                      connect, num_connect);
+  status = static_cast<ElementSequence*>(seq)->set_connectivity(entity_handle, 
+                                                                connect, num_connect);
   if (status != MB_SUCCESS) 
     aEntityFactory->notify_change_connectivity(
       entity_handle, connect, old_conn, num_connect);
@@ -1133,23 +1088,22 @@
 {
   MBErrorCode result = MB_SUCCESS;
   if (meshset) {
-    MBEntitySequence* seq;
+    EntitySequence* seq;
     result = sequence_manager()->find( meshset, seq );
     if (MB_SUCCESS != result)
       return result;
     MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
-    result = mseq->get_dimension( meshset, dimension, entities, recursive );
+    result = mseq->get_dimension( sequence_manager(), meshset, dimension, entities, recursive );
   }
   else if (dimension > 3) {
-    result = sequence_manager()->get_entities( MBENTITYSET, entities );
+    sequence_manager()->get_entities( MBENTITYSET, entities );
+    result = MB_SUCCESS;
   } 
   else {
     for (MBEntityType this_type = MBCN::TypeDimensionMap[dimension].first;
          this_type <= MBCN::TypeDimensionMap[dimension].second;
          this_type++) {
-      result = sequence_manager()->get_entities( this_type, entities );
-      if (MB_SUCCESS != result)
-        break;
+      sequence_manager()->get_entities( this_type, entities );
     }
   }
 
@@ -1166,15 +1120,17 @@
   
   MBErrorCode result = MB_SUCCESS;
   if (meshset) {
-    MBEntitySequence* seq;
+    EntitySequence* seq;
     result = sequence_manager()->find( meshset, seq );
     if (MB_SUCCESS != result)
       return result;
     MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
-    result = mseq->get_type( meshset, type, entities, recursive );
+    result = mseq->get_type( sequence_manager(), meshset, type, entities, recursive );
   }  
-  else
-    result = sequence_manager()->get_entities( type, entities );
+  else {
+    sequence_manager()->get_entities( type, entities );
+    result = MB_SUCCESS;
+  }
 
   return result;
 }
@@ -1223,20 +1179,17 @@
 {
   MBErrorCode result = MB_SUCCESS;
   if (meshset) {
-    MBEntitySequence* seq;
+    EntitySequence* seq;
     result = sequence_manager()->find( meshset, seq );
     if (MB_SUCCESS != result)
       return result;
     MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
-    result = mseq->get_entities( meshset, entities, recursive );
+    result = mseq->get_entities( sequence_manager(), meshset, entities, recursive );
   }  
   else {
     // iterate backards so range insertion is quicker
-    for (MBEntityType type = MBENTITYSET; type >= MBVERTEX; --type) {
-      result = sequence_manager()->get_entities( type, entities );
-      if (MB_SUCCESS != result) 
-        break;
-    }
+    for (MBEntityType type = MBENTITYSET; type >= MBVERTEX; --type)
+      sequence_manager()->get_entities( type, entities );
   }
 
   return result;
@@ -1256,7 +1209,7 @@
     std::copy( tmp_range.begin(), tmp_range.end(), entities.begin() + offset );
   }
   else {
-    MBEntitySequence* seq;
+    EntitySequence* seq;
     result = sequence_manager()->find( meshset, seq );
     if (MB_SUCCESS != result)
       return result;
@@ -1279,20 +1232,16 @@
     for (MBEntityType this_type = MBCN::TypeDimensionMap[dim].first;
          this_type <= MBCN::TypeDimensionMap[dim].second;
          this_type++) {
-      MBEntityID dummy = 0;
-      result = sequence_manager()->get_number_entities( this_type, dummy );
-      if (result != MB_SUCCESS) 
-        break;
-      number += dummy;
+      number += sequence_manager()->get_number_entities( this_type );
     }
   }
   else {
-    MBEntitySequence* seq;
+    EntitySequence* seq;
     result = sequence_manager()->find( meshset, seq );
     if (MB_SUCCESS != result)
       return result;
     MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
-    result = mseq->num_dimension( meshset, dim, number, recursive );
+    result = mseq->num_dimension( sequence_manager(), meshset, dim, number, recursive );
   }  
   
   return result;
@@ -1310,17 +1259,15 @@
     return MB_TYPE_OUT_OF_RANGE;
   
   if (meshset) {
-    MBEntitySequence* seq;
+    EntitySequence* seq;
     result = sequence_manager()->find( meshset, seq );
     if (MB_SUCCESS != result)
       return result;
     MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
-    result = mseq->num_type( meshset, type, num_ent, recursive );
+    result = mseq->num_type( sequence_manager(), meshset, type, num_ent, recursive );
   }
   else {
-    MBEntityID tmp;
-    result = sequence_manager()->get_number_entities( type, tmp );
-    num_ent = tmp;
+    num_ent = sequence_manager()->get_number_entities( type );
   }
   
   return result;
@@ -1347,12 +1294,12 @@
 {
   MBErrorCode result;
   if (meshset) {
-    MBEntitySequence* seq;
+    EntitySequence* seq;
     result = sequence_manager()->find( meshset, seq );
     if (MB_SUCCESS != result)
       return result;
     MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
-    return mseq->num_entities( meshset, num_ent, recursive );
+    return mseq->num_entities( sequence_manager(), meshset, num_ent, recursive );
   }
 
   num_ent = 0;
@@ -1401,7 +1348,7 @@
     return tagServer->set_mesh_data(tag_handle, tag_data);
 
   //verify handles
-  MBEntitySequence* seq;
+  EntitySequence* seq;
   const MBEntityHandle* iter;
   const MBEntityHandle* end = entity_handles + num_entities;
   for(iter = entity_handles; iter != end; ++iter)
@@ -1421,18 +1368,9 @@
                                     const void *tag_data)
 {
   //verify handles
-  MBRange::const_iterator iter;
-  MBErrorCode result;
-  for(iter = entity_handles.begin(); 
-      iter != entity_handles.end() && TYPE_FROM_HANDLE(*iter) != MBENTITYSET;
-      ++iter)
-  {
-    MBEntitySequence* seq = NULL;
-    result = sequenceManager->find(*iter, seq);
-    if(result != MB_SUCCESS)
-      return result;
-  }
-
+  MBErrorCode result = sequence_manager()->check_valid_entities( entity_handles );
+  if (MB_SUCCESS != result)
+    return result;
   return tagServer->set_data(tag_handle, entity_handles, tag_data);
 }
 
@@ -1757,15 +1695,13 @@
     return MB_TYPE_OUT_OF_RANGE;
 
     // Make sure both entities exist before trying to merge.
-  MBEntitySequence* seq = 0;
+  EntitySequence* seq = 0;
   MBErrorCode result, status;
   status = sequence_manager()->find(entity_to_keep, seq);
-  if(seq == 0 || status != MB_SUCCESS ||
-     !seq->is_valid_entity(entity_to_keep))
+  if(seq == 0 || status != MB_SUCCESS)
     return MB_ENTITY_NOT_FOUND;
   status = sequence_manager()->find(entity_to_remove, seq);
-  if(seq == 0 || status != MB_SUCCESS ||
-     !seq->is_valid_entity(entity_to_remove))
+  if(seq == 0 || status != MB_SUCCESS)
     return MB_ENTITY_NOT_FOUND;
   
     // If auto_merge is not set, all sub-entities should
@@ -2373,13 +2309,13 @@
 {
   if (0 == meshset) return MB_SUCCESS;
 
-  MBEntitySequence *seq;
+  EntitySequence *seq;
   MBErrorCode rval = sequence_manager()->find( meshset, seq );
-  if (MB_SUCCESS != rval || !seq->is_valid_entity(meshset))
+  if (MB_SUCCESS != rval)
     return MB_ENTITY_NOT_FOUND;
   MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
 
-  return mseq->get_parents( meshset, parents, num_hops );
+  return mseq->get_parents( sequence_manager(), meshset, parents, num_hops );
 }
 
 MBErrorCode MBCore::get_parent_meshsets(const MBEntityHandle meshset,
@@ -2402,13 +2338,13 @@
 {
   if (0 == meshset) return MB_SUCCESS;
 
-  MBEntitySequence *seq;
+  EntitySequence *seq;
   MBErrorCode rval = sequence_manager()->find( meshset, seq );
-  if (MB_SUCCESS != rval || !seq->is_valid_entity(meshset))
+  if (MB_SUCCESS != rval)
     return MB_ENTITY_NOT_FOUND;
   MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
 
-  return mseq->get_children( meshset, children, num_hops );
+  return mseq->get_children( sequence_manager(), meshset, children, num_hops );
 }
 
 MBErrorCode MBCore::get_child_meshsets(const MBEntityHandle meshset,
@@ -2433,13 +2369,13 @@
     return MB_SUCCESS;
   }
 
-  MBEntitySequence *seq;
+  EntitySequence *seq;
   MBErrorCode rval = sequence_manager()->find( meshset, seq );
-  if (MB_SUCCESS != rval || !seq->is_valid_entity(meshset))
+  if (MB_SUCCESS != rval)
     return MB_ENTITY_NOT_FOUND;
   MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
 
-  return mseq->num_parents( meshset, *number, num_hops );
+  return mseq->num_parents( sequence_manager(), meshset, *number, num_hops );
 }
 
 MBErrorCode MBCore::num_child_meshsets(const MBEntityHandle meshset, int* number,
@@ -2450,13 +2386,13 @@
     return MB_SUCCESS;
   }
   
-  MBEntitySequence *seq;
+  EntitySequence *seq;
   MBErrorCode rval = sequence_manager()->find( meshset, seq );
-  if (MB_SUCCESS != rval || !seq->is_valid_entity(meshset))
+  if (MB_SUCCESS != rval)
     return MB_ENTITY_NOT_FOUND;
   MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
 
-  return mseq->num_children( meshset, *number, num_hops );
+  return mseq->num_children( sequence_manager(), meshset, *number, num_hops );
 }
 
 
@@ -2728,9 +2664,9 @@
 
         // now check and reverse-evaluate them
       for (MBRange::iterator rit = adjs.begin(); rit != adjs.end(); rit++) {
-        MBEntitySequence* seq = 0;
+        EntitySequence* seq = 0;
         tmp_result = sequence_manager()->find(*rit, seq);
-        if(seq == 0 || tmp_result != MB_SUCCESS || !seq->is_valid_entity(*rit)) {
+        if(seq == 0 || tmp_result != MB_SUCCESS) {
           oss << ent_str.str() << 
             "Adjacent entity " << MBCN::EntityTypeName(TYPE_FROM_HANDLE(*rit)) << " "
               << ID_FROM_HANDLE(*rit) << " is invalid." << std::endl;
@@ -2770,11 +2706,9 @@
 
 bool MBCore::is_valid(const MBEntityHandle this_ent) const
 {
-  MBEntitySequence* seq = 0;
+  EntitySequence* seq = 0;
   MBErrorCode result = sequence_manager()->find(this_ent, seq);
-  if(seq == 0 || result != MB_SUCCESS || !seq->is_valid_entity(this_ent))
-    return false;
-  else return true;
+  return seq != 0 && result == MB_SUCCESS;
 }
 
 static unsigned long get_num_entities_with_tag( TagServer* ts, 
@@ -3013,4 +2947,106 @@
   return handleUtils;
 }
 
+void MBCore::print_database() const
+{
+  MBErrorCode rval;
+  TypeSequenceManager::iterator i;
+  const TypeSequenceManager& verts = sequence_manager()->entity_map(MBVERTEX);
+  if (!verts.empty())
+    printf("  Vertex ID  X        Y        Z        Adjacencies   \n"     
+           "  ---------- -------- -------- -------- -----------...\n");
+  const MBEntityHandle* adj;
+  int nadj;
+  for (i = verts.begin(); i != verts.end(); ++i) {
+    const VertexSequence* seq = static_cast<const VertexSequence* >(*i);
+    printf("(Sequence [%d,%d] in SequenceData [%d,%d])\n",
+      (int)ID_FROM_HANDLE(seq->start_handle()),
+      (int)ID_FROM_HANDLE(seq->end_handle()),
+      (int)ID_FROM_HANDLE(seq->data()->start_handle()),
+      (int)ID_FROM_HANDLE(seq->data()->end_handle()));
+    
+    double c[3];
+    for (MBEntityHandle h = seq->start_handle(); h <= seq->end_handle(); ++h) {
+      rval = seq->get_coordinates( h, c );
+      if (MB_SUCCESS == rval)
+        printf("  %10d %8g %8g %8g", (int)ID_FROM_HANDLE(h), c[0], c[1], c[2] );
+      else
+        printf("  %10d <       ERROR %4d       >", (int)ID_FROM_HANDLE(h), (int)rval );
+ 
+      rval = a_entity_factory()->get_adjacencies( h, adj, nadj );
+      if (MB_SUCCESS != rval) {
+        printf(" <ERROR %d>\n", (int)rval );
+        continue;
+      }
+      MBEntityType pt = MBMAXTYPE;
+      for (int j = 0; j < nadj; ++j) {
+        if (TYPE_FROM_HANDLE(adj[j]) != pt) {
+          pt = TYPE_FROM_HANDLE(adj[j]);
+          printf("  %s", pt >= MBMAXTYPE ? "INVALID TYPE" : MBCN::EntityTypeName(pt) );
+        }
+        printf(" %d", (int)ID_FROM_HANDLE(adj[j]));
+      }
+      printf("\n");
+    }
+  }
+  
+  for (MBEntityType t = MBEDGE; t < MBENTITYSET; ++t) {
+    const TypeSequenceManager& elems = sequence_manager()->entity_map(t);
+    if (elems.empty())
+      continue;
+    
+    int clen = 0;
+    for (i = elems.begin(); i != elems.end(); ++i) {
+      int n = static_cast<const ElementSequence*>(*i)->nodes_per_element();
+      if (n > clen)
+        clen = n;
+    }
 
+    clen *= 5;
+    if (clen < (int)strlen("Connectivity"))
+      clen = strlen("Connectivity");
+    std::vector<char> dashes( clen, '-' );
+    dashes.push_back( '\0' );
+    printf( "  %7s ID %-*s Adjacencies\n", MBCN::EntityTypeName(t), clen, "Connectivity" );
+    printf( "  ---------- %s -----------...\n", &dashes[0] );
+    
+    std::vector<MBEntityHandle> storage;
+    const MBEntityHandle* conn;
+    int nconn;
+    for (i = elems.begin(); i != elems.end(); ++i) {
+      const ElementSequence* seq = static_cast<const ElementSequence*>(*i);
+      printf("(Sequence [%d,%d] in SequenceData [%d,%d])\n",
+        (int)ID_FROM_HANDLE(seq->start_handle()),
+        (int)ID_FROM_HANDLE(seq->end_handle()),
+        (int)ID_FROM_HANDLE(seq->data()->start_handle()),
+        (int)ID_FROM_HANDLE(seq->data()->end_handle()));
+      
+      for (MBEntityHandle h = seq->start_handle(); h <= seq->end_handle(); ++h) {
+        printf( "  %10d", (int)ID_FROM_HANDLE(h) );
+        rval = get_connectivity( h, conn, nconn, false, &storage );
+        if (MB_SUCCESS != rval) 
+          printf( "  <ERROR %2d>%*s", (int)rval, clen-10, "" );
+        else {
+          for (int j = 0; j < nconn; ++j)
+            printf(" %4d", (int)ID_FROM_HANDLE(conn[j]));
+          printf("%*s", clen - 5*nconn, "" );
+        }
+        
+        rval = a_entity_factory()->get_adjacencies( h, adj, nadj );
+        if (MB_SUCCESS != rval) {
+          printf(" <ERROR %d>\n", (int)rval );
+          continue;
+        }
+        MBEntityType pt = MBMAXTYPE;
+        for (int j = 0; j < nadj; ++j) {
+          if (TYPE_FROM_HANDLE(adj[j]) != pt) {
+            pt = TYPE_FROM_HANDLE(adj[j]);
+            printf("  %s", pt >= MBMAXTYPE ? "INVALID TYPE" : MBCN::EntityTypeName(pt) );
+          }
+          printf(" %d", (int)ID_FROM_HANDLE(adj[j]));
+        }
+        printf("\n");
+      }
+    }
+  }
+}

Modified: MOAB/trunk/MBCore.hpp
===================================================================
--- MOAB/trunk/MBCore.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBCore.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -23,7 +23,7 @@
 class MBWriteUtil;
 class MBReadUtil;
 class AEntityFactory;
-class EntitySequenceManager;
+class SequenceManager;
 class TagServer;
 class MBError;
 class MBReaderWriterSet;
@@ -877,11 +877,12 @@
   TagServer* tag_server() {return tagServer;}
 
     //! return a reference to the sequence manager
-  EntitySequenceManager* sequence_manager() { return sequenceManager; }
-  const EntitySequenceManager* sequence_manager() const { return sequenceManager; }
+  SequenceManager* sequence_manager() { return sequenceManager; }
+  const SequenceManager* sequence_manager() const { return sequenceManager; }
 
     //! return the a_entity_factory pointer
   AEntityFactory *a_entity_factory() { return aEntityFactory; }
+  const AEntityFactory *a_entity_factory() const { return aEntityFactory; }
   
     //! return set of registered IO tools
   MBReaderWriterSet* reader_writer_set() { return readerWriterSet; }
@@ -1002,6 +1003,8 @@
     //! Return the utility for dealing with entity handles
   virtual const MBHandleUtils &handle_utils() const;
 
+  void print_database() const;
+
 private:
 
   void estimated_memory_use_internal( const MBRange* ents,
@@ -1048,7 +1051,7 @@
     //! tag server for this interface
   TagServer* tagServer;
 
-  EntitySequenceManager *sequenceManager;
+  SequenceManager *sequenceManager;
 
   AEntityFactory *aEntityFactory;
   

Modified: MOAB/trunk/MBRangeSeqIntersectIter.cpp
===================================================================
--- MOAB/trunk/MBRangeSeqIntersectIter.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBRangeSeqIntersectIter.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -19,8 +19,9 @@
  */
 
 #include "MBRangeSeqIntersectIter.hpp"
-#include "EntitySequenceManager.hpp"
+#include "SequenceManager.hpp"
 #include "EntitySequence.hpp"
+#include <assert.h>
 
 MBErrorCode MBRangeSeqIntersectIter::init( MBRange::const_iterator start,
                                            MBRange::const_iterator end )
@@ -91,44 +92,24 @@
     // (reduce mEndHandle) for the current EntitySequence.
   
     // Need to update the sequence pointer?
-  if (!mSequence || mStartHandle > mSequence->get_end_handle()) {
+  if (!mSequence || mStartHandle > mSequence->end_handle()) {
   
-      // Check that the mStartHandle is not a mesh set.
-      // We don't care if mEndHandle is (yet) because the end
-      // will be trimmed to the end of the EntitySequence
-      // containing mStartHandle.
-    if (TYPE_FROM_HANDLE(mStartHandle) >= MBENTITYSET)
+      // Check that the mStartHandle is valid
+    if (TYPE_FROM_HANDLE(mStartHandle) >= MBMAXTYPE)
       return MB_TYPE_OUT_OF_RANGE;
 
     if (MB_SUCCESS != mSequenceManager->find( mStartHandle, mSequence ))
       return find_invalid_range();
-
-    freeIndex = mSequence->get_next_free_index( -1 );
-    if (freeIndex < 0) 
-      freeIndex = mSequence->number_allocated();
   }
-  
-    // Find first hole in sequence after mStartHandle
-  MBEntityID start_index = mStartHandle - mSequence->get_start_handle();
-  while (start_index > freeIndex) {
-    freeIndex = mSequence->get_next_free_index( freeIndex );
-    if (freeIndex == -1)
-      freeIndex = mSequence->number_allocated();
-  }
-  if (start_index == freeIndex) 
-    return find_deleted_range();
     
     // if mEndHandle is past end of sequence or block of used
     // handles within sequence, shorten it.
-  MBEntityID end_index = mEndHandle - mSequence->get_start_handle();
-  if(end_index >= freeIndex)
-    mEndHandle = mSequence->get_start_handle() + (freeIndex-1);
-    
+  if(mEndHandle > mSequence->end_handle())
+    mEndHandle = mSequence->end_handle();
+  
   return MB_SUCCESS;
 }
  
-typedef std::map<MBEntityHandle,MBEntitySequence*> SeqMap;
-
 MBErrorCode MBRangeSeqIntersectIter::find_invalid_range()
 {
   assert(!mSequence);
@@ -139,10 +120,10 @@
     
     // Find the next MBEntitySequence
   MBEntityType type = TYPE_FROM_HANDLE(mStartHandle);
-  const SeqMap* map = mSequenceManager->entity_map( type );
-  SeqMap::const_iterator iter = map->upper_bound( mStartHandle );
+  const TypeSequenceManager& map = mSequenceManager->entity_map( type );
+  TypeSequenceManager::const_iterator iter = map.upper_bound( mStartHandle );
     // If no next sequence of the same type
-  if (iter == map->end()) {
+  if (iter == map.end()) {
       // If end type not the same as start type, split on type
     if (type != TYPE_FROM_HANDLE( mEndHandle )) {
       int junk;
@@ -150,37 +131,12 @@
     }
   }
     // otherwise invalid range ends at min(mEndHandle, sequence start handle - 1)
-  else if (iter->second->get_start_handle() <= mEndHandle) {
-    mEndHandle = iter->second->get_start_handle()-1;
+  else if ((*iter)->start_handle() <= mEndHandle) {
+    mEndHandle = (*iter)->start_handle()-1;
   }
   
   return MB_ENTITY_NOT_FOUND;
 }
-
-MBErrorCode MBRangeSeqIntersectIter::find_deleted_range()
-{ 
-    // If we're here, then its because freeIndex == start_index
-  assert (mSequence);
-  assert (mSequence->get_start_handle() + freeIndex >= mStartHandle);
-
-    // Find the last deleted entity before mEndHandle
-  MBEntityID end_index = mEndHandle - mSequence->get_start_handle();
-  while (freeIndex < end_index) {
-    MBEntityID index = mSequence->get_next_free_index( freeIndex );
-      // If there was a break in the span of deleted entities before
-      // mEndHandle, then set mEndHandle to the end of the span of
-      // deleted entities and stop.  In the case where we've reached
-      // the end of the list (index < 0) then freeIndex will be the
-      // last index in the sequence.
-    if (index < 0 || index - freeIndex > 1) {
-      mEndHandle = mSequence->get_start_handle() + freeIndex;
-      break;
-    }
-    freeIndex = index;
-  }
-
-  return MB_ENTITY_NOT_FOUND;
-}
         
 #if MB_RANGE_SEQ_INTERSECT_ITER_STATS
 double MBRangeSeqIntersectIter::doubleNumCalls = 0;

Modified: MOAB/trunk/MBRangeSeqIntersectIter.hpp
===================================================================
--- MOAB/trunk/MBRangeSeqIntersectIter.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBRangeSeqIntersectIter.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -16,6 +16,7 @@
 /**\file MBRangeSeqIntersectIter.hpp
  *\author Jason Kraftcheck (kraftche at cae.wisc.edu)
  *\date 2006-08-11
+ *\date 2007-11-06
  */
 
 #ifndef MB_RANGE_SEQ_INTERSECT_ITER_HPP
@@ -24,48 +25,44 @@
 #include "MBTypes.h"
 #include "MBRange.hpp"
 
-class EntitySequenceManager;
-class MBEntitySequence;
+class SequenceManager;
+class EntitySequence;
 
 #define MB_RANGE_SEQ_INTERSECT_ITER_STATS 0
 
 /** \brief Iterate over the blocks of MBEntityHandles in an MBRange that
- *         are in the same MBEntitySequence.
+ *         are in the same EntitySequence.
  *
  * Iterate over an MBRange, returning blocks of entities that are the 
  * largest ranges of contiguous handles that meet one of the following
  * conditions:
- *  - All are valid handles belonging to the same MBEntitySequence
- *  - All are invalid handles that are holes in the same MBEntitySequence
- *  - All are invalid handles of the same MBEntityType and aren't holes
- *      in any MBEntitySequence.
+ *  - All are valid handles belonging to the same EntitySequence
+ *  - All are invalid handles of the same MBEntityType
  *
  * The return type from init() or step() indicates whether or not the
  * current range contains valid entities.  If the handles are either
- * all valid or all holes in an MBEntitySequence, that sequence can
+ * all valid or all holes in an EntitySequence, that sequence can
  * be obtained with get_sequence().  get_sequence() will return NULL if
- * there is no corresponding MBEntitySequence for the block of handles.
+ * there is no corresponding EntitySequence for the block of handles.
  *
- * This class keeps a data related to the 'current' MBEntitySequence and
+ * This class keeps a data related to the 'current' EntitySequence and
  * references to the MBRange pasesd to init().  Changing either of these
  * while an instance of this class is in use would be bad.
  */
 class MBRangeSeqIntersectIter {
 public:
   
-  MBRangeSeqIntersectIter( EntitySequenceManager* sequences )
+  MBRangeSeqIntersectIter( SequenceManager* sequences )
     : mSequenceManager( sequences ),
       mSequence( 0 ),
       mStartHandle( 0 ),
       mEndHandle( 0 ),
-      mLastHandle( 0 ),
-      freeIndex( 0 )
+      mLastHandle( 0 )
     { }
     
     /** Initialize iterator to first valid subset 
      *\return - MB_SUCCESS : initial position of iterator is valid
      *        - MB_ENITITY_NOT_FOUND : range contains invalid handle -- can step past by calling again
-     *        - MB_TYPE_OUT_OF_RANGE : range contains mesh sets -- cannot step past as sets are at end of range
      *        - MB_FAILURE : No entities (start == end)
      */
   MBErrorCode init( MBRange::const_iterator start, MBRange::const_iterator end );
@@ -73,7 +70,6 @@
     /** Step iterator to next range.  
      *\return - MB_SUCCESS : there is another range, and iter has been changed to it
      *        - MB_ENITITY_NOT_FOUND : range contains invalid handle -- can step past by calling again
-     *        - MB_TYPE_OUT_OF_RANGE : range contains mesh sets -- cannot step past as sets are at end of range
      *        - MB_FAILURE : at end.
      */
   MBErrorCode step();
@@ -90,7 +86,7 @@
     /** Get the MBEntitySequence for the current block.
      *  May be NULL for invaild handles.
      */
-  MBEntitySequence* get_sequence() const
+  EntitySequence* get_sequence() const
     { return mSequence; }
   
     /** Get first handle in block */
@@ -122,20 +118,12 @@
    *\return Always returns MB_ENTITY_NOT_FOUND
    */
   MBErrorCode find_invalid_range();
-  
-  /** Handle error case where mStartHandle corresponds to a hole in an
-   *  entity sequence.  Find the end of the block of invalid entities
-   *  in the sequence.
-   *\return Always returns MB_ENTITY_NOT_FOUND
-   */
-  MBErrorCode find_deleted_range();
 
-  EntitySequenceManager* mSequenceManager; //!< THE EntitySequenceManager
-  MBEntitySequence* mSequence;             //!< MBEntitySequence corresponding to current location
+  SequenceManager* mSequenceManager;       //!< THE EntitySequenceManager
+  EntitySequence* mSequence;               //!< EntitySequence corresponding to current location
   MBRange::const_pair_iterator rangeIter;  //!< Current position in MBRange.
   MBEntityHandle mStartHandle, mEndHandle; //!< Subset of current MBEntitySequence
   MBEntityHandle mLastHandle;              //!< The last of the list of all handles in the MBRange
-  MBEntityID freeIndex;                    //!< Current position in free list of mSequence
 
 #if MB_RANGE_SEQ_INTERSECT_ITER_STATS
   static double doubleNumCalls, doubleEntCount;

Modified: MOAB/trunk/MBReadUtil.cpp
===================================================================
--- MOAB/trunk/MBReadUtil.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBReadUtil.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -25,8 +25,9 @@
 #include "MBCore.hpp"
 #include "AEntityFactory.hpp"
 #include "MBError.hpp"
-#include "EntitySequenceManager.hpp"
-#include "PolyEntitySequence.hpp"
+#include "SequenceManager.hpp"
+#include "VertexSequence.hpp"
+#include "ElementSequence.hpp"
 
 #define RR if (MB_SUCCESS != result) return result
 
@@ -48,17 +49,11 @@
 {
 
   MBErrorCode error;
-  MBEntitySequence* seq = 0;
+  EntitySequence* seq = 0;
 
-  MBEntityHandle preferred_start_handle;
-  preferred_start_handle = 
-    mMB->handle_utils().create_handle(MBVERTEX, 
-                                      preferred_start_id, 
-                                      preferred_start_proc);
- 
   // create an entity sequence for these nodes 
   error = mMB->sequence_manager()->create_entity_sequence(
-    MBVERTEX, num_nodes, 0, preferred_start_handle, 
+    MBVERTEX, num_nodes, 0, preferred_start_id, 
     preferred_start_proc, actual_start_handle,
     seq);
 
@@ -67,7 +62,10 @@
 
   arrays.resize(3);
 
-  error = static_cast<VertexEntitySequence*>(seq)->get_coordinate_arrays(arrays[0], arrays[1], arrays[2]);
+  error = static_cast<VertexSequence*>(seq)->get_coordinate_arrays(arrays[0], arrays[1], arrays[2]);
+  for (unsigned i = 0; i< arrays.size(); ++i)
+    if (arrays[i])
+      arrays[i] += (actual_start_handle - seq->start_handle());
   
   return error;
 }
@@ -83,10 +81,10 @@
 {
 
   MBErrorCode error;
-  MBEntitySequence* seq;
+  EntitySequence* seq;
   
-  if (mdb_type <= MBVERTEX || mdb_type >= MBPOLYHEDRON || mdb_type == MBPOLYGON)
-    return MB_TYPE_OUT_OF_RANGE;
+//  if (mdb_type <= MBVERTEX || mdb_type >= MBPOLYHEDRON || mdb_type == MBPOLYGON)
+//    return MB_TYPE_OUT_OF_RANGE;
 
   // make an entity sequence to hold these elements
   unsigned proc_id = proc < 0 ? parallel_rank() : proc;
@@ -97,43 +95,16 @@
     return error;
 
   // get an array for the connectivity
-  error = static_cast<ElementEntitySequence*>(seq)->get_connectivity_array(array);
+  array = static_cast<ElementSequence*>(seq)->get_connectivity_array();
+  if (!array)
+    return MB_FAILURE;
+  array += (actual_start_handle - seq->start_handle()) 
+         * static_cast<ElementSequence*>(seq)->nodes_per_element();
 
   return error;
   
 }
 
-MBErrorCode MBReadUtil::get_poly_element_array(
-      const int num_poly, 
-      const int conn_list_length,
-      const MBEntityType mdb_type,
-      const int preferred_start_id, 
-      const int preferred_start_proc,
-      MBEntityHandle& actual_start_handle, 
-      int*& last_index_array,
-      MBEntityHandle*& connectivity_array )
-{
-
-  MBErrorCode error;
-  MBEntitySequence* seq;
-  
-  if (mdb_type != MBPOLYGON && mdb_type != MBPOLYHEDRON)
-    return MB_TYPE_OUT_OF_RANGE;
-
-  error = mMB->sequence_manager()->create_entity_sequence(
-      mdb_type, num_poly, conn_list_length, preferred_start_id, 
-      preferred_start_proc, actual_start_handle, seq);
-  if (MB_SUCCESS != error || NULL == seq)
-    return error;
-
-  PolyEntitySequence *pseq = dynamic_cast<PolyEntitySequence*>(seq);
-  assert(NULL != pseq);
-  
-  pseq->get_connectivity_array( connectivity_array );
-  pseq->get_index_array( last_index_array );
-  return MB_SUCCESS;
-}
-
 MBErrorCode MBReadUtil::create_entity_sets( MBEntityID num_sets,
                                             const unsigned* flags ,
                                             MBEntityID start_id,
@@ -141,14 +112,13 @@
                                             MBEntityHandle& start_handle )
 {
   MBErrorCode error;
-  MBEntitySequence* seq;
+  EntitySequence* seq;
   error = mMB->sequence_manager()->create_meshset_sequence( num_sets,
                                                             start_id,
                                                             proc,
                                                             flags,
+                                                            start_handle,
                                                             seq );
-  if (seq)
-    start_handle = seq->get_start_handle();
   return error;
 }
 

Modified: MOAB/trunk/MBReadUtil.hpp
===================================================================
--- MOAB/trunk/MBReadUtil.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBReadUtil.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -77,31 +77,6 @@
                                   MBRange &related_ents,
                                   MBRange *all_sets);
   
-  /** Allocate storage for poly (polygon or polyhedron elements) 
-   * 
-   * Allocate storage for poly (polygon or polyhedron elements) and
-   * return connectivity arrays for direct read into memory.
-   *
-   *\param num_poly            The number of polygons to allocate handles for.
-   *\param conn_list_length    The total length of the connectivity list.
-   *\param mdb_type            <code>MBPOLYGON</code> or <code>MBPOLYHEDRON</code>
-   *\param preferred_start_id  Preferred integer id for first element
-   *\param actual_start_handle Actual integer id for first element (returned)
-   *\param last_index_array    Array of indices into <code>connectivity_array</code<
-   *\param connectivity_array  The connectivity array
-   *\author Jason Kraftcheck
-   */
-  MBErrorCode get_poly_element_array(
-      const int num_poly, 
-      const int conn_list_length,
-      const MBEntityType mdb_type,
-      const int preferred_start_id, 
-      const int preferred_start_proc, 
-      MBEntityHandle& actual_start_handle, 
-      int*& last_index_array,
-      MBEntityHandle*& connectivity_array
-      );
-
   MBErrorCode create_entity_sets(
     MBEntityID num_sets,
     const unsigned* set_flags,

Modified: MOAB/trunk/MBReadUtilIface.hpp
===================================================================
--- MOAB/trunk/MBReadUtilIface.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBReadUtilIface.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -90,40 +90,6 @@
                                           MBRange &related_ents,
                                           MBRange *all_sets) = 0;
   
-  /** Allocate storage for poly (polygon or polyhedron elements) 
-   * 
-   * Allocate storage for poly (polygon or polyhedron elements) and
-   * return connectivity arrays for direct read into memory.
-   *
-   * The format of the poly data in memory is two lists.  The array of
-   * entity handles contains a concatenation of the connectivity lists
-   * of all the polys in the sequence, in order.  The second list contains
-   * indices into the connectivity list where each index is the index of
-   * the <em>last</em> handle for the corresponding poly.
-   *
-   * For polygons, the connectivity list contains vertex handles.  For
-   * polyhedra, it contains face (element of dimension 2) handles.
-   *
-   *\param num_poly            The number of polygons to allocate handles for.
-   *\param conn_list_length    The total length of the connectivity list.
-   *\param mdb_type            <code>MBPOLYGON</code> or <code>MBPOLYHEDRON</code>
-   *\param preferred_start_id  Preferred integer id for first element
-   *\param actual_start_handle Actual integer id for first element (returned)
-   *\param last_index_array    Array of indices into <code>connectivity_array</code<
-   *\param connectivity_array  The connectivity array
-   *\author Jason Kraftcheck
-   */
-  virtual MBErrorCode get_poly_element_array(
-      const int num_poly, 
-      const int conn_list_length,
-      const MBEntityType mdb_type,
-      const int preferred_start_id, 
-      const int preferred_start_proc, 
-      MBEntityHandle& actual_start_handle, 
-      int*& last_index_array,
-      MBEntityHandle*& connectivity_array 
-   ) = 0;
-   
   virtual MBErrorCode create_entity_sets(
     MBEntityID num_sets,
     const unsigned* set_flags,

Modified: MOAB/trunk/MBTest.cpp
===================================================================
--- MOAB/trunk/MBTest.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBTest.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -47,7 +47,7 @@
 #endif
 #include "MBInternals.hpp"
 #include "MBCore.hpp"
-#include "EntitySequenceManager.hpp"
+#include "SequenceManager.hpp"
 #include "EntitySequence.hpp"
 #include "MBRangeSeqIntersectIter.hpp"
 
@@ -2254,12 +2254,13 @@
   result = MB->get_connectivity(&entity_vec[0], entity_vec.size(), nodes);
   if(result != MB_SUCCESS ) 
     return result;
+  assert( nodes.size() == 4 * temp_range.size() );
   temp_vec.clear(); 
   std::vector<double> coords(3*nodes.size());
   result = MB->get_coords(&nodes[0], nodes.size(), &coords[0]);
   if(result != MB_SUCCESS ) 
     return result;
-   
+  
   unsigned int k = 0;
   for(MBRange::iterator it = temp_range.begin(); it != temp_range.end(); it++) {
     if( coords[12*k] == 2.5 && coords[12*k+3] == 2.5 &&
@@ -3998,6 +3999,11 @@
   
   for (this_type = MBEDGE; this_type != MBKNIFE; this_type++) {
     
+      // skip remainder of the loop for POLYGONS and POLYHEDRA, which don't follow
+      // the standard canonical ordering 
+    if (this_type == MBPOLYGON || this_type == MBPOLYHEDRON)
+      continue;
+    
       // make an entity of this type
     result = MB->create_element(this_type, vertex_handles, 
                                 MBCN::VerticesPerEntity(this_type),
@@ -4007,11 +4013,6 @@
            << MBCN::EntityTypeName(this_type) << endl;
       return MB_FAILURE;
     }
-    
-      // skip remainder of the loop for POLYGONS and POLYHEDRA, which don't follow
-      // the standard canonical ordering 
-    if (this_type == MBPOLYGON || this_type == MBPOLYHEDRON)
-      continue;
 
       // now get the connectivity vector *
     const MBEntityHandle *entity_vertices;
@@ -4667,12 +4668,12 @@
 MBErrorCode mb_range_seq_intersect_test( MBInterface*) 
 {
   MBErrorCode rval;
-  EntitySequenceManager sequences(MBHandleUtils( 0, 1 ));
+  SequenceManager sequences(MBHandleUtils( 0, 1 ));
   MBRangeSeqIntersectIter iter( &sequences );
   MBRange range;
 
     // create some entity sequences
-  MBEntitySequence *ts1, *ts2, *ts3, *qs1;
+  EntitySequence *ts1, *ts2, *ts3, *qs1;
   MBEntityHandle th1, th2, th3, qh1;
   const int nt1 = 100, nt2 = 10, nt3 = 1, nq1 = 20;
   rval = sequences.create_entity_sequence( MBTRI, nt1, 3, 5, 0, th1, ts1 );
@@ -4690,10 +4691,10 @@
   
     // construct an MBRange containing all valid handles;
   range.clear();
-  ts1->get_entities( range );
-  ts2->get_entities( range );
-  ts3->get_entities( range );
-  qs1->get_entities( range );
+  range.insert( ts1->start_handle(), ts1->end_handle() );
+  range.insert( ts2->start_handle(), ts2->end_handle() );
+  range.insert( ts3->start_handle(), ts3->end_handle() );
+  range.insert( qs1->start_handle(), qs1->end_handle() );
   
     // iterate over all and check results
 
@@ -4702,9 +4703,9 @@
     return rval;
   if (ts1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts1->get_start_handle())
+  if (iter.get_start_handle() != ts1->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts1->get_end_handle())
+  if (iter.get_end_handle() != ts1->end_handle())
     return MB_FAILURE;
 
   rval = iter.step();
@@ -4712,9 +4713,9 @@
     return rval;
   if (ts2 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts2->get_start_handle())
+  if (iter.get_start_handle() != ts2->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts2->get_end_handle())
+  if (iter.get_end_handle() != ts2->end_handle())
     return MB_FAILURE;
 
   rval = iter.step();
@@ -4722,9 +4723,9 @@
     return rval;
   if (ts3 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts3->get_start_handle())
+  if (iter.get_start_handle() != ts3->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts3->get_end_handle())
+  if (iter.get_end_handle() != ts3->end_handle())
     return MB_FAILURE;
 
   rval = iter.step();
@@ -4732,9 +4733,9 @@
     return rval;
   if (qs1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_start_handle())
+  if (iter.get_start_handle() != qs1->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != qs1->get_end_handle())
+  if (iter.get_end_handle() != qs1->end_handle())
     return MB_FAILURE;
   
   if (!iter.is_at_end())
@@ -4750,9 +4751,9 @@
     return rval;
   if (qs1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_start_handle())
+  if (iter.get_start_handle() != qs1->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != qs1->get_end_handle())
+  if (iter.get_end_handle() != qs1->end_handle())
     return MB_FAILURE;
   
   if (!iter.is_at_end())
@@ -4771,9 +4772,9 @@
     return rval;
   if (ts1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts1->get_start_handle() + 1)
+  if (iter.get_start_handle() != ts1->start_handle() + 1)
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts1->get_end_handle())
+  if (iter.get_end_handle() != ts1->end_handle())
     return MB_FAILURE;
 
   rval = iter.step();
@@ -4781,9 +4782,9 @@
     return rval;
   if (ts2 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts2->get_start_handle())
+  if (iter.get_start_handle() != ts2->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts2->get_end_handle())
+  if (iter.get_end_handle() != ts2->end_handle())
     return MB_FAILURE;
   
   if (!iter.is_at_end())
@@ -4800,9 +4801,9 @@
     return rval;
   if (qs1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_start_handle() + 1)
+  if (iter.get_start_handle() != qs1->start_handle() + 1)
     return MB_FAILURE;
-  if (iter.get_end_handle() != qs1->get_end_handle() - 1)
+  if (iter.get_end_handle() != qs1->end_handle() - 1)
     return MB_FAILURE;
   
   if (!iter.is_at_end())
@@ -4814,7 +4815,7 @@
     // Iterate over two subsets of the quad sequence
     
   MBRange quads = range.subset_by_type( MBQUAD );
-  MBEntityHandle removed = qs1->get_start_handle() + nq1/2;
+  MBEntityHandle removed = qs1->start_handle() + nq1/2;
   if (quads.erase( removed ) == quads.end())
     return MB_FAILURE;
 
@@ -4823,7 +4824,7 @@
     return rval;
   if (qs1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_start_handle())
+  if (iter.get_start_handle() != qs1->start_handle())
     return MB_FAILURE;
   if (iter.get_end_handle() != removed - 1)
     return MB_FAILURE;
@@ -4835,7 +4836,7 @@
     return MB_FAILURE;
   if (iter.get_start_handle() != removed + 1)
     return MB_FAILURE;
-  if (iter.get_end_handle() != qs1->get_end_handle())
+  if (iter.get_end_handle() != qs1->end_handle())
     return MB_FAILURE;
   
   if (!iter.is_at_end())
@@ -4850,7 +4851,7 @@
   MBRange big;
   int junk;
   MBEntityHandle last = CREATE_HANDLE(MBQUAD+1, 0, junk);
-  big.insert( ts1->get_start_handle() - 1, last );
+  big.insert( ts1->start_handle() - 1, last );
 
     // first some invalid handles in the beginning of the range
   rval = iter.init( big.begin(), big.end() );
@@ -4860,7 +4861,7 @@
     return MB_FAILURE;
   if (iter.get_start_handle() != *big.begin())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts1->get_start_handle() - 1)
+  if (iter.get_end_handle() != ts1->start_handle() - 1)
     return MB_FAILURE;
 
     // next the first triangle sequence
@@ -4869,21 +4870,21 @@
     return rval;
   if (ts1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts1->get_start_handle())
+  if (iter.get_start_handle() != ts1->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts1->get_end_handle())
+  if (iter.get_end_handle() != ts1->end_handle())
     return MB_FAILURE;
 
     // next the the invalid handles between the first two tri sequences
-  if (ts1->get_end_handle() + 1 != ts2->get_start_handle()) {
+  if (ts1->end_handle() + 1 != ts2->start_handle()) {
     rval = iter.step();
     if (MB_ENTITY_NOT_FOUND != rval) 
       return MB_FAILURE;
     if (NULL != iter.get_sequence())
       return MB_FAILURE;
-    if (iter.get_start_handle() != ts1->get_end_handle()+1)
+    if (iter.get_start_handle() != ts1->end_handle()+1)
       return MB_FAILURE;
-    if (iter.get_end_handle() != ts2->get_start_handle()-1)
+    if (iter.get_end_handle() != ts2->start_handle()-1)
       return MB_FAILURE;
   }
 
@@ -4893,21 +4894,21 @@
     return rval;
   if (ts2 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts2->get_start_handle())
+  if (iter.get_start_handle() != ts2->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts2->get_end_handle())
+  if (iter.get_end_handle() != ts2->end_handle())
     return MB_FAILURE;
 
     // next the the invalid handles between the 2nd and 3rd tri sequences
-  if (ts2->get_end_handle() + 1 != ts3->get_start_handle()) {
+  if (ts2->end_handle() + 1 != ts3->start_handle()) {
     rval = iter.step();
     if (MB_ENTITY_NOT_FOUND != rval) 
       return MB_FAILURE;
     if (NULL != iter.get_sequence())
       return MB_FAILURE;
-    if (iter.get_start_handle() != ts2->get_end_handle()+1)
+    if (iter.get_start_handle() != ts2->end_handle()+1)
       return MB_FAILURE;
-    if (iter.get_end_handle() != ts3->get_start_handle()-1)
+    if (iter.get_end_handle() != ts3->start_handle()-1)
       return MB_FAILURE;
   }
 
@@ -4917,23 +4918,23 @@
     return rval;
   if (ts3 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts3->get_start_handle())
+  if (iter.get_start_handle() != ts3->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts3->get_end_handle())
+  if (iter.get_end_handle() != ts3->end_handle())
     return MB_FAILURE;
 
     // third tri sequence contains the MAX tri handle, so no more
     // invalid triangles.
     // next 1 invalid quad at the before MB_START_ID
-  if (ts3->get_end_handle() + 1 != qs1->get_start_handle()) {
+  if (ts3->end_handle() + 1 != qs1->start_handle()) {
     rval = iter.step();
     if (MB_ENTITY_NOT_FOUND != rval) 
       return MB_FAILURE;
     if (NULL != iter.get_sequence())
       return MB_FAILURE;
-    if (iter.get_start_handle() != ts3->get_end_handle()+1)
+    if (iter.get_start_handle() != ts3->end_handle()+1)
       return MB_FAILURE;
-    if (iter.get_end_handle() != qs1->get_start_handle()-1)
+    if (iter.get_end_handle() != qs1->start_handle()-1)
       return MB_FAILURE;
   }
 
@@ -4943,9 +4944,9 @@
     return rval;
   if (qs1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_start_handle())
+  if (iter.get_start_handle() != qs1->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != qs1->get_end_handle())
+  if (iter.get_end_handle() != qs1->end_handle())
     return MB_FAILURE;
 
     // next remaining invalid quad handles in the range
@@ -4954,7 +4955,7 @@
     return MB_FAILURE;
   if (0 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_end_handle() + 1)
+  if (iter.get_start_handle() != qs1->end_handle() + 1)
     return MB_FAILURE;
   if (iter.get_end_handle() != last - 1)
     return MB_FAILURE;
@@ -4980,12 +4981,15 @@
   
   
     // Create some holes
-  
-  MBEntityHandle dead1 = ts1->get_start_handle() + 1;
-  MBEntityHandle dead2 = ts1->get_start_handle() + 2;
-  MBEntityHandle dead3 = ts2->get_start_handle();
-  MBEntityHandle dead4 = ts2->get_end_handle();
-  MBEntityHandle dead5 = qs1->get_start_handle() + nq1/2;
+  MBEntityHandle ts1s  = ts1->start_handle();
+  MBEntityHandle dead1 = ts1->start_handle() + 1;
+  MBEntityHandle dead2 = ts1->start_handle() + 2;
+  MBEntityHandle ts1e  = ts1->end_handle();
+  MBEntityHandle dead3 = ts2->start_handle();
+  MBEntityHandle dead4 = ts2->end_handle();
+  MBEntityHandle qs1s  = qs1->start_handle();
+  MBEntityHandle qs1e  = qs1->end_handle();
+  MBEntityHandle dead5 = qs1->start_handle() + nq1/2;
   MBEntityHandle dead6 = dead5+1;
   rval = sequences.delete_entity( dead1 );
   if (MB_SUCCESS != rval) return rval;
@@ -5007,9 +5011,11 @@
   rval = iter.init( range.begin(), range.end() );
   if (MB_SUCCESS != rval) 
     return rval;
-  if (ts1 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      ts1s != iter.get_sequence()->start_handle() ||
+      dead1-1 != iter.get_sequence()->end_handle())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts1->get_start_handle())
+  if (iter.get_start_handle() != ts1->start_handle())
     return MB_FAILURE;
   if (iter.get_end_handle() != dead1 - 1)
     return MB_FAILURE;
@@ -5018,7 +5024,7 @@
   rval = iter.step( );
   if (MB_ENTITY_NOT_FOUND != rval) 
     return MB_FAILURE;
-  if (ts1 != iter.get_sequence())
+  if (0 != iter.get_sequence())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead1)
     return MB_FAILURE;
@@ -5029,18 +5035,20 @@
   rval = iter.step();
   if (MB_SUCCESS != rval) 
     return rval;
-  if (ts1 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      dead2+1 != iter.get_sequence()->start_handle() ||
+      ts1e != iter.get_sequence()->end_handle())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead2+1)
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts1->get_end_handle())
+  if (iter.get_end_handle() != ts1e)
     return MB_FAILURE;
 
     // next an invalid handle at the start of the second sequence
   rval = iter.step();
   if (MB_ENTITY_NOT_FOUND != rval)
     return MB_FAILURE;
-  if (ts2 != iter.get_sequence())
+  if (0 != iter.get_sequence())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead3)
     return MB_FAILURE;
@@ -5051,6 +5059,10 @@
   rval = iter.step();
   if (MB_SUCCESS != rval)
     return rval;
+  if (0 == iter.get_sequence() ||
+      dead3+1 != iter.get_sequence()->start_handle() ||
+      dead4-1 != iter.get_sequence()->end_handle())
+    return MB_FAILURE;
   if (ts2 != iter.get_sequence())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead3+1)
@@ -5062,7 +5074,7 @@
   rval = iter.step();
   if (MB_ENTITY_NOT_FOUND != rval)
     return MB_FAILURE;
-  if (ts2 != iter.get_sequence())
+  if (0 != iter.get_sequence())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead4)
     return MB_FAILURE;
@@ -5075,18 +5087,22 @@
     return rval;
   if (ts3 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts3->get_start_handle())
+  if (iter.get_start_handle() != ts3->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts3->get_end_handle())
+  if (iter.get_end_handle() != ts3->end_handle())
     return MB_FAILURE;
 
     // next the quad sequence up to the invalid handle in the middle
   rval = iter.step();
   if (MB_SUCCESS != rval)
     return rval;
+  if (0 == iter.get_sequence() ||
+      qs1s != iter.get_sequence()->start_handle() ||
+      dead5-1 != iter.get_sequence()->end_handle())
+    return MB_FAILURE;
   if (qs1 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_start_handle())
+  if (iter.get_start_handle() != qs1s)
     return MB_FAILURE;
   if (iter.get_end_handle() != dead5-1)
     return MB_FAILURE;
@@ -5095,7 +5111,7 @@
   rval = iter.step();
   if (MB_ENTITY_NOT_FOUND != rval)
     return MB_FAILURE;
-  if (qs1 != iter.get_sequence())
+  if (0 != iter.get_sequence())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead5)
     return MB_FAILURE;
@@ -5106,11 +5122,13 @@
   rval = iter.step();
   if (MB_SUCCESS != rval)
     return rval;
-  if (qs1 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      dead6+1 != iter.get_sequence()->start_handle() ||
+      qs1e != iter.get_sequence()->end_handle())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead6+1)
     return MB_FAILURE;
-  if (iter.get_end_handle() != qs1->get_end_handle())
+  if (iter.get_end_handle() != qs1e)
     return MB_FAILURE;
   
     // now at the end
@@ -5141,9 +5159,11 @@
   rval = iter.init( range.begin(), range.end() );
   if (MB_SUCCESS != rval) 
     return rval;
-  if (ts1 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      ts1s != iter.get_sequence()->start_handle() ||
+      dead1-1 != iter.get_sequence()->end_handle())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts1->get_start_handle())
+  if (iter.get_start_handle() != ts1s)
     return MB_FAILURE;
   if (iter.get_end_handle() != dead1 - 1)
     return MB_FAILURE;
@@ -5152,18 +5172,22 @@
   rval = iter.step();
   if (MB_SUCCESS != rval) 
     return rval;
-  if (ts1 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      dead2+1 != iter.get_sequence()->start_handle() ||
+      ts1e != iter.get_sequence()->end_handle())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead2+1)
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts1->get_end_handle())
+  if (iter.get_end_handle() != ts1e)
     return MB_FAILURE;
 
     // next the second sequence between deleted start and end handles
   rval = iter.step();
   if (MB_SUCCESS != rval)
     return rval;
-  if (ts2 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      dead3+1 != iter.get_sequence()->start_handle() ||
+      dead4-1 != iter.get_sequence()->end_handle())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead3+1)
     return MB_FAILURE;
@@ -5176,18 +5200,20 @@
     return rval;
   if (ts3 != iter.get_sequence())
     return MB_FAILURE;
-  if (iter.get_start_handle() != ts3->get_start_handle())
+  if (iter.get_start_handle() != ts3->start_handle())
     return MB_FAILURE;
-  if (iter.get_end_handle() != ts3->get_end_handle())
+  if (iter.get_end_handle() != ts3->end_handle())
     return MB_FAILURE;
 
     // next the quad sequence up to the hole in the middle
   rval = iter.step();
   if (MB_SUCCESS != rval)
     return rval;
-  if (qs1 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      qs1s != iter.get_sequence()->start_handle() ||
+      dead5-1 != iter.get_sequence()->end_handle())
     return MB_FAILURE;
-  if (iter.get_start_handle() != qs1->get_start_handle())
+  if (iter.get_start_handle() != qs1s)
     return MB_FAILURE;
   if (iter.get_end_handle() != dead5-1)
     return MB_FAILURE;
@@ -5196,11 +5222,13 @@
   rval = iter.step();
   if (MB_SUCCESS != rval)
     return rval;
-  if (qs1 != iter.get_sequence())
+  if (0 == iter.get_sequence() ||
+      dead6+1 != iter.get_sequence()->start_handle() ||
+      qs1e != iter.get_sequence()->end_handle())
     return MB_FAILURE;
   if (iter.get_start_handle() != dead6+1)
     return MB_FAILURE;
-  if (iter.get_end_handle() != qs1->get_end_handle())
+  if (iter.get_end_handle() != qs1e)
     return MB_FAILURE;
   
     // now at the end

Modified: MOAB/trunk/MBWriteUtil.cpp
===================================================================
--- MOAB/trunk/MBWriteUtil.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MBWriteUtil.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -20,10 +20,11 @@
 #include "MBWriteUtil.hpp"
 #include "MBCore.hpp"
 #include "MBError.hpp"
-#include "EntitySequenceManager.hpp"
+#include "SequenceManager.hpp"
+#include "ElementSequence.hpp"
+#include "VertexSequence.hpp"
 #include "TagServer.hpp"
 #include "AEntityFactory.hpp"
-#include "PolyEntitySequence.hpp"
 #include "MBTagConventions.hpp"
 
 #include <sys/types.h>
@@ -128,9 +129,9 @@
     return MB_FAILURE;
 
   // Sequence iterators
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter, seq_end;
-  seq_iter = mMB->sequence_manager()->entity_map(MBVERTEX)->begin();
-  seq_end = mMB->sequence_manager()->entity_map(MBVERTEX)->end();
+  TypeSequenceManager::iterator seq_iter, seq_end;
+  seq_iter = mMB->sequence_manager()->entity_map(MBVERTEX).begin();
+  seq_end = mMB->sequence_manager()->entity_map(MBVERTEX).end();
   
   // loop over range, getting coordinate value
   double* output_iter = output_array;
@@ -138,9 +139,9 @@
   while (iter != end)
   {
       // Find the sqeuence containing the current handle
-    while (seq_iter != seq_end && seq_iter->second->get_end_handle() < *iter)
+    while (seq_iter != seq_end && (*seq_iter)->end_handle() < *iter)
       ++seq_iter;
-    if (seq_iter == seq_end || *iter < seq_iter->second->get_start_handle())
+    if (seq_iter == seq_end || *iter < (*seq_iter)->start_handle())
       return MB_FAILURE;
     
       // Determine how much of the sequence we want.
@@ -148,19 +149,19 @@
     MBRange::const_iterator prev(end);
     --prev;
     MBEntityHandle range_end = pair->second;
-    MBEntityHandle sequence_end = seq_iter->second->get_end_handle();
+    MBEntityHandle sequence_end = (*seq_iter)->end_handle();
     MBEntityHandle end_handle = range_end > sequence_end ? sequence_end : range_end;
     if (end_handle > *prev)
       end_handle = *prev;
     MBEntityHandle count = end_handle - *iter + 1;
     
       // Get offset in sequence to start at
-    assert( *iter >= seq_iter->second->get_start_handle() );
-    MBEntityHandle offset = *iter - seq_iter->second->get_start_handle();
+    assert( *iter >= (*seq_iter)->start_handle() );
+    MBEntityHandle offset = *iter - (*seq_iter)->start_handle();
     
       // Get coordinate arrays from sequence
     double* coord_array[3];
-    static_cast<VertexEntitySequence*>(seq_iter->second)
+    static_cast<VertexSequence*>(*seq_iter)
       ->get_coordinate_arrays( coord_array[0], coord_array[1], coord_array[2]);
     
       // Copy data to ouput buffer
@@ -201,17 +202,17 @@
   MBRange::const_iterator range_iter = elements.begin();
   MBRange::const_iterator range_iter_end = elements.end();
 
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter, seq_iter_end;
+  TypeSequenceManager::iterator seq_iter, seq_iter_end;
   MBEntityType current_type = TYPE_FROM_HANDLE(*range_iter);
  
-  seq_iter = mMB->sequence_manager()->entity_map(current_type)->begin();
-  seq_iter_end = mMB->sequence_manager()->entity_map(current_type)->end();
+  seq_iter = mMB->sequence_manager()->entity_map(current_type).begin();
+  seq_iter_end = mMB->sequence_manager()->entity_map(current_type).end();
 
   // lets find the entity sequence which holds the first entity
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter_lookahead = seq_iter;
+  TypeSequenceManager::iterator seq_iter_lookahead = seq_iter;
   seq_iter_lookahead++;
   for( ; seq_iter_lookahead != seq_iter_end && 
-      seq_iter_lookahead->second->get_start_handle() < *range_iter; )
+      (*seq_iter_lookahead)->start_handle() < *range_iter; )
   {
     ++seq_iter;
     ++seq_iter_lookahead;
@@ -225,34 +226,34 @@
   {
     // find a range that fits in the current entity sequence
     for(; range_iter_lookahead != range_iter_end && 
-        *range_iter_lookahead <= seq_iter->second->get_end_handle(); 
+        *range_iter_lookahead <= (*seq_iter)->end_handle(); 
         ++range_iter_lookahead)
     {}
   
     if(current_type != TYPE_FROM_HANDLE(*range_iter))
     {
       current_type = TYPE_FROM_HANDLE(*range_iter);
-      seq_iter = mMB->sequence_manager()->entity_map(current_type)->begin();
-      seq_iter_end = mMB->sequence_manager()->entity_map(current_type)->end();
+      seq_iter = mMB->sequence_manager()->entity_map(current_type).begin();
+      seq_iter_end = mMB->sequence_manager()->entity_map(current_type).end();
 
       // lets find the entity sequence which holds the first entity of this type
-      std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter_lookahead = seq_iter;
+      TypeSequenceManager::const_iterator seq_iter_lookahead = seq_iter;
       seq_iter_lookahead++;
       for( ; seq_iter_lookahead != seq_iter_end && 
-          seq_iter_lookahead->second->get_start_handle() < *range_iter; )
+          (*seq_iter_lookahead)->start_handle() < *range_iter; )
       {
         ++seq_iter;
         ++seq_iter_lookahead;
       }
     }
 
-    int i = static_cast<ElementEntitySequence*>(seq_iter->second)->nodes_per_element();
+    int i = static_cast<ElementSequence*>(*seq_iter)->nodes_per_element();
 
     // get the connectivity array
-    MBEntityHandle* conn_array = NULL;
-    static_cast<ElementEntitySequence*>(seq_iter->second)->get_connectivity_array(conn_array);
+    MBEntityHandle* conn_array = 
+      static_cast<ElementSequence*>(*seq_iter)->get_connectivity_array();
  
-    MBEntityHandle start_handle = seq_iter->second->get_start_handle();
+    MBEntityHandle start_handle = (*seq_iter)->start_handle();
 
     for(MBRange::const_iterator tmp_iter = range_iter; 
         tmp_iter != range_iter_lookahead;
@@ -301,7 +302,7 @@
 
 
   // Sequence iterators
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter, seq_end;
+  TypeSequenceManager::const_iterator seq_iter, seq_end;
   
   // loop over range, getting coordinate value
   MBEntityType current_type = MBMAXTYPE;
@@ -316,36 +317,36 @@
     {
       if (type >= MBENTITYSET || type < MBEDGE)
         return MB_FAILURE;
-      seq_iter = mMB->sequence_manager()->entity_map(type)->begin();
-      seq_end  = mMB->sequence_manager()->entity_map(type)->end();
+      seq_iter = mMB->sequence_manager()->entity_map(type).begin();
+      seq_end  = mMB->sequence_manager()->entity_map(type).end();
       current_type = type;
     }
     
       // Find the sqeuence containing the current handle
-    while (seq_iter != seq_end && seq_iter->second->get_end_handle() < *iter)
+    while (seq_iter != seq_end && (*seq_iter)->end_handle() < *iter)
       ++seq_iter;
-    if (seq_iter == seq_end || *iter < seq_iter->second->get_start_handle())
+    if (seq_iter == seq_end || *iter < (*seq_iter)->start_handle())
       return MB_FAILURE;
  
       // get the connectivity array
     MBEntityHandle* conn_array = NULL;
-    int conn_size = static_cast<ElementEntitySequence*>(seq_iter->second)->nodes_per_element();
-    static_cast<ElementEntitySequence*>(seq_iter->second)->get_connectivity_array(conn_array);
+    int conn_size = static_cast<ElementSequence*>(*seq_iter)->nodes_per_element();
+    conn_array = static_cast<ElementSequence*>(*seq_iter)->get_connectivity_array();
    
       // Determine how much of the sequence we want.
     MBRange::pair_iterator pair(iter);
     MBRange::const_iterator prev(end);
     --prev;
     MBEntityHandle range_end = pair->second;
-    MBEntityHandle sequence_end = seq_iter->second->get_end_handle();
+    MBEntityHandle sequence_end = (*seq_iter)->end_handle();
     MBEntityHandle end_handle = range_end > sequence_end ? sequence_end : range_end;
     if (end_handle > *prev)
       end_handle = *prev;
     MBEntityHandle count = end_handle - *iter + 1;
     
       // Get offset in sequence to start at
-    assert( *iter >= seq_iter->second->get_start_handle() );
-    MBEntityHandle offset = *iter - seq_iter->second->get_start_handle();
+    assert( *iter >= (*seq_iter)->start_handle() );
+    MBEntityHandle offset = *iter - (*seq_iter)->start_handle();
 
       // Make sure sufficient space in output array
     if (output_iter + (count * conn_size) > output_end)
@@ -395,208 +396,24 @@
 }
 
 MBErrorCode MBWriteUtil::get_poly_array_size(
-      MBRange::const_iterator iter,
-      const MBRange::const_iterator end,
-      int& connectivity_size )
+      MBRange::const_iterator ,
+      const MBRange::const_iterator ,
+      int&  )
 {
-  connectivity_size = 0;
-
-  // check the data we got
-  if(iter == end)
-    return MB_FAILURE;
-
-
-  // Sequence iterators
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter, seq_end;
-  
-  // loop over range, getting coordinate value
-  MBEntityType current_type = MBMAXTYPE;
-  while (iter != end)
-  {
-      // Make sure we have the right sequence list (and get the sequence 
-      // list for the first iteration.)
-    MBEntityType type = TYPE_FROM_HANDLE(*iter);
-    if (type != current_type)
-    {
-      if (type != MBPOLYGON && type != MBPOLYHEDRON)
-        return MB_TYPE_OUT_OF_RANGE;
-      seq_iter = mMB->sequence_manager()->entity_map(type)->begin();
-      seq_end  = mMB->sequence_manager()->entity_map(type)->end();
-      current_type = type;
-    }
-    
-      // Find the sqeuence containing the current handle
-    while (seq_iter != seq_end && seq_iter->second->get_end_handle() < *iter)
-      ++seq_iter;
-    if (seq_iter == seq_end || *iter < seq_iter->second->get_start_handle())
-      return MB_FAILURE;
- 
-      // get the index array
-    int* last_index_array;
-    PolyEntitySequence* poly_seq = dynamic_cast<PolyEntitySequence*>(seq_iter->second);
-    if (NULL == poly_seq) { assert(0); return MB_FAILURE; }
-    poly_seq->get_index_array( last_index_array );
-   
-      // Determine how much of the sequence we want.
-    MBRange::pair_iterator pair(iter);
-    MBRange::const_iterator prev(end);
-    --prev;
-    MBEntityHandle range_end = pair->second;
-    MBEntityHandle sequence_end = poly_seq->get_end_handle();
-    MBEntityHandle end_handle = range_end > sequence_end ? sequence_end : range_end;
-    if (end_handle > *prev)
-      end_handle = *prev;
-    MBEntityHandle count = end_handle - *iter + 1;
-    
-      // Get offset in sequence to start at
-    assert( *iter >= poly_seq->get_start_handle() );
-    MBEntityHandle offset = *iter - poly_seq->get_start_handle();
-
-      // Calculate the connectivity length for the subset of the current sequence
-    int prev_index = (offset == 0) ? -1 : last_index_array[offset-1];
-    int last_index = last_index_array[offset + count - 1];
-    connectivity_size += last_index - prev_index;
-
-    iter += count;
-  }
-
-  return MB_SUCCESS;
+  return MB_NOT_IMPLEMENTED;
 }
 
 MBErrorCode MBWriteUtil::get_poly_arrays(
-      MBRange::const_iterator& iter,
-      const MBRange::const_iterator end,
-      const MBTag node_id_tag,
-      size_t& element_array_len,
-      int *const element_array,
-      size_t& index_array_len,
-      int*const index_array,
-      int& index_offset )
+      MBRange::const_iterator& ,
+      const MBRange::const_iterator ,
+      const MBTag ,
+      size_t& ,
+      int *const ,
+      size_t& ,
+      int*const ,
+      int&  )
 {
-  // check the data we got
-  if (iter == end)
-    return MB_FAILURE;
-  if (index_array_len < 1 || element_array_len < 1)
-    return MB_FAILURE;
-  if (!element_array || !index_array)
-    return MB_FAILURE;
-
-  TagServer* tag_server = mMB->tag_server();
-
-  // Sequence iterators
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter, seq_end;
-  
-  // Iterators into output arrays
-  int* elem_out_iter = element_array;
-  int*const elem_out_end = element_array + element_array_len;
-  int* idx_out_iter = index_array;
-  int* idx_out_end = index_array + index_array_len;
-  
-  // loop over range, getting coordinate value
-  MBEntityType current_type = MBMAXTYPE;
-  while (iter != end && idx_out_iter != idx_out_end)
-  {
-      // Make sure we have the right sequence list (and get the sequence 
-      // list for the first iteration.)
-    MBEntityType type = TYPE_FROM_HANDLE(*iter);
-    if (type != current_type)
-    {
-      if (type != MBPOLYGON && type != MBPOLYHEDRON)
-        return MB_TYPE_OUT_OF_RANGE;
-      seq_iter = mMB->sequence_manager()->entity_map(type)->begin();
-      seq_end  = mMB->sequence_manager()->entity_map(type)->end();
-      current_type = type;
-    }
-    
-      // Find the sqeuence containing the current handle
-    while (seq_iter != seq_end && seq_iter->second->get_end_handle() < *iter)
-      ++seq_iter;
-    if (seq_iter == seq_end || *iter < seq_iter->second->get_start_handle())
-      return MB_FAILURE;
- 
-      // get the arrays
-    int* last_index_array;
-    MBEntityHandle* conn_array;
-    PolyEntitySequence* poly_seq = dynamic_cast<PolyEntitySequence*>(seq_iter->second);
-    if (NULL == poly_seq) { assert(0); return MB_FAILURE; }
-    poly_seq->get_index_array( last_index_array );
-    poly_seq->get_connectivity_array( conn_array );
-   
-      // Determine how much of the sequence we want.
-    MBRange::pair_iterator pair(iter);
-    MBRange::const_iterator prev(end);
-    --prev;
-    MBEntityHandle range_end = pair->second;
-    MBEntityHandle sequence_end = poly_seq->get_end_handle();
-    MBEntityHandle end_handle = range_end > sequence_end ? sequence_end : range_end;
-    if (end_handle > *prev)
-      end_handle = *prev;
-    MBEntityHandle count = end_handle - *iter + 1;
-
-       // Get offset in sequence to start at
-    assert( *iter >= poly_seq->get_start_handle() );
-    MBEntityHandle offset = *iter - poly_seq->get_start_handle();
-   
-      // Check if sufficient space in index array
-    if (idx_out_iter + count > idx_out_end)
-      count = idx_out_end - idx_out_iter;
-
-      // Calculate the connectivity length for the subset of the current sequence
-    int prev_index = (offset == 0) ? -1 : last_index_array[offset-1];
-    int last_index = last_index_array[offset + count - 1];
-    int conn_size = last_index - prev_index;
-    
-      // Check for sufficient space in id array
-    int space = elem_out_end - elem_out_iter;
-    if (conn_size > space)
-    {
-        // back up until the data fits.
-      int* back_iter = last_index_array + offset + count - 1;
-      int* back_end  = last_index_array + offset;
-      while (back_iter != back_end && (*back_iter - prev_index) > space)
-        --back_iter;
-        
-        // not even the first poly fits?
-      if (back_iter == back_end)
-        break;
-        
-        // update how many polys we're writing
-      count = back_iter - back_end + 1;
-      last_index = *back_iter;
-      conn_size = *back_iter - prev_index;
-    }
-    
-      // Convert element handles to global IDs and write IDs into output array
-    MBErrorCode rval = tag_server->get_data( node_id_tag, 
-                                             conn_array + prev_index + 1,
-                                             conn_size,
-                                             elem_out_iter );
-    elem_out_iter += conn_size;
-    if (MB_SUCCESS != rval)
-      return rval;
-  
-      // Copy indices into output array, offsetting as necessary
-      // offsetting must account for a) initial input offset from user,
-      // b) the offset since the last sequence and c) the offset from
-      // the start of the current sequence.
-    int curr_offset = index_offset - (prev_index + 1);
-    int *const index_end = idx_out_iter + count;
-    last_index_array += offset;
-    while (idx_out_iter != index_end)
-    {
-      *idx_out_iter = *last_index_array + curr_offset;
-      ++idx_out_iter;
-      ++last_index_array;
-    }
-    index_offset += conn_size;
-
-    iter += count;
-  }
-  
-  element_array_len = elem_out_iter - element_array;
-  index_array_len = idx_out_iter - index_array;
-
-  return MB_SUCCESS;
+  return MB_NOT_IMPLEMENTED;
 }
   
 
@@ -627,17 +444,17 @@
   MBRange::const_iterator range_iter = elements.begin();
   MBRange::const_iterator range_iter_end = elements.end();
 
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter, seq_iter_end;
+  TypeSequenceManager::const_iterator seq_iter, seq_iter_end;
   MBEntityType current_type = TYPE_FROM_HANDLE(*range_iter);
  
-  seq_iter = mMB->sequence_manager()->entity_map(current_type)->begin();
-  seq_iter_end = mMB->sequence_manager()->entity_map(current_type)->end();
+  seq_iter = mMB->sequence_manager()->entity_map(current_type).begin();
+  seq_iter_end = mMB->sequence_manager()->entity_map(current_type).end();
 
   // lets find the entity sequence which holds the first entity
-  std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter_lookahead = seq_iter;
+  TypeSequenceManager::const_iterator seq_iter_lookahead = seq_iter;
   seq_iter_lookahead++;
   for( ; seq_iter_lookahead != seq_iter_end && 
-      seq_iter_lookahead->second->get_start_handle() < *range_iter; )
+      (*seq_iter_lookahead)->start_handle() < *range_iter; )
   {
     ++seq_iter;
     ++seq_iter_lookahead;
@@ -654,34 +471,34 @@
   {
     // find a range that fits in the current entity sequence
     for(; range_iter_lookahead != range_iter_end && 
-        *range_iter_lookahead <= seq_iter->second->get_end_handle(); 
+        *range_iter_lookahead <= (*seq_iter)->end_handle(); 
         ++range_iter_lookahead)
     {}
   
     if(current_type != TYPE_FROM_HANDLE(*range_iter))
     {
       current_type = TYPE_FROM_HANDLE(*range_iter);
-      seq_iter = mMB->sequence_manager()->entity_map(current_type)->begin();
-      seq_iter_end = mMB->sequence_manager()->entity_map(current_type)->end();
+      seq_iter = mMB->sequence_manager()->entity_map(current_type).begin();
+      seq_iter_end = mMB->sequence_manager()->entity_map(current_type).end();
 
       // lets find the entity sequence which holds the first entity of this type
-      std::map<MBEntityHandle, MBEntitySequence*>::const_iterator seq_iter_lookahead = seq_iter;
+      TypeSequenceManager::const_iterator seq_iter_lookahead = seq_iter;
       seq_iter_lookahead++;
       for( ; seq_iter_lookahead != seq_iter_end && 
-          seq_iter_lookahead->second->get_start_handle() < *range_iter; )
+          (*seq_iter_lookahead)->start_handle() < *range_iter; )
       {
         ++seq_iter;
         ++seq_iter_lookahead;
       }
     }
 
-    int i = static_cast<ElementEntitySequence*>(seq_iter->second)->nodes_per_element();
+    int i = static_cast<ElementSequence*>(*seq_iter)->nodes_per_element();
 
     // get the connectivity array
     MBEntityHandle* conn_array = NULL;
-    static_cast<ElementEntitySequence*>(seq_iter->second)->get_connectivity_array(conn_array);
+    conn_array = static_cast<ElementSequence*>(*seq_iter)->get_connectivity_array();
  
-    MBEntityHandle start_handle = seq_iter->second->get_start_handle();
+    MBEntityHandle start_handle = (*seq_iter)->start_handle();
 
     for(MBRange::const_iterator tmp_iter = range_iter; 
         tmp_iter != range_iter_lookahead;

Modified: MOAB/trunk/Makefile.am
===================================================================
--- MOAB/trunk/Makefile.am	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/Makefile.am	2007-11-09 22:28:33 UTC (rev 1374)
@@ -94,10 +94,9 @@
   DenseTagCollections.cpp \
   DenseTagCollections.hpp \
   DualTool.cpp \
+  ElementSequence.hpp \
   EntitySequence.cpp \
   EntitySequence.hpp \
-  EntitySequenceManager.cpp \
-  EntitySequenceManager.hpp \
   ExoIIUtil.cpp \
   ExoIIUtil.hpp \
   FileOptions.cpp \
@@ -144,24 +143,34 @@
   MeshSetSequence.cpp \
   MeshSetSequence.hpp \
   MeshTopoUtil.cpp \
-  PolyEntitySequence.cpp \
-  PolyEntitySequence.hpp \
   ReadGmsh.cpp \
   ReadGmsh.hpp \
   ReadSTL.cpp \
   ReadSTL.hpp \
   ReadVtk.cpp \
   ReadVtk.hpp \
-  ScdElementSeq.cpp \
-  ScdElementSeq.hpp \
-  ScdVertexSeq.cpp \
-  ScdVertexSeq.hpp \
+  ScdElementData.cpp \
+  ScdElementData.hpp \
+  ScdVertexData.cpp \
+  ScdVertexData.hpp \
+  SequenceData.hpp \
+  SequenceData.cpp \
+  SequenceManager.cpp \
+  SequenceManager.hpp \
   SparseTagCollections.cpp \
   SparseTagCollections.hpp \
+  StructuredElementSeq.cpp \
+  StructuredElementSeq.hpp \
   TagServer.cpp \
   TagServer.hpp \
   Tqdcfr.cpp \
   Tqdcfr.hpp \
+  TypeSequenceManager.cpp \
+  TypeSequenceManager.hpp \
+  UnstructuredElemSeq.cpp \
+  UnstructuredElemSeq.hpp \
+  VertexSequence.hpp \
+  VertexSequence.cpp \
   VtkUtil.cpp \
   VtkUtil.hpp \
   WriteAns.cpp \
@@ -265,7 +274,7 @@
 test_adj_LDADD = $(top_builddir)/libMOAB.la
 test_adj_DEPENDENCIES = $(top_builddir)/libMOAB.la
 
-seq_man_test_SOURCES = EntitySequenceManager.cpp 
+seq_man_test_SOURCES = TestTypeSequenceManager.cpp 
 seq_man_test_LDADD = $(top_builddir)/libMOAB.la
 seq_man_test_CXXFLAGS = -DTEST $(CXXFLAGS)
 

Modified: MOAB/trunk/MeshSetSequence.cpp
===================================================================
--- MOAB/trunk/MeshSetSequence.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MeshSetSequence.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -19,299 +19,143 @@
  */
 
 #include "MeshSetSequence.hpp"
-#include "EntitySequenceManager.hpp"
+#include "SequenceManager.hpp"
 
-MeshSetSequence::MeshSetSequence( EntitySequenceManager* man,
-                                  MBEntityHandle start_handle,
-                                  MBEntityID num_entities,
-                                  unsigned flags )
-  : MBEntitySequence( man, start_handle, num_entities )
+MeshSetSequence::MeshSetSequence( MBEntityHandle start,
+                                  MBEntityID count,
+                                  const unsigned* flags,
+                                  SequenceData* data )
+  : EntitySequence( start, count, data )
 {
-  std::vector<unsigned> flag_vect(num_entities, flags);
-  initialize( man, start_handle, num_entities, &flag_vect[0] );
+  initialize( flags );
 }
 
-MeshSetSequence::MeshSetSequence( EntitySequenceManager* man,
-                                  MBEntityHandle start_handle,
-                                  MBEntityID num_entities,
-                                  const unsigned* flags )
-  : MBEntitySequence( man, start_handle, num_entities )
+MeshSetSequence::MeshSetSequence( MBEntityHandle start,
+                                  MBEntityID count,
+                                  unsigned flags,
+                                  SequenceData* data )
+  : EntitySequence( start, count, data )
 {
-  initialize( man, start_handle, num_entities, flags );
+  std::vector<unsigned> vect( count, flags );
+  initialize( &vect[0] );
 }
 
-void MeshSetSequence::initialize( EntitySequenceManager* man,
-                                  MBEntityHandle start_handle,
-                                  MBEntityID num_entities,
-                                  const unsigned* flags )
+MeshSetSequence::MeshSetSequence( MBEntityHandle start,
+                                  MBEntityID count,
+                                  const unsigned* flags,
+                                  MBEntityID data_size )
+  : EntitySequence( start, count, new SequenceData( 1, start, start+data_size-1) )
 {
-  man->entity_sequence_created( this );
-  
-    // allocate storage
-  mSets = new unsigned char[SET_SIZE * num_entities];
-  mFreeEntities.clear();
-  if (flags) {
-    mNumEntities = num_entities;
-    mFirstFreeIndex = -1;
-    for (MBEntityID i = 0; i < num_entities; ++i) 
-      allocate_set( flags[i], i );
-  }
-  else {
-    man->notify_not_full( this );
-    mNumEntities = 0;
-    mFirstFreeIndex = 0;
-    mFreeEntities.resize( mNumAllocated, true );
-    for (MBEntityID i = 0; i < num_entities; ++i)
-      next_free(i) = i + 1;
-    next_free(num_entities-1) = -1; 
-  }
+  initialize( flags );
 }
 
-MeshSetSequence::~MeshSetSequence()
+MeshSetSequence::MeshSetSequence( MBEntityHandle start,
+                                  MBEntityID count,
+                                  unsigned flags,
+                                  MBEntityID data_size )
+  : EntitySequence( start, count, new SequenceData( 1, start, start+data_size-1) )
 {
-  mSequenceManager->entity_sequence_deleted( this );
-  if (mSets) {
-    for (MBEntityID i = 0; i < mNumAllocated; ++i) 
-      if (is_valid_entity(get_start_handle()+i)) 
-        deallocate_set( i );
-    delete [] mSets;
-  }
+  std::vector<unsigned> vect( count, flags );
+  initialize( &vect[0] );
 }
 
-MBEntityHandle MeshSetSequence::get_unused_handle()
+void MeshSetSequence::initialize( const unsigned* flags )
 {
-  return 0;
+  if (!data()->get_sequence_data(0))
+    data()->create_sequence_data( 0, SET_SIZE );
+ 
+  MBEntityID offset = start_handle() - data()->start_handle();
+  for (MBEntityID i = 0; i < size(); ++i)
+    allocate_set( flags[i], i+offset );
 }
 
-MBErrorCode MeshSetSequence::add_meshset(MBEntityHandle handle, unsigned flags) 
+MeshSetSequence::~MeshSetSequence()
 {
-  if (handle < get_start_handle() || handle > get_end_handle())
-    return MB_INDEX_OUT_OF_RANGE;
-    // check and update allocated flag
-  const MBEntityID index = handle - get_start_handle();
-  if (mFreeEntities.empty() || !mFreeEntities[index])
-    return MB_ALREADY_ALLOCATED;
-  mFreeEntities[index] = false;
-  
-    // remove from linked list of free entities
-  if (mFirstFreeIndex == index) 
-    mFirstFreeIndex = next_free(index);
-  else {
-#ifndef NDEBUG
-    int counter = 0;
-#endif
-    MBEntityID i = mFirstFreeIndex;
-    while (next_free(i) != index) {
-      assert( ++counter <= (number_allocated() - number_entities()) );
-      i = next_free(i);
-    }
-    next_free(i) = next_free(index);
-  }
-  
-    // update last-deleted index
-  if (mLastDeletedIndex == index)
-    mLastDeletedIndex = -1;
-  
-    // initialze entity set
-  allocate_set( flags, index );
-
-  mNumEntities++;
-  if (mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_full(this);
-    std::vector<bool> empty;
-    mFreeEntities.swap( empty);
-  }
-
-  return MB_SUCCESS;
+  MBEntityID offset = start_handle() - data()->start_handle();
+  MBEntityID count = size();
+  for (MBEntityID i = 0; i < count; ++i)
+    deallocate_set( i + offset );
 }
 
-MBErrorCode MeshSetSequence::allocate_handle(MBEntityHandle handle) 
+EntitySequence* MeshSetSequence::split( MBEntityHandle here )
 {
-  return MB_FAILURE;
+  return new MeshSetSequence( *this, here );
 }
 
-MBEntityHandle MeshSetSequence::add_meshset( unsigned flags )
+MBErrorCode MeshSetSequence::pop_back( MBEntityID count )
 {
-  if (mFirstFreeIndex == -1)
-    return 0;
-  const MBEntityID index = mFirstFreeIndex;
-  
-  mFreeEntities[index] = false;
-  mFirstFreeIndex = next_free(index);
-  if (mLastDeletedIndex == index) 
-    mLastDeletedIndex = -1;
-  
-  allocate_set( flags, index );
-  mNumEntities++;
-  if (mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_full(this);
-    std::vector<bool> empty;
-    mFreeEntities.swap( empty);
-  }
-  
-  return get_start_handle() + index;
+  MBEntityID offset = end_handle() + 1 - count - data()->start_handle();
+  MBErrorCode rval = EntitySequence::pop_back(count);
+  if (MB_SUCCESS == rval)
+    for (MBEntityID i = 0; i < count; ++i)
+      deallocate_set( i + offset );
+  return rval;
 }
 
-MBErrorCode MeshSetSequence::is_valid() const
+MBErrorCode MeshSetSequence::pop_front( MBEntityID count )
 {
-  if (mFirstFreeIndex >= number_allocated())
-    return MB_FAILURE;
+  MBEntityID offset = start_handle() - data()->start_handle();
+  MBErrorCode rval = EntitySequence::pop_front(count);
+  if (MB_SUCCESS == rval)
+    for (MBEntityID i = 0; i < count; ++i)
+      deallocate_set( i + offset );
+  return rval;
+}
 
-    // if entity sequence is full by any indicator, ensure
-    // all indicators are consistant
-  if (number_entities() == number_allocated() || mFreeEntities.empty() || mFirstFreeIndex < 0) {
-    if (number_entities() != number_allocated())
-      return MB_FAILURE;
-    if (!mFreeEntities.empty())
-      return MB_FAILURE;
-    if (mFirstFreeIndex >= 0)
-      return MB_FAILURE;
-    
-    return MB_SUCCESS;
-  }
-  
-    // make sure mFreeEntities is correctly allocated
-  if (mFreeEntities.size() != (size_t)number_allocated())
-    return MB_FAILURE;
-  
-    // count number of unallocated entities
-  size_t count = 0;
-  for (size_t i = 0; i < mFreeEntities.size(); ++i)
-    if (mFreeEntities[i])
-      ++count;
-  
-    // check number of free entities
-  if (count != (size_t)(number_allocated() - number_entities()))
-    return MB_FAILURE;
-  
-    // check linked list of free entities
-  for (MBEntityID i = mFirstFreeIndex; i != -1; i = next_free(i)) {
-    --count;
-    if (i < 0)
-      return MB_FAILURE;
-    if (i >= number_allocated())
-      return MB_FAILURE;
-    if (!mFreeEntities[i])
-      return MB_FAILURE;
-  }
-  
-    // check length of linked list
-  if (count != 0)
-    return MB_FAILURE;
-    
-    // check last deleted
-  if (mLastDeletedIndex != -1) {
-    if (mLastDeletedIndex < 0)
-      return MB_FAILURE;
-    if (mLastDeletedIndex >= number_allocated())
-      return MB_FAILURE;
-    if (!mFreeEntities[mLastDeletedIndex])
-      return MB_FAILURE;
-  }
-  
-  return MB_SUCCESS;
-}  
-
-void MeshSetSequence::free_handle( MBEntityHandle handle )
+MBErrorCode MeshSetSequence::push_back( MBEntityID count, const unsigned* flags )
 {
-  if (!is_valid_entity(handle))
-    return;
-  const MBEntityID index = handle - get_start_handle();
-  
-    // free any memory allocated by the MBMeshSet
-  deallocate_set( index );
-
-    // decerement count of valid entities
-  if(mNumEntities == mNumAllocated) {
-    mSequenceManager->notify_not_full(this);
-    mFreeEntities.resize( mNumAllocated, false );
-  }
-  --mNumEntities;
-
-    // mark this entity as invalid
-  mFreeEntities[index] = true;
-  
-    // Add this entity to the free list.
-    // Free list is maintained in sorted order.
-
-    // insert at beginning?
-  if (mFirstFreeIndex == -1 || mFirstFreeIndex > index) {
-    next_free(index) = mFirstFreeIndex;
-    mFirstFreeIndex = mLastDeletedIndex = index;
-    return;
-  }
-  
-    // Find entry to insert after.
-    
-  MBEntityID prev_index = mFirstFreeIndex;
-    // mLastDeletedIndex is a cache of the last deleted
-    // entity.  Used to speed up sequential deletion.
-  if (mLastDeletedIndex != -1 && mLastDeletedIndex < index)
-     prev_index = mLastDeletedIndex;
-    // Search list for location to insert at
-  MBEntityID next = next_free(prev_index);
-  while (next != -1 && next < index) {
-    prev_index = next;
-    next = next_free(next);
-  }
-    // insert in free list
-  mLastDeletedIndex = index;
-  next_free(index) = next_free(prev_index);
-  next_free(prev_index) = index;
-}  
-
-void MeshSetSequence::get_entities( MBRange& entities ) const
-{
-  MBRange::iterator iter = entities.insert(get_start_handle(), get_end_handle());
-  for(MBEntityID index = mFirstFreeIndex; index != -1; index = next_free(index))
-  {
-    iter += get_start_handle() + index - *iter;
-    iter = entities.erase(iter);
-  }
+  MBEntityID offset = end_handle() + 1 - data()->start_handle();
+  MBErrorCode rval = EntitySequence::append_entities( count );
+  if (MB_SUCCESS == rval)
+    for (MBEntityID i = 0; i < count; ++i)
+      allocate_set( flags[i], i + offset );
+  return rval;
 }
 
-MBEntityID MeshSetSequence::get_next_free_index( MBEntityID prev_free_index ) const
+MBErrorCode MeshSetSequence::push_front( MBEntityID count, const unsigned* flags )
 {
-  return prev_free_index < 0 ? mFirstFreeIndex : next_free(prev_free_index);
+  MBEntityID offset = start_handle() - data()->start_handle() - count;
+  MBErrorCode rval = EntitySequence::prepend_entities( count );
+  if (MB_SUCCESS == rval)
+    for (MBEntityID i = 0; i < count; ++i)
+      allocate_set( flags[i], i + offset );
+  return rval;
 }
 
-void MeshSetSequence::get_memory_use( unsigned long& used,
-                                      unsigned long& allocated ) const
+void MeshSetSequence::get_const_memory_use( unsigned long& per_ent,
+                                            unsigned long& seq_size ) const
 {
-  used = 0;
-  allocated = sizeof(*this) + mFreeEntities.capacity()/8;
-  allocated += mNumAllocated * SET_SIZE;
-  for (MBEntityHandle h = get_start_handle(); h <= get_end_handle(); ++h) {
-    if (is_valid_entity(h)) {
-      unsigned long m = get_set(h)->get_memory_use();
-      used += m;
-      allocated += m;
-    }
-    else {
-      allocated += SET_SIZE;
-    }
-  }
+  per_ent = SET_SIZE;
+  seq_size = sizeof(*this);
 }
 
-unsigned long MeshSetSequence::get_memory_use( MBEntityHandle h ) const
+unsigned long MeshSetSequence::get_per_entity_memory_use( MBEntityHandle first,
+                                                          MBEntityHandle last
+                                                        ) const
 {
-  return SET_SIZE + get_set(h)->get_memory_use();
+  if (first < start_handle())
+    first = start_handle();
+  if (last > end_handle())
+    last = end_handle();
+  
+  unsigned long sum = 0;
+  for (MBEntityHandle h = first; h <= last; ++h) 
+    sum += get_set(h)->get_memory_use();
+  return sum;
 }
 
-MBErrorCode MeshSetSequence::get_entities( MBEntityHandle handle,
+MBErrorCode MeshSetSequence::get_entities( const SequenceManager* seqman,
+                                           MBEntityHandle handle,
                                            MBRange& entities,
                                            bool recursive ) const
 {
-  if (!is_valid_entity(handle))
-    return MB_ENTITY_NOT_FOUND;
-  
   if (!recursive) {
     get_set(handle)->get_entities( entities );
     return MB_SUCCESS;
   }
   else {
     std::vector<MBMeshSet*> list;
-    MBErrorCode rval = recursive_get_sets( handle, list );
+    MBErrorCode rval = recursive_get_sets( handle, seqman, list );
     for (std::vector<MBMeshSet*>::iterator i = list.begin(); i != list.end(); ++i)
       (*i)->get_non_set_entities( entities );
     return rval;
@@ -321,115 +165,103 @@
 MBErrorCode MeshSetSequence::get_entities( MBEntityHandle handle,
                                 std::vector<MBEntityHandle>& entities ) const
 {
-  if (!is_valid_entity(handle))
-    return MB_ENTITY_NOT_FOUND;
-  
   get_set(handle)->get_entities( entities );
   return MB_SUCCESS;
 }
 
-MBErrorCode MeshSetSequence::get_dimension( MBEntityHandle handle,
+MBErrorCode MeshSetSequence::get_dimension( const SequenceManager* seqman,
+                                            MBEntityHandle handle,
                                             int dimension,
                                             MBRange& entities,
                                             bool recursive ) const
 {
-  if (!is_valid_entity(handle))
-    return MB_ENTITY_NOT_FOUND;
-  
   if (!recursive) {
     get_set(handle)->get_entities_by_dimension( dimension, entities );
     return MB_SUCCESS;
   }
   else {
     std::vector<MBMeshSet*> list;
-    MBErrorCode rval = recursive_get_sets( handle, list );
+    MBErrorCode rval = recursive_get_sets( handle, seqman, list );
     for (std::vector<MBMeshSet*>::iterator i = list.begin(); i != list.end(); ++i)
       (*i)->get_entities_by_dimension( dimension, entities );
     return rval;
   }
 }
 
-MBErrorCode MeshSetSequence::get_type(      MBEntityHandle handle,
+MBErrorCode MeshSetSequence::get_type(      const SequenceManager* seqman,
+                                            MBEntityHandle handle,
                                             MBEntityType type,
                                             MBRange& entities,
                                             bool recursive ) const
 {
-  if (!is_valid_entity(handle))
-    return MB_ENTITY_NOT_FOUND;
-  
   if (!recursive) {
     get_set(handle)->get_entities_by_type( type, entities );
     return MB_SUCCESS;
   }
   else {
     std::vector<MBMeshSet*> list;
-    MBErrorCode rval = recursive_get_sets( handle, list );
+    MBErrorCode rval = recursive_get_sets( handle, seqman, list );
     for (std::vector<MBMeshSet*>::iterator i = list.begin(); i != list.end(); ++i)
       (*i)->get_entities_by_type( type, entities );
     return rval;
   }
 }
 
-MBErrorCode MeshSetSequence::num_entities( MBEntityHandle handle,
+MBErrorCode MeshSetSequence::num_entities( const SequenceManager* seqman,
+                                           MBEntityHandle handle,
                                            int& number,
                                            bool recursive ) const
 {
-  if (!is_valid_entity(handle))
-    return MB_ENTITY_NOT_FOUND;
-  
   if (!recursive) {
     number = get_set(handle)->num_entities();
     return MB_SUCCESS;
   }
   else {
     MBRange range;
-    MBErrorCode result = get_entities( handle, range, true );
+    MBErrorCode result = get_entities( seqman, handle, range, true );
     number = range.size();
     return result;
   }
 }
 
-MBErrorCode MeshSetSequence::num_dimension( MBEntityHandle handle,
+MBErrorCode MeshSetSequence::num_dimension( const SequenceManager* seqman,
+                                            MBEntityHandle handle,
                                             int dimension,
                                             int& number,
                                             bool recursive ) const
 {
-  if (!is_valid_entity(handle))
-    return MB_ENTITY_NOT_FOUND;
-  
   if (!recursive) {
     number = get_set(handle)->num_entities_by_dimension(dimension);
     return MB_SUCCESS;
   }
   else {
     MBRange range;
-    MBErrorCode result = get_dimension( handle, dimension, range, true );
+    MBErrorCode result = get_dimension( seqman, handle, dimension, range, true );
     number = range.size();
     return result;
   }
 }
  
-MBErrorCode MeshSetSequence::num_type( MBEntityHandle handle,
+MBErrorCode MeshSetSequence::num_type( const SequenceManager* seqman,
+                                       MBEntityHandle handle,
                                        MBEntityType type,
                                        int& number,
                                        bool recursive ) const
 {
-  if (!is_valid_entity(handle))
-    return MB_ENTITY_NOT_FOUND;
-  
   if (!recursive) {
     number = get_set(handle)->num_entities_by_type(type);
     return MB_SUCCESS;
   }
   else {
     MBRange range;
-    MBErrorCode result = get_type( handle, type, range, true );
+    MBErrorCode result = get_type( seqman, handle, type, range, true );
     number = range.size();
     return result;
   }
 }
 
 MBErrorCode MeshSetSequence::recursive_get_sets( MBEntityHandle start_set,
+                              const SequenceManager* seq_sets,
                               std::vector<MBMeshSet*>& sets ) const
 {
   std::set<MBEntityHandle> visited;
@@ -442,16 +274,12 @@
     if (!visited.insert(handle).second)
       continue;
     
-    MBEntitySequence* seq;
-    MBErrorCode rval = mSequenceManager->find( handle, seq );
+    EntitySequence* seq;
+    MBErrorCode rval = seq_sets->find( handle, seq );
     if (MB_SUCCESS != rval)
       return rval;
     
     MeshSetSequence* mseq = reinterpret_cast<MeshSetSequence*>(seq);
-    if (!mseq->is_valid_entity(handle))
-      return MB_ENTITY_NOT_FOUND;
-    
-    
     MBMeshSet *ms_ptr = mseq->get_set( handle );
     sets.push_back( ms_ptr );
     
@@ -464,6 +292,7 @@
 }
 
 MBErrorCode MeshSetSequence::get_parent_child_meshsets( MBEntityHandle meshset,
+                                    const SequenceManager* seq_sets,
                                     std::vector<MBEntityHandle>& results,
                                     int num_hops, bool parents ) const
 {
@@ -490,12 +319,10 @@
       // for each set at the current num_hops
     for (i = lists[index].begin(); i != lists[index].end(); ++i) {
         // get meshset from handle
-      MBEntitySequence* seq;
-      MBErrorCode rval = mSequenceManager->find( *i, seq );
+      EntitySequence* seq;
+      MBErrorCode rval = seq_sets->find( *i, seq );
       if (MB_SUCCESS != rval)
         return rval;
-      if (!seq->is_valid_entity(*i))
-        return MB_ENTITY_NOT_FOUND;
       MBMeshSet *ms_ptr = reinterpret_cast<MeshSetSequence*>(seq)->get_set( *i );
       
         // querying for parents or children?
@@ -520,14 +347,12 @@
   return result;
 }
 
-MBErrorCode MeshSetSequence::get_parents( MBEntityHandle handle,
-                            std::vector<MBEntityHandle>& parents,
-                            int num_hops ) const
+MBErrorCode MeshSetSequence::get_parents( const SequenceManager* seqman,
+                                          MBEntityHandle handle,
+                                          std::vector<MBEntityHandle>& parents,
+                                          int num_hops ) const
 {
   if (num_hops == 1) {
-   if (!is_valid_entity(handle))
-      return MB_ENTITY_NOT_FOUND;
-    
     int count;
     const MBEntityHandle* array = get_set( handle )->get_parents(count);  
     if (parents.empty()) {
@@ -541,19 +366,17 @@
   }
   
   if (num_hops > 0)
-    return get_parent_child_meshsets( handle, parents, num_hops, true );
+    return get_parent_child_meshsets( handle, seqman, parents, num_hops, true );
   else
-    return get_parent_child_meshsets( handle, parents, -1, true );
+    return get_parent_child_meshsets( handle, seqman, parents, -1, true );
 }
 
-MBErrorCode MeshSetSequence::get_children( MBEntityHandle handle,
-                            std::vector<MBEntityHandle>& children,
-                            int num_hops ) const
+MBErrorCode MeshSetSequence::get_children( const SequenceManager* seqman,
+                                           MBEntityHandle handle,
+                                           std::vector<MBEntityHandle>& children,
+                                           int num_hops ) const
 {
   if (num_hops == 1) {
-    if (!is_valid_entity(handle))
-      return MB_ENTITY_NOT_FOUND;
-    
     int count;
     const MBEntityHandle* array = get_set( handle )->get_children(count);  
     if (children.empty()) {
@@ -567,44 +390,40 @@
   }
 
   if (num_hops > 0) 
-    return get_parent_child_meshsets( handle, children, num_hops, false );
+    return get_parent_child_meshsets( handle, seqman, children, num_hops, false );
   else 
-    return get_parent_child_meshsets( handle, children, -1, false );
+    return get_parent_child_meshsets( handle, seqman, children, -1, false );
 }
 
-MBErrorCode MeshSetSequence::num_parents( MBEntityHandle handle,
-                                         int& number,
-                                         int num_hops ) const
+MBErrorCode MeshSetSequence::num_parents( const SequenceManager* seqman,
+                                          MBEntityHandle handle,
+                                          int& number,
+                                          int num_hops ) const
 {
   if (num_hops == 1) {
-    if (!is_valid_entity(handle))
-      return MB_ENTITY_NOT_FOUND;
-    
     number = get_set( handle )->num_parents();
     return MB_SUCCESS;
   }
   
   std::vector<MBEntityHandle> parents;
-  MBErrorCode result = get_parents( handle, parents, num_hops );
+  MBErrorCode result = get_parents( seqman, handle, parents, num_hops );
   number = parents.size();
   return result;
 }
 
 
-MBErrorCode MeshSetSequence::num_children( MBEntityHandle handle,
-                                         int& number,
-                                         int num_hops ) const
+MBErrorCode MeshSetSequence::num_children( const SequenceManager* seqman,
+                                           MBEntityHandle handle,
+                                           int& number,
+                                           int num_hops ) const
 {
   if (num_hops == 1) {
-    if (!is_valid_entity(handle))
-      return MB_ENTITY_NOT_FOUND;
-    
     number = get_set( handle )->num_children();
     return MB_SUCCESS;
   }
   
   std::vector<MBEntityHandle> children;
-  MBErrorCode result = get_children( handle, children, num_hops );
+  MBErrorCode result = get_children( seqman, handle, children, num_hops );
   number = children.size();
   return result;
 }

Modified: MOAB/trunk/MeshSetSequence.hpp
===================================================================
--- MOAB/trunk/MeshSetSequence.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/MeshSetSequence.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -23,84 +23,101 @@
 
 #include "EntitySequence.hpp"
 #include "MBMeshSet.hpp"
+#include "SequenceData.hpp"
 
-class MeshSetSequence : public MBEntitySequence
+class SequenceManager;
+
+class MeshSetSequence : public EntitySequence
 {
 public:
 
-  MeshSetSequence( EntitySequenceManager* seq_man,
-                   MBEntityHandle start_handle,
-                   MBEntityID num_entities,
-                   unsigned set_flags );
+  MeshSetSequence( MBEntityHandle start,
+                   MBEntityID count,
+                   const unsigned* flags,
+                   SequenceData* data );
+  
+  MeshSetSequence( MBEntityHandle start,
+                   MBEntityID count,
+                   unsigned flags,
+                   SequenceData* data );
 
-  MeshSetSequence( EntitySequenceManager* seq_man,
-                   MBEntityHandle start_handle,
-                   MBEntityID num_entities,
-                   const unsigned* set_flags = 0 );
+  MeshSetSequence( MBEntityHandle start,
+                   MBEntityID count,
+                   const unsigned* flags,
+                   MBEntityID sequence_size );
+  
+  MeshSetSequence( MBEntityHandle start,
+                   MBEntityID count,
+                   unsigned flags,
+                   MBEntityID sequence_size );
 
   virtual ~MeshSetSequence();
-  virtual MBEntityHandle get_unused_handle();
-  MBEntityHandle add_meshset( unsigned flags );
-  MBErrorCode add_meshset( MBEntityHandle handle, unsigned flags );
-  virtual void free_handle( MBEntityHandle handle );
-  virtual void get_entities( MBRange& entities ) const;
-  virtual MBEntityID get_next_free_index( MBEntityID prev_free_index ) const;
-  virtual void get_memory_use( unsigned long& ,unsigned long& ) const;
-  virtual unsigned long get_memory_use( MBEntityHandle ) const;
+
+  EntitySequence* split( MBEntityHandle here );
   
-  MBMeshSet* get_set( MBEntityHandle h );
-  const MBMeshSet* get_set( MBEntityHandle h ) const;
+  SequenceData* create_data_subset( MBEntityHandle, MBEntityHandle ) const
+    { return 0; }
   
-  MBErrorCode get_entities( MBEntityHandle set, MBRange& entities, bool recursive ) const;
-  MBErrorCode get_entities( MBEntityHandle set, std::vector<MBEntityHandle>& entities ) const;
-  MBErrorCode get_dimension( MBEntityHandle set, int dim, MBRange& entities, bool recursive ) const;
-  MBErrorCode get_type( MBEntityHandle set, MBEntityType type, MBRange& entities, bool recursive ) const;
+  MBErrorCode pop_back( MBEntityID count );
+  MBErrorCode pop_front( MBEntityID count );
+  MBErrorCode push_back( MBEntityID count, const unsigned* flags );
+  MBErrorCode push_front( MBEntityID count, const unsigned* flags );
   
-  MBErrorCode num_entities( MBEntityHandle set, int& count, bool recursive ) const;
-  MBErrorCode num_dimension( MBEntityHandle set, int dim, int& count, bool recursive ) const;
-  MBErrorCode num_type( MBEntityHandle set, MBEntityType type, int& count, bool recursive ) const;
+  void get_const_memory_use( unsigned long& bytes_per_entity,
+                             unsigned long& size_of_sequence ) const;
+  unsigned long get_per_entity_memory_use( MBEntityHandle first,
+                                           MBEntityHandle last ) const;
 
-  MBErrorCode get_parents ( MBEntityHandle of, std::vector<MBEntityHandle>& parents, int num_hops ) const;
-  MBErrorCode get_children( MBEntityHandle of, std::vector<MBEntityHandle>& children, int num_hops ) const;
-  MBErrorCode num_parents ( MBEntityHandle of, int& number, int num_hops ) const;
-  MBErrorCode num_children( MBEntityHandle of, int& number, int num_hops ) const;
+
+  inline MBMeshSet* get_set( MBEntityHandle h );
+  inline const MBMeshSet* get_set( MBEntityHandle h ) const;
   
-  MBErrorCode is_valid() const;
+  MBErrorCode get_entities( MBEntityHandle set, std::vector<MBEntityHandle>& entities ) const;
+  MBErrorCode get_entities(  SequenceManager const* seqman, MBEntityHandle set,                    MBRange& entities, bool recursive ) const;
+  MBErrorCode get_dimension( SequenceManager const* seqman, MBEntityHandle set, int dim,           MBRange& entities, bool recursive ) const;
+  MBErrorCode get_type(      SequenceManager const* seqman, MBEntityHandle set, MBEntityType type, MBRange& entities, bool recursive ) const;
   
-protected:
-    //! allocate the handle passed in, effectively creating the entity
-  virtual MBErrorCode allocate_handle(MBEntityHandle handle);
+  MBErrorCode num_entities(  SequenceManager const* seqman, MBEntityHandle set,                    int& count, bool recursive ) const;
+  MBErrorCode num_dimension( SequenceManager const* seqman, MBEntityHandle set, int dim,           int& count, bool recursive ) const;
+  MBErrorCode num_type(      SequenceManager const* seqman, MBEntityHandle set, MBEntityType type, int& count, bool recursive ) const;
+
+  MBErrorCode get_parents ( SequenceManager const* seqman, MBEntityHandle of, std::vector<MBEntityHandle>& parents,  int num_hops ) const;
+  MBErrorCode get_children( SequenceManager const* seqman, MBEntityHandle of, std::vector<MBEntityHandle>& children, int num_hops ) const;
+  MBErrorCode num_parents ( SequenceManager const* seqman, MBEntityHandle of, int& number, int num_hops ) const;
+  MBErrorCode num_children( SequenceManager const* seqman, MBEntityHandle of, int& number, int num_hops ) const;
   
 private:
 
-  void initialize( EntitySequenceManager* seq_man,
-                   MBEntityHandle start_handle,
-                   MBEntityID num_entities,
-                   const unsigned* set_flags );
+  MeshSetSequence( MeshSetSequence& split_from, MBEntityHandle split_at )
+    : EntitySequence( split_from, split_at )
+    {}
+
+  void initialize( const unsigned* set_flags );
   
   MBErrorCode get_parent_child_meshsets( MBEntityHandle meshset,
+                                    SequenceManager const* set_sequences,
                                     std::vector<MBEntityHandle>& results,
                                     int num_hops, bool parents ) const;
                                     
   MBErrorCode recursive_get_sets( MBEntityHandle start_set,
+                            SequenceManager const* set_sequences,
                             std::vector<MBMeshSet*>& sets_out ) const ;
   
   enum {
     SET_SIZE = (sizeof(MBMeshSet_MBRange) > sizeof(MBMeshSet_Vector)) ?
                 sizeof(MBMeshSet_MBRange) : sizeof(MBMeshSet_Vector)
   };
-  
-  unsigned char* mSets;
-  
-  inline MBEntityID& next_free( MBEntityID index )
-    { return *reinterpret_cast<MBEntityID*>(mSets + SET_SIZE * index ); }
-  inline MBEntityID next_free( MBEntityID index ) const
-    { return *reinterpret_cast<MBEntityID*>(mSets + SET_SIZE * index ); }
+
+  inline const unsigned char* array() const
+    { return reinterpret_cast<const unsigned char*>(data()->get_sequence_data(0)); }
+
+  inline unsigned char* array()
+    { return reinterpret_cast<unsigned char*>(data()->get_sequence_data(0)); }
     
   inline void allocate_set( unsigned flags, MBEntityID index )
   {
     const bool tracking = (0 != (flags&MESHSET_TRACK_OWNER));
-    unsigned char* const ptr = mSets + index * SET_SIZE;
+    unsigned char* const ptr = array() + index * SET_SIZE;
     if (flags & MESHSET_ORDERED)
       new (ptr) MBMeshSet_Vector(tracking);
     else
@@ -109,7 +126,7 @@
     
   inline void deallocate_set( MBEntityID index ) 
   {
-    MBMeshSet* set = reinterpret_cast<MBMeshSet*>(mSets + SET_SIZE * index );
+    MBMeshSet* set = reinterpret_cast<MBMeshSet*>(array() + SET_SIZE * index );
     if (set->vector_based())
       reinterpret_cast<MBMeshSet_Vector*>(set)->~MBMeshSet_Vector();
     else
@@ -119,13 +136,11 @@
 
 inline MBMeshSet* MeshSetSequence::get_set( MBEntityHandle h )
 {
-  assert(is_valid_entity(h));
-  return reinterpret_cast<MBMeshSet*>(mSets + SET_SIZE*(h - get_start_handle()));
+  return reinterpret_cast<MBMeshSet*>(array() + SET_SIZE*(h - data()->start_handle()));
 }
 inline const MBMeshSet* MeshSetSequence::get_set( MBEntityHandle h ) const
 {
-  assert(is_valid_entity(h));
-  return reinterpret_cast<MBMeshSet*>(mSets + SET_SIZE*(h - get_start_handle()));
+  return reinterpret_cast<const MBMeshSet*>(array() + SET_SIZE*(h - data()->start_handle()));
 }
 
 #endif

Deleted: MOAB/trunk/PolyEntitySequence.cpp
===================================================================
--- MOAB/trunk/PolyEntitySequence.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/PolyEntitySequence.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,233 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-#ifdef WIN32
-#pragma warning(disable:4786)
-#endif
-
-#include "PolyEntitySequence.hpp"
-#include "EntitySequenceManager.hpp"
-#include "MBRange.hpp"
-
-#define CHUNK_SIZE 4096
-
-PolyEntitySequence::PolyEntitySequence(EntitySequenceManager* seq_manager,
-                                       MBEntityHandle start_handle, 
-                                       MBEntityID num_entities,
-                                       int nodes_per_element, bool all_handles_used,
-                                       bool allocate_connect)
-: ElementEntitySequence(seq_manager, start_handle, num_entities, 0, all_handles_used, false)
-{
-  if (allocate_connect) {
-      // for a poly sequence, the nodes_per_element is really the total # vertices/faces
-    polyConn.reserve(nodes_per_element);
-    mElements = &polyConn[0];
-    mLastIndex.reserve(num_entities);
-  }
-  
-  mNodesPerElement = nodes_per_element;
-}
-
-PolyEntitySequence::~PolyEntitySequence() 
-{
-    // set the mElement pointer to NULL to avoid deleting in ~ElementEntitySequence
-  mElements = NULL;
-}
-
-MBErrorCode PolyEntitySequence::get_connectivity(MBEntityHandle entity,
-                                                 const MBEntityHandle*& conn,
-                                                 int &num_vertices,
-                                                 const bool /*topological_connectivity*/,
-                                                 std::vector<MBEntityHandle>* ) const
-{
-  MBEntityID index = entity - mStartEntityHandle;
-  if (!is_valid_entity(entity))
-    return MB_ENTITY_NOT_FOUND;
-  
-  if (0 == index) {
-    conn = &polyConn[0];
-    num_vertices = mLastIndex[0]+1;
-  }
-  else {
-    conn = &polyConn[mLastIndex[index-1]+1];
-    num_vertices = mLastIndex[index] - mLastIndex[index-1];
-  }
-  
-  return MB_SUCCESS;
-}
-
-MBErrorCode PolyEntitySequence::get_connectivity(MBEntityHandle entity,
-                                                 std::vector<MBEntityHandle> &conn,
-                                                 const bool) const
-{
-  if (!is_valid_entity(entity)) return MB_ENTITY_NOT_FOUND;
-
-  const MBEntityHandle *this_conn;
-  int numv;
-  MBErrorCode result = get_connectivity(entity, this_conn, numv);
-  if (MB_SUCCESS != result) return result;
-  
-  conn.reserve(numv);
-  conn.insert(conn.end(), this_conn, this_conn+numv);
-  return MB_SUCCESS;
-}
-
-MBErrorCode PolyEntitySequence::get_connectivity_array(MBEntityHandle*& conn_array)
-{
-  conn_array = mElements;
-  return MB_SUCCESS;
-}
-
-MBErrorCode PolyEntitySequence::get_index_array(int*& index_array)
-{
-  index_array = &mLastIndex[0];
-  return MB_SUCCESS;
-}
-
-MBErrorCode PolyEntitySequence::add_entity(const MBEntityHandle *conn,
-                                           const int num_conn, MBEntityHandle &handle)
-{
-    // make sure input connectivity is the right type
-#ifndef NDEBUG
-  const int conn_dim = get_type() == MBPOLYGON ? 0 : 2;
-  for (const MBEntityHandle *it = conn; it < conn+num_conn; it++) {
-    if (MBCN::Dimension(TYPE_FROM_HANDLE(*it)) != conn_dim)
-      return MB_FAILURE;
-  }
-#endif
-
-  if (mNumEntities+mDeadEntities.size() == 0 || 
-      mLastIndex[mNumEntities+mDeadEntities.size()-1]+num_conn+1 > mNumAllocated) {
-      // reserve more space
-    if (mNumEntities+mDeadEntities.size() == 0) {
-      polyConn.reserve(CHUNK_SIZE);
-      mNumAllocated = CHUNK_SIZE;
-    }
-    else {
-      mNumAllocated *= 2;
-      polyConn.reserve(mNumAllocated);
-    }
-    
-      // need to re-assign mElements
-    mElements = &polyConn[0];
-  }
-
-    // check a few things: num entities agrees with index array size...
-  assert(mNumEntities+mDeadEntities.size() == mLastIndex.size());
-
-    // ... last index agrees with connectivity array size...
-  assert(0 == mNumEntities+mDeadEntities.size() || 
-         mLastIndex[mNumEntities+mDeadEntities.size()-1] == (int) polyConn.size()-1);
-  
-  handle = mStartEntityHandle + mNumEntities + mDeadEntities.size();
-
-    // add the last index to the index array
-  if (0 == mNumEntities+mDeadEntities.size()) 
-    mLastIndex.push_back(num_conn-1);
-  else
-    mLastIndex.push_back(mLastIndex[mNumEntities+mDeadEntities.size()-1]+num_conn);
-
-  mNumEntities++;
-  if (mNumEntities == mNumAllocated)  {
-    std::vector<bool> empty;
-    mFreeEntities.swap( empty );
-    mSequenceManager->notify_full(this);
-  }
-  mFreeEntities[handle - get_start_handle()] = false;
-  
-    // put the connectivity on the end of that array
-  polyConn.insert(polyConn.end(), conn, conn+num_conn);
-
-  return MB_SUCCESS;
-}
-
-MBErrorCode PolyEntitySequence::set_connectivity(MBEntityHandle entity, 
-                                                 const MBEntityHandle *conn,
-                                                 const int num_vertices)
-{
-    // only works if entity is next handle to be allocated
-  if (entity != mStartEntityHandle+mNumEntities+mDeadEntities.size())
-    return MB_NOT_IMPLEMENTED;
-
-  assert(TYPE_FROM_HANDLE(entity) == TYPE_FROM_HANDLE(mStartEntityHandle));
-  
-    // ok, it's the right one; just add an entity
-  MBEntityHandle dum_handle;
-  MBErrorCode result = add_entity(conn, num_vertices, dum_handle);
-  assert(entity == dum_handle);
-  return result;
-}
-
-void PolyEntitySequence::get_entities(MBRange& entities) const
-{
-  entities.insert(mStartEntityHandle, mStartEntityHandle+mNumEntities+mDeadEntities.size()-1);
-  for (std::vector<MBEntityHandle>::const_iterator vit = mDeadEntities.begin();
-       vit != mDeadEntities.end(); vit++)
-    entities.erase(*vit);
-}
-
-void PolyEntitySequence::free_handle(MBEntityHandle entity) 
-{
-    // add this handle to the dead list, and zero out its connectivity
-  MBEntityID index = entity - mStartEntityHandle;
-  if (!is_valid_entity(entity)) return;
-  
-  int start_index;
-  if (0 == index) start_index = 0;
-  else start_index = mLastIndex[index-1]+1;
-  for (int i = start_index; i <= mLastIndex[index]; i++)
-    mElements[i] = 0;
-
-    // now add it to the dead list
-  mDeadEntities.push_back(entity);
-  if (mFreeEntities.empty())
-    mFreeEntities.resize( mNumAllocated, false );
-  mFreeEntities[index] = true;
-
-    // decrement number of entities
-  mNumEntities--;
-}
-
-MBEntityHandle PolyEntitySequence::get_unused_handle() 
-{
-  return mNumEntities + mDeadEntities.size() + mStartEntityHandle;
-}
-
-
-void PolyEntitySequence::get_memory_use( unsigned long& used, 
-                                         unsigned long& allocated) const
-{
-  allocated = sizeof(*this)
-       + mFreeEntities.capacity() / 8
-       + polyConn.capacity() * sizeof(MBEntityHandle)
-       + mLastIndex.capacity() * sizeof(int)
-       + mDeadEntities.capacity() * sizeof(MBEntityHandle)
-       ;
-  used = 0;
-  for (MBEntityHandle h = get_start_handle(); h <= get_end_handle(); ++h)
-    if (is_valid_entity(h))
-      used += get_memory_use( h );
-}
-
-unsigned long PolyEntitySequence::get_memory_use( MBEntityHandle h ) const
-{
-    //MBEntityID id = h - get_start_handle();
-  unsigned long result = 0;
-  if (!h)
-    result = mLastIndex[h];
-  else if (h < mLastIndex.size())
-    result = mLastIndex[h] - mLastIndex[h-1];
-  return result * sizeof(MBEntityHandle) + sizeof(int);
-}

Deleted: MOAB/trunk/PolyEntitySequence.hpp
===================================================================
--- MOAB/trunk/PolyEntitySequence.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/PolyEntitySequence.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,99 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-/*!
- *  \class   PolyEntitySequence
- *  \authors Tim Tautges
- *  \date    2/04
- *  \brief   PolyEntitySequence is a sequence of polygons or polyhedra
- *        These entities represent connectivity as a list of
- *        handles, which represent vertices for polygons, or faces for polyhedra.  The
- *        sequence stores the number of vertices for each entity by storing the ending
- *        index for its connectivity array.
- *
- */ 
-
-#ifndef POLY_ENTITY_SEQUENCE_HPP
-#define POLY_ENTITY_SEQUENCE_HPP
-
-#ifndef IS_BUILDING_MB
-#error "PolyEntitySequence.hpp isn't supposed to be included into an application"
-#endif
-
-#include "EntitySequence.hpp"
-
-class PolyEntitySequence : public ElementEntitySequence
-{
-public:
-  
-  PolyEntitySequence(EntitySequenceManager* seq_manager, 
-                     MBEntityHandle start_handle, 
-                     MBEntityID num_entities,
-                     int nodes_per_element, bool all_handles_used,
-                     bool allocate_connect = true);
-  virtual ~PolyEntitySequence();
-
-  MBEntityHandle get_unused_handle();
-
-  virtual MBErrorCode get_connectivity(MBEntityHandle entity, 
-                                       std::vector<MBEntityHandle>& connectivity,
-                                       const bool topological_connectivity = false) const;
-  virtual MBErrorCode get_connectivity(MBEntityHandle entity, 
-                                       const MBEntityHandle*& connectivity,
-                                       int &num_vertices,
-                                       const bool topological_connectivity = false,
-                                       std::vector<MBEntityHandle>* storage = 0) const;
-
-  MBErrorCode set_connectivity(MBEntityHandle entity, const MBEntityHandle *conn,
-                               const int num_vertices);
-
-  virtual MBErrorCode get_connectivity_array(MBEntityHandle*& conn_array);
-  
-  MBErrorCode get_index_array(int*& index_array);
-  
-  virtual MBErrorCode split(MBEntityHandle , MBEntitySequence*& )
-    {return MB_FAILURE;}
-
-  // reallocated the sequence to hold extra/less nodes, pass in what you want, and will return whether it needed
-  // reallocate space for those nodes
-  MBErrorCode convert_realloc(bool&, bool&, bool&, 
-                              MBCore* , MBTag ) {return MB_FAILURE;}
-  
-  bool has_mid_edge_nodes() const {return false;}
-  bool has_mid_face_nodes() const {return false;}
-  bool has_mid_volume_nodes() const {return false;}
-
-  MBErrorCode add_entity(const MBEntityHandle *conn,
-                         const int num_conn, MBEntityHandle &handle);
-  
-
-  //! get entities in this range, will not add unused entities
-  virtual void get_entities(MBRange& entities) const;
-
-  virtual void free_handle(MBEntityHandle entity);
-
-  virtual void get_memory_use( unsigned long& used, unsigned long& allocated ) const;
-  virtual unsigned long get_memory_use( MBEntityHandle handle ) const;
-  
-private:
-
-  std::vector<MBEntityHandle> polyConn;
-
-  std::vector<int> mLastIndex;
-
-  std::vector<MBEntityHandle> mDeadEntities;
-};
-
-#endif

Modified: MOAB/trunk/ReadHDF5.cpp
===================================================================
--- MOAB/trunk/ReadHDF5.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/ReadHDF5.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -429,7 +429,11 @@
     return MB_FAILURE;
   }
   
-  rval = convert_id_to_handle( nodeSet, array, (size_t)(nodes_per_elem*count) );
+  if (elems.type == MBPOLYHEDRON)
+    rval = convert_id_to_handle( array, (size_t)(nodes_per_elem*count) );
+  else
+    rval = convert_id_to_handle( nodeSet, array, (size_t)(nodes_per_elem*count) );
+    
   return rval;
 }
 
@@ -438,30 +442,15 @@
   MBErrorCode rval;
   mhdf_Status status;
   char name[64];
-  
-    // Put elem set in list early so clean up code can 
-    // get rid of them if we fail.
-  ElemSet empty_set;
-  empty_set.type2 = elem_group;
-  elemList.push_back( empty_set );
-  std::list<ElemSet>::iterator it = elemList.end();
-  --it;
-  ElemSet& elems = *it;
-  
+   
   mhdf_getElemTypeName( filePtr, elem_group, name, sizeof(name), &status );
   if (mhdf_isError( &status ))
   {
     readUtil->report_error( mhdf_message( &status ) );
     return MB_FAILURE;
   }
+  MBEntityType type = MBCN::EntityTypeFromName( name );
 
-  elems.type = MBCN::EntityTypeFromName( name );
-  if (elems.type == MBMAXTYPE)
-  {
-    readUtil->report_error( "Unknown element type: \"%s\".\n", name );
-    return MB_FAILURE;
-  }
-
   long count, first_id, data_len;
   hid_t handles[2];
   mhdf_openPolyConnectivity( filePtr, elem_group, &count, &data_len,
@@ -471,57 +460,61 @@
     readUtil->report_error( mhdf_message( &status ) );
     return MB_FAILURE;
   }
-  elems.first_id = first_id;
+
+  ElemSet empty_set;
+  empty_set.type = MBCN::EntityTypeFromName( name );
+  empty_set.type2 = elem_group;
   
-  MBEntityHandle handle;
-  MBEntityHandle* conn_array;
-  int* index_array;
-  rval = readUtil->get_poly_element_array( count, data_len, elems.type, 
-                                           first_id, readUtil->parallel_rank(), 
-                                           handle, index_array, conn_array );
-  if (MB_SUCCESS != rval)
-  {
-    mhdf_closeData( filePtr, handles[0], &status );
-    mhdf_closeData( filePtr, handles[1], &status );
-    return rval;
+  MBEntityHandle h;
+  bool first = true;
+  long connend = -1;
+  std::vector<MBEntityHandle> connectivity; 
+  for (long i = 0; i < count; ++i) {
+    long prevend = connend;
+    mhdf_readPolyConnIndices( handles[0], i, 1, H5T_NATIVE_LONG, &connend, &status );
+    if (mhdf_isError( &status ))
+    {
+      readUtil->report_error( mhdf_message( &status ) );
+      mhdf_closeData( filePtr, handles[0], &status );
+      mhdf_closeData( filePtr, handles[1], &status );
+      return MB_FAILURE;
+    }
+    
+    connectivity.resize( connend - prevend );
+    mhdf_readPolyConnIDs( handles[1], prevend+1, connectivity.size(), handleType,
+                          &connectivity[0], &status );
+    if (mhdf_isError( &status ))
+    {
+      readUtil->report_error( mhdf_message( &status ) );
+      mhdf_closeData( filePtr, handles[0], &status );
+      mhdf_closeData( filePtr, handles[1], &status );
+      return MB_FAILURE;
+    }
+    
+    rval = iFace->create_element( type, &connectivity[0], connectivity.size(), h );
+    if (MB_SUCCESS != rval) 
+    {
+      mhdf_closeData( filePtr, handles[0], &status );
+      mhdf_closeData( filePtr, handles[1], &status );
+      return rval;
+    }
+    rval= convert_id_to_handle( &connectivity[0], connectivity.size() );
+    if (MB_SUCCESS != rval) 
+    {
+      mhdf_closeData( filePtr, handles[0], &status );
+      mhdf_closeData( filePtr, handles[1], &status );
+      return rval;
+    }
+    
+    if (first || elemList.back().range.back() + 1 >= h) {
+      elemList.push_back( empty_set );
+      elemList.back().first_id = first_id + i;
+      first = false;
+    }
+    elemList.back().range.insert( h );
   }
-  elems.range.insert( handle, handle + count - 1 );
-  
-  mhdf_readPolyConnIndices( handles[0], 0, count, H5T_NATIVE_INT,  
-                            index_array, &status );
-  if (mhdf_isError( &status ))
-  {
-    readUtil->report_error( mhdf_message( &status ) );
-    mhdf_closeData( filePtr, handles[0], &status );
-    mhdf_closeData( filePtr, handles[1], &status );
-    return MB_FAILURE;
-  }
-  
-  mhdf_readPolyConnIDs( handles[1], 0, data_len, handleType,
-                        conn_array, &status );
-  if (mhdf_isError( &status ))
-  {
-    readUtil->report_error( mhdf_message( &status ) );
-    mhdf_closeData( filePtr, handles[0], &status );
-    mhdf_closeData( filePtr, handles[1], &status );
-    return MB_FAILURE;
-  }
-  
-  mhdf_closeData( filePtr, handles[0], &status );
-  if (mhdf_isError( &status ))
-  {
-    readUtil->report_error( mhdf_message( &status ) );
-    mhdf_closeData( filePtr, handles[0], &status );
-    return MB_FAILURE;
-  }
-  mhdf_closeData( filePtr, handles[1], &status );
-  if (mhdf_isError( &status ))
-  {
-    readUtil->report_error( mhdf_message( &status ) );
-    return MB_FAILURE;
-  }
-  
-  return convert_id_to_handle( conn_array, (size_t)data_len );
+ 
+  return MB_SUCCESS;
 }
 
 template <typename T>

Modified: MOAB/trunk/ReadVtk.cpp
===================================================================
--- MOAB/trunk/ReadVtk.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/ReadVtk.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -261,32 +261,6 @@
   return mdbImpl->add_entities(mCurrentMeshHandle, range);
 }
 
-MBErrorCode ReadVtk::allocate_poly_elems( long num_elements,
-                                          int connectivity_length,
-                                          MBEntityType type,
-                                          MBEntityHandle& start_handle_out,
-                                          MBEntityHandle*& conn_array_out,
-                                          int*& index_array_out,
-                                          std::vector<MBRange>& append_to_this )
-{
-  MBErrorCode result;
-  
-  start_handle_out = 0;
-  result = readMeshIface->get_poly_element_array( num_elements,
-                                                  connectivity_length,
-                                                  type,
-                                                  0, 0,
-                                                  start_handle_out,
-                                                  index_array_out,
-                                                  conn_array_out );
-  if (MB_SUCCESS != result)
-    return result;
-  
-  MBRange range(start_handle_out, start_handle_out+num_elements-1);
-  append_to_this.push_back( range );
-  return mdbImpl->add_entities(mCurrentMeshHandle, range);
-}
-
 MBErrorCode ReadVtk::vtk_read_dataset( FileTokenizer& tokens,
                                        MBRange& vertex_list,
                                        std::vector<MBRange>& element_list )
@@ -513,23 +487,28 @@
   {
     case 0:
       result = MB_FAILURE;
+      break;
     case 1:
       readMeshIface->report_error( 
                        "Vertex element type at line %d",
                        tokens.line_number() );
       result = MB_FAILURE;
+      break;
     case 2:
       readMeshIface->report_error( 
                "Unsupported type: polylines at line %d",
                tokens.line_number() );
       result = MB_FAILURE;
+      break;
     case 3:
       result = vtk_read_polygons( tokens, start_handle, elem_list );
+      break;
     case 4:
       readMeshIface->report_error( 
                "Unsupported type: triangle strips at line %d",
                tokens.line_number() );
       result = MB_FAILURE;
+      break;
   }
   
   return result;
@@ -546,32 +525,41 @@
       !tokens.get_newline( ))
     return MB_FAILURE;
 
-    // Create polygons
-  MBEntityHandle first_poly = 0;
-  MBEntityHandle* conn_array = 0;
-  int* index_array = 0;
-  result = allocate_poly_elems( size[0], size[1] - size[0], MBPOLYGON,
-                                first_poly, conn_array, index_array,
-                                elem_list );
-  if (MB_SUCCESS != result)
-    return result;
-                                                  
-    // Read connectivity
-  int prev_index = -1;
-  for (int i = 0; i < size[0]; ++i)
-  {
+  const MBRange empty;
+  std::vector<MBEntityHandle> conn_hdl;
+  std::vector<long> conn_idx;
+  MBEntityHandle first = 0, prev = 0, handle;
+  for (int i = 0; i < size[0]; ++i) {
     long count;
-    if (!tokens.get_long_ints( 1, &count ) ||
-        !tokens.get_long_ints( count, (long*)conn_array ))
+    if (!tokens.get_long_ints( 1, &count ))
       return MB_FAILURE;
+    conn_idx.resize(count);
+    conn_hdl.resize(count);
+    if (!tokens.get_long_ints( count, &conn_idx[0]))
+      return MB_FAILURE;
     
-    *index_array = ( prev_index += count );
     for (long j = 0; j < count; ++j)
-    {
-      *conn_array += first_vtx;
-      ++conn_array;
+      conn_hdl[j] = first_vtx + conn_idx[j];
+    
+    result = mdbImpl->create_element( MBPOLYGON, &conn_hdl[0], count, handle );
+    if (MB_SUCCESS != result)
+      return result;
+    
+    if (prev +1 != handle) {
+      if (first) { // true except for first iteration (first == 0)
+        if (first < elem_list.back().front()) // only need new range if order would get mixed up
+          elem_list.push_back( empty );
+        elem_list.back().insert( first, prev );
+      }
+      first = handle;
     }
-  } 
+    prev = handle;
+  }
+  if (first) { // true unless no elements (size[0] == 0)
+    if (first < elem_list.back().front()) // only need new range if order would get mixed up
+      elem_list.push_back( empty );
+    elem_list.back().insert( first, prev );
+  }
   
   return MB_SUCCESS;
 }
@@ -638,51 +626,41 @@
       return MB_FAILURE;
       
     MBEntityType type = VtkUtil::vtkElemTypes[vtk_type].mb_type;
-    int num_vtx = VtkUtil::vtkElemTypes[vtk_type].num_nodes;
-    
     if (type == MBMAXTYPE) {
       readMeshIface->report_error( "Unsupported VTK element type: %s (%d)\n",
                                    VtkUtil::vtkElemTypes[vtk_type].name, vtk_type );
       return MB_FAILURE;
     }
     
+    int num_vtx = *conn_iter;
+    if (type != MBPOLYGON && num_vtx != (int)VtkUtil::vtkElemTypes[vtk_type].num_nodes) {
+      readMeshIface->report_error(
+        "Cell %ld is of type '%s' but has %u vertices.\n",
+        id, VtkUtil::vtkElemTypes[vtk_type].name, num_vtx );
+      return MB_FAILURE;
+    }
+    
       // Find any subsequent elements of the same type
-    long end_id = id + 1;
-    while ( end_id < num_elems[0] && (unsigned)types[end_id] == vtk_type)
+    std::vector<long>::iterator conn_iter2 = conn_iter + num_vtx + 1;
+    long end_id = id + 1; 
+    while ( end_id < num_elems[0] && 
+            (unsigned)types[end_id] == vtk_type &&
+            *conn_iter2 == num_vtx) {
       ++end_id;
+      conn_iter2 += num_vtx + 1;
+    }
     
       // Allocate element block
     long num_elem = end_id - id;
     MBEntityHandle start_handle = 0;
     MBEntityHandle* conn_array;
-    int* index_array = 0;
     
-    if (type == MBPOLYGON)
-    {
-        // Calculate total length of connectivity list
-      std::vector<long>::iterator conn_iter2 = conn_iter;
-      long conn_len = 0;
-      for (i = 0; i < num_elem; ++i)
-      {
-        conn_len += *conn_iter2;
-        conn_iter2 += *conn_iter2 + 1;
-      }
-      
-        // Allocate elements
-      result = allocate_poly_elems( num_elem, conn_len, type, start_handle,
-                                    conn_array, index_array, elem_list );
-    }
-    else
-    {
-      result = allocate_elements( num_elem, num_vtx, type, start_handle,
-                                  conn_array, elem_list );
-    }
-    
+    result = allocate_elements( num_elem, num_vtx, type, start_handle,
+                                conn_array, elem_list );
     if (MB_SUCCESS != result)
       return result;
 
       // Store element connectivity
-    int index_array_prev = -1;
     for ( ; id < end_id; ++id)
     {
       if (conn_iter == connectivity.end()) 
@@ -692,16 +670,9 @@
         return MB_FAILURE;
       }
 
-        // For poly elements, store end index
-      if (index_array)
+        // make sure connectivity length is correct.
+      if (*conn_iter != num_vtx)
       {
-        index_array_prev += num_vtx = *conn_iter;
-        *index_array = index_array_prev;
-        ++index_array;
-      }
-        // For non-poly elements, make sure connectivity length is correct.
-      else if (*conn_iter != num_vtx)
-      {
         readMeshIface->report_error(
           "Cell %ld is of type '%s' but has %u vertices.\n",
           id, VtkUtil::vtkElemTypes[vtk_type].name, num_vtx );

Modified: MOAB/trunk/ReadVtk.hpp
===================================================================
--- MOAB/trunk/ReadVtk.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/ReadVtk.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -61,14 +61,6 @@
                                  MBEntityHandle*& conn_array_out,
                                  std::vector<MBRange>& append_to_this );
 
-  MBErrorCode allocate_poly_elems( long num_elements,
-                                   int connectivity_length,
-                                   MBEntityType type,
-                                   MBEntityHandle& start_handle_out,
-                                   MBEntityHandle*& conn_array_out,
-                                   int*& index_array_out,
-                                   std::vector<MBRange>& append_to_this );
-
   MBErrorCode vtk_read_dataset( FileTokenizer& tokens,
                                 MBRange& vertex_list,
                                 std::vector<MBRange>& element_list );

Copied: MOAB/trunk/ScdElementData.cpp (from rev 1308, MOAB/trunk/ScdElementSeq.cpp)
===================================================================
--- MOAB/trunk/ScdElementData.cpp	                        (rev 0)
+++ MOAB/trunk/ScdElementData.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,149 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ * 
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+
+#include "ScdElementData.hpp"
+#include "ScdVertexData.hpp"
+#include "MBInterface.hpp"
+#include "MBReadUtilIface.hpp"
+#include "MBCN.hpp"
+#include "MBInternals.hpp"
+#include <assert.h>
+
+MBEntityID ScdElementData::calc_num_entities(MBEntityHandle start_handle,
+                                int irange, int jrange, int krange)
+{
+  size_t result = 1;
+  switch (MBCN::Dimension(TYPE_FROM_HANDLE(start_handle))) {
+    default: result = 0; assert( false ); 
+    case 3: result *= krange;
+    case 2: result *= jrange;
+    case 1: result *= irange;
+  }
+  return result;
+}
+
+ScdElementData::ScdElementData(
+                             MBEntityHandle start_handle,
+                             const int imin, const int jmin, const int kmin,
+                             const int imax, const int jmax, const int kmax) 
+    : SequenceData(0, start_handle,
+                   start_handle + 
+                   calc_num_entities( start_handle, imax-imin, jmax-jmin, kmax-kmin )
+                   - 1)
+{
+    // need to have meaningful parameters
+  assert(imax >= imin && jmax >= jmin && kmax >= kmin);
+    
+  elementParams[0] = HomCoord(imin, jmin, kmin);
+  elementParams[1] = HomCoord(imax, jmax, kmax);
+  elementParams[2] = HomCoord(1, 1, 1);
+  
+    // assign and compute parameter stuff
+  dIJK[0] = elementParams[1][0] - elementParams[0][0] + 1;
+  dIJK[1] = elementParams[1][1] - elementParams[0][1] + 1;
+  dIJK[2] = elementParams[1][2] - elementParams[0][2] + 1;
+  dIJKm1[0] = dIJK[0] - 1;
+  dIJKm1[1] = dIJK[1] - 1;
+  dIJKm1[2] = dIJK[2] - 1;
+}
+
+ScdElementData::~ScdElementData() 
+{
+}
+
+bool ScdElementData::boundary_complete() const
+{
+    // test the bounding vertex sequences to see if they fully define the
+    // vertex parameter space for this rectangular block of elements
+
+  int p;
+  std::vector<VertexDataRef> minlist, maxlist;
+
+    // pseudo code:
+    // for each vertex sequence v:
+  for (std::vector<VertexDataRef>::const_iterator vseq = vertexSeqRefs.begin();
+       vseq != vertexSeqRefs.end(); vseq++)
+  {
+    //   test min corner mincorner:
+    bool mincorner = true;
+    //   for each p = (i-1,j,k), (i,j-1,k), (i,j,k-1):
+    for (p = 0; p < 3; p++) {
+
+    //     for each vsequence v' != v:
+      for (std::vector<VertexDataRef>::const_iterator othervseq = vertexSeqRefs.begin();
+           othervseq != vertexSeqRefs.end(); othervseq++) 
+      {
+        if (othervseq == vseq) continue;        
+    //       if v.min-p contained in v'
+        if ((*othervseq).contains((*vseq).minmax[0]-HomCoord::unitv[p])) {
+    //         mincorner = false
+          mincorner = false;
+          break;
+        }
+      }
+      if (!mincorner) break;
+    }
+  
+    bool maxcorner = true;
+    //   for each p = (i-1,j,k), (i,j-1,k), (i,j,k-1):
+    for (p = 0; p < 3; p++) {
+
+    //     for each vsequence v' != v:
+      for (std::vector<VertexDataRef>::const_iterator othervseq = vertexSeqRefs.begin();
+           othervseq != vertexSeqRefs.end(); othervseq++) 
+      {
+        if (othervseq == vseq) continue;        
+    //       if v.max+p contained in v'
+        if ((*othervseq).contains((*vseq).minmax[1]+HomCoord::unitv[p])) {
+    //         maxcorner = false
+          maxcorner = false;
+          break;
+        }
+      }
+      if (!maxcorner) break;
+    }
+
+    //   if mincorner add to min corner list minlist
+    if (mincorner) minlist.push_back(*vseq);
+    //   if maxcorner add to max corner list maxlist
+    if (maxcorner) maxlist.push_back(*vseq);
+  }
+  
+    // 
+    // if minlist.size = 1 & maxlist.size = 1 & minlist[0] = esequence.min &
+    //         maxlist[0] = esequence.max+(1,1,1)
+  if (minlist.size() == 1 && maxlist.size() == 1 &&
+      minlist[0].minmax[0] == elementParams[0] && 
+      maxlist[0].minmax[1] == elementParams[1])
+      //   complete
+    return true;
+    // else
+
+  return false;
+}
+
+
+SequenceData* ScdElementData::subset( MBEntityHandle /*start*/, 
+                                      MBEntityHandle /*end*/,
+                                      const int* /*sequence_data_sizes*/,
+                                      const int* /*tag_data_sizes*/ ) const
+{
+  return 0;
+}
+
+unsigned long ScdElementData::get_memory_use() const
+{
+  return sizeof(*this) + vertexSeqRefs.capacity() * sizeof(VertexDataRef);
+}

Copied: MOAB/trunk/ScdElementData.hpp (from rev 1308, MOAB/trunk/ScdElementSeq.hpp)
===================================================================
--- MOAB/trunk/ScdElementData.hpp	                        (rev 0)
+++ MOAB/trunk/ScdElementData.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,305 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ * 
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+
+#ifndef SCD_ELEMENT_DATA_HPP
+#define SCD_ELEMENT_DATA_HPP
+
+//
+// Class: ScdElementSeq
+//
+// Purpose: represent a rectangular element of mesh
+//
+// A ScdElement represents a rectangular element of mesh, including both vertices and
+// elements, and the parametric space used to address that element.  Vertex data,
+// i.e. coordinates, may not be stored directly in the element, but the element returns
+// information about the vertex handles of vertices in the element.  Vertex and element
+// handles associated with the element are each contiguous.
+
+#include "SequenceData.hpp"
+#include "HomXform.hpp"
+#include "MBCN.hpp"
+#include "ScdVertexData.hpp"
+#include "MBInternals.hpp"
+#include "MBRange.hpp"
+
+#include <vector>
+#include <algorithm>
+
+  //! structure to hold references to bounding vertex blocks
+class VertexDataRef
+{
+private:
+  HomCoord minmax[2];
+  HomXform xform, invXform;
+  ScdVertexData *srcSeq;
+public:
+  friend class ScdElementData;
+  
+  VertexDataRef(const HomCoord &min, const HomCoord &max,
+                const HomXform &tmp_xform, ScdVertexData *this_seq);
+    
+  bool contains(const HomCoord &coords) const;
+};
+
+class ScdElementData : public SequenceData
+{
+
+private:
+
+    //! parameter min/max/stride, in homogeneous coords ijkh
+  HomCoord elementParams[3];
+
+    //! difference between max and min params plus one (i.e. # VERTICES in
+    //! each parametric direction)
+  int dIJK[3];
+  
+    //! difference between max and min params (i.e. # ELEMENTS in
+    //! each parametric direction)
+  int dIJKm1[3];
+
+    //! bare constructor, so compiler doesn't create one for me
+  ScdElementData();
+
+    //! list of bounding vertex blocks
+  std::vector<VertexDataRef> vertexSeqRefs;
+
+public:
+
+    //! constructor
+  ScdElementData( MBEntityHandle start_handle,
+                  const int imin, const int jmin, const int kmin,
+                  const int imax, const int jmax, const int kmax);
+  
+  virtual ~ScdElementData();
+  
+    //! get handle of vertex at homogeneous coords
+  inline MBEntityHandle get_vertex(const HomCoord &coords) const;
+  inline MBEntityHandle get_vertex(int i, int j, int k) const
+    { return get_vertex(HomCoord(i,j,k)); }
+  
+    //! get handle of element at i, j, k
+  MBEntityHandle get_element(const int i, const int j, const int k) const;
+  
+    //! get min params for this element
+  const HomCoord &min_params() const;
+
+    //! get max params for this element
+  const HomCoord &max_params() const;
+  
+    //! get the number of vertices in each direction, inclusive
+  void param_extents(int &di, int &dj, int &dk) const;
+
+    //! given a handle, get the corresponding parameters
+  MBErrorCode get_params(const MBEntityHandle ehandle,
+                          int &i, int &j, int &k) const;
+  
+    //! convenience functions for parameter extents
+  int i_min() const {return (elementParams[0].hom_coord())[0];}
+  int j_min() const {return (elementParams[0].hom_coord())[1];}
+  int k_min() const {return (elementParams[0].hom_coord())[2];}
+  int i_max() const {return (elementParams[1].hom_coord())[0];}
+  int j_max() const {return (elementParams[1].hom_coord())[1];}
+  int k_max() const {return (elementParams[1].hom_coord())[2];}
+
+    //! test the bounding vertex sequences and determine whether they fully
+    //! define the vertices covering this element block's parameter space
+  bool boundary_complete() const;
+
+    //! test whether this sequence contains these parameters
+  inline bool contains(const HomCoord &coords) const;
+
+    //! get connectivity of an entity given entity's parameters
+  inline MBErrorCode get_params_connectivity(const int i, const int j, const int k,
+                                       std::vector<MBEntityHandle>& connectivity) const;
+  
+    //! add a vertex seq ref to this element sequence;
+    //! if bb_input is true, bounding box (in eseq-local coords) of vseq being added 
+    //! is input in bb_min and bb_max (allows partial sharing of vseq rather than the whole
+    //! vseq); if it's false, the whole vseq is referenced and the eseq-local coordinates
+    //! is computed from the transformed bounding box of the vseq
+  MBErrorCode add_vsequence(ScdVertexData *vseq, 
+                             const HomCoord &p1, const HomCoord &q1,
+                             const HomCoord &p2, const HomCoord &q2,
+                             const HomCoord &p3, const HomCoord &q3,
+                             bool bb_input = false,
+                             const HomCoord &bb_min = HomCoord::unitv[0],
+                             const HomCoord &bb_max = HomCoord::unitv[0]);
+
+
+  SequenceData* subset( MBEntityHandle start, 
+                        MBEntityHandle end,
+                        const int* sequence_data_sizes,
+                        const int* tag_data_sizes ) const;
+  
+  static MBEntityID calc_num_entities( MBEntityHandle start_handle,
+                                       int irange,
+                                       int jrange,
+                                       int krange );
+
+  unsigned long get_memory_use() const;
+};
+
+inline MBEntityHandle ScdElementData::get_element(const int i, const int j, const int k) const
+{
+  return start_handle() + (i-i_min()) + (j-j_min())*dIJKm1[0] + (k-k_min())*dIJKm1[0]*dIJKm1[1];
+}
+
+inline const HomCoord &ScdElementData::min_params() const
+{
+  return elementParams[0];
+}
+
+inline const HomCoord &ScdElementData::max_params() const
+{
+  return elementParams[1];
+}
+
+  //! get the number of vertices in each direction, inclusive
+inline void ScdElementData::param_extents(int &di, int &dj, int &dk) const
+{
+  di = dIJK[0];
+  dj = dIJK[1];
+  dk = dIJK[2];
+}
+
+inline MBErrorCode ScdElementData::get_params(const MBEntityHandle ehandle,
+                                              int &i, int &j, int &k) const
+{
+  if (TYPE_FROM_HANDLE(ehandle) != TYPE_FROM_HANDLE(start_handle())) return MB_FAILURE;
+
+  int hdiff = ehandle - start_handle();
+
+    // use double ?: test below because on some platforms, both sides of the : are
+    // evaluated, and if dIJKm1[1] is zero, that'll generate a divide-by-zero
+  k = (dIJKm1[1] > 0 ? hdiff / (dIJKm1[1] > 0 ? dIJKm1[0]*dIJKm1[1] : 1) : 0);
+  j = (hdiff - (k*dIJKm1[0]*dIJKm1[1])) / dIJKm1[0];
+  i = hdiff % dIJKm1[0];
+
+  k += elementParams[0].k();
+  j += elementParams[0].j();
+  i += elementParams[0].i();
+
+  return (ehandle >= start_handle() &&
+          ehandle < start_handle()+size() &&
+          i >= i_min() && i <= i_max() &&
+          j >= j_min() && j <= j_max() &&
+          k >= k_min() && k <= k_max()) ? MB_SUCCESS : MB_FAILURE;
+}
+
+inline bool ScdElementData::contains(const HomCoord &temp) const 
+{
+    // upper bound is < instead of <= because element params max is one less
+    // than vertex params max
+  return (temp >= elementParams[0] && temp < elementParams[1]);
+}
+  
+inline bool VertexDataRef::contains(const HomCoord &coords) const 
+{
+  return (minmax[0] <= coords && minmax[1] >= coords);
+}
+
+inline VertexDataRef::VertexDataRef(const HomCoord &this_min, const HomCoord &this_max,
+                                    const HomXform &tmp_xform, ScdVertexData *this_seq)
+    : xform(tmp_xform), invXform(tmp_xform.inverse()), srcSeq(this_seq)
+{
+  minmax[0] = HomCoord(this_min);
+  minmax[1] = HomCoord(this_max); 
+}
+
+inline MBEntityHandle ScdElementData::get_vertex(const HomCoord &coords) const
+{
+   for (std::vector<VertexDataRef>::const_iterator it = vertexSeqRefs.begin();
+        it != vertexSeqRefs.end(); it++) {
+     if ((*it).minmax[0] <= coords && (*it).minmax[1] >= coords) {
+         // first get the vertex block-local parameters
+       HomCoord local_coords = coords / (*it).xform;
+    
+      // now get the vertex handle for those coords
+       return (*it).srcSeq->get_vertex(local_coords);
+     }
+   }
+   
+     // got here, it's an error
+   return 0;
+}
+
+inline MBErrorCode ScdElementData::add_vsequence(ScdVertexData *vseq, 
+                                                 const HomCoord &p1, const HomCoord &q1,
+                                                 const HomCoord &p2, const HomCoord &q2, 
+                                                 const HomCoord &p3, const HomCoord &q3,
+                                                 bool bb_input,
+                                                 const HomCoord &bb_min,
+                                                 const HomCoord &bb_max)
+{
+    // compute the transform given the vseq-local parameters and the mapping to
+    // this element sequence's parameters passed in minmax
+  HomXform M;
+  M.three_pt_xform(p1, q1, p2, q2, p3, q3);
+  
+    // min and max in element seq's parameter system may not be same as those in 
+    // vseq's system, so need to take min/max
+
+  HomCoord minmax[2];
+  if (bb_input) {
+    minmax[0] = bb_min;
+    minmax[1] = bb_max;
+  }
+  else {
+    minmax[0] = vseq->min_params() * M;
+    minmax[1] = vseq->max_params() * M;
+  }
+  
+    // check against other vseq's to make sure they don't overlap
+  for (std::vector<VertexDataRef>::const_iterator vsit = vertexSeqRefs.begin();
+       vsit != vertexSeqRefs.end(); vsit++) 
+    if ((*vsit).contains(minmax[0]) || (*vsit).contains(minmax[1])) 
+      return MB_FAILURE;
+    
+  HomCoord tmp_min(std::min(minmax[0].i(), minmax[1].i()), 
+                   std::min(minmax[0].j(), minmax[1].j()), 
+                   std::min(minmax[0].k(), minmax[1].k()));
+  HomCoord tmp_max(std::max(minmax[0].i(), minmax[1].i()), 
+                   std::max(minmax[0].j(), minmax[1].j()), 
+                   std::max(minmax[0].k(), minmax[1].k()));
+
+  
+    // set up a new vertex sequence reference
+  VertexDataRef tmp_seq_ref(tmp_min, tmp_max, M, vseq);
+
+    // add to the list
+  vertexSeqRefs.push_back(tmp_seq_ref);
+  
+  return MB_SUCCESS;
+}
+
+inline MBErrorCode ScdElementData::get_params_connectivity(const int i, const int j, const int k,
+                                                           std::vector<MBEntityHandle>& connectivity) const
+{
+  if (contains(HomCoord(i, j, k)) == false) return MB_FAILURE;
+  
+  connectivity.push_back(get_vertex(i, j, k));
+  connectivity.push_back(get_vertex(i+1, j, k));
+  if (MBCN::Dimension(TYPE_FROM_HANDLE(start_handle())) < 2) return MB_SUCCESS;
+  connectivity.push_back(get_vertex(i+1, j+1, k));
+  connectivity.push_back(get_vertex(i, j+1, k));
+  if (MBCN::Dimension(TYPE_FROM_HANDLE(start_handle())) < 3) return MB_SUCCESS;
+  connectivity.push_back(get_vertex(i, j, k+1));
+  connectivity.push_back(get_vertex(i+1, j, k+1));
+  connectivity.push_back(get_vertex(i+1, j+1, k+1));
+  connectivity.push_back(get_vertex(i, j+1, k+1));
+  return MB_SUCCESS;
+}
+
+#endif

Deleted: MOAB/trunk/ScdElementSeq.cpp
===================================================================
--- MOAB/trunk/ScdElementSeq.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/ScdElementSeq.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,146 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-#include "ScdElementSeq.hpp"
-#include "ScdVertexSeq.hpp"
-#include "MBInterface.hpp"
-#include "MBReadUtilIface.hpp"
-#include "MBCN.hpp"
-#include "MBInternals.hpp"
-
-ScdElementSeq::ScdElementSeq(EntitySequenceManager *seq_mgr,
-                             MBEntityHandle start_handle,
-                             const int imin, const int jmin, const int kmin,
-                             const int imax, const int jmax, const int kmax) 
-    : ElementEntitySequence(seq_mgr, start_handle,
-                            (imax-imin)*(jmax-jmin)*(kmax-kmin),
-                            MBCN::VerticesPerEntity(TYPE_FROM_HANDLE(start_handle)), 
-                            true, false)
-  
-{
-    // need to have meaningful parameters
-  assert(imax >= imin && jmax >= jmin && kmax >= kmin);
-
-    // correct num entities if necessary
-  if (mNumEntities == 0) {
-    int this_dim = MBCN::Dimension(TYPE_FROM_HANDLE(start_handle));
-    if (this_dim == 1) mNumEntities = imax - imin;
-    else if (this_dim == 2) mNumEntities = (imax - imin)*(jmax - jmin);
-
-      // if neither of the previous two tests passed, it's an error
-    else assert(false);
-
-    assert(mNumEntities > 0);
-  }
-    
-  elementParams[0] = HomCoord(imin, jmin, kmin);
-  elementParams[1] = HomCoord(imax, jmax, kmax);
-  elementParams[2] = HomCoord(1, 1, 1);
-  
-    // assign and compute parameter stuff
-  dIJK[0] = elementParams[1][0] - elementParams[0][0] + 1;
-  dIJK[1] = elementParams[1][1] - elementParams[0][1] + 1;
-  dIJK[2] = elementParams[1][2] - elementParams[0][2] + 1;
-  dIJKm1[0] = dIJK[0] - 1;
-  dIJKm1[1] = dIJK[1] - 1;
-  dIJKm1[2] = dIJK[2] - 1;
-}
-
-ScdElementSeq::~ScdElementSeq() 
-{
-}
-
-bool ScdElementSeq::boundary_complete() const
-{
-    // test the bounding vertex sequences to see if they fully define the
-    // vertex parameter space for this rectangular block of elements
-
-  int p;
-  std::vector<VertexSeqRef> minlist, maxlist;
-
-    // pseudo code:
-    // for each vertex sequence v:
-  for (std::vector<VertexSeqRef>::const_iterator vseq = vertexSeqRefs.begin();
-       vseq != vertexSeqRefs.end(); vseq++)
-  {
-    //   test min corner mincorner:
-    bool mincorner = true;
-    //   for each p = (i-1,j,k), (i,j-1,k), (i,j,k-1):
-    for (p = 0; p < 3; p++) {
-
-    //     for each vsequence v' != v:
-      for (std::vector<VertexSeqRef>::const_iterator othervseq = vertexSeqRefs.begin();
-           othervseq != vertexSeqRefs.end(); othervseq++) 
-      {
-        if (othervseq == vseq) continue;        
-    //       if v.min-p contained in v'
-        if ((*othervseq).contains((*vseq).minmax[0]-HomCoord::unitv[p])) {
-    //         mincorner = false
-          mincorner = false;
-          break;
-        }
-      }
-      if (!mincorner) break;
-    }
-  
-    bool maxcorner = true;
-    //   for each p = (i-1,j,k), (i,j-1,k), (i,j,k-1):
-    for (p = 0; p < 3; p++) {
-
-    //     for each vsequence v' != v:
-      for (std::vector<VertexSeqRef>::const_iterator othervseq = vertexSeqRefs.begin();
-           othervseq != vertexSeqRefs.end(); othervseq++) 
-      {
-        if (othervseq == vseq) continue;        
-    //       if v.max+p contained in v'
-        if ((*othervseq).contains((*vseq).minmax[1]+HomCoord::unitv[p])) {
-    //         maxcorner = false
-          maxcorner = false;
-          break;
-        }
-      }
-      if (!maxcorner) break;
-    }
-
-    //   if mincorner add to min corner list minlist
-    if (mincorner) minlist.push_back(*vseq);
-    //   if maxcorner add to max corner list maxlist
-    if (maxcorner) maxlist.push_back(*vseq);
-  }
-  
-    // 
-    // if minlist.size = 1 & maxlist.size = 1 & minlist[0] = esequence.min &
-    //         maxlist[0] = esequence.max+(1,1,1)
-  if (minlist.size() == 1 && maxlist.size() == 1 &&
-      minlist[0].minmax[0] == elementParams[0] && 
-      maxlist[0].minmax[1] == elementParams[1])
-      //   complete
-    return true;
-    // else
-
-  return false;
-}
-
-void ScdElementSeq::get_memory_use( unsigned long& used, 
-                                    unsigned long& allocated) const
-{ 
-  allocated = used = sizeof(*this) + sizeof(VertexSeqRef) * vertexSeqRefs.capacity();
-}
-
-unsigned long ScdElementSeq::get_memory_use( MBEntityHandle ) const
-{
-  return 0;
-}
-

Deleted: MOAB/trunk/ScdElementSeq.hpp
===================================================================
--- MOAB/trunk/ScdElementSeq.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/ScdElementSeq.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,431 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-#ifndef SCDELEMENTSEQ
-#define SCDELEMENTSEQ
-
-//
-// Class: ScdElementSeq
-//
-// Purpose: represent a rectangular element of mesh
-//
-// A ScdElement represents a rectangular element of mesh, including both vertices and
-// elements, and the parametric space used to address that element.  Vertex data,
-// i.e. coordinates, may not be stored directly in the element, but the element returns
-// information about the vertex handles of vertices in the element.  Vertex and element
-// handles associated with the element are each contiguous.
-
-#include "EntitySequence.hpp"
-#include "HomXform.hpp"
-#include "MBCN.hpp"
-#include "ScdVertexSeq.hpp"
-#include "MBInternals.hpp"
-#include "MBRange.hpp"
-
-#include <vector>
-
-  //! structure to hold references to bounding vertex blocks
-class VertexSeqRef
-{
-private:
-  HomCoord minmax[2];
-  HomXform xform, invXform;
-  ScdVertexSeq *srcSeq;
-public:
-  friend class ScdElementSeq;
-  
-  VertexSeqRef(const HomCoord &min, const HomCoord &max,
-               const HomXform &tmp_xform, ScdVertexSeq *this_seq);
-    
-  bool contains(const HomCoord &coords) const;
-};
-
-class ScdElementSeq : public ElementEntitySequence
-{
-
-private:
-
-    //! parameter min/max/stride, in homogeneous coords ijkh
-  HomCoord elementParams[3];
-
-    //! difference between max and min params plus one (i.e. # VERTICES in
-    //! each parametric direction)
-  int dIJK[3];
-  
-    //! difference between max and min params (i.e. # ELEMENTS in
-    //! each parametric direction)
-  int dIJKm1[3];
-
-    //! bare constructor, so compiler doesn't create one for me
-  ScdElementSeq();
-
-    //! list of bounding vertex blocks
-  std::vector<VertexSeqRef> vertexSeqRefs;
-
-public:
-
-    //! constructor
-  ScdElementSeq(EntitySequenceManager *seq_mgr,
-                MBEntityHandle start_handle,
-                const int imin, const int jmin, const int kmin,
-                const int imax, const int jmax, const int kmax);
-  
-  virtual ~ScdElementSeq();
-
-    //! get handle of vertex at i, j, k
-  MBEntityHandle get_vertex(const int i, const int j, const int k) const;
-  
-    //! get handle of vertex at homogeneous coords
-  inline MBEntityHandle get_vertex(const HomCoord &coords) const;
-  
-    //! get handle of element at i, j, k
-  MBEntityHandle get_element(const int i, const int j, const int k) const;
-  
-    //! get handle of element at homogeneous coords
-  MBEntityHandle get_element(const HomCoord &coords) const;
-  
-    //! get min params for this element
-  void min_params(HomCoord &coords) const;
-  void min_params(int &i, int &j, int &k) const;
-  const HomCoord &min_params() const;
-
-    //! get max params for this element
-  void max_params(HomCoord &coords) const;
-  void max_params(int &i, int &j, int &k) const;
-  const HomCoord &max_params() const;
-  
-    //! get the number of vertices in each direction, inclusive
-  void param_extents(int &di, int &dj, int &dk) const;
-
-    //! given a handle, get the corresponding parameters
-  MBErrorCode get_params(const MBEntityHandle ehandle,
-                          int &i, int &j, int &k) const;
-  
-    //! convenience functions for parameter extents
-  int i_min() const {return (elementParams[0].hom_coord())[0];}
-  int j_min() const {return (elementParams[0].hom_coord())[1];}
-  int k_min() const {return (elementParams[0].hom_coord())[2];}
-  int i_max() const {return (elementParams[1].hom_coord())[0];}
-  int j_max() const {return (elementParams[1].hom_coord())[1];}
-  int k_max() const {return (elementParams[1].hom_coord())[2];}
-
-    //! test the bounding vertex sequences and determine whether they fully
-    //! define the vertices covering this element block's parameter space
-  bool boundary_complete() const;
-
-    //! test whether this sequence contains these parameters
-  bool contains(const int i, const int j, const int k) const;
-  inline bool contains(const HomCoord &coords) const;
-  
-    // from parent class
-  virtual MBEntityHandle get_unused_handle();
-
-  virtual MBErrorCode get_connectivity(MBEntityHandle entity, 
-                                       std::vector<MBEntityHandle>& connectivity,
-                                       const bool topological_connectivity = false) const;
-
-  virtual MBErrorCode get_connectivity(MBEntityHandle entity, 
-                                       const MBEntityHandle*& connectivity,
-                                       int &num_vertices,
-                                       const bool topological_connectivity = false,
-                                       std::vector<MBEntityHandle>* stroage = 0) const;
-
-    //! get connectivity of an entity given entity's parameters
-  inline MBErrorCode get_params_connectivity(const int i, const int j, const int k,
-                                       std::vector<MBEntityHandle>& connectivity) const;
-  
-    //! add a vertex seq ref to this element sequence;
-    //! if bb_input is true, bounding box (in eseq-local coords) of vseq being added 
-    //! is input in bb_min and bb_max (allows partial sharing of vseq rather than the whole
-    //! vseq); if it's false, the whole vseq is referenced and the eseq-local coordinates
-    //! is computed from the transformed bounding box of the vseq
-  MBErrorCode add_vsequence(ScdVertexSeq *vseq, 
-                             const HomCoord &p1, const HomCoord &q1,
-                             const HomCoord &p2, const HomCoord &q2,
-                             const HomCoord &p3, const HomCoord &q3,
-                             bool bb_input = false,
-                             const HomCoord &bb_min = HomCoord::unitv[0],
-                             const HomCoord &bb_max = HomCoord::unitv[0]);
-  
-  virtual MBErrorCode set_connectivity(MBEntityHandle entity, const MBEntityHandle *conn,
-                                const int num_vertices);
-
-  virtual void get_entities(MBRange& ) const;
-  
-  virtual MBErrorCode split(MBEntityHandle , 
-                             MBEntitySequence*& );
-  
-  virtual MBErrorCode convert_realloc(bool& , bool& , bool& , 
-                                      MBCore*, MBTag );
-  
-  virtual void get_memory_use( unsigned long& used, unsigned long& allocated ) const;
-  virtual unsigned long get_memory_use( MBEntityHandle h ) const;
-  
-};
-
-inline MBEntityHandle ScdElementSeq::get_element(const int i, const int j, const int k) const
-{
-  return mStartEntityHandle + (i-i_min()) + (j-j_min())*dIJKm1[0] + (k-k_min())*dIJKm1[0]*dIJKm1[1];
-}
-  
-inline MBEntityHandle ScdElementSeq::get_element(const HomCoord &coord) const
-{
-  return get_element(coord.i(), coord.j(), coord.k());
-}
-  
-  //! get min params for this element
-inline void ScdElementSeq::min_params(HomCoord &coords) const
-{
-  coords = elementParams[0];
-}
-
-inline void ScdElementSeq::min_params(int &i, int &j, int &k) const
-{
-  i = elementParams[0].i();
-  j = elementParams[0].j();
-  k = elementParams[0].k();
-}
-
-inline const HomCoord &ScdElementSeq::min_params() const
-{
-  return elementParams[0];
-}
-
-//! get max params for this element
-inline void ScdElementSeq::max_params(HomCoord &coords) const
-{
-  coords = elementParams[1];
-}
-  
-inline void ScdElementSeq::max_params(int &i, int &j, int &k) const
-{
-  i = elementParams[1].i();
-  j = elementParams[1].j();
-  k = elementParams[1].k();
-}
-
-inline const HomCoord &ScdElementSeq::max_params() const
-{
-  return elementParams[1];
-}
-
-  //! get the number of vertices in each direction, inclusive
-inline void ScdElementSeq::param_extents(int &di, int &dj, int &dk) const
-{
-  di = dIJK[0];
-  dj = dIJK[1];
-  dk = dIJK[2];
-}
-
-inline MBErrorCode ScdElementSeq::get_params(const MBEntityHandle ehandle,
-                                              int &i, int &j, int &k) const
-{
-  if (TYPE_FROM_HANDLE(ehandle) != TYPE_FROM_HANDLE(mStartEntityHandle)) return MB_FAILURE;
-
-  int hdiff = ehandle - mStartEntityHandle;
-
-    // use double ?: test below because on some platforms, both sides of the : are
-    // evaluated, and if dIJKm1[1] is zero, that'll generate a divide-by-zero
-  k = (dIJKm1[1] > 0 ? hdiff / (dIJKm1[1] > 0 ? dIJKm1[0]*dIJKm1[1] : 1) : 0);
-  j = (hdiff - (k*dIJKm1[0]*dIJKm1[1])) / dIJKm1[0];
-  i = hdiff % dIJKm1[0];
-
-  k += elementParams[0].k();
-  j += elementParams[0].j();
-  i += elementParams[0].i();
-
-  return (ehandle >= mStartEntityHandle &&
-          ehandle < mStartEntityHandle+number_entities() &&
-          i >= i_min() && i <= i_max() &&
-          j >= j_min() && j <= j_max() &&
-          k >= k_min() && k <= k_max()) ? MB_SUCCESS : MB_FAILURE;
-}
-
-inline bool ScdElementSeq::contains(const int i, const int j, const int k) const 
-{
-  return contains(HomCoord(i, j, k));
-}
-
-inline bool ScdElementSeq::contains(const HomCoord &temp) const 
-{
-    // upper bound is < instead of <= because element params max is one less
-    // than vertex params max
-  return (temp >= elementParams[0] && temp < elementParams[1]);
-}
-
-inline MBEntityHandle ScdElementSeq::get_vertex(const int i, const int j, const int k) const
-{
-  return get_vertex(HomCoord(i,j,k));
-}
-
-inline MBEntityHandle ScdElementSeq::get_unused_handle() 
-{
-  return MB_FAILURE;
-}
-
-inline MBErrorCode ScdElementSeq::set_connectivity(MBEntityHandle , 
-                                                    const MBEntityHandle *,
-                                                    const int )
-{
-  return MB_FAILURE;
-}
-
-inline void ScdElementSeq::get_entities(MBRange& range) const
-{
-  range.insert(mStartEntityHandle, mStartEntityHandle+mNumEntities-1);
-}
-  
-inline MBErrorCode ScdElementSeq::split(MBEntityHandle , 
-                                        MBEntitySequence*& )
-{
-  return MB_FAILURE;
-}
-
-  // reallocated the sequence to hold extra/less nodes, pass in what you want, 
-  // and will return whether it needed
-  // reallocate space for those nodes
-inline MBErrorCode ScdElementSeq::convert_realloc(bool& , bool& , bool& , 
-                                                  MBCore*, MBTag )
-{
-  return MB_NOT_IMPLEMENTED;
-}
-  
-inline bool VertexSeqRef::contains(const HomCoord &coords) const 
-{
-  return (minmax[0] <= coords && minmax[1] >= coords);
-}
-
-inline VertexSeqRef::VertexSeqRef(const HomCoord &this_min, const HomCoord &this_max,
-                                  const HomXform &tmp_xform, ScdVertexSeq *this_seq)
-    : xform(tmp_xform), invXform(tmp_xform.inverse()), srcSeq(this_seq)
-{
-  minmax[0] = HomCoord(this_min);
-  minmax[1] = HomCoord(this_max); 
-}
-
-inline MBErrorCode ScdElementSeq::get_connectivity(MBEntityHandle entity, 
-                                                   std::vector<MBEntityHandle>& connectivity,
-                                                   const bool) const
-{
-    // get parameter values
-  int i, j, k;
-  MBErrorCode result = get_params(entity, i, j, k);
-  if (MB_SUCCESS != result) return result;
-  
-  return get_params_connectivity(i, j, k, connectivity);
-}
-
-inline MBErrorCode ScdElementSeq::get_connectivity(MBEntityHandle handle, 
-                                                   const MBEntityHandle*& conn,
-                                                   int &len,
-                                                   const bool topo,
-                                                   std::vector<MBEntityHandle>* storage) const 
-{
-  if (!storage)
-    return MB_NOT_IMPLEMENTED;
-    
-  MBErrorCode result = get_connectivity( handle, *storage, topo );
-  conn = &(*storage)[0];
-  len = storage->size();
-  return result;
-}
-
-inline MBEntityHandle ScdElementSeq::get_vertex(const HomCoord &coords) const
-{
-   for (std::vector<VertexSeqRef>::const_iterator it = vertexSeqRefs.begin();
-        it != vertexSeqRefs.end(); it++) {
-     if ((*it).minmax[0] <= coords && (*it).minmax[1] >= coords) {
-         // first get the vertex block-local parameters
-       HomCoord local_coords = coords / (*it).xform;
-    
-      // now get the vertex handle for those coords
-       return (*it).srcSeq->get_vertex(local_coords);
-     }
-   }
-   
-     // got here, it's an error
-   return 0;
-}
-
-inline MBErrorCode ScdElementSeq::add_vsequence(ScdVertexSeq *vseq, 
-                                                 const HomCoord &p1, const HomCoord &q1,
-                                                 const HomCoord &p2, const HomCoord &q2, 
-                                                 const HomCoord &p3, const HomCoord &q3,
-                                                 bool bb_input,
-                                                 const HomCoord &bb_min,
-                                                 const HomCoord &bb_max)
-{
-    // compute the transform given the vseq-local parameters and the mapping to
-    // this element sequence's parameters passed in minmax
-  HomXform M;
-  M.three_pt_xform(p1, q1, p2, q2, p3, q3);
-  
-    // min and max in element seq's parameter system may not be same as those in 
-    // vseq's system, so need to take min/max
-
-  HomCoord minmax[2];
-  if (bb_input) {
-    minmax[0] = bb_min;
-    minmax[1] = bb_max;
-  }
-  else {
-    minmax[0] = vseq->min_params() * M;
-    minmax[1] = vseq->max_params() * M;
-  }
-  
-    // check against other vseq's to make sure they don't overlap
-  for (std::vector<VertexSeqRef>::const_iterator vsit = vertexSeqRefs.begin();
-       vsit != vertexSeqRefs.end(); vsit++) 
-    if ((*vsit).contains(minmax[0]) || (*vsit).contains(minmax[1])) 
-      return MB_FAILURE;
-    
-#define MIN(a,b) ((a <= b) ? a : b)
-#define MAX(a,b) ((a >= b) ? a : b)
-  HomCoord tmp_min(MIN(minmax[0].i(), minmax[1].i()), 
-                   MIN(minmax[0].j(), minmax[1].j()), 
-                   MIN(minmax[0].k(), minmax[1].k()));
-  HomCoord tmp_max(MAX(minmax[0].i(), minmax[1].i()), 
-                   MAX(minmax[0].j(), minmax[1].j()), 
-                   MAX(minmax[0].k(), minmax[1].k()));
-
-  
-    // set up a new vertex sequence reference
-  VertexSeqRef tmp_seq_ref(tmp_min, tmp_max, M, vseq);
-
-    // add to the list
-  vertexSeqRefs.push_back(tmp_seq_ref);
-  
-  return MB_SUCCESS;
-}
-
-inline MBErrorCode ScdElementSeq::get_params_connectivity(const int i, const int j, const int k,
-                                                           std::vector<MBEntityHandle>& connectivity) const
-{
-  if (contains(i, j, k) == false) return MB_FAILURE;
-  
-  connectivity.push_back(get_vertex(i, j, k));
-  connectivity.push_back(get_vertex(i+1, j, k));
-  if (MBCN::Dimension(get_type()) < 2) return MB_SUCCESS;
-  connectivity.push_back(get_vertex(i+1, j+1, k));
-  connectivity.push_back(get_vertex(i, j+1, k));
-  if (MBCN::Dimension(get_type()) < 3) return MB_SUCCESS;
-  connectivity.push_back(get_vertex(i, j, k+1));
-  connectivity.push_back(get_vertex(i+1, j, k+1));
-  connectivity.push_back(get_vertex(i+1, j+1, k+1));
-  connectivity.push_back(get_vertex(i, j+1, k+1));
-  return MB_SUCCESS;
-}
-
-#endif

Copied: MOAB/trunk/ScdVertexData.cpp (from rev 1308, MOAB/trunk/ScdVertexSeq.cpp)
===================================================================
--- MOAB/trunk/ScdVertexData.cpp	                        (rev 0)
+++ MOAB/trunk/ScdVertexData.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,49 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ * 
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+
+#include "ScdVertexData.hpp"
+#include <assert.h>
+
+    //! constructor
+ScdVertexData::ScdVertexData(const MBEntityHandle start_vertex, 
+                             const int imin, const int jmin, const int kmin,
+                             const int imax, const int jmax, const int kmax) 
+    : SequenceData( 3, start_vertex, 
+                   start_vertex + (imax-imin+1)*(jmax-jmin+1)*(kmax-kmin+1) - 1 )
+{
+    // need to have meaningful parameters
+  assert(imax >= imin && jmax >= jmin && kmax >= kmin);
+  
+  vertexParams[0] = HomCoord(imin, jmin, kmin);
+  vertexParams[1] = HomCoord(imax, jmax, kmax);
+  vertexParams[2] = HomCoord(1,1,1);
+  
+  dIJK[0] = imax-imin+1; dIJK[1] = jmax-jmin+1; dIJK[2] = kmax-kmin+1;
+  dIJKm1[0] = dIJK[0]-1;
+  dIJKm1[1] = dIJK[1]-1;
+  dIJKm1[2] = dIJK[2]-1;
+  
+  create_sequence_data( 0, sizeof(double) );
+  create_sequence_data( 1, sizeof(double) );
+  create_sequence_data( 2, sizeof(double) );
+}
+
+SequenceData* ScdVertexData::subset( MBEntityHandle /*start*/, 
+                                     MBEntityHandle /*end*/,
+                                     const int* /*sequence_data_sizes*/,
+                                     const int* /*tag_data_sizes*/ ) const
+{
+  return 0;
+}

Copied: MOAB/trunk/ScdVertexData.hpp (from rev 1308, MOAB/trunk/ScdVertexSeq.hpp)
===================================================================
--- MOAB/trunk/ScdVertexData.hpp	                        (rev 0)
+++ MOAB/trunk/ScdVertexData.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,176 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ * 
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+
+#ifndef SCD_VERTEX_DATA_HPP
+#define SCD_VERTEX_DATA_HPP
+
+//
+// Class: ScdVertexData
+//
+// Purpose: represent a rectangular vertex block of mesh
+//
+// A ScdVertex represents a rectangular vertex block of mesh, including both vertices and
+// the parametric space used to address those vertices.
+
+#include "SequenceData.hpp"
+#include "HomXform.hpp"
+
+class ScdVertexData : public SequenceData
+{
+
+private:
+
+    //! parameter min/max, in homogeneous coords ijkh (extra row for stride eventually)
+  HomCoord vertexParams[3];
+
+    //! difference between max and min params plus one (i.e. # VERTICES in
+    //! each parametric direction)
+  int dIJK[3];
+  
+    //! difference between max and min params (i.e. # VERTEXS in
+    //! each parametric direction)
+  int dIJKm1[3];
+
+public:
+
+    //! constructor
+  ScdVertexData(const MBEntityHandle start_vertex, 
+                const int imin, const int jmin, const int kmin,
+                const int imax, const int jmax, const int kmax) ;
+  
+  virtual ~ScdVertexData() {};
+
+    //! get handle of vertex at i, j, k
+  MBEntityHandle get_vertex(const int i, const int j, const int k) const;
+
+    //! get handle of vertex at homogeneous coordinates
+  MBEntityHandle get_vertex(const HomCoord &coords) const;
+
+    //! get the parameters of a given handle; return MB_FAILURE if vhandle not in this
+    //! sequence
+  MBErrorCode get_params(const MBEntityHandle vhandle,
+                          int &i, int &j, int &k) const;
+  
+    //! get min params for this vertex
+  void min_params(int &i, int &j, int &k) const;
+  
+    //! get max params for this vertex
+  void max_params(int &i, int &j, int &k) const;
+
+    //! get the min params
+  const HomCoord &min_params() const;
+
+    //! get the max params
+  const HomCoord &max_params() const;
+  
+    //! get the number of vertices in each direction, inclusive
+  void param_extents(int &di, int &dj, int &dk) const;
+
+    //! convenience functions for parameter extents
+  int i_min() const {return vertexParams[0].hom_coord()[0];}
+  int j_min() const {return vertexParams[0].hom_coord()[1];}
+  int k_min() const {return vertexParams[0].hom_coord()[2];}
+  int i_max() const {return vertexParams[1].hom_coord()[0];}
+  int j_max() const {return vertexParams[1].hom_coord()[1];}
+  int k_max() const {return vertexParams[1].hom_coord()[2];}
+
+    //! return whether this vseq's parameter space contains these parameters
+  bool contains(const HomCoord &coords) const;
+  bool contains(const int i, const int j, const int k) const;
+
+  SequenceData* subset( MBEntityHandle start, 
+                        MBEntityHandle end,
+                        const int* sequence_data_sizes,
+                        const int* tag_data_sizes ) const;
+};
+
+inline MBEntityHandle ScdVertexData::get_vertex(const int i, const int j, 
+                                                const int k) const
+{
+  return start_handle() + (i-i_min()) + (j-j_min())*dIJK[0] + 
+    (k-k_min())*dIJK[0]*dIJK[1];
+}
+
+inline MBEntityHandle ScdVertexData::get_vertex(const HomCoord &coords) const
+{
+  return get_vertex(coords.hom_coord()[0], coords.hom_coord()[1], coords.hom_coord()[2]);
+}
+
+inline MBErrorCode ScdVertexData::get_params(const MBEntityHandle vhandle,
+                                             int &i, int &j, int &k) const
+{
+  if (TYPE_FROM_HANDLE(vhandle) != MBVERTEX) return MB_FAILURE;
+
+  int hdiff = vhandle - start_handle();
+
+  k = hdiff / (dIJK[0]*dIJK[1]);
+  j = (hdiff - (k*dIJK[0]*dIJK[1])) / dIJK[0];
+  i = hdiff % dIJK[0];
+
+  k += vertexParams[0].k();
+  j += vertexParams[0].j();
+  i += vertexParams[0].i();
+
+  return (vhandle >= start_handle() &&
+          i >= i_min() && i <= i_max() &&
+          j >= j_min() && j <= j_max() &&
+          k >= k_min() && k <= k_max()) ? MB_SUCCESS : MB_FAILURE;
+}
+  
+  //! get min params for this vertex
+inline void ScdVertexData::min_params(int &i, int &j, int &k) const
+{
+  i = i_min();
+  j = j_min();
+  k = k_min();
+}
+
+//! get max params for this vertex
+inline void ScdVertexData::max_params(int &i, int &j, int &k) const
+{
+  i = i_max();
+  j = j_max();
+  k = k_max();
+}
+
+inline const HomCoord &ScdVertexData::min_params() const 
+{
+  return vertexParams[0];
+}
+
+inline const HomCoord &ScdVertexData::max_params() const 
+{
+  return vertexParams[1];
+}
+
+  //! get the number of vertices in each direction, inclusive
+inline void ScdVertexData::param_extents(int &di, int &dj, int &dk) const
+{
+  di = dIJK[0];
+  dj = dIJK[1];
+  dk = dIJK[2];
+}
+
+inline bool ScdVertexData::contains(const HomCoord &coords) const
+{
+  return (coords >= vertexParams[0] && coords <= vertexParams[1]) ? true : false;
+}
+
+inline bool ScdVertexData::contains(const int i, const int j, const int k) const
+{
+  return contains(HomCoord(i, j, k));
+}
+
+#endif

Deleted: MOAB/trunk/ScdVertexSeq.cpp
===================================================================
--- MOAB/trunk/ScdVertexSeq.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/ScdVertexSeq.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,52 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-#include "ScdVertexSeq.hpp"
-
-    //! constructor
-ScdVertexSeq::ScdVertexSeq(EntitySequenceManager* seq_mgr,
-                           const MBEntityHandle start_vertex, 
-                           const int imin, const int jmin, const int kmin,
-                           const int imax, const int jmax, const int kmax) 
-    : VertexEntitySequence(seq_mgr, start_vertex, 
-                           (imax-imin+1)*(jmax-jmin+1)*(kmax-kmin+1), 
-                           true)
-{
-    // need to have meaningful parameters
-  assert(imax >= imin && jmax >= jmin && kmax >= kmin);
-  
-  vertexParams[0] = HomCoord(imin, jmin, kmin);
-  vertexParams[1] = HomCoord(imax, jmax, kmax);
-  vertexParams[2] = HomCoord(1,1,1);
-  
-  dIJK[0] = imax-imin+1; dIJK[1] = jmax-jmin+1; dIJK[2] = kmax-kmin+1;
-  dIJKm1[0] = dIJK[0]-1;
-  dIJKm1[1] = dIJK[1]-1;
-  dIJKm1[2] = dIJK[2]-1;
-}
-  
-MBEntityHandle ScdVertexSeq::get_unused_handle()
-{
-  assert(false);
-  return 0;
-}
-
-void ScdVertexSeq::get_memory_use( unsigned long& used, 
-                                   unsigned long& allocated) const
-{
-  VertexEntitySequence::get_memory_use( used, allocated );
-  allocated += sizeof(*this) - sizeof(VertexEntitySequence);
-}
-

Deleted: MOAB/trunk/ScdVertexSeq.hpp
===================================================================
--- MOAB/trunk/ScdVertexSeq.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/ScdVertexSeq.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -1,176 +0,0 @@
-/**
- * MOAB, a Mesh-Oriented datABase, is a software component for creating,
- * storing and accessing finite element mesh data.
- * 
- * Copyright 2004 Sandia Corporation.  Under the terms of Contract
- * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
- * retains certain rights in this software.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- * 
- */
-
-#ifndef SCDVERTEXSEQ
-#define SCDVERTEXSEQ
-
-//
-// Class: ScdVertexSeq
-//
-// Purpose: represent a rectangular vertex block of mesh
-//
-// A ScdVertex represents a rectangular vertex block of mesh, including both vertices and
-// the parametric space used to address those vertices.
-
-#include "EntitySequence.hpp"
-#include "HomXform.hpp"
-
-class ScdVertexSeq : public VertexEntitySequence
-{
-
-private:
-
-    //! parameter min/max, in homogeneous coords ijkh (extra row for stride eventually)
-  HomCoord vertexParams[3];
-
-    //! difference between max and min params plus one (i.e. # VERTICES in
-    //! each parametric direction)
-  int dIJK[3];
-  
-    //! difference between max and min params (i.e. # VERTEXS in
-    //! each parametric direction)
-  int dIJKm1[3];
-
-public:
-
-    //! constructor
-  ScdVertexSeq(EntitySequenceManager* seq_mgr,
-               const MBEntityHandle start_vertex, 
-               const int imin, const int jmin, const int kmin,
-               const int imax, const int jmax, const int kmax) ;
-  
-  virtual ~ScdVertexSeq() {};
-
-    //! get handle of vertex at i, j, k
-  MBEntityHandle get_vertex(const int i, const int j, const int k) const;
-
-    //! get handle of vertex at homogeneous coordinates
-  MBEntityHandle get_vertex(const HomCoord &coords) const;
-
-    //! get the parameters of a given handle; return MB_FAILURE if vhandle not in this
-    //! sequence
-  MBErrorCode get_params(const MBEntityHandle vhandle,
-                          int &i, int &j, int &k) const;
-  
-    //! get min params for this vertex
-  void min_params(int &i, int &j, int &k) const;
-  
-    //! get max params for this vertex
-  void max_params(int &i, int &j, int &k) const;
-
-    //! get the min params
-  const HomCoord &min_params() const;
-
-    //! get the max params
-  const HomCoord &max_params() const;
-  
-    //! get the number of vertices in each direction, inclusive
-  void param_extents(int &di, int &dj, int &dk) const;
-
-    //! convenience functions for parameter extents
-  int i_min() const {return vertexParams[0].hom_coord()[0];}
-  int j_min() const {return vertexParams[0].hom_coord()[1];}
-  int k_min() const {return vertexParams[0].hom_coord()[2];}
-  int i_max() const {return vertexParams[1].hom_coord()[0];}
-  int j_max() const {return vertexParams[1].hom_coord()[1];}
-  int k_max() const {return vertexParams[1].hom_coord()[2];}
-
-    //! return whether this vseq's parameter space contains these parameters
-  bool contains(const HomCoord &coords) const;
-  bool contains(const int i, const int j, const int k) const;
-  
-  MBEntityHandle get_unused_handle();
-
-  virtual void get_memory_use( unsigned long& used, unsigned long& allocated ) const;
-};
-
-inline MBEntityHandle ScdVertexSeq::get_vertex(const int i, const int j, 
-                                                const int k) const
-{
-  return mStartEntityHandle + (i-i_min()) + (j-j_min())*dIJK[0] + 
-    (k-k_min())*dIJK[0]*dIJK[1];
-}
-
-inline MBEntityHandle ScdVertexSeq::get_vertex(const HomCoord &coords) const
-{
-  return get_vertex(coords.hom_coord()[0], coords.hom_coord()[1], coords.hom_coord()[2]);
-}
-
-inline MBErrorCode ScdVertexSeq::get_params(const MBEntityHandle vhandle,
-                                             int &i, int &j, int &k) const
-{
-  if (TYPE_FROM_HANDLE(vhandle) != MBVERTEX) return MB_FAILURE;
-
-  int hdiff = vhandle - mStartEntityHandle;
-
-  k = hdiff / (dIJK[0]*dIJK[1]);
-  j = (hdiff - (k*dIJK[0]*dIJK[1])) / dIJK[0];
-  i = hdiff % dIJK[0];
-
-  k += vertexParams[0].k();
-  j += vertexParams[0].j();
-  i += vertexParams[0].i();
-
-  return (vhandle >= mStartEntityHandle &&
-          i >= i_min() && i <= i_max() &&
-          j >= j_min() && j <= j_max() &&
-          k >= k_min() && k <= k_max()) ? MB_SUCCESS : MB_FAILURE;
-}
-  
-  //! get min params for this vertex
-inline void ScdVertexSeq::min_params(int &i, int &j, int &k) const
-{
-  i = i_min();
-  j = j_min();
-  k = k_min();
-}
-
-//! get max params for this vertex
-inline void ScdVertexSeq::max_params(int &i, int &j, int &k) const
-{
-  i = i_max();
-  j = j_max();
-  k = k_max();
-}
-
-inline const HomCoord &ScdVertexSeq::min_params() const 
-{
-  return vertexParams[0];
-}
-
-inline const HomCoord &ScdVertexSeq::max_params() const 
-{
-  return vertexParams[1];
-}
-
-  //! get the number of vertices in each direction, inclusive
-inline void ScdVertexSeq::param_extents(int &di, int &dj, int &dk) const
-{
-  di = dIJK[0];
-  dj = dIJK[1];
-  dk = dIJK[2];
-}
-
-inline bool ScdVertexSeq::contains(const HomCoord &coords) const
-{
-  return (coords >= vertexParams[0] && coords <= vertexParams[1]) ? true : false;
-}
-
-inline bool ScdVertexSeq::contains(const int i, const int j, const int k) const
-{
-  return contains(HomCoord(i, j, k));
-}
-
-#endif

Added: MOAB/trunk/SequenceData.cpp
===================================================================
--- MOAB/trunk/SequenceData.cpp	                        (rev 0)
+++ MOAB/trunk/SequenceData.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,129 @@
+#include "SequenceData.hpp"
+#include <assert.h>
+
+SequenceData::~SequenceData()
+{
+  for (int i = -numSequenceData; i <= numTagData; ++i)
+    free( arraySet[i] );
+  free( arraySet - numSequenceData );
+}
+
+void* SequenceData::create_data( int index, int bytes_per_ent, void* initial_value )
+{  
+  char* array = (char*)malloc( bytes_per_ent * size() );
+  if (initial_value) {
+    memcpy( array, initial_value, bytes_per_ent );
+    const size_t last = size() / 2;
+    size_t count;
+    for (count = 1; count < last; count *= 2) 
+      memcpy( array + count * bytes_per_ent, array, count * bytes_per_ent );
+    memcpy( array + count * bytes_per_ent, array, (size() - count) * bytes_per_ent ); 
+  }
+  else {
+    memset( array, 0, bytes_per_ent * size() );
+  }
+  
+  arraySet[index] = array;
+  return array;
+}
+
+void* SequenceData::create_sequence_data( int array_num,
+                                          int bytes_per_ent,
+                                          void* initial_value )
+{
+  const int index = -1 - array_num;
+  assert( array_num < numSequenceData );
+  assert( !arraySet[index] );
+  return create_data( index, bytes_per_ent, initial_value );
+}
+
+
+void* SequenceData::create_custom_data( int array_num, size_t total_bytes )
+{
+  const int index = -1 - array_num;
+  assert( array_num < numSequenceData );
+  assert( !arraySet[index] );
+
+  void* array = malloc( total_bytes );
+  memset( array, 0, total_bytes );
+  arraySet[index] = array;
+  return array;
+}
+
+SequenceData::AdjacencyDataType* SequenceData::allocate_adjacency_data()
+{
+  assert( !arraySet[0] );
+  const size_t s = sizeof(AdjacencyDataType*) * size();
+  arraySet[0] = malloc( s );
+  memset( arraySet[0], 0, s );
+  return reinterpret_cast<AdjacencyDataType*>(arraySet[0]);
+}
+
+void* SequenceData::create_tag_data( int tag_num,
+                                     int bytes_per_ent,
+                                     void* initial_val )
+{
+  const int index = tag_num + 1;
+  if (tag_num >= numTagData) {
+    void** list = arraySet - numSequenceData;
+    const size_t size = sizeof(void*) * (numSequenceData + tag_num + 1);
+    list = (void**)realloc( list, size );
+    arraySet = list + numSequenceData;
+    memset( arraySet + numTagData, 0, sizeof(void*) * (tag_num + 1 - numTagData) );
+  }
+  
+  assert( !arraySet[index] );
+  return create_data( index, bytes_per_ent, initial_val );
+}
+
+SequenceData* SequenceData::subset( MBEntityHandle start,
+                                    MBEntityHandle end,
+                                    const int* sequence_data_sizes,
+                                    const int* tag_data_sizes ) const
+{
+  return new SequenceData( this, start, end, sequence_data_sizes, tag_data_sizes );
+}
+
+SequenceData::SequenceData( const SequenceData* from,
+                            MBEntityHandle start, 
+                            MBEntityHandle end,
+                            const int* sequence_data_sizes,
+                            const int* tag_data_sizes )
+  : numSequenceData( from->numSequenceData ),
+    numTagData( from->numTagData ),
+    startHandle( start ),
+    endHandle( end )
+{
+  assert( start <= end );
+  assert( from != 0 );
+  assert( from->start_handle() <= start );
+  assert( from->end_handle() >= end );
+
+  void** array = (void**)malloc( sizeof(void*) * (numSequenceData + numTagData + 1) );
+  arraySet = array + numSequenceData;
+  const size_t offset = start - from->start_handle();
+  const size_t count = end - start + 1;
+  
+  for (int i = 0; i < numSequenceData; ++i)
+    copy_data_subset( -1 - i, sequence_data_sizes[i], from->get_sequence_data(i), offset, count );
+  copy_data_subset( 0, sizeof(AdjacencyDataType*), from->get_adjacency_data(), offset, count );
+  for (int i = 0; i< numTagData; ++i)
+    copy_data_subset( 1 + i, tag_data_sizes[i], from->get_tag_data(i), offset, count );
+}
+
+void SequenceData::copy_data_subset( int index, 
+                                     int size_per_ent, 
+                                     const void* source, 
+                                     size_t offset, 
+                                     size_t count )
+{
+  if (!source)
+    arraySet[index] = 0;
+  else {
+    arraySet[index] = malloc( count * size_per_ent );
+    memcpy( arraySet[index], 
+            (const char*)source + offset * size_per_ent, 
+            count * size_per_ent );
+  }
+}
+

Added: MOAB/trunk/SequenceData.hpp
===================================================================
--- MOAB/trunk/SequenceData.hpp	                        (rev 0)
+++ MOAB/trunk/SequenceData.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,104 @@
+#ifndef SEQUENCE_DATA_HPP
+#define SEQUENCE_DATA_HPP
+
+
+#include "TypeSequenceManager.hpp"
+
+#include <vector>
+
+class TagServer;
+
+class SequenceData
+{
+public:
+
+  typedef std::vector<MBEntityHandle> AdjacencyDataType;
+
+  inline SequenceData( int num_sequence_arrays, 
+                       MBEntityHandle start,
+                       MBEntityHandle end );
+  
+  virtual ~SequenceData();
+  
+  MBEntityHandle start_handle() const 
+    { return startHandle; }
+  
+  MBEntityHandle end_handle() const
+    { return endHandle; }
+    
+  MBEntityID size() const
+    { return endHandle + 1 - startHandle; }
+    
+  void*       get_sequence_data( int array_num )       
+                { return arraySet[-1-array_num]; }
+  void const* get_sequence_data( int array_num ) const 
+                { return arraySet[-1-array_num]; }
+  
+  AdjacencyDataType*       get_adjacency_data( )       
+                { return reinterpret_cast<AdjacencyDataType*>(arraySet[0]); }
+  AdjacencyDataType const* get_adjacency_data( ) const 
+                { return reinterpret_cast<AdjacencyDataType const*>(arraySet[0]); }
+  
+  void*       get_tag_data( int tag_num )              
+                { return tag_num < numTagData  ? arraySet[tag_num+1] : 0; }
+  void const* get_tag_data( int tag_num ) const        
+                { return tag_num < numTagData  ? arraySet[tag_num+1] : 0; }
+  
+  void* create_sequence_data( int array_num, 
+                              int bytes_per_ent,
+                              void* initial_val = 0 );
+                             
+  void* create_custom_data( int array_num, size_t total_bytes );
+  
+  AdjacencyDataType* allocate_adjacency_data();
+  
+  void* create_tag_data( int tag_num, int bytes_per_ent, void* initial_val = 0 );
+  
+  SequenceData* subset( MBEntityHandle start, 
+                        MBEntityHandle end,
+                        const int* sequence_data_sizes,
+                        const int* tag_data_sizes ) const;
+  
+  TypeSequenceManager::SequenceDataPtr seqManData;
+  
+  void move_tag_data( SequenceData* destination, TagServer* tag_server ) {}
+  
+protected:
+
+  SequenceData( const SequenceData* subset_from,
+                MBEntityHandle start, 
+                MBEntityHandle end,
+                const int* sequence_data_sizes,
+                const int* tag_data_sizes );
+
+private:
+
+  void* create_data( int index, int bytes_per_ent, void* initial_val = 0 );
+  void copy_data_subset( int index, 
+                         int size_per_ent, 
+                         const void* source, 
+                         size_t offset, 
+                         size_t count );
+
+  const int numSequenceData;
+  int numTagData;
+  void** arraySet;
+  MBEntityHandle startHandle, endHandle;
+};
+
+inline SequenceData::SequenceData( int num_sequence_arrays, 
+                                   MBEntityHandle start,
+                                   MBEntityHandle end )
+  : numSequenceData(num_sequence_arrays),
+    numTagData(0),
+    startHandle(start),
+    endHandle(end)
+{
+  const size_t size = sizeof(void*) * (num_sequence_arrays + 1);
+  void** data = (void**)malloc( size );
+  memset( data, 0, size );
+  arraySet = data + num_sequence_arrays;
+}
+
+
+#endif

Added: MOAB/trunk/SequenceManager.cpp
===================================================================
--- MOAB/trunk/SequenceManager.cpp	                        (rev 0)
+++ MOAB/trunk/SequenceManager.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,661 @@
+#include "SequenceManager.hpp"
+#include "VertexSequence.hpp"
+#include "UnstructuredElemSeq.hpp"
+#include "ScdVertexData.hpp"
+#include "MeshSetSequence.hpp"
+#include "StructuredElementSeq.hpp"
+#include "HomXform.hpp"
+
+#include <assert.h>
+
+const int DEFAULT_VERTEX_SEQUENCE_SIZE = 4096;
+const int DEFAULT_ELEMENT_SEQUENCE_SIZE = DEFAULT_VERTEX_SEQUENCE_SIZE;
+const int DEFAULT_POLY_SEQUENCE_SIZE = 4 * DEFAULT_ELEMENT_SEQUENCE_SIZE;
+const int DEFAULT_MESHSET_SEQUENCE_SIZE = DEFAULT_VERTEX_SEQUENCE_SIZE;
+
+MBErrorCode SequenceManager::check_valid_entities( const MBRange& entities ) const
+{
+  MBErrorCode rval;
+  MBRange::const_pair_iterator i;
+  for (i = entities.const_pair_begin(); i != entities.const_pair_end(); ++i) {
+    const MBEntityType type1 = TYPE_FROM_HANDLE(i->first);
+    const MBEntityType type2 = TYPE_FROM_HANDLE(i->second);
+    if (type1 == type2) {
+      rval = typeData[type1].check_valid_handles( i->first, i->second );
+      if (MB_SUCCESS != rval)
+        return rval;
+    }
+    else {
+      int junk;
+      MBEntityHandle split = CREATE_HANDLE( type2, 0, junk );
+      rval = typeData[type1].check_valid_handles( i->first, split-1 );
+      if (MB_SUCCESS != rval)
+        return rval;
+      rval = typeData[type2].check_valid_handles( split, i->second );
+      if (MB_SUCCESS != rval)
+        return rval;
+    }
+  }
+  return MB_SUCCESS;
+}
+
+MBErrorCode SequenceManager::delete_entity( MBEntityHandle entity )
+{
+  return typeData[TYPE_FROM_HANDLE(entity)].erase( entity );
+}
+
+MBErrorCode SequenceManager::delete_entities( const MBRange& entities )
+{
+  MBErrorCode rval = check_valid_entities( entities );
+  if (MB_SUCCESS != rval)
+    return rval;
+  
+  MBErrorCode result = MB_SUCCESS;
+  MBRange::const_pair_iterator i;
+  for (i = entities.const_pair_begin(); i != entities.const_pair_end(); ++i) {
+    const MBEntityType type1 = TYPE_FROM_HANDLE(i->first);
+    const MBEntityType type2 = TYPE_FROM_HANDLE(i->second);
+    if (type1 == type2) {
+      rval = typeData[type1].erase( i->first, i->second );
+      if (MB_SUCCESS != rval)
+        return result = rval;
+    }
+    else {
+      int junk;
+      MBEntityHandle split = CREATE_HANDLE( type2, 0, junk );
+      rval = typeData[type1].erase( i->first, split-1 );
+      if (MB_SUCCESS != rval)
+        return result = rval;
+      rval = typeData[type2].erase( split, i->second );
+      if (MB_SUCCESS != rval)
+        return result = rval;
+    }
+  }
+  return result;
+}
+  
+MBErrorCode SequenceManager::create_vertex( unsigned proc_id,
+                                            const double coords[3],
+                                            MBEntityHandle& handle )
+{
+  if (proc_id == (unsigned)-1)
+    proc_id = handleUtils.proc_rank();
+
+  const MBEntityHandle start = handleUtils.create_handle( MBVERTEX, handleUtils.first_id(proc_id), proc_id );
+  const MBEntityHandle   end = handleUtils.create_handle( MBVERTEX, handleUtils. last_id(proc_id), proc_id );
+  bool append;
+  TypeSequenceManager::iterator seq = typeData[MBVERTEX].find_free_handle( start, end, append );
+  VertexSequence* vseq;
+  
+  if (seq == typeData[MBVERTEX].end()) {
+    SequenceData* seq_data = 0;
+    handle = typeData[MBVERTEX].find_free_sequence( DEFAULT_VERTEX_SEQUENCE_SIZE, start, end, seq_data );
+    if (!handle) 
+      return MB_FAILURE;
+    
+    if (seq_data) 
+      vseq = new VertexSequence( handle, 1, seq_data );
+    else
+      vseq = new VertexSequence( handle, 1, DEFAULT_VERTEX_SEQUENCE_SIZE );
+      
+    MBErrorCode rval = typeData[MBVERTEX].insert_sequence( vseq );
+    if (MB_SUCCESS != rval) {
+      SequenceData* vdata = vseq->data();
+      delete vseq;
+      if (!seq_data)
+        delete vdata;
+    
+      return rval;
+    }
+  }
+  else {  
+    vseq = reinterpret_cast<VertexSequence*>(*seq);
+    if (append) {
+      vseq->push_back( 1 );
+      handle = vseq->end_handle();
+      typeData[MBVERTEX].notify_appended( seq );
+    }
+    else {
+      vseq->push_front( 1 );
+      handle = vseq->start_handle();
+      typeData[MBVERTEX].notify_prepended( seq );
+    }
+  }
+  
+  return vseq->set_coordinates( handle, coords );
+}
+
+  
+MBErrorCode SequenceManager::create_element( MBEntityType type,
+                                             unsigned proc_id,
+                                             const MBEntityHandle* conn,
+                                             unsigned conn_len,
+                                             MBEntityHandle& handle )
+{
+  if (proc_id == (unsigned)-1)
+    proc_id = handleUtils.proc_rank();
+
+  if (type <= MBVERTEX || type >= MBENTITYSET)
+    return MB_TYPE_OUT_OF_RANGE;
+  
+  const MBEntityHandle start = handleUtils.create_handle( type, handleUtils.first_id(proc_id), proc_id );
+  const MBEntityHandle   end = handleUtils.create_handle( type, handleUtils. last_id(proc_id), proc_id );
+  bool append;
+  TypeSequenceManager::iterator seq = typeData[type].find_free_handle( start, end, append, conn_len );
+  UnstructuredElemSeq* eseq;
+  
+  if (seq == typeData[type].end()) {
+    SequenceData* seq_data = 0;
+    unsigned size = DEFAULT_ELEMENT_SEQUENCE_SIZE;
+    if (type == MBPOLYGON || type == MBPOLYHEDRON) {
+      size = DEFAULT_POLY_SEQUENCE_SIZE / conn_len;
+      if (!size)
+        size = 1;
+    }
+    
+    handle = typeData[type].find_free_sequence( size, start, end, seq_data, conn_len );
+    if (!handle) 
+      return MB_FAILURE;
+    
+    if (seq_data) 
+      eseq = new UnstructuredElemSeq( handle, 1, conn_len, seq_data );
+    else
+      eseq = new UnstructuredElemSeq( handle, 1, conn_len, size );
+      
+    MBErrorCode rval = typeData[type].insert_sequence( eseq );
+    if (MB_SUCCESS != rval) {
+      SequenceData* vdata = eseq->data();
+      delete eseq;
+      if (!seq_data)
+        delete vdata;
+    
+      return rval;
+    }
+  }
+  else {  
+    eseq = reinterpret_cast<UnstructuredElemSeq*>(*seq);
+    if (append) {
+      eseq->push_back( 1 );
+      handle = eseq->end_handle();
+      typeData[type].notify_appended( seq );
+    }
+    else {
+      eseq->push_front( 1 );
+      handle = eseq->start_handle();
+      typeData[type].notify_prepended( seq );
+    }
+  }
+  
+  return eseq->set_connectivity( handle, conn, conn_len );
+}
+
+
+  
+MBErrorCode SequenceManager::create_mesh_set( unsigned proc_id,
+                                              unsigned flags,
+                                              MBEntityHandle& handle )
+{
+  if (proc_id == (unsigned)-1)
+    proc_id = handleUtils.proc_rank();
+
+  const MBEntityHandle start = handleUtils.create_handle( MBENTITYSET, handleUtils.first_id(proc_id), proc_id );
+  const MBEntityHandle   end = handleUtils.create_handle( MBENTITYSET, handleUtils. last_id(proc_id), proc_id );
+  bool append;
+  TypeSequenceManager::iterator seq = typeData[MBENTITYSET].find_free_handle( start, end, append );
+  MeshSetSequence* msseq;
+  
+  if (seq == typeData[MBENTITYSET].end()) {
+    SequenceData* seq_data = 0;
+    handle = typeData[MBENTITYSET].find_free_sequence( DEFAULT_MESHSET_SEQUENCE_SIZE, start, end, seq_data );
+    if (!handle) 
+      return MB_FAILURE;
+    
+    if (seq_data) 
+      msseq = new MeshSetSequence( handle, 1, flags, seq_data );
+    else
+      msseq = new MeshSetSequence( handle, 1, flags, DEFAULT_MESHSET_SEQUENCE_SIZE );
+      
+    MBErrorCode rval = typeData[MBENTITYSET].insert_sequence( msseq );
+    if (MB_SUCCESS != rval) {
+      SequenceData* vdata = msseq->data();
+      delete msseq;
+      if (!seq_data)
+        delete vdata;
+    
+      return rval;
+    }
+  }
+  else {  
+    msseq = reinterpret_cast<MeshSetSequence*>(*seq);
+    if (append) {
+      msseq->push_back( 1, &flags );
+      handle = msseq->end_handle();
+      typeData[MBENTITYSET].notify_appended( seq );
+    }
+    else {
+      msseq->push_front( 1, &flags );
+      handle = msseq->start_handle();
+      typeData[MBENTITYSET].notify_prepended( seq );
+    }
+  }
+  
+  return MB_SUCCESS;
+}
+
+MBErrorCode SequenceManager::allocate_mesh_set( MBEntityHandle handle,
+                                                unsigned flags )
+{
+  SequenceData* data = 0;
+  TypeSequenceManager::iterator seqptr; 
+  MBEntityHandle block_start = 1, block_end = 0;
+  MBErrorCode rval = typeData[MBENTITYSET].is_free_handle( handle, seqptr, data, block_start, block_end );
+  if (MB_SUCCESS != rval)
+    return rval;
+  
+  MeshSetSequence* seq;
+  if (seqptr != typeData[MBENTITYSET].end()) {
+    seq = static_cast<MeshSetSequence*>(*seqptr);
+    if (seq->start_handle() + 1 == handle) {
+      rval = seq->push_front( 1, &flags );
+      if (MB_SUCCESS == rval) {
+        rval = typeData[MBENTITYSET].notify_prepended( seqptr );
+        if (MB_SUCCESS != rval)
+          seq->pop_front( 1 );
+      }
+      return rval;
+    }
+    else if (seq->end_handle() == handle + 1) {
+      rval = seq->push_back( 1, &flags );
+      if (MB_SUCCESS == rval) {
+        rval = typeData[MBENTITYSET].notify_appended( seqptr );
+        if (MB_SUCCESS != rval)
+          seq->pop_back( 1 );
+      }
+      return rval;
+    }
+    else
+      return MB_FAILURE; // should be unreachable
+  }
+  else {
+    if (data) {
+      seq = new MeshSetSequence( handle, 1, flags, data );
+    }
+    else {
+      assert( handle >= block_start && handle <= block_end );
+      trim_sequence_block( handle, block_end, DEFAULT_MESHSET_SEQUENCE_SIZE );
+      seq = new MeshSetSequence( handle, 1, flags, block_end - handle + 1 );
+    }
+    
+    MBErrorCode rval = typeData[MBENTITYSET].insert_sequence( seq );
+    if (MB_SUCCESS != rval) {
+      SequenceData* vdata = seq->data();
+      delete seq;
+      if (!data)
+        delete vdata;
+      return rval;
+    }
+  
+    return MB_SUCCESS;
+  }
+}
+
+void
+SequenceManager::trim_sequence_block( MBEntityHandle start_handle,
+                                      MBEntityHandle& end_handle,
+                                      unsigned max_size )
+{
+  assert( end_handle >= start_handle );
+  assert( (int)max_size > 0 ); // cast to int also prohibits some rediculously large values
+  
+    // if input range is larger than preferred size, trim it
+  if (end_handle - start_handle >= max_size)
+    end_handle = start_handle + max_size - 1;
+
+    // if range spans more than one proc, trim it to one
+  const unsigned rank = handleUtils.rank_from_handle( start_handle );
+  if (handleUtils.rank_from_handle(end_handle) != rank) 
+    end_handle = handleUtils.create_handle( TYPE_FROM_HANDLE(start_handle),
+                                            handleUtils.max_id(),
+                                            rank );
+}
+
+MBEntityHandle 
+SequenceManager::sequence_start_handle( MBEntityType type,
+                                        MBEntityID count,
+                                        int size,
+                                        MBEntityID start,
+                                        int proc,
+                                        SequenceData*& data )
+{
+  TypeSequenceManager &tsm = typeData[type];
+  data = 0;
+  MBEntityHandle handle = handleUtils.create_handle( type, start, proc );
+  if (start < MB_START_ID ||
+      !tsm.is_free_sequence( handle, count, data, size )) {
+    MBEntityHandle pstart = handleUtils.create_handle( type, MB_START_ID, proc );
+    MBEntityHandle pend   = handleUtils.create_handle( type, MB_END_ID,  proc );
+    handle = tsm.find_free_sequence( count, pstart, pend, data, size );
+  }
+  return handle;
+}
+
+MBErrorCode 
+SequenceManager::create_entity_sequence( MBEntityType type,
+                                         MBEntityID count,
+                                         int size,
+                                         MBEntityID start,
+                                         int proc,
+                                         MBEntityHandle& handle,
+                                         EntitySequence*& sequence )
+{
+  if (proc == -1)
+    proc = handleUtils.proc_rank();
+
+  SequenceData* data = 0;
+  handle = sequence_start_handle( type, count, size, start, proc, data );
+  if (!handle)
+    return MB_MEMORY_ALLOCATION_FAILED;
+  
+  switch (type) {
+  case MBENTITYSET:
+  case MBMAXTYPE:
+    return MB_TYPE_OUT_OF_RANGE;
+  
+  case MBVERTEX:
+    if (size != 0)
+      return MB_INDEX_OUT_OF_RANGE;
+
+    if (data)
+      sequence = new VertexSequence( handle, count, data );
+    else 
+      sequence = new VertexSequence( handle, count, count );
+    
+    break;
+  
+  default:
+    if (size == 0)
+      return MB_INDEX_OUT_OF_RANGE;
+
+    if (data)
+      sequence = new UnstructuredElemSeq( handle, count, size, data );
+    else 
+      sequence = new UnstructuredElemSeq( handle, count, size, count );
+
+    break;
+  }
+  
+  MBErrorCode result = typeData[type].insert_sequence( sequence );
+  if (MB_SUCCESS != result) {
+      // change to NULL if had an existing data or if no existing data,
+      // change to the new data created
+    data = data ? 0 : sequence->data();
+    delete sequence;
+    delete data;
+    return result;
+  }
+  
+  return MB_SUCCESS;
+}
+
+
+MBErrorCode 
+SequenceManager::create_meshset_sequence( MBEntityID count,
+                                          MBEntityID start,
+                                          int proc,
+                                          const unsigned* flags,
+                                          MBEntityHandle& handle,
+                                          EntitySequence*& sequence )
+{
+  if (proc == -1)
+    proc = handleUtils.proc_rank();
+
+  SequenceData* data = 0;
+  handle = sequence_start_handle( MBENTITYSET, count, 0, start, proc, data );
+  if (!handle)
+    return MB_MEMORY_ALLOCATION_FAILED;
+  
+  if (data)
+    sequence = new MeshSetSequence( handle, count, flags, data );
+  else
+    sequence = new MeshSetSequence( handle, count, flags, count );
+  
+  
+  
+  MBErrorCode result = typeData[MBENTITYSET].insert_sequence( sequence );
+  if (MB_SUCCESS != result) {
+      // change to NULL if had an existing data or if no existing data,
+      // change to the new data created
+    data = data ? 0 : sequence->data();
+    delete sequence;
+    delete data;
+    return result;
+  }
+  
+  return MB_SUCCESS;
+}
+
+
+MBErrorCode 
+SequenceManager::create_meshset_sequence( MBEntityID count,
+                                          MBEntityID start,
+                                          int proc,
+                                          unsigned flags,
+                                          MBEntityHandle& handle,
+                                          EntitySequence*& sequence )
+{
+  if (proc == -1)
+    proc = handleUtils.proc_rank();
+
+  SequenceData* data = 0;
+  handle = sequence_start_handle( MBENTITYSET, count, 0, start, proc, data );
+  if (!handle)
+    return MB_MEMORY_ALLOCATION_FAILED;
+  
+  if (data)
+    sequence = new MeshSetSequence( handle, count, flags, data );
+  else
+    sequence = new MeshSetSequence( handle, count, flags, count );
+  
+  
+  
+  MBErrorCode result = typeData[MBENTITYSET].insert_sequence( sequence );
+  if (MB_SUCCESS != result) {
+      // change to NULL if had an existing data or if no existing data,
+      // change to the new data created
+    data = data ? 0 : sequence->data();
+    delete sequence;
+    delete data;
+    return result;
+  }
+  
+  return MB_SUCCESS;
+}
+
+MBErrorCode
+SequenceManager::create_scd_sequence( int imin, int jmin, int kmin,
+                                      int imax, int jmax, int kmax,
+                                      MBEntityType type,
+                                      MBEntityID start_id_hint,
+                                      int processor_id,
+                                      MBEntityHandle& handle,
+                                      EntitySequence*& sequence )
+{
+  if (processor_id == -1)
+    processor_id = handleUtils.proc_rank();
+
+  int this_dim = MBCN::Dimension(type);
+
+    // use > instead of != in the following assert to also catch cases where imin > imax, etc.
+  assert((this_dim < 3 || kmax > kmin) &&
+         (this_dim < 2 || jmax > jmin) &&
+         (this_dim < 1 || imax > imin));
+
+    // compute # entities; not as easy as it would appear...
+  MBEntityID num_ent;
+  if (MBVERTEX == type)
+    num_ent = (MBEntityID)(imax-imin+1)*(MBEntityID)(jmax-jmin+1)*(MBEntityID)(kmax-kmin+1);
+  else {
+    num_ent = (imax-imin) *
+      (this_dim >= 2 ? (jmax-jmin) : 1) *
+      (this_dim >= 3 ? (kmax-kmin) : 1);
+  }
+  
+    // get a start handle
+  SequenceData* data = 0;
+  handle = sequence_start_handle( type, num_ent, -1, start_id_hint, processor_id, data );
+  if (!handle)
+    return MB_MEMORY_ALLOCATION_FAILED;
+  assert(!data);
+  
+  switch (type) {
+  case MBVERTEX:
+    data = new ScdVertexData( handle, imin, jmin, kmin, imax, jmax, kmax );
+    sequence = new VertexSequence( handle, data->size(), data );
+    break;
+  case MBEDGE:
+  case MBQUAD:
+  case MBHEX:
+    sequence = new StructuredElementSeq( handle, imin, jmin, kmin, imax, jmax, kmax );
+    break;
+  default:
+    return MB_TYPE_OUT_OF_RANGE;
+  }
+  
+  MBErrorCode result = typeData[type].insert_sequence( sequence );
+  if (MB_SUCCESS != result) {
+    data = sequence->data();
+    delete sequence;
+    delete data;
+    return result;
+  }
+  
+  return MB_SUCCESS;
+}
+
+MBErrorCode
+SequenceManager::create_scd_sequence( const HomCoord& coord_min,
+                                      const HomCoord& coord_max,
+                                      MBEntityType type,
+                                      MBEntityID start_id_hint,
+                                      int processor_id,
+                                      MBEntityHandle& first_handle_out,
+                                      EntitySequence*& sequence_out )
+{
+  return create_scd_sequence( coord_min.i(), coord_min.j(), coord_min.k(),
+                              coord_max.i(), coord_max.j(), coord_max.k(),
+                              type, start_id_hint,  processor_id,
+                              first_handle_out, sequence_out );
+}
+ 
+MBErrorCode
+SequenceManager::replace_subsequence( EntitySequence* new_seq, TagServer* ts )
+{
+  const MBEntityType type = TYPE_FROM_HANDLE(new_seq->start_handle());
+  return typeData[type].replace_subsequence( new_seq, ts );
+}
+
+void SequenceManager::get_memory_use( unsigned long& total_entity_storage,
+                                      unsigned long& total_storage ) const
+
+{
+  total_entity_storage = 0;
+  total_storage = 0;
+  unsigned long temp_entity, temp_total;
+  for (MBEntityType i = MBVERTEX; i < MBMAXTYPE; ++i) {
+    temp_entity = temp_total = 0;
+    get_memory_use( i, temp_entity, temp_total );
+    total_entity_storage += temp_entity;
+    total_storage        += temp_total;
+  }
+}
+
+void SequenceManager::get_memory_use( MBEntityType type,
+                                      unsigned long& total_entity_storage,
+                                      unsigned long& total_storage ) const
+{
+  typeData[type].get_memory_use( total_entity_storage, total_storage );
+}
+
+void SequenceManager::get_memory_use( const MBRange& entities,
+                                      unsigned long& total_entity_storage,
+                                      unsigned long& total_amortized_storage ) const
+{
+  total_entity_storage = 0;
+  total_amortized_storage = 0;
+  unsigned long temp_entity, temp_total;
+  MBRange::const_pair_iterator i;
+  for (i = entities.const_pair_begin(); i != entities.const_pair_end(); ++i) {
+    const MBEntityType t1 = TYPE_FROM_HANDLE(i->first);
+    const MBEntityType t2 = TYPE_FROM_HANDLE(i->second);
+    if (t1 == t2) {
+      temp_entity = temp_total = 0;
+      typeData[t1].get_memory_use( i->first, i->second, temp_entity, temp_total );
+      total_entity_storage += temp_entity;
+      total_amortized_storage += temp_total;
+    }
+    else {
+      int junk;
+ 
+      temp_entity = temp_total = 0;
+      typeData[t1].get_memory_use( i->first, CREATE_HANDLE(t1,MB_END_ID,junk), temp_entity, temp_total );
+      total_entity_storage += temp_entity;
+      total_amortized_storage += temp_total;
+
+      temp_entity = temp_total = 0;
+      typeData[t2].get_memory_use( CREATE_HANDLE(t2,MB_START_ID,junk), i->second, temp_entity, temp_total );
+      total_entity_storage += temp_entity;
+      total_amortized_storage += temp_total;
+    }
+  }
+}
+
+// These are meant to be called from the debugger (not declared in any header)
+// so leave them out of release builds (-DNDEBUG).
+#ifndef NDEBUG
+#include <iostream>
+
+std::ostream& operator<<( std::ostream& s, const TypeSequenceManager& seq_man )
+{
+  const SequenceData* prev_data = 0;
+  for (TypeSequenceManager::const_iterator i = seq_man.begin(); i != seq_man.end(); ++i) {
+    const EntitySequence* seq = *i;
+    if (seq->data() != prev_data) {
+      prev_data = seq->data();
+      s << "SequenceData [" 
+        << ID_FROM_HANDLE(seq->data()->start_handle())
+        << ","
+        << ID_FROM_HANDLE(seq->data()->end_handle())
+        << "]"
+        << std::endl;
+    }
+    s << "  Sequence [" 
+      << ID_FROM_HANDLE(seq->start_handle())
+      << ","
+      << ID_FROM_HANDLE(seq->end_handle())
+      << "]"
+      << std::endl;
+  }
+  return s;
+}
+
+std::ostream& operator<<( std::ostream& s, const SequenceManager& seq_man )
+{
+  for (MBEntityType t = MBVERTEX; t < MBMAXTYPE; ++t) 
+    if (!seq_man.entity_map(t).empty()) 
+      s << std::endl 
+        << "****************** " << MBCN::EntityTypeName( t ) << " ******************"
+        << std::endl << seq_man.entity_map(t) << std::endl;
+  return s;
+}
+
+void print_sequences( const SequenceManager& seqman )
+{
+  std::cout << seqman << std::endl;
+}
+
+void print_sequences( const TypeSequenceManager& seqman )
+{
+  std::cout << seqman << std::endl;
+}
+
+#endif

Added: MOAB/trunk/SequenceManager.hpp
===================================================================
--- MOAB/trunk/SequenceManager.hpp	                        (rev 0)
+++ MOAB/trunk/SequenceManager.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,146 @@
+#ifndef SEQUENCE_MANAGER_HPP
+#define SEQUENCE_MANAGER_HPP
+
+#include "TypeSequenceManager.hpp"
+#include "MBHandleUtils.hpp"
+
+class HomCoord;
+class TagServer;
+
+class SequenceManager 
+{
+  public:
+  
+    SequenceManager( const MBHandleUtils& handle_utils ) 
+      : handleUtils(handle_utils)
+      {}
+    
+    MBErrorCode find( MBEntityHandle handle, EntitySequence*& sequence_out ) const
+      { 
+        return typeData[TYPE_FROM_HANDLE(handle)].find( handle, sequence_out );
+      }
+    
+    void get_entities( MBEntityType type, MBRange& entities_out ) const
+      { typeData[type].get_entities( entities_out ); }
+    
+    MBEntityID get_number_entities( MBEntityType type ) const
+      { return typeData[type].get_number_entities(); }
+    
+    MBErrorCode replace_subsequence( EntitySequence* new_seq, TagServer* ts );
+    
+    MBErrorCode check_valid_entities( const MBRange& entities ) const;
+    
+    MBErrorCode delete_entity( MBEntityHandle entity );
+    
+    MBErrorCode delete_entities( const MBRange& entities );
+    
+    MBErrorCode create_vertex( unsigned processor_id,
+                               const double coords[3],
+                               MBEntityHandle& handle_out );
+    
+    MBErrorCode create_element( MBEntityType type,
+                                unsigned processor_id,
+                                const MBEntityHandle* conn_array,
+                                unsigned num_vertices,
+                                MBEntityHandle& handle_out );
+    
+    MBErrorCode create_mesh_set( unsigned processor_id,
+                                 unsigned flags,
+                                 MBEntityHandle& handle_out );
+    
+    MBErrorCode allocate_mesh_set( MBEntityHandle at_this_handle,
+                                   unsigned flags );
+    
+    MBErrorCode create_entity_sequence( MBEntityType type,
+                                        MBEntityID num_entities,
+                                        int nodes_per_entity,
+                                        MBEntityID start_id_hint,
+                                        int processor_id,
+                                        MBEntityHandle& first_handle_out,
+                                        EntitySequence*& sequence_out );
+    
+    MBErrorCode create_meshset_sequence( MBEntityID num_sets,
+                                         MBEntityID start_id_hint,
+                                         int processor_id,
+                                         const unsigned* flags,
+                                         MBEntityHandle& first_handle_out,
+                                         EntitySequence*& sequence_out );
+    
+    MBErrorCode create_meshset_sequence( MBEntityID num_sets,
+                                         MBEntityID start_id_hint,
+                                         int processor_id,
+                                         unsigned flags,
+                                         MBEntityHandle& first_handle_out,
+                                         EntitySequence*& sequence_out );
+    
+    MBErrorCode create_scd_sequence( int imin, int jmin, int kmin,
+                                     int imax, int jmax, int kmax,
+                                     MBEntityType type,
+                                     MBEntityID start_id_hint,
+                                     int processor_id,
+                                     MBEntityHandle& first_handle_out,
+                                     EntitySequence*& sequence_out );
+    
+    MBErrorCode create_scd_sequence( const HomCoord& coord_min,
+                                     const HomCoord& coord_max,
+                                     MBEntityType type,
+                                     MBEntityID start_id_hint,
+                                     int processor_id,
+                                     MBEntityHandle& first_handle_out,
+                                     EntitySequence*& sequence_out );
+                                     
+    
+    
+    
+    const TypeSequenceManager& entity_map( MBEntityType type ) const
+      { return typeData[type]; }
+    
+    void get_memory_use( unsigned long& total_entity_storage,
+                         unsigned long& total_storage ) const;
+                         
+    void get_memory_use( MBEntityType type,
+                         unsigned long& total_entity_storage,
+                         unsigned long& total_storage ) const;
+    
+    void get_memory_use( const MBRange& entities,
+                         unsigned long& total_entity_storage,
+                         unsigned long& total_amortized_storage ) const;
+    
+  
+  private:
+  
+    /**\brief Utility function for allocate_mesh_set (and similar)
+     *
+     * Given a block of available handles, determine the non-strict
+     * subset at which to create a new EntitySequence.
+     */
+    void trim_sequence_block( MBEntityHandle start_handle,
+                              MBEntityHandle& end_handle_in_out,
+                              unsigned maximum_sequence_size );
+  
+  
+      /**\brief Get range of handles in which to create an entity sequence
+       *
+       * Get range of handles in whcih to place a new entity sequence.
+       *\param type              The MBEntityType for the contents of the sequence
+       *\param entity_count      The number of entities in the range
+       *\param values_per_entity Vertices per element, zero for other types
+       *\param start_id_hint     Preferred id of first handle
+       *\param processor_rank    MPI processor ID
+       *\param data_out  Output: Either NULL or an existing SequenceData
+       *                         with a sufficiently large block to accomodate 
+       *                         the handle range.
+       *\return zero if no available handle range, start handle otherwise.
+       */
+    MBEntityHandle sequence_start_handle(   MBEntityType type,
+                                              MBEntityID entity_count,
+                                                     int values_per_entity,
+                                              MBEntityID start_id_hint,
+                                                     int processor_rank,
+                                          SequenceData*& data_out );
+  
+    const MBHandleUtils handleUtils;
+    TypeSequenceManager typeData[MBMAXTYPE];
+};
+
+#endif

Copied: MOAB/trunk/StructuredElementSeq.cpp (from rev 1308, MOAB/trunk/ScdElementSeq.cpp)
===================================================================
--- MOAB/trunk/StructuredElementSeq.cpp	                        (rev 0)
+++ MOAB/trunk/StructuredElementSeq.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,100 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ * 
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+
+#include "StructuredElementSeq.hpp"
+#include "ScdVertexData.hpp"
+#include "ScdElementData.hpp"
+#include "MBInterface.hpp"
+#include "MBReadUtilIface.hpp"
+#include "MBCN.hpp"
+#include "MBInternals.hpp"
+
+StructuredElementSeq::StructuredElementSeq(MBEntityHandle start_handle,
+                             const int imin, const int jmin, const int kmin,
+                             const int imax, const int jmax, const int kmax) 
+  : ElementSequence( start_handle, 
+                     ScdElementData::calc_num_entities( start_handle,
+                                                        imax-imin,
+                                                        jmax-jmin,
+                                                        kmax-kmin ),
+                     MBCN::VerticesPerEntity(TYPE_FROM_HANDLE(start_handle)),
+                     new ScdElementData( start_handle, 
+                                        imin, jmin, kmin,
+                                        imax, jmax, kmax ) )
+{
+}
+
+StructuredElementSeq::~StructuredElementSeq() 
+{
+}
+
+MBErrorCode StructuredElementSeq::get_connectivity( 
+                                        MBEntityHandle handle,
+                                        std::vector<MBEntityHandle>& connect,
+                                        bool /*topological*/ ) const
+{
+  int i, j, k;
+  MBErrorCode rval = get_params( handle, i, j, k );
+  if (MB_SUCCESS == rval)
+    rval = get_params_connectivity( i, j, k, connect );
+  return rval;
+}
+
+MBErrorCode StructuredElementSeq::get_connectivity( 
+                                        MBEntityHandle handle,
+                                        MBEntityHandle const*& connect,
+                                        int &connect_length,
+                                        bool topo,
+                                        std::vector<MBEntityHandle>* storage
+                                        ) const
+{
+  if (!storage) {
+    connect = 0;
+    connect_length = 0;
+    return MB_NOT_IMPLEMENTED;
+  }
+  
+  MBErrorCode rval = get_connectivity( handle, *storage, topo );
+  connect = &(*storage)[0];
+  connect_length = storage->size();
+  return rval;
+}
+
+MBErrorCode StructuredElementSeq::set_connectivity( 
+                                        MBEntityHandle,
+                                        MBEntityHandle const*,
+                                        int )
+{
+  return MB_NOT_IMPLEMENTED;
+}
+
+MBEntityHandle* StructuredElementSeq::get_connectivity_array()
+  { return 0; }
+
+int StructuredElementSeq::values_per_entity() const
+  { return -1; } // never reuse freed handles for structured elements 
+
+EntitySequence* StructuredElementSeq::split( MBEntityHandle here )
+  { return new StructuredElementSeq( *this, here ); }
+
+SequenceData* StructuredElementSeq::create_data_subset( MBEntityHandle, MBEntityHandle ) const
+  { return 0; }
+
+void StructuredElementSeq::get_const_memory_use( unsigned long& bytes_per_entity,
+                                                 unsigned long& sequence_size ) const
+{
+  sequence_size = sizeof(*this);
+  bytes_per_entity = sdata()->get_memory_use() / sdata()->size();
+}

Copied: MOAB/trunk/StructuredElementSeq.hpp (from rev 1308, MOAB/trunk/ScdElementSeq.hpp)
===================================================================
--- MOAB/trunk/StructuredElementSeq.hpp	                        (rev 0)
+++ MOAB/trunk/StructuredElementSeq.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,158 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ * 
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ */
+
+#ifndef STRUCTURED_ELEMENT_SEQUENCE
+#define STRUCTURED_ELEMENT_SEQUENCE
+
+//
+// Class: StructuredElementSeq
+//
+// Purpose: represent a rectangular element of mesh
+//
+// A ScdElement represents a rectangular element of mesh, including both vertices and
+// elements, and the parametric space used to address that element.  Vertex data,
+// i.e. coordinates, may not be stored directly in the element, but the element returns
+// information about the vertex handles of vertices in the element.  Vertex and element
+// handles associated with the element are each contiguous.
+
+#include "ElementSequence.hpp"
+#include "ScdElementData.hpp"
+
+class StructuredElementSeq : public ElementSequence
+{
+public:
+
+    //! constructor
+  StructuredElementSeq(
+                MBEntityHandle start_handle,
+                const int imin, const int jmin, const int kmin,
+                const int imax, const int jmax, const int kmax);
+  
+  virtual ~StructuredElementSeq();
+
+  ScdElementData* sdata() 
+    { return reinterpret_cast<ScdElementData*>(data()); }
+  ScdElementData const* sdata() const 
+    { return reinterpret_cast<const ScdElementData*>(data()); }
+
+    //! get handle of vertex at i, j, k
+  MBEntityHandle get_vertex(const int i, const int j, const int k) const
+    { return get_vertex( HomCoord(i,j,k) ); }
+  
+    //! get handle of vertex at homogeneous coords
+  inline MBEntityHandle get_vertex(const HomCoord &coords) const
+    { return sdata()->get_vertex(coords); }
+  
+    //! get handle of element at i, j, k
+  MBEntityHandle get_element(const int i, const int j, const int k) const
+    { return sdata()->get_element( i, j, k ); }
+  
+    //! get handle of element at homogeneous coords
+  MBEntityHandle get_element(const HomCoord &coords) const
+    { return sdata()->get_element( coords.i(), coords.j(), coords.k() ); }
+  
+    //! get min params for this element
+  const HomCoord &min_params() const
+    { return sdata()->min_params(); }
+  void min_params(HomCoord &coords) const
+    { coords = min_params(); }
+  void min_params(int &i, int &j, int &k) const
+    { i = min_params().i(); j = min_params().j(); k = min_params().k(); }
+
+    //! get max params for this element
+  const HomCoord &max_params() const
+    { return sdata()->max_params(); }
+  void max_params(HomCoord &coords) const
+    { coords = max_params(); }
+  void max_params(int &i, int &j, int &k) const
+    { i = max_params().i(); j = max_params().j(); k = max_params().k(); }
+  
+    //! get the number of vertices in each direction, inclusive
+  void param_extents(int &di, int &dj, int &dk) const
+    { sdata()->param_extents( di, dj, dk ); }
+
+    //! given a handle, get the corresponding parameters
+  MBErrorCode get_params(const MBEntityHandle ehandle,
+                          int &i, int &j, int &k) const
+    { return sdata()->get_params( ehandle, i, j, k ); }
+  
+    //! convenience functions for parameter extents
+  int i_min() const {return min_params().i();}
+  int j_min() const {return min_params().j();}
+  int k_min() const {return min_params().k();}
+  int i_max() const {return max_params().i();}
+  int j_max() const {return max_params().j();}
+  int k_max() const {return max_params().k();}
+
+    //! test the bounding vertex sequences and determine whether they fully
+    //! define the vertices covering this element block's parameter space
+  inline bool boundary_complete() const
+    { return sdata()->boundary_complete(); }
+
+    //! test whether this sequence contains these parameters
+  bool contains(const int i, const int j, const int k) const
+    { return sdata()->contains(HomCoord(i,j,k)); }
+  inline bool contains(const HomCoord &coords) const
+    { return sdata()->contains(coords); }
+
+    //! get connectivity of an entity given entity's parameters
+  MBErrorCode get_params_connectivity(const int i, const int j, const int k,
+                                std::vector<MBEntityHandle>& connectivity) const
+    { return sdata()->get_params_connectivity( i, j, k, connectivity ); }
+  
+  
+    /***************** Methods from ElementSequence *****************/
+
+  virtual MBErrorCode get_connectivity( MBEntityHandle handle,
+                                        std::vector<MBEntityHandle>& connect,
+                                        bool topological = false ) const;
+  
+  virtual MBErrorCode get_connectivity( MBEntityHandle handle,
+                                        MBEntityHandle const*& connect,
+                                        int &connect_length,
+                                        bool topological = false,
+                                        std::vector<MBEntityHandle>* storage = 0
+                                       ) const;
+
+  virtual MBErrorCode set_connectivity( MBEntityHandle handle,
+                                        MBEntityHandle const* connect,
+                                        int connect_length );
+  
+  virtual MBEntityHandle* get_connectivity_array();
+  
+  
+    /***************** Methods from EntitySequence *****************/
+
+    /* Replace the ElementSequence implementation of this method with
+     * one that always returns zero, because we cannot re-use handles
+     * that are within a ScdElementData
+     */
+  virtual int values_per_entity() const;
+
+  virtual EntitySequence* split( MBEntityHandle here );
+
+  virtual SequenceData* create_data_subset( MBEntityHandle start_handle,
+                                            MBEntityHandle end_handle ) const;
+
+  virtual void get_const_memory_use( unsigned long& bytes_per_entity,
+                                     unsigned long& size_of_sequence ) const;
+
+protected:
+  StructuredElementSeq( StructuredElementSeq& split_from, MBEntityHandle here )
+    : ElementSequence( split_from, here )
+    {}
+};
+
+#endif

Added: MOAB/trunk/TestTypeSequenceManager.cpp
===================================================================
--- MOAB/trunk/TestTypeSequenceManager.cpp	                        (rev 0)
+++ MOAB/trunk/TestTypeSequenceManager.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,972 @@
+#include "TypeSequenceManager.hpp"
+#include "EntitySequence.hpp"
+#include "SequenceData.hpp"
+#include "TestUtil.hpp"
+
+void test_basic();
+void test_lower_bound();
+void test_upper_bound();
+void test_find();
+void test_get_entities();
+void test_insert_sequence_merge();
+void test_insert_sequence_nomerge();
+void test_remove_sequence();
+void test_replace_subsequence();
+void test_erase();
+void test_find_free_handle();
+void test_find_free_sequence();
+void test_is_free_sequence();
+void test_is_free_handle();
+
+/* Construct sequence of vertex handles containing 
+   the ID ranges: { [3,7], [100,111], [1001] }
+   referencing a SequnceData with the range [1,22000]
+ */
+void make_basic_sequence( TypeSequenceManager& seq );
+void make_basic_sequence( TypeSequenceManager& seq,
+                          EntitySequence*& seq3to7,
+                          EntitySequence*& seq100to111,
+                          EntitySequence*& seq1001 );
+
+/* Compare expected sequence contents to actual contents.
+ * Also does some consistency checks.
+ */
+bool seqman_equal( const MBEntityHandle pair_array[][2],
+                     unsigned num_pairs,
+                     const TypeSequenceManager& seqman );
+
+/*
+ * Insert a sequence into a sequence manager.  Delete passed
+ * sequence (and optionally SequenceData) if insertion fails.
+ */
+MBErrorCode insert_seq( TypeSequenceManager& seqman, 
+                        MBEntityHandle start_handle,
+                        MBEntityID count,
+                        SequenceData* data,
+                        bool del_data = false );
+
+int main( )
+{
+  if (RUN_TEST( test_basic )) {
+    printf( "BASIC USE TEST FAILED\nCANNOT TEST FURTHER\n" );
+    return 1;
+  }
+
+  int error_count = 0;
+  error_count += RUN_TEST( test_lower_bound );
+  error_count += RUN_TEST( test_upper_bound );
+  error_count += RUN_TEST( test_find );
+  error_count += RUN_TEST( test_get_entities );
+  error_count += RUN_TEST( test_insert_sequence_merge );
+  error_count += RUN_TEST( test_insert_sequence_nomerge );
+  error_count += RUN_TEST( test_remove_sequence );
+  error_count += RUN_TEST( test_replace_subsequence );
+  error_count += RUN_TEST( test_erase );
+  error_count += RUN_TEST( test_find_free_handle );
+  error_count += RUN_TEST( test_find_free_sequence );
+  error_count += RUN_TEST( test_is_free_sequence );
+  
+  if (!error_count) 
+    printf( "ALL TESTS PASSED\n");
+  else
+    printf( "%d TESTS FAILED\n", error_count );
+  
+  return error_count;
+}
+
+class DumSeq : public EntitySequence {
+  private:
+    int valsPerEnt;
+  public: 
+    DumSeq( MBEntityHandle start, MBEntityID count, SequenceData* data, int vals_per_ent = 0 )
+      : EntitySequence( start, count, data), valsPerEnt(vals_per_ent) {}
+    DumSeq( SequenceData* data, int vals_per_ent = 0 )
+      : EntitySequence( data->start_handle(), data->size(), data), valsPerEnt(vals_per_ent) {}
+    
+    DumSeq( DumSeq& split_from, MBEntityHandle here )
+      : EntitySequence( split_from, here ), valsPerEnt(split_from.valsPerEnt) {}
+    
+    virtual ~DumSeq() {}
+    
+    EntitySequence* split( MBEntityHandle here )
+      { return new DumSeq(*this, here); }
+    
+    SequenceData* create_data_subset( MBEntityHandle a, MBEntityHandle b ) const
+      { return data()->subset( a, b, 0, 0 ); }
+    
+    void get_const_memory_use( unsigned long& a, unsigned long& b) const
+      { a = b = 0; }
+    unsigned long get_per_entity_memory_use( MBEntityHandle, MBEntityHandle) const
+      { return 0; }
+    
+    int values_per_entity() const
+      { return valsPerEnt; }
+};
+  
+void make_basic_sequence( TypeSequenceManager& seqman )
+{
+  EntitySequence *s, *t, *u;
+  make_basic_sequence( seqman, s, t, u );
+}
+
+
+MBErrorCode insert_seq( TypeSequenceManager& seqman, 
+                        MBEntityHandle start_handle,
+                        MBEntityID count,
+                        SequenceData* data,
+                        bool del_data )
+{
+  EntitySequence* seq = new DumSeq( start_handle, count, data );
+  MBErrorCode rval = seqman.insert_sequence( seq );
+  if (MB_SUCCESS != rval) {
+    delete seq;
+    if (del_data)
+      delete data;
+  }
+  else {
+    CHECK( start_handle >= seq->start_handle() );
+    CHECK( start_handle+count-1 <= seq->  end_handle() );
+  }
+  return rval;
+}
+
+void make_basic_sequence( TypeSequenceManager& seqman,
+                          EntitySequence*& seq1,
+                          EntitySequence*& seq2,
+                          EntitySequence*& seq3 )
+{
+  CHECK( seqman.empty() );
+  CHECK_EQUAL( (MBEntityID)0, seqman.get_number_entities() );
+  
+  SequenceData* data = new SequenceData( 0, 1, 22000 );
+  CHECK_ERR( insert_seq( seqman,    3,  5, data ) );
+  CHECK_ERR( insert_seq( seqman,  100, 12, data ) );
+  CHECK_ERR( insert_seq( seqman, 1001,  1, data ) );
+
+  CHECK( !seqman.empty() );
+  CHECK_EQUAL( (MBEntityID)18, seqman.get_number_entities() );
+  
+  TypeSequenceManager::iterator iter = seqman.begin();
+  CHECK( iter != seqman.end() );
+  seq1 = *iter;
+  CHECK_EQUAL( (MBEntityHandle)3, seq1->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)7, seq1->end_handle() );
+  CHECK_EQUAL( data, seq1->data() );
+  CHECK_EQUAL( (MBEntityID)5, seq1->size() );
+  
+  ++iter;
+  CHECK( iter != seqman.end() );
+  seq2 = *iter;
+  CHECK_EQUAL( (MBEntityHandle)100, seq2->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)111, seq2->end_handle() );
+  CHECK_EQUAL( data, seq2->data() );
+  CHECK_EQUAL( (MBEntityID)12, seq2->size() );
+  
+  ++iter;
+  seq3 = *iter;
+  CHECK( iter != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)1001, seq3->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)1001, seq3->end_handle() );
+  CHECK_EQUAL( data, seq3->data() );
+  CHECK_EQUAL( (MBEntityID)1, seq3->size() );
+  
+  ++iter;
+  CHECK( iter == seqman.end() );
+}
+
+void test_basic( )
+{
+  TypeSequenceManager seqman;
+  make_basic_sequence( seqman );
+}
+
+void test_lower_bound()
+{
+  TypeSequenceManager::const_iterator i;
+  TypeSequenceManager seqman;
+  EntitySequence *seq1, // 3 to 7
+                 *seq2, // 100 to 111
+                 *seq3; // 1001
+  make_basic_sequence( seqman, seq1, seq2, seq3 );
+  
+  i = seqman.lower_bound( 2 );
+  CHECK_EQUAL( seq1, *i );
+  i = seqman.lower_bound( 3 );
+  CHECK_EQUAL( seq1, *i );
+  i = seqman.lower_bound( 4 );
+  CHECK_EQUAL( seq1, *i );
+  i = seqman.lower_bound( 7 );
+  CHECK_EQUAL( seq1, *i );
+  
+  i = seqman.lower_bound( 8 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.lower_bound( 99 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.lower_bound( 100 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.lower_bound( 110 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.lower_bound( 111 );
+  CHECK_EQUAL( seq2, *i );
+  
+  i = seqman.lower_bound( 112 );
+  CHECK_EQUAL( seq3, *i );
+  i = seqman.lower_bound( 1000 );
+  CHECK_EQUAL( seq3, *i );
+  i = seqman.lower_bound( 1001 );
+  CHECK_EQUAL( seq3, *i );
+  
+  i = seqman.lower_bound( 1002 );
+  CHECK( i == seqman.end() );
+}
+
+
+void test_upper_bound()
+{
+  TypeSequenceManager::const_iterator i;
+  TypeSequenceManager seqman;
+  EntitySequence *seq1, // 3 to 7
+                 *seq2, // 100 to 111
+                 *seq3; // 1001
+  make_basic_sequence( seqman, seq1, seq2, seq3 );
+  
+  i = seqman.upper_bound( 2 );
+  CHECK_EQUAL( seq1, *i );
+  
+  i = seqman.upper_bound( 3 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.upper_bound( 4 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.upper_bound( 7 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.upper_bound( 8 );
+  CHECK_EQUAL( seq2, *i );
+  i = seqman.upper_bound( 99 );
+  CHECK_EQUAL( seq2, *i );
+  
+  i = seqman.upper_bound( 100 );
+  CHECK_EQUAL( seq3, *i );
+  i = seqman.upper_bound( 110 );
+  CHECK_EQUAL( seq3, *i );
+  i = seqman.upper_bound( 111 );
+  CHECK_EQUAL( seq3, *i );
+  i = seqman.upper_bound( 112 );
+  CHECK_EQUAL( seq3, *i );
+  i = seqman.upper_bound( 1000 );
+  CHECK_EQUAL( seq3, *i );
+
+  i = seqman.upper_bound( 1001 );
+  CHECK( i == seqman.end() );
+}
+
+void test_find()
+{
+  TypeSequenceManager seqman;
+  EntitySequence *seq1, // 3 to 7
+                 *seq2, // 100 to 111
+                 *seq3, // 1001
+                 *seq;
+  make_basic_sequence( seqman, seq1, seq2, seq3 );
+  
+  seq = seqman.find( 2 );
+  CHECK_EQUAL( NULL, seq );
+  seq = seqman.find( 3 );
+  CHECK_EQUAL( seq1, seq );
+  seq = seqman.find( 4 );
+  CHECK_EQUAL( seq1, seq );
+  seq = seqman.find( 7 );
+  CHECK_EQUAL( seq1, seq );
+  seq = seqman.find( 8 );
+  CHECK_EQUAL( NULL, seq );
+
+  seq = seqman.find( 99 );
+  CHECK_EQUAL( NULL, seq );
+  seq = seqman.find( 100 );
+  CHECK_EQUAL( seq2, seq );
+  seq = seqman.find( 110 );
+  CHECK_EQUAL( seq2, seq );
+  seq = seqman.find( 111 );
+  CHECK_EQUAL( seq2, seq );
+  seq = seqman.find( 112 );
+  CHECK_EQUAL( NULL, seq );
+
+  seq = seqman.find( 1000 );
+  CHECK_EQUAL( NULL, seq );
+  seq = seqman.find( 1001 );
+  CHECK_EQUAL( seq3, seq );
+  seq = seqman.find( 1002 );
+  CHECK_EQUAL( NULL, seq );
+}
+
+bool seqman_equal( const MBEntityHandle pair_array[][2],
+                     unsigned num_pairs,
+                     const TypeSequenceManager& seqman )
+{
+  unsigned i;
+  TypeSequenceManager::const_iterator j = seqman.begin();
+  EntitySequence* seq = 0;
+  for (i = 0; i < num_pairs; ++i, ++j) {
+    if (j == seqman.end())
+      break;
+    
+    if (seq && seq->end_handle() >= (*j)->start_handle()) {
+      printf( "Sequence [%lu,%lu] overlaps sequence [%lu,%lu]\n",
+       (unsigned long)ID_FROM_HANDLE( seq->start_handle()),
+       (unsigned long)ID_FROM_HANDLE( seq->  end_handle()),
+       (unsigned long)ID_FROM_HANDLE((*j)->start_handle()),
+       (unsigned long)ID_FROM_HANDLE((*j)->  end_handle()));
+      return false;
+    }
+    
+    if (seq && seq->data() != (*j)->data() && 
+        seq->data()->end_handle() >= (*j)->data()->start_handle()) {
+      printf( "SequenceData [%lu,%lu] overlaps SequenceData [%lu,%lu]\n",
+       (unsigned long)ID_FROM_HANDLE( seq->data()->start_handle()),
+       (unsigned long)ID_FROM_HANDLE( seq->data()->  end_handle()),
+       (unsigned long)ID_FROM_HANDLE((*j)->data()->start_handle()),
+       (unsigned long)ID_FROM_HANDLE((*j)->data()->  end_handle()));
+      return false;
+    }
+    
+    seq = *j;
+    if (seq->start_handle() > seq->end_handle()) {
+      printf( "Inverted sequence [%lu,%lu]\n",
+       (unsigned long)ID_FROM_HANDLE( seq->start_handle()),
+       (unsigned long)ID_FROM_HANDLE( seq->  end_handle()));
+      return false;
+    }
+   
+    if (pair_array[i][0] != seq->start_handle() ||
+        pair_array[i][1] != seq->end_handle())
+      break;
+      
+    if (seq->data()->start_handle() > seq->start_handle() ||
+        seq->data()->  end_handle() < seq->  end_handle()) {
+      printf( "Sequence [%lu,%lu] has data [%lu,%lu]\n",
+       (unsigned long)ID_FROM_HANDLE(seq->start_handle()),
+       (unsigned long)ID_FROM_HANDLE(seq->  end_handle()),
+       (unsigned long)ID_FROM_HANDLE(seq->data()->start_handle()),
+       (unsigned long)ID_FROM_HANDLE(seq->data()->  end_handle()));
+      return false;
+    }
+  }
+  
+  if (i == num_pairs && j == seqman.end())
+    return true;
+  
+  if (i < num_pairs)
+    printf("Sequence Mismatch: Expected: [%lu,%lu], got ",
+       (unsigned long)ID_FROM_HANDLE(pair_array[i][0]),
+       (unsigned long)ID_FROM_HANDLE(pair_array[i][1]) );
+  else
+    printf("Sequence Mismatch: Expected END, got " );
+  
+  if (j == seqman.end())
+    printf( "END.\n" );
+  else
+    printf("[%lu,%lu]\n",
+      (unsigned long)ID_FROM_HANDLE((*j)->start_handle()),
+      (unsigned long)ID_FROM_HANDLE((*j)->end_handle()) );
+  
+  return false;
+}
+
+void test_get_entities()
+{
+  TypeSequenceManager seqman;
+  make_basic_sequence( seqman );
+
+  CHECK( !seqman.empty() );
+  CHECK_EQUAL( (MBEntityID)18, seqman.get_number_entities() );
+
+  MBRange entities;
+  seqman.get_entities( entities );
+  CHECK_EQUAL( (MBEntityHandle)18, entities.size() );
+
+  MBEntityHandle pairs[][2] = { {3, 7}, {100, 111}, {1001, 1001} };
+  CHECK( seqman_equal( pairs, 3, seqman ) );
+}
+
+
+void test_insert_sequence_merge()
+{
+  TypeSequenceManager seqman;
+  make_basic_sequence( seqman );
+  SequenceData* data = (*seqman.begin())->data();
+
+    // append a sequence
+  CHECK_ERR( insert_seq( seqman, 1003, 1, data ) );
+  MBEntityHandle exp1[][2] = { { 3, 7}, {100, 111}, {1001, 1001}, {1003, 1003} };
+  CHECK( seqman_equal( exp1, 4, seqman ) );
+  
+    // prepend a sequence
+  CHECK_ERR( insert_seq( seqman, 1, 1, data ) );
+  MBEntityHandle exp2[][2] = { {1, 1}, { 3, 7}, {100, 111}, {1001, 1001}, {1003, 1003} };
+  CHECK( seqman_equal( exp2, 5, seqman ) );
+  
+    // insert sequence in middle
+  CHECK_ERR( insert_seq( seqman, 150, 11, data ) );
+  MBEntityHandle exp3[][2] = { {1, 1}, { 3, 7}, {100, 111}, {150,160}, {1001, 1001}, {1003, 1003} };
+  CHECK( seqman_equal( exp3, 6, seqman ) );
+  
+    // merge sequence with predecessor
+  CHECK_ERR( insert_seq( seqman, 8, 13, data ) );
+  MBEntityHandle exp4[][2] = { {1, 1}, { 3, 20}, {100, 111}, {150,160}, {1001, 1001}, {1003, 1003} };
+  CHECK( seqman_equal( exp4, 6, seqman ) );
+  
+    // merge sequence with following one
+  CHECK_ERR( insert_seq( seqman, 87, 13, data ) );
+  MBEntityHandle exp5[][2] = { {1, 1}, { 3, 20}, {87, 111}, {150,160}, {1001, 1001}, {1003, 1003} };
+  CHECK( seqman_equal( exp5, 6, seqman ) );
+  
+    // merge sequence with two adjacent ones
+  CHECK_ERR( insert_seq( seqman, 2, 1, data ) );
+  MBEntityHandle exp6[][2] = { {1, 20}, {87, 111}, {150,160}, {1001, 1001}, {1003, 1003} };
+  CHECK( seqman_equal( exp6, 5, seqman ) );
+  
+    // try to insert a sequence that overlaps on the end
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, insert_seq( seqman, 900, 1001, data ) );
+  
+    // try to insert a sequence that overlaps at the start
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, insert_seq( seqman, 111, 140, data ) );
+}
+
+
+void test_insert_sequence_nomerge()
+{
+  TypeSequenceManager seqman;
+  
+    // make sure inserting a sequence w/out a SequenceData fails
+  CHECK_EQUAL( MB_FAILURE, insert_seq( seqman, 1, 5, NULL ) );
+  CHECK( seqman.empty() );
+  
+    // Now set up a TypeSequenceManager for testing
+  
+    // Insert an EntitySequence for which the corresponding SequenceData
+    // is exactly the same size.
+  SequenceData *data1 = new SequenceData( 0, 3, 7 );
+  DumSeq* seq = new DumSeq( data1 );
+  MBErrorCode rval = seqman.insert_sequence( seq );
+  if (MB_SUCCESS != rval) {
+    delete seq;
+    delete data1;
+  }
+  CHECK_ERR( rval );
+  
+    // Insert an EntitySequence with additional room on both ends of
+    // the SequenceData
+  SequenceData *data2 = new SequenceData( 0, 100, 999 );
+  seq = new DumSeq( 200, 100, data2 );
+  rval = seqman.insert_sequence( seq );
+  if (MB_SUCCESS != rval) {
+    delete seq;
+    delete data2;
+  }
+  CHECK_ERR( rval );
+  
+    // Insert another EntitySequence sharing the previous SequenceData
+  seq = new DumSeq( 400, 100, data2 );
+  rval = seqman.insert_sequence( seq );
+  if (MB_SUCCESS != rval)
+    delete seq;
+  CHECK_ERR( rval );
+  
+    
+      // Setup complete, begin tests
+  
+    // Test inserting sequence that appends an existing sequence
+    // but overlaps underling SequenceData boundary
+  SequenceData* data = new SequenceData( 0, 999, 1000 );
+  seq = new DumSeq( data );
+  rval = seqman.insert_sequence( seq );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, rval );
+  delete seq;
+  delete data;
+  
+    // Test inserting sequence that prepends an existing sequence
+    // but overlaps underling SequenceData boundary
+  data = new SequenceData( 0, 50, 199 );
+  seq = new DumSeq( data );
+  rval = seqman.insert_sequence( seq );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, rval );
+  delete seq;
+  delete data;
+  
+    // Test fits within existing, but has different SequenceData
+  data = new SequenceData( 0, 500, 599 );
+  seq = new DumSeq( data );
+  rval = seqman.insert_sequence( seq );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, rval );
+  delete seq;
+  delete data;
+  
+    // Make sure we're starting out with what we exoect
+  MBEntityHandle exp1[][2] = { { 3, 7}, {200, 299}, {400, 499} };
+  CHECK( seqman_equal( exp1, 3, seqman ) );
+  
+    // Test fits within existing, and has same data
+  CHECK_ERR( insert_seq( seqman, 600, 100, data2 ) );
+  MBEntityHandle exp2[][2] = { { 3, 7}, {200, 299}, {400, 499}, {600, 699} };
+  CHECK( seqman_equal( exp2, 4, seqman ) );
+  
+    // Test is entirely outside existing data
+  CHECK_ERR( insert_seq( seqman, 2000, 2, new SequenceData( 0, 2000, 2001 ), true ) );
+  MBEntityHandle exp3[][2] = { { 3, 7}, {200, 299}, {400, 499}, {600, 699}, {2000, 2001} };
+  CHECK( seqman_equal( exp3, 5, seqman ) );
+  
+    // Test abutts end of exising data
+  CHECK_ERR( insert_seq( seqman, 1000, 6, new SequenceData( 0, 1000, 1005 ), true ) );
+  MBEntityHandle exp4[][2] = { { 3, 7}, {200, 299}, {400, 499}, {600, 699}, {1000,1005}, {2000, 2001} };
+  CHECK( seqman_equal( exp4, 6, seqman ) );
+ 
+    // Test abutts beginning of exising data
+  CHECK_ERR( insert_seq( seqman, 50, 50, new SequenceData( 0, 50, 99 ), true ) );
+  MBEntityHandle exp5[][2] = { { 3, 7}, {50, 99}, {200, 299}, {400, 499}, {600, 699}, {1000,1005}, {2000, 2001} };
+  CHECK( seqman_equal( exp5, 7, seqman ) );
+}
+
+void test_remove_sequence()
+{
+  TypeSequenceManager seqman;
+  EntitySequence *seq1, // 3 to 7
+                 *seq2, // 100 to 111
+                 *seq3; // 1001
+  make_basic_sequence( seqman, seq1, seq2, seq3 );
+
+    // test removing something that hasn't been inserted
+  bool last;
+  DumSeq junk( 3, 5, NULL );
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, seqman.remove_sequence( &junk, last ) );
+  MBEntityHandle exp1[][2] = { { 3, 7}, {100, 111}, {1001, 1001} };
+  CHECK( seqman_equal( exp1, 3, seqman ) );
+  
+    // remove the middle sequence
+  CHECK_ERR( seqman.remove_sequence( seq2, last ) );
+  CHECK( !last );
+  delete seq2;
+  
+    // remove the first sequence
+  CHECK_ERR( seqman.remove_sequence( seq1, last ) );
+  CHECK( !last );
+  delete seq1;
+  
+    // remove the last sequence
+  CHECK_ERR( seqman.remove_sequence( seq3, last ) );
+  CHECK( last );
+  SequenceData* data = seq3->data();
+  delete seq3;
+  delete data;
+}
+
+void test_replace_subsequence()
+{
+  MBErrorCode rval;
+  TypeSequenceManager seqman;
+
+    // create an intial set
+  SequenceData* data1 = new SequenceData( 0, 51, 950 );
+  CHECK_ERR( insert_seq( seqman, 101, 100, data1, true ) );
+  CHECK_ERR( insert_seq( seqman, 301, 300, data1 ) );
+  CHECK_ERR( insert_seq( seqman, 701, 100, data1 ) );
+  
+    // try a sequence that is outside all existing data
+  SequenceData* data = new SequenceData( 0, 10, 20 );
+  DumSeq* seq = new DumSeq( data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  CHECK_EQUAL( MB_FAILURE, rval );
+  delete seq;
+  delete data;
+  
+    // try a sequence that overlaps the start of the data
+  data = new SequenceData( 0, 40, 60 );
+  seq = new DumSeq( data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  CHECK_EQUAL( MB_FAILURE, rval );
+  delete seq;
+  delete data;
+  
+    // try a sequence that is within the data but not within any sequence
+  data = new SequenceData( 0, 60, 70 );
+  seq = new DumSeq( data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  CHECK_EQUAL( MB_FAILURE, rval );
+  delete seq;
+  delete data;
+  
+    // try a sequence that overlaps an existing sequence
+  data = new SequenceData( 0, 60, 101 );
+  seq = new DumSeq( data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  CHECK_EQUAL( MB_FAILURE, rval );
+  delete seq;
+  delete data;
+  
+    // try a sequence that should work, but with a SequenceData that 
+    // overlaps an existing sequence
+  data = new SequenceData( 0, 150, 200 );
+  seq = new DumSeq( 190, 200, data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  CHECK_EQUAL( MB_FAILURE, rval );
+  delete seq;
+  delete data;
+  
+    // check that we're starting with what we expect
+  MBEntityHandle exp1[][2] = { {101, 200}, {301, 600}, {701, 800} };
+  CHECK( seqman_equal( exp1, 3, seqman ) );
+  
+    // split at start of sequence
+  data = new SequenceData( 0, 101, 105 );
+  seq = new DumSeq( data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  if (MB_SUCCESS != rval) {
+    delete seq;
+    delete data;
+  }
+  CHECK_ERR( rval );
+  MBEntityHandle exp2[][2] = { {101, 105}, {106, 200}, {301, 600}, {701, 800} };
+  CHECK( seqman_equal( exp2, 4, seqman ) );
+  
+    // split at end of sequence
+  data = new SequenceData( 0, 750, 800 );
+  seq = new DumSeq( data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  if (MB_SUCCESS != rval) {
+    delete seq;
+    delete data;
+  }
+  CHECK_ERR( rval );
+  MBEntityHandle exp3[][2] = { {101, 105}, {106, 200}, {301, 600}, {701, 749}, {750,800} };
+  CHECK( seqman_equal( exp3, 5, seqman ) );
+  
+    // split at middle of sequence
+  data = new SequenceData( 0, 400, 499 );
+  seq = new DumSeq( data );
+  rval = seqman.replace_subsequence( seq, 0 );
+  if (MB_SUCCESS != rval) {
+    delete seq;
+    delete data;
+  }
+  CHECK_ERR( rval );
+  MBEntityHandle exp4[][2] = { {101, 105}, {106, 200}, {301, 399}, {400,499}, {500,600}, {701, 749}, {750,800} };
+  CHECK( seqman_equal( exp4, 7, seqman ) );
+}
+
+void test_erase()
+{
+  TypeSequenceManager seqman;
+  make_basic_sequence( seqman );
+
+    // verify initial state
+  MBEntityHandle exp1[][2] = { {3, 7}, {100, 111}, {1001, 1001} };
+  CHECK( seqman_equal( exp1, 3, seqman ) );
+
+    // try erasing invalid handles at start of existing sequence
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, seqman.erase( 1000, 1001 ) );
+    // try erasing invalid entities at end of existing sequence
+  CHECK_EQUAL( MB_ENTITY_NOT_FOUND, seqman.erase( 3, 8 ) );
+    // verify initial state
+  CHECK( seqman_equal( exp1, 3, seqman ) );
+  
+    // erase from front of sequence
+  CHECK_ERR( seqman.erase( 3, 6 ) );
+  MBEntityHandle exp2[][2] = { {7, 7}, {100, 111}, {1001, 1001} };
+  CHECK( seqman_equal( exp2, 3, seqman ) );
+  
+    // erase from end of sequence
+  CHECK_ERR( seqman.erase( 110, 111 ) );
+  MBEntityHandle exp3[][2] = { {7, 7}, {100, 109}, {1001, 1001} };
+  CHECK( seqman_equal( exp3, 3, seqman ) );
+  
+    // erase from middle of sequence
+  CHECK_ERR( seqman.erase( 105, 107 ) );
+  MBEntityHandle exp4[][2] = { {7, 7}, {100, 104}, {108,109}, {1001, 1001} };
+  CHECK( seqman_equal( exp4, 4, seqman ) );
+  
+    // erase sequence
+  CHECK_ERR( seqman.erase( 7, 7 ) );
+  MBEntityHandle exp5[][2] = { {100, 104}, {108,109}, {1001, 1001} };
+  CHECK( seqman_equal( exp5, 3, seqman ) );
+  
+    // erase sequence
+  CHECK_ERR( seqman.erase( 108, 109 ) );
+  MBEntityHandle exp6[][2] = { {100, 104}, {1001, 1001} };
+  CHECK( seqman_equal( exp6, 2, seqman ) );
+  
+    // erase sequence
+  CHECK_ERR( seqman.erase( 100, 104 ) );
+  MBEntityHandle exp7[][2] = { {1001, 1001} };
+  CHECK( seqman_equal( exp7, 1, seqman ) );
+  
+    // erase sequence
+  CHECK_ERR( seqman.erase( 1001, 1001 ) );
+  CHECK( seqman.empty() );
+}
+
+
+void test_find_free_handle()
+{
+  bool append;
+  TypeSequenceManager::iterator seq;
+  TypeSequenceManager seqman;
+  make_basic_sequence( seqman ); // { [3,7], [100,111], [1001] }
+  
+  seq = seqman.find_free_handle( 0, MB_END_ID, append );
+  CHECK( seq != seqman.end() );
+    // expect the first available handle (2).
+  CHECK_EQUAL( (MBEntityHandle)3, (*seq)->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)7, (*seq)->  end_handle() );
+  CHECK( !append );
+
+    // Expect end() if no adjacent sequence
+  seq = seqman.find_free_handle( 9, 98, append );
+  CHECK( seq == seqman.end() );
+  
+    // Try a limited handle range
+  seq = seqman.find_free_handle( 8, 99, append );
+  CHECK( seq != seqman.end() );
+    // expect the first available handle (8).
+  CHECK_EQUAL( (MBEntityHandle)3, (*seq)->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)7, (*seq)->  end_handle() );
+  CHECK( append );
+  
+    // Try an unambigious case (above tests have multiple
+    // possible answers, were we assume the first available
+    // handle).
+  seq = seqman.find_free_handle( 8, 98, append );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)3, (*seq)->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)7, (*seq)->  end_handle() );
+  CHECK( append );
+  
+    // Try an unambigious case (above tests have multiple
+    // possible answers, were we assume the first available
+    // handle).
+  seq = seqman.find_free_handle( 9, 99, append );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)100, (*seq)->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)111, (*seq)->  end_handle() );
+  CHECK( !append );
+  
+    // Try a case where the expected result handle
+    // is in the middle of the input range.
+  seq = seqman.find_free_handle( 900, 1100, append );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)1001, (*seq)->start_handle() );
+  CHECK_EQUAL( (MBEntityHandle)1001, (*seq)->  end_handle() );
+    // Expect first available handle
+  CHECK( !append );
+}
+
+
+void test_find_free_sequence()
+{
+  MBEntityHandle start;
+  SequenceData* data = 0;
+  TypeSequenceManager seqman;
+  make_basic_sequence( seqman ); // { [3,7], [100,111], [1001] }
+  SequenceData* expdata = (*seqman.begin())->data();
+  
+  start = seqman.find_free_sequence( 2, 1, 3, data );
+  CHECK_EQUAL( expdata, data );
+  CHECK_EQUAL( (MBEntityHandle)1, start );
+  
+  start = seqman.find_free_sequence( 3, 1, 7, data );
+  CHECK_EQUAL( (MBEntityHandle)0, start );
+  CHECK_EQUAL( NULL, data );
+  
+  start = seqman.find_free_sequence( 30, 1, 120, data );
+  CHECK_EQUAL( expdata, data );
+  CHECK( start == 8 || start == 70 );
+  
+  start = seqman.find_free_sequence( 10, 92, 999, data );
+  CHECK_EQUAL( expdata, data );
+  CHECK_EQUAL( (MBEntityHandle)112, start );
+  
+  start = seqman.find_free_sequence( 100, 1, 600, data );
+  CHECK_EQUAL( expdata, data );
+  CHECK_EQUAL( (MBEntityHandle)112, start );
+  
+  start = seqman.find_free_sequence( 1000, 1, MB_END_ID, data );
+  CHECK_EQUAL( expdata, data );
+  CHECK_EQUAL( (MBEntityHandle)1002, start );
+  
+  start = seqman.find_free_sequence( 980, 1, 1800, data );
+  CHECK_EQUAL( (MBEntityHandle)0, start );
+  CHECK_EQUAL( NULL, data );
+}
+
+
+void test_is_free_sequence()
+{
+  SequenceData* data = 0;
+  TypeSequenceManager seqman;
+  make_basic_sequence( seqman ); // { [3,7], [100,111], [1001] }
+  SequenceData* expdata = (*seqman.begin())->data();
+  
+  CHECK( !seqman.is_free_sequence( 1, 3, data ) );
+  CHECK(  seqman.is_free_sequence( 1, 2, data ) );
+  CHECK_EQUAL( expdata, data );
+  CHECK( !seqman.is_free_sequence( 7, 93, data ) );
+  CHECK( !seqman.is_free_sequence( 8, 93, data ) );
+  CHECK(  seqman.is_free_sequence( 8, 92, data ) );
+  CHECK_EQUAL( expdata, data );
+  CHECK( !seqman.is_free_sequence( 111, 890, data ) );
+  CHECK( !seqman.is_free_sequence( 112, 890, data ) );
+  CHECK(  seqman.is_free_sequence( 112, 879, data ) );
+  CHECK_EQUAL( expdata, data );
+  CHECK(  seqman.is_free_sequence( 1002, 1, data ) );
+  CHECK_EQUAL( expdata, data );
+  CHECK(  seqman.is_free_sequence( 2000, 20000, data ) );
+  CHECK_EQUAL( expdata, data );
+}    
+
+
+void test_is_free_handle()
+{
+  // Construct a TypeSequenceManager with the following data:
+  // EntitySequence: |[1,500]|   |[601,1000]|       |[2500,2599]|    |[2800,2999]|
+  // SequenceData:   |       [1,1000]       |    |         [2001,3000]            |
+  TypeSequenceManager seqman;
+  SequenceData* data1 = new SequenceData(0,   1,1000);
+  SequenceData* data2 = new SequenceData(0,2001,3000);
+  CHECK( insert_seq( seqman,    1, 500, data1, true  ) );
+  CHECK( insert_seq( seqman,  601, 400, data1, false ) );
+  CHECK( insert_seq( seqman, 2500, 100, data2, true  ) );
+  CHECK( insert_seq( seqman, 2800, 200, data2, false ) );
+  
+  // Begin tests
+  TypeSequenceManager::iterator seq;
+  SequenceData* data;
+  MBEntityHandle first, last;
+ 
+  // Test handle in use
+
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle(   1, seq, data, first, last ) );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle( 300, seq, data, first, last ) );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle( 500, seq, data, first, last ) );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle( 601, seq, data, first, last ) );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle(2500, seq, data, first, last ) );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle(2599, seq, data, first, last ) );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle(2800, seq, data, first, last ) );
+  CHECK_EQUAL( MB_ALREADY_ALLOCATED, seqman.is_free_handle(2999, seq, data, first, last ) );
+ 
+ 
+  // Test prepend to sequence
+  
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 600, seq, data, first, last ) );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)601, (*seq)->start_handle() );
+  CHECK_EQUAL( data1, data );
+  CHECK_EQUAL( (MBEntityHandle)600, first );
+  CHECK_EQUAL( (MBEntityHandle)600, last );
+  
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 2499, seq, data, first, last ) );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)2500, (*seq)->start_handle() );
+  CHECK_EQUAL( data2, data );
+  CHECK_EQUAL( (MBEntityHandle)2499, first );
+  CHECK_EQUAL( (MBEntityHandle)2499, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 2799, seq, data, first, last ) );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)2800, (*seq)->start_handle() );
+  CHECK_EQUAL( data2, data );
+  CHECK_EQUAL( (MBEntityHandle)2799, first );
+  CHECK_EQUAL( (MBEntityHandle)2799, last );
+ 
+  // Test append to sequence
+  
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 501, seq, data, first, last ) );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)1, (*seq)->start_handle() );
+  CHECK_EQUAL( data1, data );
+  CHECK_EQUAL( (MBEntityHandle)501, first );
+  CHECK_EQUAL( (MBEntityHandle)501, last );
+  
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 2600, seq, data, first, last ) );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)2500, (*seq)->start_handle() );
+  CHECK_EQUAL( data2, data );
+  CHECK_EQUAL( (MBEntityHandle)2600, first );
+  CHECK_EQUAL( (MBEntityHandle)2600, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 3000, seq, data, first, last ) );
+  CHECK( seq != seqman.end() );
+  CHECK_EQUAL( (MBEntityHandle)2800, (*seq)->start_handle() );
+  CHECK_EQUAL( data2, data );
+  CHECK_EQUAL( (MBEntityHandle)3000, first );
+  CHECK_EQUAL( (MBEntityHandle)3000, last );
+  
+    // Test new sequence in existing SequenceData
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 502, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( data1, data );
+  CHECK_EQUAL( (MBEntityHandle)501, first );
+  CHECK_EQUAL( (MBEntityHandle)600, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 599, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( data1, data );
+  CHECK_EQUAL( (MBEntityHandle)501, first );
+  CHECK_EQUAL( (MBEntityHandle)600, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 2001, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( data2, data );
+  CHECK_EQUAL( (MBEntityHandle)2001, first );
+  CHECK_EQUAL( (MBEntityHandle)2499, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 2498, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( data2, data );
+  CHECK_EQUAL( (MBEntityHandle)2001, first );
+  CHECK_EQUAL( (MBEntityHandle)2499, last );
+
+    // Test new SequenceData 
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 1001, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( (SequenceData*)0, data );
+  CHECK_EQUAL( (MBEntityHandle)1001, first );
+  CHECK_EQUAL( (MBEntityHandle)2000, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 1500, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( (SequenceData*)0, data );
+  CHECK_EQUAL( (MBEntityHandle)1001, first );
+  CHECK_EQUAL( (MBEntityHandle)2000, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 2000, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( (SequenceData*)0, data );
+  CHECK_EQUAL( (MBEntityHandle)1001, first );
+  CHECK_EQUAL( (MBEntityHandle)2000, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 3001, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( (SequenceData*)0, data );
+  CHECK_EQUAL( (MBEntityHandle)3001, first );
+  CHECK_EQUAL( (MBEntityHandle)MB_END_ID, last );
+
+  seq = seqman.end(); data = 0; first = last = 0;
+  CHECK( seqman.is_free_handle( 10000, seq, data, first, last ) );
+  CHECK( seqman.end() == seq );
+  CHECK_EQUAL( (SequenceData*)0, data );
+  CHECK_EQUAL( (MBEntityHandle)3001, first );
+  CHECK_EQUAL( (MBEntityHandle)MB_END_ID, last );
+}

Added: MOAB/trunk/TestUtil.hpp
===================================================================
--- MOAB/trunk/TestUtil.hpp	                        (rev 0)
+++ MOAB/trunk/TestUtil.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,225 @@
+#ifndef TEST_UTIL_HPP
+#define TEST_UTIL_HPP
+
+#define NOFORK
+
+#include <math.h>
+
+/** Check that A is MB_SUCCESS */
+#define CHECK_ERR( A )                    check_equal( MB_SUCCESS, (A), "MB_SUCCESS", #A, __LINE__, __FILE__ )
+/**  Ensure that A is true */
+#define CHECK( A )                        check_true( (A), #A, __LINE__, __FILE__ )
+/** Check that two values are equal */
+#define CHECK_EQUAL( EXP, ACT )           check_equal( (EXP), (ACT), #EXP, #ACT, __LINE__, __FILE__ )
+/** Check that two real (float or double) values are equal within EPS */
+#define CHECK_REAL_EQUAL( EXP, ACT, EPS ) check_equal( (EXP), (ACT), (EPS), #EXP, #ACT, __LINE__, __FILE__ )
+/** Run a test
+ *  Argument should be a function with the signature:  void func(void)
+ */
+#define RUN_TEST( FUNC )           run_test( &FUNC, #FUNC )
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#if defined(_MSC_VER) || defined(NOFORK)
+   struct ErrorExcept{};
+#  define FLAG_ERROR throw ErrorExcept()
+#else
+#  include <sys/types.h>
+#  include <sys/wait.h>
+#  include <unistd.h>
+#  include <errno.h>
+#  define FLAG_ERROR exit(1)
+#endif
+
+
+/* Make sure IS_BUILDING_MB is defined so we can include MBInternals.hpp */
+#include "MBTypes.h"
+#ifndef IS_BUILDING_MB
+#  define IS_BUILDING_MB
+#  include "MBInternals.hpp"
+#  undef IS_BUILDING_MB
+#else
+#  include "MBInternals.hpp"
+#endif
+
+typedef void (*test_func)(void);
+int run_test( test_func test, const char* func_name )
+{
+  printf("Running %s ...\n", func_name );
+  
+#if defined(_MSC_VER) || defined(NOFORK) 
+  /* On Windows, run all tests in same process.
+     Flag errors by throwing an exception.
+   */
+  try {
+    (*test)();
+    return 0;
+  }
+  catch (ErrorExcept) {
+    printf( "  %s: FAILED\n", func_name );
+    return 1;
+  }
+  catch (...) {
+    printf( "  %s: UNCAUGHT EXCEPTION\n", func_name );
+    return 1;
+  }
+    
+#else
+    /* For non-Windows OSs, fork() and run test in child process. */
+  pid_t pid = fork();
+  int status;
+  
+    /* Fork failed? */
+  if (pid == -1) {  
+    perror( "fork()" );
+    abort(); /* abort all tests (can't fork child processes) */
+  }
+  
+    /* If child process*/
+  if (pid == 0) {
+    (*test)();  /* call test function */
+    exit(0);    /* if function returned, then it succeeded */
+  }
+  
+    /* If here, then parent process */
+    
+    /* Wait until child process exists */
+  waitpid( pid, &status, 0 );
+  
+    /* Check child exit status */
+  if (WIFSIGNALED(status)) {
+    if (WTERMSIG(status))
+      printf("  %s: TERMINATED (signal %d)\n", func_name, (int)WTERMSIG(status) );
+    if (WCOREDUMP(status))
+      printf("  %s: CORE DUMP\n", func_name);
+    return 1;
+  }
+  else if(WEXITSTATUS(status)) {
+    printf( "  %s: FAILED\n", func_name );
+    return 1;
+  }
+  else {
+    return 0;
+  }
+#endif
+}
+
+
+#define EQUAL_TEST_IMPL( TEST, TYPE ) if( !(TEST) ) { \
+  printf( "Equality Test Failed: %s == %s\n", sA, sB ); \
+  printf( "  at line %d of '%s'\n", line, file ); \
+  printf( "  Expected value: %" #TYPE "\n", A ); \
+  printf( "  Actual value:   %" #TYPE "\n", B ); \
+  printf( "\n" ); \
+  FLAG_ERROR; \
+}
+
+void check_equal( int A, int B, const char* sA, const char* sB, int line, const char* file )
+  {  EQUAL_TEST_IMPL( A == B, d ) }
+
+void check_equal( unsigned A, unsigned B, const char* sA, const char* sB, int line, const char* file )
+  {  EQUAL_TEST_IMPL( A == B, u ) }
+
+void check_equal( long A, long B, const char* sA, const char* sB, int line, const char* file )
+  {  EQUAL_TEST_IMPL( A == B, ld ) }
+
+void check_equal( unsigned long A, unsigned long B, const char* sA, const char* sB, int line, const char* file )
+  {  EQUAL_TEST_IMPL( A == B, lu ) }
+
+void check_equal( void* A, void* B, const char* sA, const char* sB, int line, const char* file )
+  {  EQUAL_TEST_IMPL( A == B, p ) }
+
+void check_equal( float A, float B, float eps, const char* sA, const char* sB, int line, const char* file )
+  {  EQUAL_TEST_IMPL( fabsf(A - B) <= eps, f ) }
+
+void check_equal( double A, double B, float eps, const char* sA, const char* sB, int line, const char* file )
+  {  EQUAL_TEST_IMPL( fabs(A - B) <= eps, f ) }
+
+const char* mb_error_str( MBErrorCode err )
+{
+  switch (err) {
+    case MB_SUCCESS                 : return "Success";
+    case MB_INDEX_OUT_OF_RANGE      : return "Index Out of Range";
+    case MB_TYPE_OUT_OF_RANGE       : return "Type Out of Range";
+    case MB_MEMORY_ALLOCATION_FAILED: return "Memory Alloc. Failed";
+    case MB_ENTITY_NOT_FOUND        : return "Entity Not Found";
+    case MB_MULTIPLE_ENTITIES_FOUND : return "Multiple Entities Found";
+    case MB_TAG_NOT_FOUND           : return "Tag Not Found";
+    case MB_FILE_DOES_NOT_EXIST     : return "File Not Found";
+    case MB_FILE_WRITE_ERROR        : return "File Write Error";
+    case MB_NOT_IMPLEMENTED         : return "Not Implemented";
+    case MB_ALREADY_ALLOCATED       : return "Already Allocated";
+    case MB_FAILURE                 : return "Failure";
+    default                         : return "(unknown)";
+  }
+}
+
+
+void check_equal( MBErrorCode A, MBErrorCode B, const char* sA, const char* sB, int line, const char* file )
+{
+  if (A == B)
+    return;
+  
+  printf( "MBErrorCode Test Failed: %s == %s\n", sA, sB ); 
+  printf( "  at line %d of '%s'\n", line, file ); 
+  printf( "  Expected value: %s (%d)\n", mb_error_str(A), (int)A ); 
+  printf( "  Actual value:   %s (%d)\n", mb_error_str(B), (int)B ); 
+  printf( "\n" ); 
+  FLAG_ERROR; 
+}
+
+const char* mb_type_str( MBEntityType type )
+{
+  switch(type) {
+    case MBVERTEX    : return "Vertex";
+    case MBEDGE      : return "Edge";
+    case MBTRI       : return "Triangle";
+    case MBQUAD      : return "Quadrilateral";
+    case MBPOLYGON   : return "Polygon";
+    case MBTET       : return "Tetrahedron";
+    case MBPYRAMID   : return "Pyramid";
+    case MBPRISM     : return "Prism (wedge)";
+    case MBKNIFE     : return "Knife";
+    case MBHEX       : return "Hexahedron";
+    case MBPOLYHEDRON: return "Polyhedron";
+    case MBENTITYSET : return "Entity (Mesh) Set";
+    case MBMAXTYPE   : return "(max type)";
+    default          : return "(unknown)";
+  }
+}
+
+const char* mb_type_str( MBEntityHandle a )
+  { return mb_type_str( TYPE_FROM_HANDLE(a) ); }
+/*
+void check_equal( MBEntityHandle A, MBEntityHandle B, const char* sA, const char* sB, int line, const char* file )
+{
+  if (A == B)
+    return;
+  
+  printf( "Entity handles not equal: %s == %s\n", sA, sB );
+  printf( "  at line %d of '%s'\n", line, file ); 
+  if (A) 
+    printf( "  Expected value: %lx (%s %ld)\n", (unsigned long)A, mb_type_str( A ), (long)ID_FROM_HANDLE(A) ); 
+  else 
+    printf( "  Expected value: 0\n" ); 
+  if (B)
+    printf( "  Actual value:   %lx (%s %ld)\n", (unsigned long)B, mb_type_str( B ), (long)ID_FROM_HANDLE(B) ); 
+  else 
+    printf( "  Actual value: 0\n" ); 
+  printf( "\n" ); 
+  FLAG_ERROR; 
+}  
+*/
+
+void check_true( bool cond, const char* str, int line, const char* file )
+{
+  if( !cond ) { 
+    printf( "Test Failed: %s\n", str ); 
+    printf( "  at line %d of '%s'\n", line, file ); 
+    printf( "\n" ); 
+    FLAG_ERROR; 
+  }
+}
+
+#endif

Added: MOAB/trunk/TypeSequenceManager.cpp
===================================================================
--- MOAB/trunk/TypeSequenceManager.cpp	                        (rev 0)
+++ MOAB/trunk/TypeSequenceManager.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,887 @@
+#include "TypeSequenceManager.hpp"
+#include "SequenceData.hpp"
+#include <assert.h>
+#include "MBCN.hpp"
+TypeSequenceManager::~TypeSequenceManager()
+{
+    // We assume that for there to be multiple squences referenceing
+    // the same SequenceData, there must be some portion of the
+    // SequenceData that is unused.  Otherwise the sequences should
+    // have been merged.  Given that assumption, it is the case that
+    // either a) a SequenceData is in availableList or b) the 
+    // SequenceData is referenced by exactly one sequence.
+
+    // Delete every entity sequence
+  for (iterator i = begin(); i != end(); ++i) {
+    EntitySequence* seq = *i;
+      // check for case b) above
+    if (seq->using_entire_data()) {
+        // delete sequence before data, because sequence
+        // has a pointer to data and may try to dereference
+        // that pointer during its destruction.
+      SequenceData* data = seq->data();
+      delete seq;
+      delete data;
+    }
+    else {
+      delete seq;
+    }
+  }
+  sequenceSet.clear();
+  
+    // case a) above
+  for (data_iterator i = availableList.begin(); i != availableList.end(); ++i)
+    delete *i;
+  availableList.clear();
+}
+
+MBErrorCode TypeSequenceManager::merge_internal( iterator i, iterator j )
+{
+  EntitySequence* dead = *j;
+  sequenceSet.erase( j );
+  MBErrorCode rval = (*i)->merge( *dead );
+  if (MB_SUCCESS != rval) {
+    sequenceSet.insert( dead );
+    return rval;
+  }
+  
+  if (lastReferenced == dead)
+    lastReferenced = *i;
+  delete dead;
+    
+    // If merging results in no unused portions of the SequenceData,
+    // remove it from the available list.
+  if ((*i)->using_entire_data())
+    availableList.erase( (*i)->data() );
+  
+  return MB_SUCCESS;
+}
+
+MBErrorCode TypeSequenceManager::check_merge_next( iterator i )
+{
+  iterator j = i; ++j;
+  if (j == end() || (*j)->data() != (*i)->data() || 
+     (*j)->start_handle() > (*i)->end_handle() + 1)
+    return MB_SUCCESS;
+    
+  assert( (*i)->end_handle() + 1 == (*j)->start_handle() );
+  return merge_internal( i, j );
+}  
+
+MBErrorCode TypeSequenceManager::check_merge_prev( iterator i )
+{
+  if (i == begin())
+    return MB_SUCCESS;
+    
+  iterator j = i; --j;
+  if ((*j)->data() != (*i)->data() || 
+     (*j)->end_handle() + 1 < (*i)->start_handle())
+    return MB_SUCCESS;
+    
+  assert( (*j)->end_handle() + 1 == (*i)->start_handle() );
+  return merge_internal( i, j );
+}  
+
+MBErrorCode TypeSequenceManager::insert_sequence( EntitySequence* seq_ptr )
+{
+  if (!seq_ptr->data())
+    return MB_FAILURE;
+
+  iterator i = lower_bound( seq_ptr->start_handle() );
+  if (i != end()) {
+    if ((*i)->start_handle() <= seq_ptr->end_handle())
+      return MB_ALREADY_ALLOCATED;
+    if (seq_ptr->data() != (*i)->data() &&
+        (*i)->data()->start_handle() <= seq_ptr->data()->end_handle())
+      return MB_ALREADY_ALLOCATED;
+  }
+  
+  if (i != begin()) {
+    iterator j = i; --j;
+    if (seq_ptr->data() != (*j)->data() &&
+        (*j)->data()->end_handle() >= seq_ptr->data()->start_handle())
+      return MB_ALREADY_ALLOCATED;
+  }
+
+  i = sequenceSet.insert( i, seq_ptr );
+  
+    // merge with previous sequence ?
+  if (seq_ptr->start_handle() > seq_ptr->data()->start_handle() && i != begin()) {
+    if (MB_SUCCESS != check_merge_prev( i )) {
+      sequenceSet.erase( i );
+      return MB_FAILURE;
+    }
+  }
+  
+    // merge with next sequence ?
+  if ((*i)->end_handle() < (*i)->data()->end_handle()) {
+    if (MB_SUCCESS != check_merge_next( i )) {
+      sequenceSet.erase( i );
+      return MB_FAILURE;
+    }
+  }
+      
+  
+    // We merged adjacent sequences sharing a SequenceData, so
+    // we can safely assume that unless this EntitySequence is
+    // using the entire SequenceData, there are unused portions.
+  if (!seq_ptr->using_entire_data()) 
+    availableList.insert( seq_ptr->data() );
+   
+   // lastReferenced is only allowed to be NULL if there are
+   // no sequences (avoids unnecessary if's in fast path).
+  if (!lastReferenced)
+    lastReferenced = seq_ptr;
+ 
+    // Each SequenceData has a pointer to the first EntitySequence
+    // referencing it.  Update that pointer if the new sequence is
+    // the first one.
+  if ((*i)->start_handle() == (*i)->data()->start_handle() ||
+      lower_bound( (*i)->data()->start_handle() ) == i)
+    (*i)->data()->seqManData.firstSequence = i;
+  
+  assert( check_valid_data( seq_ptr ) );
+  return MB_SUCCESS;
+}
+
+MBErrorCode TypeSequenceManager::replace_subsequence( EntitySequence* seq_ptr,
+                                                      TagServer* tag_server )
+{
+    // find the sequence of interest
+  iterator i = lower_bound( seq_ptr->start_handle() );
+  if (i == end() || (*i)->data() == seq_ptr->data())
+    return MB_FAILURE;
+  if (seq_ptr->start_handle() < (*i)->start_handle() ||
+      seq_ptr->end_handle() > (*i)->end_handle())
+    return MB_FAILURE;
+  if (!seq_ptr->using_entire_data())
+    return MB_FAILURE;
+  (*i)->data()->move_tag_data( seq_ptr->data(), tag_server );
+  
+    // Get the range of sequences that point to the shared
+    // SequenceData instance.  Untimately, should have the
+    // following iterators:
+    // p : first sequence sharing SequenceData
+    // j : last sequence before where seq_ptr will be (or i if no prev seq)
+    // i : first sequence after where seq_ptr will be (or j if no next seq)
+    // n : the last sequence sharing the SequenceData 
+  iterator j = i, n = i;
+  iterator p = (*i)->data()->seqManData.firstSequence;
+  for (++n; n != end() && (*n)->data() == (*i)->data(); ++n); 
+  --n;
+  
+    // quick case -- replacing entire, single sequence
+  if (n == p && 
+     (*i)->start_handle() == seq_ptr->start_handle() &&
+     (*i)->end_handle() == seq_ptr->end_handle())
+  {
+      // delete old sequence and data (sequence first in case
+      // it needs to reference data during destruction)
+    EntitySequence* seq = *i;
+    SequenceData* data = seq->data();
+    if (!seq->using_entire_data())
+      availableList.erase( data );
+    delete seq;
+    delete data;
+    
+      // update structures for tracking sequences
+    iterator d = i++;
+    sequenceSet.erase( d );
+    i = sequenceSet.insert( i, seq_ptr );
+    if (!seq_ptr->using_entire_data())
+      availableList.insert( seq_ptr->data() );
+      
+      // make sure lastReferenced isn't stale
+    if (lastReferenced == seq)
+      lastReferenced = seq_ptr;
+    
+      // each SequenceData has pointer to the first EntitySequence referencing it
+    seq_ptr->data()->seqManData.firstSequence = i;
+    
+    assert( check_valid_data( seq_ptr ) );
+    return MB_SUCCESS;
+  }
+  
+    // remove entities in new sequence from current sequence list
+  if ((*i)->start_handle() == seq_ptr->start_handle()) {
+    if (j != p)
+      --j;
+    (*i)->pop_front( seq_ptr->end_handle() - seq_ptr->start_handle() + 1 );
+  }
+  else if ((*i)->end_handle() == seq_ptr->end_handle()) {
+    (*i)->pop_back( seq_ptr->end_handle() - seq_ptr->start_handle() + 1 );
+    if (i != n)
+      ++i;
+  }
+  else {
+    i = split_sequence( i, seq_ptr->start_handle() );
+    (*i)->pop_front( seq_ptr->end_handle() - seq_ptr->start_handle() + 1 );
+    if (n == j) 
+      n = i;
+  }
+  
+    // split underlying sequence data
+  SequenceData* dead_data = (*i)->data();
+  availableList.erase( dead_data );
+  if (p != i) {
+    SequenceData* new_data = (*p)->create_data_subset( (*p)->start_handle(), (*j)->end_handle() );
+    new_data->seqManData.firstSequence = p;
+    iterator s = j;
+    for (++s; p != s; ++p)
+      (*p)->data( new_data );
+     dead_data->move_tag_data( new_data, tag_server );
+  }
+  if (j != n) {
+    SequenceData* new_data = (*n)->create_data_subset( (*i)->start_handle(), (*n)->end_handle() );
+    new_data->seqManData.firstSequence = i;
+    for (++n; i != n; ++i)
+      (*i)->data( new_data );
+    dead_data->move_tag_data( new_data, tag_server );
+  }
+  delete dead_data;
+  
+    // put new sequence in lists
+  return insert_sequence( seq_ptr );
+}
+  
+    
+
+TypeSequenceManager::iterator TypeSequenceManager::erase( iterator i )
+{
+  EntitySequence* seq = *i;
+  SequenceData* data = seq->data();
+  iterator j;
+
+    // check if we need to delete the referenced SequenceData also
+  bool delete_data;
+  if (seq->using_entire_data()) // only sequence
+    delete_data = true;
+  else if (data->seqManData.firstSequence != i) {// earlier sequence?
+    delete_data = false;
+    availableList.insert( data );
+  }
+  else { // later sequence ?
+    j = i; ++j;
+    delete_data = (j == end() || (*j)->data() != data);
+    if (delete_data)
+      availableList.erase( data );
+    else {
+      availableList.insert( data );
+      data->seqManData.firstSequence = j;
+    }
+  }
+    
+    // remove sequence, updating i to be next sequence
+  j = i++;
+  sequenceSet.erase( j );
+  
+    // Make sure lastReferenced isn't stale.  It can only be NULL if
+    // no sequences.
+  if (lastReferenced == seq)
+    lastReferenced = sequenceSet.empty() ? 0 : *sequenceSet.begin();
+  
+    // Always delete sequence before the SequenceData it references.
+  assert( 0 == find(seq->start_handle()) );
+  delete seq;
+  if (delete_data)
+    delete data;
+  else {
+    assert( check_valid_data( *data->seqManData.firstSequence ) );
+    assert( lastReferenced != seq );
+  }
+  return i;
+}
+  
+
+MBErrorCode TypeSequenceManager::remove_sequence( const EntitySequence* seq_ptr,
+                                                  bool& unreferenced_data )
+{
+    // remove sequence from set
+  iterator i = lower_bound( seq_ptr->start_handle() );
+  if (i == end() || *i != seq_ptr)
+    return MB_ENTITY_NOT_FOUND;
+  sequenceSet.erase( i );
+  
+    // check if this is the only sequence referencing its data
+  if (seq_ptr->using_entire_data()) 
+    unreferenced_data = true;
+  else {
+    i = lower_bound( seq_ptr->data()->start_handle() );
+    unreferenced_data = i == end() || (*i)->data() != seq_ptr->data();
+    if (unreferenced_data)
+      availableList.erase( seq_ptr->data() );
+    else
+      seq_ptr->data()->seqManData.firstSequence = i; // might be 'i' already
+  }
+  
+  if (lastReferenced == seq_ptr) 
+    lastReferenced = sequenceSet.empty() ? 0 : *sequenceSet.begin();
+  
+  return MB_SUCCESS;
+}
+
+TypeSequenceManager::iterator
+TypeSequenceManager::find_free_handle(  MBEntityHandle min_start_handle,
+                                        MBEntityHandle max_end_handle,
+                                        bool& append_out,
+                                        int values_per_ent )
+{
+  for (data_iterator i = availableList.begin(); i != availableList.end(); ++i) {
+    if ((*(*i)->seqManData.firstSequence)->values_per_entity() != values_per_ent)
+      continue;
+    
+    if ((*i)->start_handle() > max_end_handle || (*i)->end_handle() < min_start_handle)
+      continue;
+    
+    for (iterator j = (*i)->seqManData.firstSequence;
+         j != end() && (*j)->start_handle() <= (max_end_handle + 1) && (*j)->data() == *i;
+         ++j) {
+      if ((*j)->end_handle() + 1 < min_start_handle) 
+        continue;
+      if ((*j)->start_handle() > (*i)->start_handle() && 
+          (*j)->start_handle() > min_start_handle) {
+        append_out = false;
+        return j;
+      }
+      if ((*j)->end_handle() < (*i)->end_handle() && 
+          (*j)->end_handle() < max_end_handle) {
+        append_out = true;
+        return j;
+      }
+    }
+  }
+  
+  return end();
+}
+
+bool TypeSequenceManager::is_free_sequence( MBEntityHandle start, 
+                                            MBEntityID num_entities,
+                                            SequenceData*& data_out,
+                                            int values_per_ent )
+{
+  data_out = 0;
+  if (empty())
+    return true;
+    
+  const_iterator i = lower_bound( start );
+  if (i == end()) {
+    --i;  // safe because already tested empty()
+      // if we don't overlap the last data object...
+    if ((*i)->data()->end_handle() < start)
+      return true;
+    data_out = (*i)->data();
+    if ((*i)->values_per_entity() != values_per_ent)
+      return false;
+      // if we overlap a data object, we must be entirely inside of it
+    return start + num_entities - 1 <= (*i)->data()->end_handle();
+  }
+  
+    // check if we fit in the block of free handles
+  if (start + num_entities > (*i)->start_handle()) // start + num + 1 >= i->start
+    return false;
+  
+    // check if we overlap the data for the next sequence
+  if (start + num_entities > (*i)->data()->start_handle()) {
+    data_out = (*i)->data();
+    if ((*i)->values_per_entity() != values_per_ent)
+      return false;
+      // if overlap, must be entirely contained
+    return start >= data_out->start_handle();
+  }
+  
+    // check if we overlap the data for the previous sequence
+  if (i != begin()) {
+    --i;
+    if ((*i)->data()->end_handle() >= start) {
+      data_out = (*i)->data();
+      if ((*i)->values_per_entity() != values_per_ent)
+        return false;
+      return start + num_entities - 1 <= (*i)->data()->end_handle();
+    }
+  }
+  
+    // unused handle block that overlaps no SequenceData
+  return true;
+}
+
+
+MBEntityHandle TypeSequenceManager::find_free_block( MBEntityID num_entities,
+                                                     MBEntityHandle min_start_handle,
+                                                     MBEntityHandle max_end_handle )
+{
+  const_iterator i = lower_bound( min_start_handle );
+  if (i == end())
+    return min_start_handle;
+  
+  if ((*i)->start_handle() < min_start_handle + num_entities)
+    return min_start_handle;
+  
+  MBEntityHandle prev_end = (*i)->end_handle(); ++i;
+  for (; i != end(); prev_end = (*i)->end_handle(), ++i) {
+    MBEntityID len = (*i)->start_handle() - prev_end - 1;
+    if (len >= num_entities) 
+      break;
+  }
+  
+  if (prev_end + num_entities > max_end_handle)
+    return 0;
+  else
+    return prev_end + 1;
+}
+
+struct range_data {
+  MBEntityID num_entities;
+  MBEntityHandle min_start_handle, max_end_handle;
+  MBEntityHandle first, last;
+};
+
+static bool check_range( const range_data& d,
+                         bool prefer_end,
+                         MBEntityHandle& result )
+{
+  MBEntityHandle first = std::max( d.min_start_handle, d.first );
+  MBEntityHandle  last = std::min( d.max_end_handle, d.last );
+  if (last < first + d.num_entities - 1) {
+    result = 0;
+    return false;
+  }
+  
+  result = prefer_end ? last + 1 - d.num_entities : first;
+  return true;
+}
+
+MBEntityHandle TypeSequenceManager::find_free_sequence( MBEntityID num_entities, 
+                                                        MBEntityHandle min_start_handle,
+                                                        MBEntityHandle max_end_handle,
+                                                        SequenceData*& data_out,
+                                                        int num_verts )
+{
+  if (max_end_handle < min_start_handle + num_entities - 1)
+    return 0;
+  
+  MBEntityHandle result;
+  iterator p, i = lower_bound( min_start_handle );
+  range_data d = { num_entities, min_start_handle, max_end_handle, 0, 0 };
+  
+  if (i == end()) {
+    data_out = 0;
+    return min_start_handle;
+  }
+  else if (i == begin()) {
+    if ((*i)->values_per_entity() == num_verts) {
+      d.first = (*i)->data()->start_handle();
+      d.last  = (*i)->start_handle() - 1;
+      if (check_range( d, true, result )) {
+        data_out = (*i)->data();
+        return result;
+      }
+    }
+    d.first = min_start_handle;
+    d.last = (*i)->data()->start_handle() - 1;
+    if (check_range( d, true, result)) {
+      data_out = 0;
+      return result;
+    }
+    p = i++;
+  }
+  else {
+    p = i;
+    --p;
+  }
+  
+  for (; i != end() && (*i)->start_handle() < max_end_handle; p = i++) {
+    if ((*p)->data() == (*i)->data()) {
+      if ((*p)->values_per_entity() == num_verts) {
+        d.first = (*p)->end_handle() + 1;
+        d.last = (*i)->start_handle() - 1;
+        if (check_range( d, false, result )) {
+          data_out = (*p)->data();
+          return result;
+        }
+      }
+    }
+    else {
+      if ((*p)->values_per_entity() == num_verts) {
+        d.first = (*p)->end_handle() + 1;
+        d.last = (*p)->data()->end_handle();
+        if (check_range( d, false, result )) {
+          data_out = (*p)->data();
+          return result;
+        }
+      }
+      if ((*i)->values_per_entity() == num_verts) {
+        d.first = (*i)->data()->start_handle();
+        d.last = (*i)->start_handle() - 1;
+        if (check_range( d, true, result )) {
+          data_out = (*i)->data();
+          return result;
+        }
+      }
+      d.first = (*p)->data()->end_handle() + 1;
+      d.last  = (*i)->data()->start_handle() - 1;
+      if (check_range( d, false, result )) {
+        data_out = 0;
+        return result;
+      }
+    }
+  }
+  
+  if ((*p)->values_per_entity() == num_verts) {
+    d.first = (*p)->end_handle() + 1;
+    d.last = (*p)->data()->end_handle();
+    if (check_range( d, false, result )) {
+      data_out = (*p)->data();
+      return result;
+    }
+  }
+  
+  d.first = (*p)->data()->end_handle() + 1;
+  d.last = max_end_handle;
+  if (check_range( d, false, result )) {
+    data_out = 0;
+    return result;
+  }
+  
+  data_out = 0;
+  return 0;
+}
+
+MBErrorCode TypeSequenceManager::check_valid_handles( MBEntityHandle first,
+                                                      MBEntityHandle last ) const
+{
+  iterator i = lower_bound( first );
+  if (i == end() || (*i)->start_handle() > first)
+    return MB_ENTITY_NOT_FOUND;
+
+  while ((*i)->end_handle() < last) {
+    MBEntityHandle prev_end = (*i)->end_handle();
+    ++i;
+    if (i == end() || prev_end + 1 != (*i)->start_handle())
+      return MB_ENTITY_NOT_FOUND;
+  }
+  
+  return MB_SUCCESS;
+}
+
+MBErrorCode TypeSequenceManager::erase( MBEntityHandle h )
+{
+  EntitySequence* seq = find(h);
+  if (!seq)
+    return MB_ENTITY_NOT_FOUND;
+  
+  if (seq->start_handle() == h) {
+    if (seq->end_handle() != h) 
+      return seq->pop_front(1);
+    SequenceData* data = seq->data();
+    bool delete_data;
+    MBErrorCode rval = remove_sequence( seq, delete_data );
+    if (MB_SUCCESS != rval)
+      return rval;
+    delete seq;
+    if (delete_data)
+      delete data;
+    return MB_SUCCESS;
+  }
+  else if (seq->end_handle() == h)
+    return seq->pop_back(1);
+  else {
+    iterator i = lower_bound( h );
+    i = split_sequence( i, h );
+    seq = *i;
+    assert(seq->start_handle() == h);
+    return seq->pop_front(1);
+  }
+}
+
+MBErrorCode TypeSequenceManager::erase( MBEntityHandle first, MBEntityHandle last )
+{
+    // first check that all entities in range are valid
+
+  MBErrorCode rval = check_valid_handles( first, last );
+  if (MB_SUCCESS != rval)
+    return rval;
+  
+    // now remove entities
+    
+    // get first sequence intersecting range
+  iterator i = lower_bound( first );
+  if (i == end())  // shouuldn't be possible given check_valid_handles call above.
+    return MB_ENTITY_NOT_FOUND;
+    
+    // if range is entirely in interior of sequence, need to split sequence.
+  if ((*i)->start_handle() < first && (*i)->end_handle() > last) {
+    if ((*i)->using_entire_data())
+      availableList.insert( (*i)->data() );
+    i = split_sequence( i, first );
+    (*i)->pop_front( last - first + 1 );
+    assert( check_valid_data( *i ) );
+    return MB_SUCCESS;
+  }
+
+    // if range doesn't entirely contain first sequence, remove some
+    // handles from the end of the sequence and advance to the next 
+    // sequence.
+  if ((*i)->start_handle() < first) {
+    if ((*i)->using_entire_data())
+      availableList.insert( (*i)->data() );
+    (*i)->pop_back((*i)->end_handle() - first + 1);
+    ++i;
+  }
+
+    // destroy all sequences contained entirely within the range
+  while (i != end() && (*i)->end_handle() <= last)
+    i = erase(i);
+
+    // if necesessary, remove entities from the beginning of the
+    // last sequence.
+  if (i != end() && (*i)->start_handle() <= last) {
+    if ((*i)->using_entire_data())
+      availableList.insert( (*i)->data() );
+    (*i)->pop_front( last - (*i)->start_handle() + 1 );
+    assert( check_valid_data( *i ) );
+  }
+  
+  return MB_SUCCESS;
+}
+
+TypeSequenceManager::iterator 
+TypeSequenceManager::split_sequence( iterator i, MBEntityHandle h )
+{
+  EntitySequence* seq = (*i)->split( h );
+  if (!seq)
+    return end();
+  
+  i = sequenceSet.insert( i, seq );
+  assert( check_valid_data( *i ) );
+  return i;
+}
+
+MBErrorCode 
+TypeSequenceManager::is_free_handle( MBEntityHandle handle,
+                                     iterator& seq_iter_out,
+                                     SequenceData*& data_ptr_out,
+                                     MBEntityHandle& block_start,
+                                     MBEntityHandle& block_end,
+                                     int values_per_ent )
+{
+  int junk;
+  block_start = CREATE_HANDLE( TYPE_FROM_HANDLE(handle), MB_START_ID, junk );
+  block_end   = CREATE_HANDLE( TYPE_FROM_HANDLE(handle), MB_END_ID,   junk );
+  
+  iterator i = lower_bound( handle );
+  if (i != end()) {
+    block_end = (*i)->start_handle() - 1;
+
+        // if sequence contains handle, then already allocated
+    if ((*i)->start_handle() <= handle)
+      return MB_ALREADY_ALLOCATED;
+    
+      // handle is not within an existing sequence, but is 
+      // within an existing SequenceData...
+    if ((*i)->data()->start_handle() <= handle) {
+        // if values_per_entity don't match, can't put new entity
+        // in existing SequenceData
+      if ((*i)->values_per_entity() != values_per_ent)
+        return MB_ALREADY_ALLOCATED;
+        
+      data_ptr_out = (*i)->data();
+      if (block_end == handle) { 
+        // prepend to existing sequence
+        seq_iter_out = i;
+        block_start = handle;
+      }
+      else { 
+        // add new sequence to existing SequenceData
+        seq_iter_out = end();
+        if (i == begin() || (*--i)->data() != data_ptr_out) 
+          block_start = data_ptr_out->start_handle();
+        else 
+          block_start = (*i)->end_handle() + 1;
+      }
+      return MB_SUCCESS;
+    }
+  }
+  
+  if (i != begin()) {
+    --i;
+    block_start = (*i)->end_handle() + 1;
+    
+      // handle is within previous sequence data...
+    if ((*i)->data()->end_handle() >= handle) {
+        // if values_per_entity don't match, can't put new entity
+        // in existing SequenceData
+      if ((*i)->values_per_entity() != values_per_ent)
+        return MB_ALREADY_ALLOCATED;
+      
+      data_ptr_out = (*i)->data();
+      if (block_start == handle) {
+        // append to existing sequence
+        seq_iter_out = i;
+        block_end = handle;
+      }
+      else {
+        // add new sequence to existing SequenceData
+        seq_iter_out = end();
+        if (++i == end() || (*i)->data() != data_ptr_out)
+          block_end = data_ptr_out->end_handle();
+        else
+          block_end = (*i)->start_handle() - 1;
+      }
+      return MB_SUCCESS;
+    }
+  }
+  
+  seq_iter_out = end();
+  data_ptr_out = 0;
+  return MB_SUCCESS;
+}
+
+MBErrorCode TypeSequenceManager::notify_appended( iterator seq )
+{
+  MBErrorCode rval = check_merge_next( seq );
+  if ((*seq)->using_entire_data())
+    availableList.erase( (*seq)->data() );
+  return rval;
+}
+
+MBErrorCode TypeSequenceManager::notify_prepended( iterator seq )
+{
+  MBErrorCode rval =  check_merge_prev( seq );
+  if ((*seq)->using_entire_data())
+    availableList.erase( (*seq)->data() );
+  return rval;
+}
+
+void TypeSequenceManager::get_memory_use( unsigned long& entity_storage,
+                                          unsigned long& total_storage ) const
+{
+  entity_storage = total_storage = 0;
+  if (empty())
+    return;
+  
+  MBEntityType mytype = TYPE_FROM_HANDLE(lastReferenced->start_handle());
+  int junk;
+  get_memory_use( CREATE_HANDLE( mytype, MB_START_ID, junk ), 
+                  CREATE_HANDLE( mytype, MB_END_ID,   junk ),
+                  entity_storage,
+                  total_storage );
+} 
+
+void TypeSequenceManager::append_memory_use( MBEntityHandle first,
+                                             MBEntityHandle last,
+                                             const SequenceData* data,
+                                             unsigned long& entity_storage,
+                                             unsigned long& total_storage ) const
+{
+  const unsigned long allocated_count = data->size();
+
+  unsigned long bytes_per_ent, seq_size;
+  const_iterator i = data->seqManData.firstSequence;
+  (*i)->get_const_memory_use( bytes_per_ent, seq_size );
+  
+  unsigned long other_ent_mem = 0;
+  unsigned long occupied_count = 0, entity_count = 0, sequence_count = 0;
+  for (; i != end() && (*i)->data() == data; ++i) {
+    occupied_count += (*i)->size();
+    ++sequence_count;
+    
+    MBEntityHandle start = std::max( first, (*i)->start_handle() );
+    MBEntityHandle  stop = std::min(  last, (*i)->end_handle() );
+    if (stop < start)
+      continue;
+    
+    entity_count += stop - start + 1;
+    other_ent_mem += (*i)->get_per_entity_memory_use( start, stop );    
+  }
+  
+  unsigned long sum = sequence_count * seq_size + 
+                      allocated_count * bytes_per_ent;
+
+    // watch for overflow
+  if (std::numeric_limits<unsigned long>::max() / entity_count <= sum) {
+    total_storage  += sum * (entity_count /  occupied_count) + other_ent_mem;
+    entity_storage += sum * (entity_count / allocated_count) + other_ent_mem; 
+  }
+  else { 
+    total_storage  += sum * entity_count /  occupied_count + other_ent_mem;
+    entity_storage += sum * entity_count / allocated_count + other_ent_mem;
+  }
+}
+
+void TypeSequenceManager::get_memory_use( MBEntityHandle first,
+                                          MBEntityHandle last,
+                                          unsigned long& entity_storage,
+                                          unsigned long& total_storage ) const
+{
+  entity_storage = total_storage = 0;
+  
+  while (first <= last) {
+    const_iterator i = lower_bound(first);
+    if (i == end())
+      return;
+    
+    SequenceData* data = (*i)->data();
+    if (first < data->end_handle()) {
+      append_memory_use( first, last, data, entity_storage, total_storage );
+      first  = data->end_handle() + 1;
+    }
+  }
+}
+
+#ifndef NDEBUG
+bool TypeSequenceManager::check_valid_data( const EntitySequence* seq ) const
+{
+    // caller passed a sequence that should be contained, so cannot be empty
+  if (empty())
+    return false;
+  
+    // make sure lastReferenced points to something
+  if (!lastReferenced)
+    return false;
+  const EntitySequence* seq2 = find( lastReferenced->start_handle() );
+  if (seq2 != lastReferenced)
+    return false;
+  
+    // make sure passed sequence is in list
+  seq2 = find( seq->start_handle() );
+  if (seq2 != seq)
+    return false;
+  
+    // check all sequences referencing the same SequenceData
+  const SequenceData* data = seq->data();
+  const_iterator i = lower_bound( data->start_handle() );
+  if (i != data->seqManData.firstSequence)
+    return false;
+  
+  if (i != begin()) {
+    const_iterator j = i;
+    --j;
+    if ((*j)->end_handle() >= data->start_handle())
+      return false;
+    if ((*j)->data()->end_handle() >= data->start_handle())
+      return false;
+  }
+  
+  for (;;) {
+    seq2 = *i;
+    ++i;
+    if (i == end())
+      return true;
+    if ((*i)->data() != data)
+      break;
+    
+    if (seq2->end_handle() >= (*i)->start_handle())
+      return false;
+  }
+    
+  if ((*i)->start_handle() <= data->end_handle())
+    return false;
+  if ((*i)->data()->start_handle() <= data->end_handle())
+    return false;
+  
+  return true;
+}
+#endif
+
+    
+  

Added: MOAB/trunk/TypeSequenceManager.hpp
===================================================================
--- MOAB/trunk/TypeSequenceManager.hpp	                        (rev 0)
+++ MOAB/trunk/TypeSequenceManager.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,374 @@
+#ifndef TYPE_SEQUENCE_MANAGER_HPP
+#define TYPE_SEQUENCE_MANAGER_HPP
+
+#include "EntitySequence.hpp"
+#include "MBRange.hpp"
+
+#include <set>
+class TagServer;
+
+/**\brief Maintain data structures organizing EntitySequence instances
+ *
+ * EntitySequenceManager is a composition of instances of TypeSequenceManager,
+ * one instance for each MBEntityType.  The TypeSequenceManager provides
+ * organization, owership, and querying of EntitySequences for a specific
+ * MBEntityType.
+ */
+class TypeSequenceManager
+{
+public:
+  /**\brief Comparision function used in std::set
+   *
+   * Define less-than comparison for EntitySequence pointers as a comparison
+   * of the entity handles in the pointed-to EntitySequences.
+   */
+  class SequenceCompare {
+    public: bool operator()( const EntitySequence* a, const EntitySequence* b ) const
+      { return a->end_handle() < b->start_handle(); }
+  };
+  /**\brief Dummy EntitySequence for use in querying set container */
+  class DummySequence : public EntitySequence {
+    public:
+      DummySequence( MBEntityHandle start )
+        : EntitySequence( start ) {}
+  
+      EntitySequence* split( MBEntityHandle ) 
+        { return 0; }
+      SequenceData* create_data_subset( MBEntityHandle, MBEntityHandle ) const
+        { return 0; }
+      
+      void get_const_memory_use( unsigned long& a, unsigned long& b) const
+        { a = b = 0; }
+      unsigned long get_per_entity_memory_use( MBEntityHandle, MBEntityHandle) const
+        { return 0; }
+  };
+
+  /**\brief Type of container for organizing EntitySequence instances */
+  typedef std::set<EntitySequence*,SequenceCompare> set_type;
+  /**\brief Iterator for set_type */
+  typedef set_type::iterator iterator;
+  /**\brief Iterator for set_type */
+  typedef set_type::const_iterator const_iterator;
+  /**\brief Type of container for organizing SequenceData instaces */
+  typedef std::set<SequenceData*> data_set_type;
+  /**\brief iterator type for data_set_type */
+  typedef data_set_type::iterator data_iterator;
+  
+  struct SequenceDataPtr {
+    private:
+    friend class TypeSequenceManager;
+    TypeSequenceManager::iterator firstSequence;
+  };
+private:
+  mutable EntitySequence* lastReferenced;//!< Last accessed EntitySequence - Null only if no sequences
+  set_type sequenceSet;          //!< Set of all managed EntitySequence instances
+  data_set_type availableList;   //!< SequenceData containing unused entries
+
+  iterator erase( iterator i );  //!< Remove a sequence
+  
+  iterator split_sequence( iterator i, MBEntityHandle h ); //!< split a sequence
+
+  void append_memory_use( MBEntityHandle first,
+                          MBEntityHandle last,
+                          const SequenceData* data,
+                          unsigned long& entity_storage,
+                          unsigned long& total_storage ) const;
+
+    // check if sequence at passed iterator should be merged with
+    // the subsequent sequence, and if so merge them retaining i.
+  MBErrorCode check_merge_next( iterator i );
+    // check if sequence at passed iterator should be merged with
+    // the previous sequence, and if so merge them retaining i.
+  MBErrorCode check_merge_prev( iterator i );
+    // common code for check_merge_next and check_merge_prev
+  MBErrorCode merge_internal( iterator keep, iterator dead );
+
+#ifndef NDEBUG
+  bool check_valid_data( const EntitySequence* seq ) const;
+#endif
+
+public:
+
+  /**\brief Add an entity sequence
+   *
+   * Take ownership of passed EntitySequence, and update relevant
+   * data structures.  Sequence may not overlap with any existing
+   * sequence.  
+   *
+   * NOTE:  Sequence may be merged with other, existing sequences.
+   *        This function will always ensure that the passed 
+   *        EntitySequence* is the remaining one, but the passed
+   *        sequence may have modified start and end handles.
+   */
+  MBErrorCode insert_sequence( EntitySequence* seq_ptr );
+  
+  /**\brief Remove an entity sequence.
+   *
+   * Give up ownership of specified EntitySequence, and remove it
+   * from all internal data structures.  Passes back bool flag to
+   * notify caller that ownership of the correspoding SequenceData
+   * is also relinquished because the specified EntitySequence is
+   * the last one referencing it.  
+   */
+  MBErrorCode remove_sequence( const EntitySequence* seq_ptr,
+                               bool& is_last_user_of_sequence_data );
+                               
+                               
+  /**\brief Replace sequence or subset of sequence
+   *
+   * Replace one sequence or a subset of one sequence with 
+   * another sequence.  With fail if a) the existing
+   * sequence is not a subset of an existing sequence or
+   * b) existing sequence shares a SequenceData with the 
+   * passed sequence.
+   *
+   * This method is provided for use when changing the
+   * number of nodes in elements. 
+   */
+  MBErrorCode replace_subsequence( EntitySequence* seq_ptr, TagServer* ts );
+  
+  TypeSequenceManager() : lastReferenced(0) {}
+  
+  ~TypeSequenceManager();
+
+    /**\brief Start of EntitySequence set */
+  const_iterator begin() const { return sequenceSet.begin(); }
+
+    /**\brief End of EntitySequence set */
+  const_iterator   end() const { return sequenceSet.end();   }
+
+    /**\brief Return EntitySequence for specified handle.
+     *
+     *  Return EntitySequence for specified handle, or if
+     *  no such sequence, the next one.  Returns end() if
+     *  all sequences have ranges less than specified handle.
+     */
+  const_iterator lower_bound( MBEntityHandle h ) const 
+    { 
+      DummySequence f(h);
+      return sequenceSet.lower_bound( &f ); 
+    }
+
+    /**\brief Return EntitySequence after specified handle.
+     *
+     *  Return EntitySequence with smallest start handle 
+     *  that is greater than input handle.  Returns end() if
+     *  all sequences have start handles less than specified 
+     *  handle.
+     */
+  const_iterator upper_bound( MBEntityHandle h ) const 
+    { 
+      DummySequence f(h);
+      return sequenceSet.upper_bound( &f ); 
+    }
+
+    /**\brief Get EntitySequence for handle. 
+     *\return EntitySequence for handle, or NULL if no such sequence.
+     */
+  inline EntitySequence* find( MBEntityHandle h ) const;
+  inline MBErrorCode find( MBEntityHandle h, EntitySequence*& ) const;
+  
+    /**\brief Get handles for all entities in all sequences. */
+  inline void get_entities( MBRange& entities_out ) const;
+  
+    /**\brief Get number of entities represented by all sequences. */
+  inline MBEntityID get_number_entities() const;
+  
+  MBErrorCode check_valid_handles( MBEntityHandle first, MBEntityHandle last ) const;
+  
+    /**\brief Remove entities 
+     *
+     * Update EntitySequence data as necessary to "delete" the
+     * specified entities (e.g. split sequences, delete sequences,
+     * free SequenceData instances, etc.)
+     */
+  MBErrorCode erase( MBEntityHandle first, MBEntityHandle last );
+  MBErrorCode erase( MBEntityHandle entity );
+  
+  /**\brief Test if this instance contains no sequences */
+  bool empty() const
+    { return 0 == lastReferenced; }
+  
+  /**\brief Allocate a handle in an existing entity sequence
+   *
+   * Find an existing entity sequence to which a new handle can
+   * be prepended or appended.  The 'append_out' flag indicates
+   * to the caller that the new handle should be appended to the
+   * returned sequence if true, and prepended if false.  
+   *
+   * If no appropriate EntitySequence is available, NULL will
+   * be returned.  The caller will typically then want to use
+   * find_free_sequence() to find appropriate values for the
+   * creation of a new EntitySequence.
+   */
+  iterator find_free_handle( MBEntityHandle min_start_handle,
+                             MBEntityHandle max_end_handle,
+                             bool& append_out,
+                             int values_per_ent = 0 );
+  
+    /**\brief Find block of free handles
+     *
+     * Find block of free handles, such that block does not
+     * overlap any existing EntitySequence. 
+     *\return First handle of block, or zero if no block found.
+     */
+  MBEntityHandle find_free_block( MBEntityID num_entities, 
+                                  MBEntityHandle min_start_handle,
+                                  MBEntityHandle max_end_handle );
+  
+    /**\brief Find block of free handles
+     *
+     * Find block of free handles, such that block a) does not
+     * overlap any existing EntitySequence and b) is either 
+     * entirely within one existing SequenceData or does not 
+     * overlap any SequenceData.
+     *\param num_entities      Size of handle block.
+     *\param min_start_handle  Block may not contain any handle less than this.
+     *\param max_end_handle    Block may not contain any handle greater than this.
+     *\param sequence_data_out If block is within an unused portion of an 
+     *                         existing SequenceData, a pointer to that 
+     *                         SequenceData.  NULL otherwise.
+     *\return values_per_ent   Matched against EntitySequence::values_per_entity.
+     *                         An existing SequenceData will not be returned if
+     *                         the existing EntitySequences using have a different
+     *                         value than the passed one.
+     */
+  MBEntityHandle find_free_sequence( MBEntityID num_entities, 
+                                     MBEntityHandle min_start_handle,
+                                     MBEntityHandle max_end_handle,
+                                     SequenceData*& sequence_data_out,
+                                     int values_per_ent = 0 );
+
+    /**\brief Check if block of handles is free.
+     *
+     * Check if block of handles is free and can be allocated
+     * as a single EntitySequence.  If the block of handles
+     * is contained within an unused portion of a SequenceData,
+     * the SequenceData is returned.
+     */
+  bool is_free_sequence( MBEntityHandle start_handle, 
+                         MBEntityID num_entities,
+                         SequenceData*& sequence_data_out,
+                         int values_per_ent = 0 );
+  
+    /**\brief Check if specific handle is free for allocation
+     *
+     * Check if a specific handle is not currently allocated
+     * and can be allocated with the passed value of values_per_ent.
+     * For example, the handle may not be allocated, but it may
+     * fall within an existing SequenceData.  In that case, it
+     * must be possible to store the speciified values_per_ent
+     * in the existing SequenceData.
+     *
+     * There are four possible return 'states' from this function:
+     *
+     * - handle is not available or cannot be allocated with specified
+     *   values_per_ent.  Returned error code is MB_ALREADY_ALLOCATED.
+     *
+     * - handle can be appended or prepended to an existing sequence.
+     *   seq_ptr_out is set to the sequence the handle should be added to.
+     *
+     * - handle cannot be appended to an existing sequence but falls
+     *   within an existing SequenceData. The caller is expected
+     *   to create a new sequence referencing that SequenceData. seq_ptr_out
+     *   is NULL and data_ptr_out is set to existing SequenceData.
+     *
+     * - handle does not correspond to any existing sequence or data.
+     *   The caller is expected to create a new sequence and SequenceData.
+     *   Both seq_ptr_out and data_ptr_out are set to NULL.  
+     *   block_start and block_end are set to start and end handles of the
+     *   largest sequence that can be allocated and contain the input handle.
+     *
+     *\param handle The handle the caller wishes to allocate as a new entity
+     *\param seq_ptr_out Output: pointer to sequence to append or prepend to
+     *              to allocate handle.  end() if no such sequence.
+     *\param data_ptr_out Output: Pointer to existing SequenceData containing input
+     *              handle, or NULL if no such SequenceData.
+     *\param block_start Output: Smallest possible start handle for new sequence.
+     *\param block_end   Output: Largest possible end handle for new sequence.
+     */
+  MBErrorCode is_free_handle( MBEntityHandle handle,
+                              iterator& seq_ptr_out,
+                              SequenceData*& data_ptr_out,
+                              MBEntityHandle& block_start,
+                              MBEntityHandle& block_end,
+                              int values_per_ent = 0 );  
+
+    /**\brief Notify that sequence was prepended to
+     *
+     * Notify of sequence modifications so we can check if
+     * sequence needs to be merged.
+     */
+  MBErrorCode notify_prepended( iterator seq );
+  
+    /**\brief Notify that sequence was appended to
+     *
+     * Notify of sequence modifications so we can check if
+     * sequence needs to be merged.
+     */
+  MBErrorCode notify_appended( iterator seq );
+    
+  void get_memory_use( unsigned long& total_entity_storage,
+                       unsigned long& total_storage ) const;
+  
+  void get_memory_use( MBEntityHandle start, MBEntityHandle end,
+                       unsigned long& total_entity_storage,
+                       unsigned long& total_amortized_storage ) const;
+                       
+  unsigned long get_sequence_count() const
+    { return sequenceSet.size(); }
+};
+
+inline EntitySequence* TypeSequenceManager::find( MBEntityHandle h ) const
+{
+  if (!lastReferenced) // only null if empty
+    return 0;
+  else if (h >= lastReferenced->start_handle() && h <= lastReferenced->end_handle())
+    return lastReferenced;
+  else {
+    DummySequence seq(h);
+    iterator i = sequenceSet.find( &seq );
+    return i == end() ? 0 : (lastReferenced = *i);
+  }
+}   
+
+inline MBErrorCode TypeSequenceManager::find( MBEntityHandle h, EntitySequence*& seq ) const
+{
+  if (!lastReferenced) { // only null if empty
+    seq = 0;
+    return MB_ENTITY_NOT_FOUND;
+  }
+  else if (h >= lastReferenced->start_handle() && h <= lastReferenced->end_handle()) {
+    seq = lastReferenced;
+    return MB_SUCCESS;
+  }
+  else {
+    DummySequence ds(h);
+    iterator i = sequenceSet.lower_bound( &ds );
+    if (i == end() || (*i)->start_handle() > h ) {
+      seq = 0;
+      return MB_ENTITY_NOT_FOUND;
+    }
+    else {
+      seq = lastReferenced = *i;
+      return MB_SUCCESS;
+    }
+  }
+}   
+  
+inline void TypeSequenceManager::get_entities( MBRange& entities_out ) const
+{
+  MBRange::iterator in = entities_out.begin();
+  for (const_iterator i = begin(); i != end(); ++i)
+    in = entities_out.insert( in, (*i)->start_handle(), (*i)->end_handle() );
+}
+
+inline MBEntityID TypeSequenceManager::get_number_entities() const
+{
+  MBEntityID count = 0;
+  for (const_iterator i = begin(); i != end(); ++i)
+    count += (*i)->size();
+  return count;
+}
+
+#endif

Added: MOAB/trunk/UnstructuredElemSeq.cpp
===================================================================
--- MOAB/trunk/UnstructuredElemSeq.cpp	                        (rev 0)
+++ MOAB/trunk/UnstructuredElemSeq.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,105 @@
+#include "UnstructuredElemSeq.hpp"
+#include "SequenceData.hpp"
+#include "MBCN.hpp"
+
+UnstructuredElemSeq::UnstructuredElemSeq( MBEntityHandle start_handle, 
+                                          MBEntityID entity_count, 
+                                          unsigned nodes_per_entity,
+                                          SequenceData* data )
+  : ElementSequence( start_handle, entity_count, nodes_per_entity, data )
+  {}
+  
+
+UnstructuredElemSeq::UnstructuredElemSeq( MBEntityHandle start_handle, 
+                                          MBEntityID entity_count, 
+                                          unsigned nodes_per_entity,
+                                          MBEntityID data_size )
+  : ElementSequence( start_handle, entity_count, nodes_per_entity,
+                      new SequenceData( 1, start_handle, start_handle + data_size - 1))
+{
+  data()->create_sequence_data( 0, nodes_per_entity * sizeof(MBEntityHandle) );
+}
+
+
+UnstructuredElemSeq::~UnstructuredElemSeq()
+{}
+
+
+int UnstructuredElemSeq::values_per_entity() const
+  { return nodes_per_element(); }
+
+
+EntitySequence*
+UnstructuredElemSeq::split( MBEntityHandle here )
+{
+  if (here <= start_handle() || here > end_handle())
+    return 0;
+  
+  return new UnstructuredElemSeq( *this, here );
+}
+
+
+SequenceData*
+UnstructuredElemSeq::create_data_subset( MBEntityHandle start, MBEntityHandle end ) const
+{
+  int esize = nodes_per_element() * sizeof(MBEntityHandle);
+  return data()->subset(start, end, &esize, 0 );
+}
+
+
+void
+UnstructuredElemSeq::get_const_memory_use( unsigned long& bytes_per_entity,
+                                           unsigned long& size_of_sequence ) const
+{
+  bytes_per_entity = nodes_per_element() * sizeof(MBEntityHandle);
+  size_of_sequence = sizeof(this);
+}
+
+MBErrorCode
+UnstructuredElemSeq::get_connectivity( MBEntityHandle handle,
+                                       std::vector<MBEntityHandle>& connect,
+                                       bool topological ) const
+{
+  MBEntityHandle const* conn = get_array() + nodes_per_element() * (handle - start_handle());
+  int len = topological ? MBCN::VerticesPerEntity(type()) : nodes_per_element();
+  connect.reserve( connect.size() + len );
+  std::copy( conn, conn+len, std::back_inserter( connect ) );
+  return MB_SUCCESS;
+}
+
+
+MBErrorCode
+UnstructuredElemSeq::get_connectivity( MBEntityHandle handle,
+                                       MBEntityHandle const*& conn_ptr,
+                                       int& len,
+                                       bool topological,
+                                       std::vector<MBEntityHandle>* ) const
+{
+  conn_ptr = get_array() + nodes_per_element() * (handle - start_handle());
+  len = topological ? MBCN::VerticesPerEntity(type()) : nodes_per_element();
+  return MB_SUCCESS;
+}
+
+
+MBErrorCode
+UnstructuredElemSeq::set_connectivity( MBEntityHandle handle,
+                                       MBEntityHandle const* connect,
+                                       int connect_length )
+{
+  if ((unsigned)connect_length != nodes_per_element())
+    return MB_INDEX_OUT_OF_RANGE;
+  MBEntityHandle* conn_ptr = get_array() + nodes_per_element() * (handle - start_handle());
+  std::copy( connect, connect+connect_length, conn_ptr );
+  return MB_SUCCESS;
+}
+
+
+MBEntityHandle* UnstructuredElemSeq::get_connectivity_array()
+  { return get_array(); }
+
+MBErrorCode UnstructuredElemSeq::push_back( MBEntityID count )
+  { return EntitySequence::append_entities(count); }
+
+MBErrorCode UnstructuredElemSeq::push_front( MBEntityID count )
+  { return EntitySequence::prepend_entities(count); }
+

Added: MOAB/trunk/UnstructuredElemSeq.hpp
===================================================================
--- MOAB/trunk/UnstructuredElemSeq.hpp	                        (rev 0)
+++ MOAB/trunk/UnstructuredElemSeq.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,74 @@
+#ifndef UNSTRUCTURED_ELEM_SEQ_HPP
+#define UNSTRUCTURED_ELEM_SEQ_HPP
+
+#include "ElementSequence.hpp"
+#include "SequenceData.hpp"
+
+
+class UnstructuredElemSeq : public ElementSequence
+{
+public:
+
+  UnstructuredElemSeq( MBEntityHandle start_handle, 
+                       MBEntityID entity_count, 
+                       unsigned nodes_per_entity,
+                       SequenceData* data );
+
+  UnstructuredElemSeq( MBEntityHandle start_handle, 
+                       MBEntityID entity_count, 
+                       unsigned nodes_per_entity,
+                       MBEntityID sequence_data_size);
+
+  ~UnstructuredElemSeq();
+
+  int values_per_entity() const;
+  
+  EntitySequence* split( MBEntityHandle here );
+  
+  SequenceData* create_data_subset( MBEntityHandle start, MBEntityHandle end ) const;
+                       
+  MBErrorCode get_connectivity( MBEntityHandle handle,
+                                std::vector<MBEntityHandle>& connect,
+                                bool topological = false ) const;
+  
+  MBErrorCode get_connectivity( MBEntityHandle handle,
+                                MBEntityHandle const*& connect,
+                                int &connect_length,
+                                bool topological = false,
+                                std::vector<MBEntityHandle>* storage = 0
+                               ) const;
+
+  MBErrorCode set_connectivity( MBEntityHandle handle,
+                                MBEntityHandle const* connect,
+                                int connect_length );
+  
+  MBEntityHandle* get_connectivity_array();
+  
+  MBErrorCode push_front( MBEntityID count );
+  MBErrorCode push_back ( MBEntityID count );
+  
+  
+  void get_const_memory_use( unsigned long& bytes_per_entity,
+                             unsigned long& size_of_sequence ) const;
+protected:
+
+  inline MBEntityHandle const* get_array() const
+  {
+    return reinterpret_cast<MBEntityHandle const*>(data()->get_sequence_data(0))
+      + nodes_per_element() * (start_handle() - data()->start_handle());
+  }
+
+  inline MBEntityHandle* get_array()
+  {
+    return reinterpret_cast<MBEntityHandle*>(data()->get_sequence_data(0))
+      + nodes_per_element() * (start_handle() - data()->start_handle());
+  }
+
+  UnstructuredElemSeq( UnstructuredElemSeq& split_from, MBEntityHandle here )
+    : ElementSequence( split_from, here )
+   {}
+};
+
+#endif
+
+  

Added: MOAB/trunk/VertexSequence.cpp
===================================================================
--- MOAB/trunk/VertexSequence.cpp	                        (rev 0)
+++ MOAB/trunk/VertexSequence.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,25 @@
+#include "VertexSequence.hpp"
+
+VertexSequence::~VertexSequence() {}
+
+EntitySequence* VertexSequence::split( MBEntityHandle here )
+  { return new VertexSequence( *this, here ); }
+
+SequenceData* VertexSequence::create_data_subset( MBEntityHandle start,
+                                                  MBEntityHandle end ) const
+{
+  const int sizes[] = { sizeof(double), sizeof(double), sizeof(double) };
+  return data()->subset(start, end, sizes, 0 );
+}
+  
+MBErrorCode VertexSequence::push_back( MBEntityID count )
+  { return EntitySequence::append_entities(count); }
+
+MBErrorCode VertexSequence::push_front( MBEntityID count )
+  { return EntitySequence::prepend_entities(count); }
+
+void VertexSequence::get_const_memory_use( unsigned long& per_ent, unsigned long& seq ) const
+{
+  per_ent = 3 * sizeof(double);
+  seq = sizeof(*this);
+}

Added: MOAB/trunk/VertexSequence.hpp
===================================================================
--- MOAB/trunk/VertexSequence.hpp	                        (rev 0)
+++ MOAB/trunk/VertexSequence.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -0,0 +1,171 @@
+#ifndef VERTEX_SEQUENCE_HPP
+#define VERTEX_SEQUENCE_HPP
+
+#include "EntitySequence.hpp"
+#include "SequenceData.hpp"
+
+class VertexSequence : public EntitySequence
+{
+public:
+
+  VertexSequence( MBEntityHandle start,
+                  MBEntityID count,
+                  SequenceData* data )
+    : EntitySequence( start, count, data )
+    {}
+  
+  VertexSequence( MBEntityHandle start,
+                  MBEntityID count,
+                  MBEntityID data_size )
+    : EntitySequence( start, count, new SequenceData( 3, start, start+data_size-1 ) )
+    {
+      data()->create_sequence_data( X, sizeof(double) );
+      data()->create_sequence_data( Y, sizeof(double) );
+      data()->create_sequence_data( Z, sizeof(double) );
+    } 
+  
+  virtual ~VertexSequence();
+  
+  inline MBErrorCode get_coordinates( MBEntityHandle handle,
+                                      double& x,
+                                      double& y,
+                                      double& z ) const;
+
+  inline MBErrorCode get_coordinates( MBEntityHandle handle,
+                                      double coords[3] ) const;
+
+  inline MBErrorCode get_coordinates_ref( MBEntityHandle handle,
+                                          const double*& x,
+                                          const double*& y,
+                                          const double*& z ) const;
+
+  inline MBErrorCode set_coordinates( MBEntityHandle entity,
+                                      double x, 
+                                      double y,
+                                      double z );
+
+  inline MBErrorCode set_coordinates( MBEntityHandle entity,
+                                      const double xyz[3] );
+
+  inline MBErrorCode get_coordinate_arrays( double*& x, 
+                                            double*& y, 
+                                            double*& z );
+
+  inline MBErrorCode get_coordinate_arrays( const double*& x,
+                                            const double*& y,
+                                            const double*& z ) const;
+ 
+  EntitySequence* split( MBEntityHandle here );
+  
+  SequenceData* create_data_subset( MBEntityHandle start, MBEntityHandle end ) const;
+  
+  MBErrorCode push_front( MBEntityID count );
+  MBErrorCode push_back( MBEntityID count );
+  
+  void get_const_memory_use( unsigned long& bytes_per_entity,
+                             unsigned long& size_of_sequence ) const;
+                             
+private:
+
+  enum Coord{ X = 0, Y = 1, Z = 2 };
+
+  inline double* array( Coord coord )
+  { 
+    return reinterpret_cast<double*>(data()->get_sequence_data( coord ));
+  }
+
+  inline const double* array( Coord coord ) const
+  { 
+    return reinterpret_cast<const double*>(data()->get_sequence_data( coord ));
+  }
+  
+  inline double* x_array() { return array(X); }
+  inline double* y_array() { return array(Y); }
+  inline double* z_array() { return array(Z); }
+  
+  inline const double* x_array() const { return array(X); }
+  inline const double* y_array() const { return array(Y); }
+  inline const double* z_array() const { return array(Z); }
+  
+  VertexSequence( VertexSequence& split_from, MBEntityHandle here )
+    : EntitySequence( split_from, here )
+    {}
+};
+
+  
+MBErrorCode VertexSequence::get_coordinates( MBEntityHandle handle,
+                                             double& x,
+                                             double& y,
+                                             double& z ) const
+{
+  MBEntityID offset = handle - data()->start_handle();
+  x = x_array()[offset];
+  y = y_array()[offset];
+  z = z_array()[offset];
+  return MB_SUCCESS;
+}
+
+MBErrorCode VertexSequence::get_coordinates( MBEntityHandle handle,
+                                             double coords[3] ) const
+{
+  MBEntityID offset = handle - data()->start_handle();
+  coords[X] = x_array()[offset];
+  coords[Y] = y_array()[offset];
+  coords[Z] = z_array()[offset];
+  return MB_SUCCESS;
+}
+  
+
+MBErrorCode VertexSequence::get_coordinates_ref( MBEntityHandle handle,
+                                                 const double*& x,
+                                                 const double*& y,
+                                                 const double*& z ) const
+{
+  MBEntityID offset = handle - data()->start_handle();
+  x = x_array()+offset;
+  y = y_array()+offset;
+  z = z_array()+offset;
+  return MB_SUCCESS;
+}
+
+MBErrorCode VertexSequence::set_coordinates( MBEntityHandle entity,
+                                             double x, 
+                                             double y,
+                                             double z )
+{
+  MBEntityID offset = entity - data()->start_handle();
+  x_array()[offset] = x;
+  y_array()[offset] = y;
+  z_array()[offset] = z;
+  return MB_SUCCESS;
+}
+
+MBErrorCode VertexSequence::set_coordinates( MBEntityHandle entity,
+                                             const double* xyz )
+{
+  MBEntityID offset = entity - data()->start_handle();
+  x_array()[offset] = xyz[0];
+  y_array()[offset] = xyz[1];
+  z_array()[offset] = xyz[2];
+  return MB_SUCCESS;
+}
+
+MBErrorCode VertexSequence::get_coordinate_arrays( double*& x, 
+                                                   double*& y, 
+                                                   double*& z )
+{
+  MBEntityID offset = start_handle() - data()->start_handle();
+  x = x_array()+offset;
+  y = y_array()+offset;
+  z = z_array()+offset;
+  return MB_SUCCESS;
+}
+  
+MBErrorCode VertexSequence::get_coordinate_arrays( const double*& x,
+                                                   const double*& y,
+                                                   const double*& z ) const
+{
+  return get_coordinates_ref( start_handle(), x, y, z ); 
+}
+
+#endif

Modified: MOAB/trunk/WriteHDF5.cpp
===================================================================
--- MOAB/trunk/WriteHDF5.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/WriteHDF5.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -217,9 +217,6 @@
       return mhdf_node_type_handle();
     case MBENTITYSET:
       return mhdf_set_type_handle();
-    case MBPOLYGON:
-    case MBPOLYHEDRON:
-      return MBCN::EntityTypeName( type );
     default:
       sprintf( buffer, "%s%d", MBCN::EntityTypeName( type ), num_nodes );
       return buffer;
@@ -402,14 +399,16 @@
   
     // Write element connectivity
   for (ex_itor = exportList.begin(); ex_itor != exportList.end(); ++ex_itor)
-  {
-    if (ex_itor->type == MBPOLYGON || ex_itor->type == MBPOLYHEDRON)
-      result = write_poly( *ex_itor );
-    else
-      result = write_elems( *ex_itor );
-    if (MB_SUCCESS != result)
+    if (MB_SUCCESS != write_elems( *ex_itor ))
       goto write_fail;
-  }
+//  {
+//    if (ex_itor->type == MBPOLYGON || ex_itor->type == MBPOLYHEDRON)
+//      result = write_poly( *ex_itor );
+//    else
+//      result = write_elems( *ex_itor );
+//    if (MB_SUCCESS != result)
+//      goto write_fail;
+//  }
 
 DEBUGOUT("Writing sets.\n");
   
@@ -485,86 +484,73 @@
   }
   return MB_SUCCESS;
 }
-   
-  // For a given element type, get all higher-order configurations
-  // (and the linear element type) has a list of node counts in
-  // each.
-MBErrorCode WriteHDF5::midnode_combinations( MBEntityType type,
-                                    std::vector<int>& combinations )
+
+MBErrorCode WriteHDF5::initialize_mesh( const MBRange ranges[5] )
 {
-  combinations.clear();
+  MBErrorCode rval;
+  
+  if (!ranges[0].all_of_type(MBVERTEX))
+    return MB_FAILURE;
+  nodeSet.range = ranges[0];
+  nodeSet.type = MBVERTEX;
+  nodeSet.num_nodes = 1;
+  
+  if (!ranges[4].all_of_type(MBENTITYSET))
+    return MB_FAILURE;
+  setSet.range = ranges[4];
+  setSet.type = MBENTITYSET;
+  setSet.num_nodes = 0;
 
-  int dimension = MBCN::Dimension( type );
-  int num_faces = 0;
-  int num_edges = 0;
-  switch (dimension) 
+  exportList.clear();
+  std::vector<MBRange> bins(1024); // sort entities by connectivity length
+                                   // resize is expensive due to MBRange copy, so start big
+  for (MBEntityType type = MBEDGE; type < MBENTITYSET; ++type)
   {
-    case 3:
-      num_faces = MBCN::NumSubEntities( type, 2 );
-      num_edges = MBCN::NumSubEntities( type, 1 );
-      combinations.push_back(num_faces + num_edges + 1);
-      combinations.push_back(num_faces + num_edges);
-      combinations.push_back(num_faces + 1);
-      combinations.push_back(num_faces);
-    case 2:
-      num_edges = MBCN::NumSubEntities( type, 1 );
-      combinations.push_back(num_edges + 1);
-      combinations.push_back(num_edges);
-    case 1:
-      combinations.push_back(1);
-      combinations.push_back(0);
-      break;
-    default:
-      assert(0);
-      return MB_FAILURE;
-  }
+    ExportSet set;
+    const int dim = MBCN::Dimension(type);
 
-  return MB_SUCCESS;
-}
+      // Group entities by connectivity length
+    bins.clear();
+    std::pair<MBRange::const_iterator,MBRange::const_iterator> p = ranges[dim].equal_range(type);
+    MBRange::const_iterator i = p.first;
+    while (i != p.second) {
+      MBRange::const_iterator first = i;
+      MBEntityHandle const* conn;
+      int len, firstlen;
+      rval = iFace->get_connectivity( *i, conn, firstlen );
+      if (MB_SUCCESS != rval)
+        return rval;
+      
+      for (++i; i != p.second; ++i) {
+        rval = iFace->get_connectivity( *i, conn, len );
+        if (MB_SUCCESS != rval)
+          return rval;
+        
+        if (len != firstlen)
+          break;
+      }
+      
+      if (firstlen >= (int)bins.size())
+        bins.resize(firstlen+1);
+      bins[firstlen].merge( first, i );
+    }
 
-  // Get the subset of the passed range that matches both the passed
-  // element type and the number of nodes in the element (the
-  // subtype, e.g. HEX20 vs. HEX8).
-MBErrorCode WriteHDF5::subrange_by_type_and_conn( const MBRange& input,
-                                                  MBEntityType type,
-                                                  int nodecount,
-                                                  MBRange& output )
-{
-  MBRange::const_iterator itor;
-  const MBEntityHandle* junk;
-  MBErrorCode rval;
-  int num_nodes;
-  output.clear();
-  for (itor = input.begin(); itor != input.end(); ++itor)
-  {
-      // The connectivity pointer (junk) is passed back.  
-    rval = iFace->get_connectivity( *itor, junk, num_nodes, false );
-    CHK_MB_ERR_0(rval);
-    if (num_nodes == nodecount && 
-        iFace->type_from_handle(*itor) == type)
-      output.insert( *itor );
+      // Create ExportSet for each group
+    for (std::vector<MBRange>::iterator j = bins.begin(); j != bins.end(); ++j) {
+      if (j->empty())
+        continue;
+        
+      set.range.clear();
+      set.type = type;
+      set.num_nodes = j - bins.begin();
+      exportList.push_back( set );
+      exportList.back().range.swap( *j );
+    }
   }
-  return MB_SUCCESS;
+    
+  return MB_SUCCESS;  
 }
 
-  // Get the subset of a range containing a given element type.
-MBErrorCode WriteHDF5::subrange_by_type( const MBRange& input,
-                                         MBEntityType type,
-                                         MBRange& output )
-{
-  int err;
-  MBEntityHandle first, last;
-  first = CREATE_HANDLE(  type, 0, err ); assert(!err);
-  last = CREATE_HANDLE( type+1, 0, err ); assert(!err);
-  MBRange::const_iterator start, end;
-  start = input.lower_bound( input.begin(), input.end(), first );
-  end = input.lower_bound( start, input.end(), last );
-  
-  output.clear();
-  output.merge( start, end );
-  return MB_SUCCESS;
-}
-
                                          
   // Gather the mesh to be written from a list of owning meshsets.
 MBErrorCode WriteHDF5::gather_mesh_info( 
@@ -572,13 +558,9 @@
 {
   MBErrorCode rval;
   
-  nodeSet.range.clear();
-  setSet.range.clear();
-  exportList.clear();
-  
   int dim;
-  MBRange range;
-  MBRange ranges[4];
+  MBRange range;      // temporary storage
+  MBRange ranges[5];  // lists of entities to export, grouped by dimension
   
     // Gather list of all related sets
   std::vector<MBEntityHandle> stack(export_sets);
@@ -587,14 +569,14 @@
   while( !stack.empty() )
   {
     MBEntityHandle meshset = stack.back(); stack.pop_back();
-    setSet.range.insert( meshset );
+    ranges[4].insert( meshset );
   
       // Get contained sets
     range.clear();
     rval = iFace->get_entities_by_type( meshset, MBENTITYSET, range );
     CHK_MB_ERR_0(rval);
     for (MBRange::iterator ritor = range.begin(); ritor != range.end(); ++ritor)
-      if (setSet.range.find( *ritor ) == setSet.range.end())
+      if (ranges[4].find( *ritor ) == ranges[4].end())
         stack.push_back( *ritor );
     
       // Get child sets
@@ -603,14 +585,14 @@
     CHK_MB_ERR_0(rval);
     for (std::vector<MBEntityHandle>::iterator vitor = set_children.begin();
          vitor != set_children.end(); ++vitor )
-      if (setSet.range.find( *vitor ) == setSet.range.end())
+      if (ranges[4].find( *vitor ) == ranges[4].end())
         stack.push_back( *vitor );
   }
   
     // Gather list of all mesh entities from list of sets,
     // grouped by dimension.
-  for (MBRange::iterator setitor = setSet.range.begin();
-       setitor != setSet.range.end(); ++setitor)
+  for (MBRange::iterator setitor = ranges[4].begin();
+       setitor != ranges[4].end(); ++setitor)
   {
     for (dim = 0; dim < 4; ++dim)
     {
@@ -639,126 +621,36 @@
     ranges[0].merge( range );      
   }
   
-    // Split lists by element type and number of nodes
-  nodeSet.range = ranges[0];
-  nodeSet.type = MBVERTEX;
-  nodeSet.num_nodes = 1;
-  setSet.type = MBENTITYSET;
-  setSet.num_nodes = 0;
-  
-  std::vector<int> node_counts;
-  for (MBEntityType type = MBEDGE; type < MBENTITYSET; ++type)
-  {
-    ExportSet set;
-    dim = MBCN::Dimension(type);
-
-    if (ranges[dim].empty())
-      continue;
-    
-    if (type == MBPOLYGON || type == MBPOLYHEDRON)
-    {
-      rval = subrange_by_type( ranges[dim], type, set.range );
-      CHK_MB_ERR_0(rval);
-      
-      if (!set.range.empty())
-      {
-        set.type = type;
-        set.num_nodes = 0;
-        exportList.push_back( set );
-      }
-    }
-    else
-    {      
-      node_counts.clear();
-      rval = midnode_combinations( type, node_counts );
-      CHK_MB_ERR_0(rval);
-
-      const int num_corners = MBCN::VerticesPerEntity( type );
-      for (std::vector<int>::iterator itor = node_counts.begin();
-           itor != node_counts.end(); ++itor)
-      {
-        set.range.clear();
-        int num_nodes = *itor + num_corners;
-        rval = subrange_by_type_and_conn( ranges[dim], type, num_nodes, set.range );
-        CHK_MB_ERR_0(rval);
-
-        if (!set.range.empty())
-        {
-          set.type = type;
-          set.num_nodes = num_nodes;
-          exportList.push_back( set );
-        }
-      }
-    }
-  }
-    
-  return MB_SUCCESS;  
+  return initialize_mesh( ranges );
 }
 
   // Gather all the mesh and related information to be written.
 MBErrorCode WriteHDF5::gather_all_mesh( )
 {
   MBErrorCode rval;
-  
-    // Get all nodes
-  nodeSet.range.clear();
-  rval = iFace->get_entities_by_type( 0, MBVERTEX, nodeSet.range );
-  CHK_MB_ERR_0(rval);
-  nodeSet.type = MBVERTEX;
-  nodeSet.num_nodes = 1;
-  
-    // Get all sets
-  setSet.range.clear();
-  rval = iFace->get_entities_by_type( 0, MBENTITYSET, setSet.range );
-  CHK_MB_ERR_0(rval);
-  setSet.type = MBENTITYSET;
-  setSet.num_nodes = 0;
+  MBRange ranges[5];
 
-    // Get all elements, grouped by type and number of higher-order nodes
-  exportList.clear();
-  std::vector<int> node_counts;
-  for (MBEntityType type = MBEDGE; type < MBENTITYSET; ++type)
-  {
-    ExportSet set;
-    
-    MBRange range;
-    rval = iFace->get_entities_by_type( 0, type, range );
-    CHK_MB_ERR_0(rval);
-      
-    if (range.empty())
-      continue;
-    
-    if (type == MBPOLYGON || type == MBPOLYHEDRON)
-    {
-      set.range = range;
-      set.type = type;
-      set.num_nodes = 0;
-      exportList.push_back( set );
-      continue;
-    }
-    
-    rval = midnode_combinations( type, node_counts );
-    CHK_MB_ERR_0(rval);
-      
-    const int num_corners = MBCN::VerticesPerEntity( type );
-    for (std::vector<int>::iterator itor = node_counts.begin();
-         itor != node_counts.end(); ++itor)
-    {
-      set.range.clear();
-      int num_nodes = *itor + num_corners;
-      rval = subrange_by_type_and_conn( range, type, num_nodes, set.range );
-      CHK_MB_ERR_0(rval);
-      
-      if (!set.range.empty())
-      {
-        set.type = type;
-        set.num_nodes = num_nodes;
-        exportList.push_back( set );
-      }
-    }
-  }
-  
-  return MB_SUCCESS;
+  rval = iFace->get_entities_by_type( 0, MBVERTEX, ranges[0] );
+  if (MB_SUCCESS != rval)
+    return rval;
+
+  rval = iFace->get_entities_by_dimension( 0, 1, ranges[1] );
+  if (MB_SUCCESS != rval)
+    return rval;
+
+  rval = iFace->get_entities_by_dimension( 0, 2, ranges[2] );
+  if (MB_SUCCESS != rval)
+    return rval;
+
+  rval = iFace->get_entities_by_dimension( 0, 3, ranges[3] );
+  if (MB_SUCCESS != rval)
+    return rval;
+
+  rval = iFace->get_entities_by_type( 0, MBENTITYSET, ranges[4] );
+  if (MB_SUCCESS != rval)
+    return rval;
+
+  return initialize_mesh( ranges );
 }
   
 MBErrorCode WriteHDF5::write_nodes( )
@@ -865,70 +757,6 @@
   return MB_SUCCESS;
 }
 
-
-MBErrorCode WriteHDF5::write_poly( ExportSet& elems )
-{
-  mhdf_Status status;
-  MBErrorCode rval;
-  long first_id, table_size, poly_count;
-  
-  assert( elems.type == MBPOLYGON || elems.type == MBPOLYHEDRON );
-  
-  const MBRange::const_iterator end = elems.range.end();
-  MBRange::const_iterator iter;
-
-    // Create the tables in the file and assign IDs to polys
-  hid_t handles[2];
-  mhdf_openPolyConnectivity( filePtr, 
-                             MBCN::EntityTypeName(elems.type),
-                             &poly_count,
-                             &table_size,
-                             &first_id,
-                             handles,
-                             &status );
-  CHK_MHDF_ERR_0(status);
-  assert (first_id <= elems.first_id);
-  assert ((unsigned long)poly_count >= elems.offset + elems.range.size());
-  
-    // Split the data buffer into two chunks, one for the indices
-    // and one for the IDs.  Assume average of 4 IDs per poly.
-  
-  size_t chunk_size = bufferSize / (5*sizeof(id_t));
-  id_t* idx_buffer = (id_t*)dataBuffer;
-  id_t* conn_buffer = idx_buffer + chunk_size;
-  
-  long offset[2] = {0,0};
-  id_t index_offset = 0;
-  iter = elems.range.begin();
-  
-  while (iter != end)
-  {
-    size_t num_idx = chunk_size;
-    size_t num_conn = 4*chunk_size;
-    rval = writeUtil->get_poly_arrays( iter, end, idTag,
-                                       num_conn, conn_buffer,
-                                       num_idx,  idx_buffer,
-                                       index_offset );
-    CHK_MB_ERR_2(rval, handles, status);
-    
-    mhdf_writePolyConnIndices( handles[0], offset[0], num_idx, id_type, idx_buffer, &status );
-    CHK_MHDF_ERR_2(status, handles);
-    offset[0] += num_idx;
-    
-    mhdf_writePolyConnIDs( handles[1], offset[1], num_conn, id_type, conn_buffer, &status );
-    CHK_MHDF_ERR_2(status, handles);
-    offset[1] += num_conn;
-  }
-
-  mhdf_closeData( filePtr, handles[0], &status );
-  CHK_MHDF_ERR_1(status,  handles[1]);
-
-  mhdf_closeData( filePtr, handles[1], &status );
-  CHK_MHDF_ERR_0(status);
- 
-  return MB_SUCCESS;
-}
-
 MBErrorCode WriteHDF5::get_set_info( MBEntityHandle set,
                                      long& num_entities,
                                      long& num_children,
@@ -1893,26 +1721,10 @@
     // Create element tables
   for (ex_itor = exportList.begin(); ex_itor != exportList.end(); ++ex_itor)
   {
-    if (ex_itor->type == MBPOLYGON || ex_itor->type == MBPOLYHEDRON)
-    {
-      int mb_count;
-      rval = writeUtil->get_poly_array_size( ex_itor->range.begin(),
-                                             ex_itor->range.end(),
-                                             mb_count );
-      CHK_MB_ERR_0(rval);
-      
-      rval = create_poly_tables( ex_itor->type,
-                                 ex_itor->range.size(),
-                                 mb_count,
-                                 first_id );
-    }
-    else
-    {
-      rval = create_elem_tables( ex_itor->type,
-                                 ex_itor->num_nodes,
-                                 ex_itor->range.size(),
-                                 first_id );
-    }
+    rval = create_elem_tables( ex_itor->type,
+                               ex_itor->num_nodes,
+                               ex_itor->range.size(),
+                               first_id );
     CHK_MB_ERR_0(rval);
       
     writeUtil->assign_ids( ex_itor->range, idTag, (id_t)first_id );
@@ -2051,34 +1863,6 @@
 }
 
 
-
-MBErrorCode WriteHDF5::create_poly_tables( MBEntityType mb_type,
-                                           id_t num_poly,
-                                           id_t connectivity_size,
-                                           long& first_id_out )
-{
-  char name[64];
-  mhdf_Status status;
-  hid_t handles[2];
-  
-  strcpy( name, MBCN::EntityTypeName(mb_type) );
-  mhdf_addElement( filePtr, name, mb_type, &status );
-  CHK_MHDF_ERR_0(status);
-  
-  mhdf_createPolyConnectivity( filePtr, 
-                               name, 
-                               num_poly, 
-                               connectivity_size, 
-                               &first_id_out,
-                               handles,
-                               &status );
-  CHK_MHDF_ERR_0(status);
-  mhdf_closeData( filePtr, handles[0], &status );
-  mhdf_closeData( filePtr, handles[1], &status );
-  
-  return MB_SUCCESS;
-}
-
 MBErrorCode WriteHDF5::count_set_size( const MBRange& sets, 
                                        MBRange& compressed_sets,
                                        long& contents_length_out,

Modified: MOAB/trunk/WriteHDF5.hpp
===================================================================
--- MOAB/trunk/WriteHDF5.hpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/WriteHDF5.hpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -114,16 +114,6 @@
                                   int nodes_per_element,
                                   id_t number_elements,
                                   long& first_id_out );
-
-  /** Helper function for create-file
-   *
-   * Create zero-ed tables where poly connectivity and 
-   * adjacency data will be stored.
-   */
-  MBErrorCode create_poly_tables( MBEntityType mb_type,
-                                  id_t number_elements,
-                                  id_t connectivity_length,
-                                  long& first_id_out );
   
   /** Helper function for create-file
    *
@@ -256,35 +246,6 @@
 
   //! Zero the ID tag on all entities in the mesh.
   MBErrorCode clear_all_id_tags();
-  
-  /** Get the subset of an entity range that is a specified element type
-   * \param input_range  The range to subset
-   * \param type         The base element type
-   * \param number_nodes The number of nodes per element
-   * \param output_range The result subset of the input range.
-   */
-  MBErrorCode subrange_by_type_and_conn( const MBRange& input_range,
-                                         MBEntityType type,
-                                         int number_nodes,
-                                         MBRange& output_range );
-  
-  /** Get the subrange of a given range containing the specified elem type
-   * \param input_range  The range to subset
-   * \param type         The element type
-   * \param output_range The subset of input_range of type <code>type</code>
-   */
-  MBErrorCode subrange_by_type( const MBRange& input_range,
-                                MBEntityType type,
-                                MBRange& output_range );
-  
-  /** Get higher-order types
-   *
-   * For each higher-order type of the element, append the number of
-   * of nodes beyond those of the base/simplest type that that higher-
-   * order type has.  Also appends zero to the list for the base type.
-   */
-  MBErrorCode midnode_combinations( MBEntityType type,
-                                    std::vector<int>& combinations );
 
   //! Get information about a meshset
   MBErrorCode get_set_info( MBEntityHandle set,
@@ -337,6 +298,9 @@
   
   //! Same as gather_mesh_info, except for entire mesh
   MBErrorCode gather_all_mesh( );
+  
+  //! Initialize internal data structures from gathered mesh
+  MBErrorCode initialize_mesh( const MBRange entities_by_dim[5] );
  
   /** Write out the nodes.
    *
@@ -353,16 +317,6 @@
    */
   MBErrorCode write_elems( ExportSet& elemset );
   
-  /** Write out poly(hedr/g)on connectivity
-   *
-   * Write connectivity for passed set of poly
-   *
-   * Note: Assigns element IDs.
-   * Note: Must do write of lower-dimension entities first so
-   *       IDs get assigned to them.
-   */
-  MBErrorCode write_poly( ExportSet& elemset );
-  
   /** Write out meshsets
    * 
    * Write passed set of meshsets, including parent/child relations.

Modified: MOAB/trunk/scdseq_test.cpp
===================================================================
--- MOAB/trunk/scdseq_test.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/scdseq_test.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -17,27 +17,29 @@
 // test the structured sequence stuff
 //
 
-#include "ScdVertexSeq.hpp"
-#include "ScdElementSeq.hpp"
-#include "EntitySequenceManager.hpp"
-#include "EntitySequence.hpp"
+#include "ScdVertexData.hpp"
+#include "ScdElementData.hpp"
+#include "SequenceManager.hpp"
+#include "StructuredElementSeq.hpp"
+#include "VertexSequence.hpp"
 #include "MBCore.hpp"
 
 #include <iostream>
+#include <assert.h>
 
 int test_vertex_seq(MBCore *gMB);
-MBErrorCode check_vertex_sequence(const ScdVertexSeq *this_seq, 
+MBErrorCode check_vertex_sequence(const ScdVertexData *this_seq, 
                                  const int imin, const int jmin, const int kmin, 
                                  const int imax, const int jmax, const int kmax, 
                                  const MBEntityHandle this_start);
-MBErrorCode evaluate_vertex_sequence(ScdVertexSeq *this_seq);
+MBErrorCode evaluate_vertex_sequence(ScdVertexData *this_seq);
 
 int test_element_seq(MBCore *gMB);
-MBErrorCode check_element_sequence(const ScdElementSeq *this_seq, 
+MBErrorCode check_element_sequence(const StructuredElementSeq *this_seq, 
                                     const HomCoord &min_params,
                                     const HomCoord &max_params,
                                     const MBEntityHandle this_start);
-MBErrorCode evaluate_element_sequence(ScdElementSeq *this_seq);
+MBErrorCode evaluate_element_sequence(StructuredElementSeq *this_seq);
 int eseq_test1a(MBCore *gMB, HomCoord tmp_min, HomCoord tmp_max);
 int eseq_test1b(MBCore *gMB, HomCoord tmp_min, HomCoord tmp_max);
 int eseq_test1c(MBCore *gMB, HomCoord tmp_min, HomCoord tmp_max);
@@ -48,25 +50,25 @@
 
 int create_1d_3_sequences(MBCore *gMB,
                           HomCoord tmp_min, HomCoord tmp_max,
-                          ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                          ScdElementSeq **eseq, MBEntityHandle *estart);
+                          ScdVertexData **vseq, MBEntityHandle *vstart,
+                          StructuredElementSeq **eseq, MBEntityHandle *estart);
 
 int create_2d_3_sequences(MBCore *gMB,
-                          ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                          ScdElementSeq **eseq, MBEntityHandle *estart);
+                          ScdVertexData **vseq, MBEntityHandle *vstart,
+                          StructuredElementSeq **eseq, MBEntityHandle *estart);
 
 int create_2dtri_3_sequences(MBCore *gMB,
                              const int int1, const int int2, const int int3,
-                             ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                             ScdElementSeq **eseq, MBEntityHandle *estart);
+                             ScdVertexData **vseq, MBEntityHandle *vstart,
+                             StructuredElementSeq **eseq, MBEntityHandle *estart);
 int create_3dtri_3_sequences(MBCore *gMB,
                              const int int1, const int int2, const int int3, const int int4,
-                             ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                             ScdElementSeq **eseq, MBEntityHandle *estart);
+                             ScdVertexData **vseq, MBEntityHandle *vstart,
+                             StructuredElementSeq **eseq, MBEntityHandle *estart);
 
 // first comes general-capability code used by various tests; main and test functions
 // come after these, starting with main
-MBErrorCode check_vertex_sequence(const ScdVertexSeq *this_seq, 
+MBErrorCode check_vertex_sequence(const ScdVertexData *this_seq, 
                                    const int imin, const int jmin, const int kmin, 
                                    const int imax, const int jmax, const int kmax, 
                                    const MBEntityHandle this_start) 
@@ -94,7 +96,7 @@
     result = MB_FAILURE;
   }
   
-  if (this_start != this_seq->get_start_handle()) {
+  if (this_start != this_seq->start_handle()) {
     std::cout << "Start handle for sequence wrong." << std::endl;
     result = MB_FAILURE;
   }
@@ -102,7 +104,7 @@
   return result;
 }
   
-MBErrorCode check_element_sequence(const ScdElementSeq *this_seq, 
+MBErrorCode check_element_sequence(const StructuredElementSeq *this_seq, 
                                     const HomCoord &min_params,
                                     const HomCoord &max_params,
                                     const MBEntityHandle this_start) 
@@ -130,7 +132,7 @@
     result = MB_FAILURE;
   }
   
-  if (this_start != this_seq->get_start_handle()) {
+  if (this_start != this_seq->start_handle()) {
     std::cout << "Start handle for sequence wrong." << std::endl;
     result = MB_FAILURE;
   }
@@ -143,7 +145,7 @@
   return result;
 }
   
-MBErrorCode evaluate_vertex_sequence(ScdVertexSeq *this_seq) 
+MBErrorCode evaluate_vertex_sequence(ScdVertexData *this_seq) 
 {
   MBErrorCode result = MB_SUCCESS;
   
@@ -153,7 +155,7 @@
   this_seq->max_params(imax, jmax, kmax);
 
     // then the start vertex
-  MBEntityHandle start_handle = this_seq->get_start_handle();
+  MBEntityHandle start_handle = this_seq->start_handle();
   
     // now evaluate all the vertices in forward and reverse
   MBEntityHandle tmp_handle, tmp_handle2;
@@ -200,7 +202,7 @@
   return result;
 }
 
-MBErrorCode evaluate_element_sequence(ScdElementSeq *this_seq) 
+MBErrorCode evaluate_element_sequence(StructuredElementSeq *this_seq) 
 {
   MBErrorCode result = MB_SUCCESS;
   
@@ -282,18 +284,18 @@
 int test_vertex_seq(MBCore *gMB) 
 {
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
   
   int errors = 0;
   MBEntityHandle oned_start, twod_start, threed_start;
-  MBEntitySequence *dum_seq = NULL;
-  ScdVertexSeq *oned_seq = NULL, *twod_seq = NULL, *threed_seq = NULL;
+  EntitySequence *dum_seq = NULL;
+  ScdVertexData *oned_seq = NULL, *twod_seq = NULL, *threed_seq = NULL;
   
     // make a 1d sequence
   MBErrorCode result = seq_mgr->create_scd_sequence(-10, 0, 0, 10, 0, 0,
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      oned_start, dum_seq);
-  if (NULL != dum_seq) oned_seq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) oned_seq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   if (MB_FAILURE == result || oned_start == 0 || dum_seq == NULL ||
       oned_seq == NULL) {
     std::cout << "Problems creating a 1d sequence." << std::endl;
@@ -318,9 +320,9 @@
     // make a 2d sequence
   dum_seq = NULL;
   result = seq_mgr->create_scd_sequence(-10, -10, 0, 10, 10, 0,
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         twod_start, dum_seq);
-  if (NULL != dum_seq) twod_seq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) twod_seq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   if (MB_FAILURE == result || twod_start == 0 || dum_seq == NULL ||
       twod_seq == NULL) {
     std::cout << "Problems creating a 2d sequence." << std::endl;
@@ -345,9 +347,9 @@
     // make a 3d sequence
   dum_seq = NULL;
   result = seq_mgr->create_scd_sequence(-10, -10, -10, 10, 10, 10,
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         threed_start, dum_seq);
-  if (NULL != dum_seq) threed_seq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) threed_seq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   if (MB_FAILURE == result || threed_start == 0 || dum_seq == NULL ||
       threed_seq == NULL) {
     std::cout << "Problems creating a 3d sequence." << std::endl;
@@ -404,28 +406,29 @@
   tmp_min[1] = tmp_min[2] = tmp_max[1] = tmp_max[2] = 0;
 
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
   
   MBEntityHandle oned_start;
-  MBEntitySequence *dum_seq;
-  ScdVertexSeq *oned_seq = NULL;
+  EntitySequence *dum_seq;
+  ScdVertexData *oned_seq = NULL;
   MBErrorCode result = seq_mgr->create_scd_sequence(tmp_min, tmp_max,
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      oned_start, dum_seq);
-  if (NULL != dum_seq) oned_seq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) oned_seq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && oned_start != 0 && dum_seq != NULL && oned_seq != NULL);
 
     // now create the element sequence
   MBEntityHandle eseq_start;
-  ScdElementSeq *eseq = NULL;
+  StructuredElementSeq *eseq = NULL;
   result = seq_mgr->create_scd_sequence(tmp_min, tmp_max,
-                                        MBEDGE, 1,
+                                        MBEDGE, 1, 0,
                                         eseq_start, dum_seq);
-  if (NULL != dum_seq) eseq = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && eseq_start != 0 && dum_seq != NULL && eseq != NULL);
   
     // add vertex seq to element seq
-  result = eseq->add_vsequence(oned_seq,
+  result = dynamic_cast<ScdElementData*>(eseq->data())
+               ->add_vsequence(oned_seq,
                                tmp_min, tmp_min,
                                tmp_max, tmp_max,
                                tmp_min, tmp_min);
@@ -460,30 +463,31 @@
   tmp_min[2] = tmp_max[2] = 0;
 
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
   
   MBEntityHandle twod_start;
-  MBEntitySequence *dum_seq;
-  ScdVertexSeq *twod_seq = NULL;
+  EntitySequence *dum_seq;
+  ScdVertexData *twod_seq = NULL;
   MBErrorCode result = seq_mgr->create_scd_sequence(tmp_min, tmp_max,
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      twod_start, dum_seq);
-  if (NULL != dum_seq) twod_seq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) twod_seq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && twod_start != 0 && dum_seq != NULL && twod_seq != NULL);
 
     // now create the element sequence
   MBEntityHandle eseq_start;
-  ScdElementSeq *eseq = NULL;
+  StructuredElementSeq *eseq = NULL;
   result = seq_mgr->create_scd_sequence(tmp_min, tmp_max,
-                                        MBQUAD, 1,
+                                        MBQUAD, 1, 0,
                                         eseq_start, dum_seq);
-  if (NULL != dum_seq) eseq = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && eseq_start != 0 && dum_seq != NULL && eseq != NULL);
   
     // add vertex seq to element seq; first need to construct proper 3pt input (p1 is tmp_min)
   HomCoord p2(tmp_max.i(), tmp_min.j(), tmp_min.k());
   HomCoord p3(tmp_min.i(), tmp_max.j(), tmp_min.k());
-  result = eseq->add_vsequence(twod_seq,
+  result = dynamic_cast<ScdElementData*>(eseq->data())
+               ->add_vsequence(twod_seq,
                                tmp_min, tmp_min,
                                p2, p2, p3, p3);
   if (MB_SUCCESS != result) {
@@ -514,31 +518,31 @@
   int errors = 0;
 
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
   
   MBEntityHandle threed_start;
-  MBEntitySequence *dum_seq;
-  ScdVertexSeq *threed_seq = NULL;
+  EntitySequence *dum_seq;
+  ScdVertexData *threed_seq = NULL;
   MBErrorCode result = seq_mgr->create_scd_sequence(tmp_min, tmp_max,
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      threed_start, dum_seq);
-  if (NULL != dum_seq) threed_seq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) threed_seq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && threed_start != 0 && dum_seq != NULL && threed_seq != NULL);
 
     // now create the element sequence
   MBEntityHandle eseq_start;
-  ScdElementSeq *eseq = NULL;
+  StructuredElementSeq *eseq = NULL;
   result = seq_mgr->create_scd_sequence(tmp_min, tmp_max,
-                                        MBHEX, 1,
+                                        MBHEX, 1, 0,
                                         eseq_start, dum_seq);
-  if (NULL != dum_seq) eseq = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && eseq_start != 0 && dum_seq != NULL && eseq != NULL);
   
     // add vertex seq to element seq; first need to construct proper 3pt input (p1 is tmp_min)
   HomCoord p2(tmp_max.i(), tmp_min.j(), tmp_min.k());
   HomCoord p3(tmp_min.i(), tmp_max.j(), tmp_min.k());
-  result = eseq->add_vsequence(threed_seq,
-                               tmp_min, tmp_min, p2, p2, p3, p3);
+  result = dynamic_cast<ScdElementData*>(eseq->data())
+            ->add_vsequence(threed_seq, tmp_min, tmp_min, p2, p2, p3, p3);
   if (MB_SUCCESS != result) {
     std::cout << "Couldn't add vsequence to 3d single-block element sequence." << std::endl;
     errors++;
@@ -565,8 +569,8 @@
     // TEST 2a: 1d composite block, 0d difference between owning/sharing blocks
     // create vertex seq
   
-  ScdVertexSeq *vseq[3];
-  ScdElementSeq *eseq[3];
+  ScdVertexData *vseq[3];
+  StructuredElementSeq *eseq[3];
   MBEntityHandle vstart[3], estart[3];
   
   int errors = create_1d_3_sequences(gMB, tmp_min, tmp_max,
@@ -607,8 +611,8 @@
     // TEST 2b: 2d composite block, 0d difference between owning/sharing blocks
     // create vertex seq
   
-  ScdVertexSeq *vseq[3];
-  ScdElementSeq *eseq[3];
+  ScdVertexData *vseq[3];
+  StructuredElementSeq *eseq[3];
   MBEntityHandle vstart[3], estart[3];
   
   int errors = create_2d_3_sequences(gMB, vseq, vstart, eseq, estart);
@@ -650,8 +654,8 @@
     // TEST 2c: 2d composite block, 0d difference between owning/sharing blocks,
     // tri-valent shared vertex between the three blocks
 
-  ScdVertexSeq *vseq[3];
-  ScdElementSeq *eseq[3];
+  ScdVertexData *vseq[3];
+  StructuredElementSeq *eseq[3];
   MBEntityHandle vstart[3], estart[3];
 
     // interval settings: only 3 of them
@@ -685,8 +689,8 @@
     // TEST 2d: 3d composite block, 0d difference between owning/sharing blocks,
     // tri-valent shared edge between the three blocks
 
-  ScdVertexSeq *vseq[3];
-  ScdElementSeq *eseq[3];
+  ScdVertexData *vseq[3];
+  StructuredElementSeq *eseq[3];
   MBEntityHandle vstart[3], estart[3];
 
     // interval settings: only 3 of them
@@ -717,8 +721,8 @@
 
 int create_1d_3_sequences(MBCore *gMB,
                           HomCoord tmp_min, HomCoord tmp_max,
-                          ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                          ScdElementSeq **eseq, MBEntityHandle *estart) 
+                          ScdVertexData **vseq, MBEntityHandle *vstart,
+                          StructuredElementSeq **eseq, MBEntityHandle *estart) 
 {
   int errors = 0;
   
@@ -733,32 +737,32 @@
   HomCoord vseq2_minmax[2] = {HomCoord(tmp_min), HomCoord(tmp_max[0]-5*idiff, tmp_max[1], tmp_max[2])};
   
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
 
     // create three vertex sequences
-  MBEntitySequence *dum_seq;
+  EntitySequence *dum_seq;
   vseq[0] = vseq[1] = vseq[2] = NULL;
   
 
     // first vertex sequence 
   MBErrorCode result = seq_mgr->create_scd_sequence(vseq0_minmax[0], vseq0_minmax[1],
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      vstart[0], dum_seq);
-  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[0] != 0 && dum_seq != NULL && vseq[0] != NULL);
 
     // second vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq1_minmax[0], vseq1_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[1], dum_seq);
-  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[1] != 0 && dum_seq != NULL && vseq[1] != NULL);
 
     // third vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq2_minmax[0], vseq2_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[2], dum_seq);
-  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[2] != 0 && dum_seq != NULL && vseq[2] != NULL);
 
     // now create the three element sequences
@@ -766,13 +770,13 @@
   
     // create the first element sequence
   result = seq_mgr->create_scd_sequence(vseq0_minmax[0], vseq0_minmax[1],
-                                        MBEDGE, 1,
+                                        MBEDGE, 1, 0,
                                         estart[0], dum_seq);
-  if (NULL != dum_seq) eseq[0] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[0] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[0] != 0 && dum_seq != NULL && eseq[0] != NULL);
   
     // add first vertex seq to first element seq, forward orientation, unity transform
-  result = eseq[0]->add_vsequence(vseq[0],
+  result = eseq[0]->sdata()->add_vsequence(vseq[0],
                                   vseq0_minmax[0], vseq0_minmax[0],
                                   vseq0_minmax[1], vseq0_minmax[1],
                                   vseq0_minmax[0], vseq0_minmax[0]);
@@ -786,14 +790,14 @@
     // end of the previous eseq
   result = seq_mgr->create_scd_sequence(HomCoord(vseq0_minmax[1].i(), 0, 0),
                                         HomCoord(1+vseq0_minmax[1].i()+vseq1_minmax[1].i()-vseq1_minmax[0].i(), 0, 0),
-                                        MBEDGE, 1,
+                                        MBEDGE, 1, 0,
                                         estart[1], dum_seq);
-  if (NULL != dum_seq) eseq[1] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[1] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[1] != 0 && dum_seq != NULL && eseq[1] != NULL);
   
     // add shared vertex from first vseq to this eseq; parameter space should be the same since
     // we're adding to that parameter space
-  result = eseq[1]->add_vsequence(vseq[0],
+  result = eseq[1]->sdata()->add_vsequence(vseq[0],
                                   vseq0_minmax[0], vseq0_minmax[0],
                                   vseq0_minmax[1], vseq0_minmax[1],
                                   vseq0_minmax[0], vseq0_minmax[0],
@@ -807,7 +811,7 @@
   
     // add second vseq to this eseq, but reversed; parameter space should be such that the
     // last vertex in the second vseq occurs first, and so on
-  result = eseq[1]->add_vsequence(vseq[1],
+  result = eseq[1]->sdata()->add_vsequence(vseq[1],
                                   vseq1_minmax[1], eseq[1]->min_params()+HomCoord::unitv[0],
                                   vseq1_minmax[0], eseq[1]->max_params(),
                                   vseq1_minmax[1], eseq[1]->min_params()+HomCoord::unitv[0]);
@@ -821,14 +825,14 @@
     // end of the previous eseq
   result = seq_mgr->create_scd_sequence(eseq[1]->max_params(),
                                         HomCoord(eseq[1]->max_params().i()+1+vseq2_minmax[1].i()-vseq2_minmax[0].i(),0,0),
-                                        MBEDGE, 1,
+                                        MBEDGE, 1, 0,
                                         estart[2], dum_seq);
-  if (NULL != dum_seq) eseq[2] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[2] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[2] != 0 && dum_seq != NULL && eseq[2] != NULL);
   
     // add shared vertex from second vseq to this eseq; parameter space mapping such that we get
     // first vertex only of that vseq
-  result = eseq[2]->add_vsequence(vseq[1],
+  result = eseq[2]->sdata()->add_vsequence(vseq[1],
                                   vseq0_minmax[0], eseq[2]->min_params(),
                                   vseq0_minmax[0]+HomCoord::unitv[0], eseq[2]->min_params()-HomCoord::unitv[0],
                                   vseq0_minmax[0], eseq[2]->min_params(),
@@ -841,7 +845,7 @@
   }
   
     // add third vseq to this eseq, forward orientation
-  result = eseq[2]->add_vsequence(vseq[2],
+  result = eseq[2]->sdata()->add_vsequence(vseq[2],
                                   vseq2_minmax[0], eseq[2]->min_params()+HomCoord::unitv[0],
                                   vseq2_minmax[1], eseq[2]->max_params(),
                                   vseq1_minmax[0], eseq[2]->min_params()+HomCoord::unitv[0]);
@@ -854,8 +858,8 @@
 }
 
 int create_2d_3_sequences(MBCore *gMB,
-                          ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                          ScdElementSeq **eseq, MBEntityHandle *estart) 
+                          ScdVertexData **vseq, MBEntityHandle *vstart,
+                          StructuredElementSeq **eseq, MBEntityHandle *estart) 
 {
     // create 3 rectangular esequences attached end to end and back (periodic); vsequences are 
     // assorted orientations, esequences have globally-consistent (periodic in i) parameter space
@@ -867,31 +871,31 @@
   HomCoord vseq2_minmax[2] = {HomCoord(0,0,0), HomCoord(8,5,0)};
 
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
 
     // create three vertex sequences
-  MBEntitySequence *dum_seq;
+  EntitySequence *dum_seq;
   vseq[0] = vseq[1] = vseq[2] = NULL;
 
     // first vertex sequence 
   MBErrorCode result = seq_mgr->create_scd_sequence(vseq0_minmax[0], vseq0_minmax[1],
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      vstart[0], dum_seq);
-  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[0] != 0 && dum_seq != NULL && vseq[0] != NULL);
 
     // second vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq1_minmax[0], vseq1_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[1], dum_seq);
-  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[1] != 0 && dum_seq != NULL && vseq[1] != NULL);
 
     // third vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq2_minmax[0], vseq2_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[2], dum_seq);
-  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[2] != 0 && dum_seq != NULL && vseq[2] != NULL);
 
     // now create the three element sequences
@@ -899,13 +903,13 @@
   
     // create the first element sequence
   result = seq_mgr->create_scd_sequence(vseq0_minmax[0], vseq0_minmax[1],
-                                        MBQUAD, 1,
+                                        MBQUAD, 1, 0,
                                         estart[0], dum_seq);
-  if (NULL != dum_seq) eseq[0] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[0] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[0] != 0 && dum_seq != NULL && eseq[0] != NULL);
   
     // add first vertex seq to first element seq, forward orientation, unity transform
-  result = eseq[0]->add_vsequence(vseq[0],
+  result = eseq[0]->sdata()->add_vsequence(vseq[0],
                                     // p1: imin,jmin
                                   vseq0_minmax[0], vseq0_minmax[0],
                                     // p2: imax,jmin
@@ -925,14 +929,14 @@
   result = seq_mgr->create_scd_sequence(HomCoord(eseq[0]->max_params().i(), eseq[0]->min_params().j(), 0),
                                         HomCoord(vseq0_minmax[1].i()+1+vseq1_minmax[1].i()-vseq1_minmax[0].i(), 
                                                  eseq[0]->max_params().j(), 0),
-                                        MBQUAD, 1,
+                                        MBQUAD, 1, 0,
                                         estart[1], dum_seq);
-  if (NULL != dum_seq) eseq[1] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[1] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[1] != 0 && dum_seq != NULL && eseq[1] != NULL);
   
     // add shared side from first vseq to this eseq; parameter space should be the same since
     // we're adding to that parameter space
-  result = eseq[1]->add_vsequence(vseq[0],
+  result = eseq[1]->sdata()->add_vsequence(vseq[0],
                                   vseq0_minmax[0], vseq0_minmax[0],
                                   vseq0_minmax[1], vseq0_minmax[1],
                                   vseq0_minmax[0], vseq0_minmax[0],
@@ -946,7 +950,7 @@
   }
   
     // add second vseq to this eseq, with different orientation but all of it (no bb input)
-  result = eseq[1]->add_vsequence(vseq[1],
+  result = eseq[1]->sdata()->add_vsequence(vseq[1],
                                     // p1: one right of top left
                                   vseq1_minmax[0], 
                                   HomCoord(eseq[1]->min_params().i()+1, eseq[1]->max_params().j(), 0),
@@ -968,14 +972,14 @@
                                           // add one extra for each of left and right sides
                                         HomCoord(eseq[1]->max_params().i()+1+vseq2_minmax[1].i()-vseq2_minmax[0].i()+1,
                                                  eseq[1]->max_params().j(),0),
-                                        MBEDGE, 1,
+                                        MBEDGE, 1, 0,
                                         estart[2], dum_seq);
-  if (NULL != dum_seq) eseq[2] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[2] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[2] != 0 && dum_seq != NULL && eseq[2] != NULL);
   
     // add shared side from second vseq to this eseq; parameter space mapping such that we get
     // a side only of that vseq
-  result = eseq[2]->add_vsequence(vseq[1],
+  result = eseq[2]->sdata()->add_vsequence(vseq[1],
                                     // p1: bottom left
                                   vseq1_minmax[1], eseq[2]->min_params(),
                                     // p2: one right from p1
@@ -995,7 +999,7 @@
   
     // add shared side from first vseq to this eseq; parameter space mapping such that we get
     // a side only of that vseq
-  result = eseq[2]->add_vsequence(vseq[0],
+  result = eseq[2]->sdata()->add_vsequence(vseq[0],
                                     // p1: bottom right
                                   vseq1_minmax[0], HomCoord(eseq[2]->max_params().i(), eseq[2]->min_params().j(),0),
                                     // p2: one right from p1
@@ -1015,7 +1019,7 @@
   
 
     // add third vseq to this eseq
-  result = eseq[2]->add_vsequence(vseq[2],
+  result = eseq[2]->sdata()->add_vsequence(vseq[2],
                                     // p1: top right and left one
                                   vseq2_minmax[0], eseq[2]->max_params()-HomCoord::unitv[0],
                                     // p2: one left of p1
@@ -1033,8 +1037,8 @@
 
 int create_2dtri_3_sequences(MBCore *gMB,
                              const int int1, const int int2, const int int3,
-                             ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                             ScdElementSeq **eseq, MBEntityHandle *estart) 
+                             ScdVertexData **vseq, MBEntityHandle *vstart,
+                             StructuredElementSeq **eseq, MBEntityHandle *estart) 
 {
     // create 3 rectangular esequences arranged such that the all share a common (tri-valent) corner;
     // orient each region such that its origin is at the tri-valent corner and the k direction is
@@ -1053,31 +1057,31 @@
   HomCoord vseq2_minmax[2] = {HomCoord(0,0,0), HomCoord(int2-1,int3-1,0)};
 
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
 
     // create three vertex sequences
-  MBEntitySequence *dum_seq;
+  EntitySequence *dum_seq;
   vseq[0] = vseq[1] = vseq[2] = NULL;
 
     // first vertex sequence 
   MBErrorCode result = seq_mgr->create_scd_sequence(vseq0_minmax[0], vseq0_minmax[1],
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      vstart[0], dum_seq);
-  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[0] != 0 && dum_seq != NULL && vseq[0] != NULL);
 
     // second vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq1_minmax[0], vseq1_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[1], dum_seq);
-  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[1] != 0 && dum_seq != NULL && vseq[1] != NULL);
 
     // third vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq2_minmax[0], vseq2_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[2], dum_seq);
-  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[2] != 0 && dum_seq != NULL && vseq[2] != NULL);
 
 
@@ -1094,13 +1098,13 @@
   
     // create the first element sequence
   result = seq_mgr->create_scd_sequence(eseq0_minmax[0], eseq0_minmax[1], 
-                                        MBQUAD, 1,
+                                        MBQUAD, 1, 0,
                                         estart[0], dum_seq);
-  if (NULL != dum_seq) eseq[0] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[0] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[0] != 0 && dum_seq != NULL && eseq[0] != NULL);
   
     // only need to add one vseq to this, unity transform
-  result = eseq[0]->add_vsequence(vseq[0],
+  result = eseq[0]->sdata()->add_vsequence(vseq[0],
                                     // trick: if I know it's going to be unity, just input
                                     // 3 sets of equivalent points
                                   vseq0_minmax[0], vseq0_minmax[0],
@@ -1114,13 +1118,13 @@
   
     // create the second element sequence
   result = seq_mgr->create_scd_sequence(eseq1_minmax[0], eseq1_minmax[1],
-                                        MBQUAD, 1,
+                                        MBQUAD, 1, 0,
                                         estart[1], dum_seq);
-  if (NULL != dum_seq) eseq[1] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[1] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[1] != 0 && dum_seq != NULL && eseq[1] != NULL);
   
     // add shared side from first vseq to this eseq, with bb to get just the line
-  result = eseq[1]->add_vsequence(vseq[0],
+  result = eseq[1]->sdata()->add_vsequence(vseq[0],
                                     // p1: origin in both systems
                                   vseq0_minmax[0], eseq0_minmax[0],
                                     // p2: one unit along the shared line (i in one, j in other)
@@ -1137,7 +1141,7 @@
   }
   
     // add second vseq to this eseq, with different orientation but all of it (no bb input)
-  result = eseq[1]->add_vsequence(vseq[1],
+  result = eseq[1]->sdata()->add_vsequence(vseq[1],
                                     // p1: origin/i+1 (vseq/eseq)
                                   vseq1_minmax[0], eseq1_minmax[0]+HomCoord::unitv[0],
                                     // p2: j+1 from p1
@@ -1153,13 +1157,13 @@
   
     // create the third element sequence
   result = seq_mgr->create_scd_sequence(eseq2_minmax[0], eseq2_minmax[1],
-                                        MBQUAD, 1,
+                                        MBQUAD, 1, 0,
                                         estart[2], dum_seq);
-  if (NULL != dum_seq) eseq[2] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[2] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[2] != 0 && dum_seq != NULL && eseq[2] != NULL);
   
     // add shared side from second vseq to this eseq
-  result = eseq[2]->add_vsequence(vseq[1],
+  result = eseq[2]->sdata()->add_vsequence(vseq[1],
                                     // p1: origin/j+1 (vseq/eseq)
                                   vseq1_minmax[0], eseq[2]->min_params()+HomCoord::unitv[1],
                                     // p2: i+1/j+2 (vseq/eseq)
@@ -1177,7 +1181,7 @@
   }
   
     // add shared side from first vseq to this eseq
-  result = eseq[2]->add_vsequence(vseq[0],
+  result = eseq[2]->sdata()->add_vsequence(vseq[0],
                                     // p1: origin/origin
                                   vseq1_minmax[0], eseq2_minmax[0],
                                     // p2: j+1/i+1
@@ -1195,7 +1199,7 @@
   }
 
     // add third vseq to this eseq
-  result = eseq[2]->add_vsequence(vseq[2],
+  result = eseq[2]->sdata()->add_vsequence(vseq[2],
                                     // p1: origin/i+1,j+1
                                   vseq2_minmax[0], eseq[2]->min_params()+HomCoord::unitv[0]+HomCoord::unitv[1],
                                     // p2: i+1 from p1
@@ -1212,8 +1216,8 @@
 
 int create_3dtri_3_sequences(MBCore *gMB,
                              const int int1, const int int2, const int int3, const int int4,
-                             ScdVertexSeq **vseq, MBEntityHandle *vstart,
-                             ScdElementSeq **eseq, MBEntityHandle *estart) 
+                             ScdVertexData **vseq, MBEntityHandle *vstart,
+                             StructuredElementSeq **eseq, MBEntityHandle *estart) 
 {
     // create 3 brick esequences arranged such that the all share a common (tri-valent) edge;
     // orient each region similarly to the 2dtri_3_esequences test problem, swept into 3d in the 
@@ -1233,31 +1237,31 @@
   HomCoord vseq2_minmax[2] = {HomCoord(0,0,0), HomCoord(int2-1,int3-1,int4)};
 
     // get the seq manager from gMB
-  EntitySequenceManager *seq_mgr = gMB->sequence_manager();
+  SequenceManager *seq_mgr = gMB->sequence_manager();
 
     // create three vertex sequences
-  MBEntitySequence *dum_seq;
+  EntitySequence *dum_seq;
   vseq[0] = vseq[1] = vseq[2] = NULL;
 
     // first vertex sequence 
   MBErrorCode result = seq_mgr->create_scd_sequence(vseq0_minmax[0], vseq0_minmax[1],
-                                                     MBVERTEX, 1,
+                                                     MBVERTEX, 1, 0,
                                                      vstart[0], dum_seq);
-  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[0] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[0] != 0 && dum_seq != NULL && vseq[0] != NULL);
 
     // second vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq1_minmax[0], vseq1_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[1], dum_seq);
-  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[1] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[1] != 0 && dum_seq != NULL && vseq[1] != NULL);
 
     // third vertex sequence 
   result = seq_mgr->create_scd_sequence(vseq2_minmax[0], vseq2_minmax[1],
-                                        MBVERTEX, 1,
+                                        MBVERTEX, 1, 0,
                                         vstart[2], dum_seq);
-  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexSeq*>(dum_seq);
+  if (NULL != dum_seq) vseq[2] = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart[2] != 0 && dum_seq != NULL && vseq[2] != NULL);
 
 
@@ -1274,13 +1278,13 @@
   
     // create the first element sequence
   result = seq_mgr->create_scd_sequence(eseq0_minmax[0], eseq0_minmax[1], 
-                                        MBHEX, 1,
+                                        MBHEX, 1, 0,
                                         estart[0], dum_seq);
-  if (NULL != dum_seq) eseq[0] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[0] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[0] != 0 && dum_seq != NULL && eseq[0] != NULL);
   
     // only need to add one vseq to this, unity transform
-  result = eseq[0]->add_vsequence(vseq[0],
+  result = eseq[0]->sdata()->add_vsequence(vseq[0],
                                     // trick: if I know it's going to be unity, just input
                                     // 3 sets of equivalent points
                                   vseq0_minmax[0], vseq0_minmax[0],
@@ -1294,13 +1298,13 @@
   
     // create the second element sequence
   result = seq_mgr->create_scd_sequence(eseq1_minmax[0], eseq1_minmax[1],
-                                        MBHEX, 1,
+                                        MBHEX, 1, 0,
                                         estart[1], dum_seq);
-  if (NULL != dum_seq) eseq[1] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[1] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[1] != 0 && dum_seq != NULL && eseq[1] != NULL);
   
     // add shared side from first vseq to this eseq, with bb to get just the face
-  result = eseq[1]->add_vsequence(vseq[0],
+  result = eseq[1]->sdata()->add_vsequence(vseq[0],
                                     // p1: origin in both systems
                                   vseq0_minmax[0], eseq0_minmax[0],
                                     // p2: one unit along the shared line (i in one, j in other)
@@ -1318,7 +1322,7 @@
   }
   
     // add second vseq to this eseq, with different orientation but all of it (no bb input)
-  result = eseq[1]->add_vsequence(vseq[1],
+  result = eseq[1]->sdata()->add_vsequence(vseq[1],
                                     // p1: origin/i+1 (vseq/eseq)
                                   vseq1_minmax[0], eseq1_minmax[0]+HomCoord::unitv[0],
                                     // p2: j+1 from p1
@@ -1334,13 +1338,13 @@
   
     // create the third element sequence
   result = seq_mgr->create_scd_sequence(eseq2_minmax[0], eseq2_minmax[1],
-                                        MBHEX, 1,
+                                        MBHEX, 1, 0,
                                         estart[2], dum_seq);
-  if (NULL != dum_seq) eseq[2] = dynamic_cast<ScdElementSeq*>(dum_seq);
+  if (NULL != dum_seq) eseq[2] = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart[2] != 0 && dum_seq != NULL && eseq[2] != NULL);
   
     // add shared side from second vseq to this eseq
-  result = eseq[2]->add_vsequence(vseq[1],
+  result = eseq[2]->sdata()->add_vsequence(vseq[1],
                                     // p1: origin/j+1 (vseq/eseq)
                                   vseq1_minmax[0], eseq[2]->min_params()+HomCoord::unitv[1],
                                     // p2: i+1/j+2 (vseq/eseq)
@@ -1359,7 +1363,7 @@
   }
   
     // add shared side from first vseq to this eseq
-  result = eseq[2]->add_vsequence(vseq[0],
+  result = eseq[2]->sdata()->add_vsequence(vseq[0],
                                     // p1: origin/origin
                                   vseq1_minmax[0], eseq2_minmax[0],
                                     // p2: j+1/i+1
@@ -1378,7 +1382,7 @@
   }
 
     // add third vseq to this eseq
-  result = eseq[2]->add_vsequence(vseq[2],
+  result = eseq[2]->sdata()->add_vsequence(vseq[2],
                                     // p1: origin/i+1,j+1
                                   vseq2_minmax[0], eseq[2]->min_params()+HomCoord::unitv[0]+HomCoord::unitv[1],
                                     // p2: i+1 from p1

Modified: MOAB/trunk/test/perf/perf.cpp
===================================================================
--- MOAB/trunk/test/perf/perf.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/test/perf/perf.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -27,15 +27,18 @@
 #endif
 #endif
 
+#define IS_BUILDING_MB
+
 #include <stdlib.h>
 #include <stdio.h>
+#include <assert.h>
 #include <iostream>
 #include "MBCore.hpp"
 #include "MBReadUtilIface.hpp"
-#include "ScdVertexSeq.hpp"
-#include "ScdElementSeq.hpp"
+#include "VertexSequence.hpp"
+#include "StructuredElementSeq.hpp"
 #include "EntitySequence.hpp"
-#include "EntitySequenceManager.hpp"
+#include "SequenceManager.hpp"
 #include "HomXform.hpp"
 
 double LENGTH = 1.0;
@@ -394,27 +397,27 @@
   print_time(false, ttime0, utime, stime);
 
     // make a 3d block of vertices
-  MBEntitySequence *dum_seq = NULL;
-  ScdVertexSeq *vseq = NULL;
-  ScdElementSeq *eseq = NULL;
-  EntitySequenceManager *seq_mgr = dynamic_cast<MBCore*>(gMB)->sequence_manager();
+  EntitySequence *dum_seq = NULL;
+  ScdVertexData *vseq = NULL;
+  StructuredElementSeq *eseq = NULL;
+  SequenceManager *seq_mgr = dynamic_cast<MBCore*>(gMB)->sequence_manager();
   HomCoord vseq_minmax[2] = {HomCoord(0,0,0), 
                              HomCoord(nelem, nelem, nelem)};
   MBEntityHandle vstart, estart;
   
   MBErrorCode result = seq_mgr->create_scd_sequence(vseq_minmax[0], vseq_minmax[1],
-                                                    MBVERTEX, 1, vstart, dum_seq);
-  if (NULL != dum_seq) vseq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+                                                    MBVERTEX, 1, -1, vstart, dum_seq);
+  if (NULL != dum_seq) vseq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart != 0 && dum_seq != NULL && vseq != NULL);
     // now the element sequence
   result = seq_mgr->create_scd_sequence(vseq_minmax[0], vseq_minmax[1], 
-                                        MBHEX, 1, estart, dum_seq);
-  if (NULL != dum_seq) eseq = dynamic_cast<ScdElementSeq*>(dum_seq);
+                                        MBHEX, 1, -1, estart, dum_seq);
+  if (NULL != dum_seq) eseq = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart != 0 && dum_seq != NULL && eseq != NULL);
   
     // only need to add one vseq to this, unity transform
     // trick: if I know it's going to be unity, just input 3 sets of equivalent points
-  result = eseq->add_vsequence(vseq, vseq_minmax[0], vseq_minmax[0], vseq_minmax[0], 
+  result = eseq->sdata()->add_vsequence(vseq, vseq_minmax[0], vseq_minmax[0], vseq_minmax[0], 
                                vseq_minmax[0], vseq_minmax[0], vseq_minmax[0]);
   assert(MB_SUCCESS == result);
 
@@ -465,7 +468,7 @@
   // create a sequence to hold the node coordinates
   // get the current number of entities and start at the next slot
   std::vector<double*> coord_arrays;
-  MBErrorCode result = readMeshIface->get_node_arrays(3, num_verts, 1, vstart, coord_arrays);
+  MBErrorCode result = readMeshIface->get_node_arrays(3, num_verts, 1, -1, vstart, coord_arrays);
   assert(MB_SUCCESS == result && 1 == vstart &&
          coord_arrays[0] && coord_arrays[1] && coord_arrays[2]);
     // memcpy the coordinate data into place
@@ -474,7 +477,7 @@
   memcpy(coord_arrays[2], &coords[2*num_verts], sizeof(double)*num_verts);
   
   MBEntityHandle *conn = 0;
-  result = readMeshIface->get_element_array(num_elems, 8, MBHEX, 1, estart, conn);
+  result = readMeshIface->get_element_array(num_elems, 8, MBHEX, 1, -1, estart, conn);
   assert(MB_SUCCESS == result);
   memcpy(conn, connect, num_elems*8*sizeof(MBEntityHandle));
   result = readMeshIface->update_adjacencies(estart, num_elems, 8, conn);

Modified: MOAB/trunk/tools/mbperf/mbperf.cpp
===================================================================
--- MOAB/trunk/tools/mbperf/mbperf.cpp	2007-11-09 22:17:23 UTC (rev 1373)
+++ MOAB/trunk/tools/mbperf/mbperf.cpp	2007-11-09 22:28:33 UTC (rev 1374)
@@ -35,12 +35,14 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <assert.h>
 #include "MBCore.hpp"
 #include "MBReadUtilIface.hpp"
-#include "ScdVertexSeq.hpp"
-#include "ScdElementSeq.hpp"
+#include "ScdVertexData.hpp"
+#include "VertexSequence.hpp"
+#include "StructuredElementSeq.hpp"
 #include "EntitySequence.hpp"
-#include "EntitySequenceManager.hpp"
+#include "SequenceManager.hpp"
 #include "HomXform.hpp"
 
 double LENGTH = 1.0;
@@ -450,27 +452,28 @@
             << std::endl;
 
     // make a 3d block of vertices
-  MBEntitySequence *dum_seq = NULL;
-  ScdVertexSeq *vseq = NULL;
-  ScdElementSeq *eseq = NULL;
-  EntitySequenceManager *seq_mgr = dynamic_cast<MBCore*>(gMB)->sequence_manager();
+  EntitySequence *dum_seq = NULL;
+  ScdVertexData *vseq = NULL;
+  StructuredElementSeq *eseq = NULL;
+  SequenceManager *seq_mgr = dynamic_cast<MBCore*>(gMB)->sequence_manager();
   HomCoord vseq_minmax[2] = {HomCoord(0,0,0), 
                              HomCoord(nelem, nelem, nelem)};
   MBEntityHandle vstart, estart;
   
   MBErrorCode result = seq_mgr->create_scd_sequence(vseq_minmax[0], vseq_minmax[1],
-                                                    MBVERTEX, 1, vstart, dum_seq);
-  if (NULL != dum_seq) vseq = dynamic_cast<ScdVertexSeq*>(dum_seq);
+                                                    MBVERTEX, 1, 0, vstart, dum_seq);
+  if (NULL != dum_seq) vseq = dynamic_cast<ScdVertexData*>(dum_seq->data());
   assert (MB_FAILURE != result && vstart != 0 && dum_seq != NULL && vseq != NULL);
     // now the element sequence
   result = seq_mgr->create_scd_sequence(vseq_minmax[0], vseq_minmax[1], 
-                                        MBHEX, 1, estart, dum_seq);
-  if (NULL != dum_seq) eseq = dynamic_cast<ScdElementSeq*>(dum_seq);
+                                        MBHEX, 1, 0, estart, dum_seq);
+  if (NULL != dum_seq) eseq = dynamic_cast<StructuredElementSeq*>(dum_seq);
   assert (MB_FAILURE != result && estart != 0 && dum_seq != NULL && eseq != NULL);
   
     // only need to add one vseq to this, unity transform
     // trick: if I know it's going to be unity, just input 3 sets of equivalent points
-  result = eseq->add_vsequence(vseq, vseq_minmax[0], vseq_minmax[0], vseq_minmax[0], 
+  result = dynamic_cast<ScdElementData*>(eseq->data())
+         ->add_vsequence(vseq, vseq_minmax[0], vseq_minmax[0], vseq_minmax[0], 
                                vseq_minmax[0], vseq_minmax[0], vseq_minmax[0]);
   assert(MB_SUCCESS == result);
 




More information about the moab-dev mailing list