Back to index

salome-gui  6.5.0
Classes | Functions
SUIT_TreeSync.h File Reference
#include <QList>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  DiffItem< SrcItem, TrgItem >
 The structure representing difference between source and destination items. More...

Functions

template<class SrcItem , class TrgItem , class TreeData >
TrgItem synchronize (const SrcItem &r1, const TrgItem &r2, const TreeData &td)
 Synchronize two data trees by recurive comparing of the corresponding items.
template<class SrcItem , class TrgItem , class TreeData >
QList< DiffItem< SrcItem,
TrgItem > > 
diffSiblings (const SrcItem &src, const TrgItem &trg, const TreeData &td)
 Compare children of the source and target trees to find differences.
template<class SrcItem , class TrgItem , class TreeData >
TrgItem createSubTree (const SrcItem &src, const TrgItem &parent, const TrgItem &after, const TreeData &td)
 Create an item with all its children recursively in the target tree.
template<class SrcItem , class TrgItem , class TreeData >
const QList< TrgItem >
::const_iterator 
findEqual (const SrcItem &it, const typename QList< TrgItem >::const_iterator &first, const typename QList< TrgItem >::const_iterator &last, const TreeData &td)
 Find the item in the target tree which correspond to the specified source tree item.

Class Documentation

struct DiffItem

template<class SrcItem, class TrgItem>
struct DiffItem< SrcItem, TrgItem >

The structure representing difference between source and destination items.

The different combinations of source and target items values imply the different actions to be performed in the target data tree:

  • source item is null, target item is not null : the item should be removed from the target tree
  • source item is not null, target item is null : new item should be added to the target tree
  • both source and target items are not null : the target item can be updated if necessary
  • both source and target items are null : error

Definition at line 42 of file SUIT_TreeSync.h.

Class Members
SrcItem mySrc source tree item
TrgItem myTrg target tree item

Function Documentation

template<class SrcItem , class TrgItem , class TreeData >
TrgItem createSubTree ( const SrcItem &  src,
const TrgItem &  parent,
const TrgItem &  after,
const TreeData &  td 
)

Create an item with all its children recursively in the target tree.

Parameters:
srcsource tree item
parentparent item in the target tree
afteritem in the target tree after which new item shoud be inserted
tdfunctor class
Returns:
created item
See also:
synchronize()

Definition at line 264 of file SUIT_TreeSync.h.

{
  if ( src == td.nullSrc() )
    return td.nullTrg();

  TrgItem nitem = td.createItem( src, parent, after );
  if ( nitem == td.nullTrg() )
    return nitem;

  QList<SrcItem> ch = td.children( src );
  typename QList<SrcItem>::const_iterator anIt = ch.begin(), aLast = ch.end();
  TrgItem last = td.nullTrg();
  for( ; anIt != aLast; anIt++ )
    last = createSubTree( *anIt, nitem, last, td );

  return nitem;
}

Here is the caller graph for this function:

template<class SrcItem , class TrgItem , class TreeData >
QList< DiffItem< SrcItem, TrgItem > > diffSiblings ( const SrcItem &  src,
const TrgItem &  trg,
const TreeData &  td 
)

Compare children of the source and target trees to find differences.

Parameters:
srcparent source item
trgparent target item
tdfunctor class
Returns:
list of the differences
See also:
synchronize()

Definition at line 202 of file SUIT_TreeSync.h.

{
  //if( src==td.nullSrc() || trg==td.nullTrg() )
  //  return;
  
  QList< DiffItem<SrcItem,TrgItem> > d;
   
  QList<SrcItem> src_ch = td.children( src );
  QList<TrgItem> trg_ch = td.children( trg );

  typename QList<SrcItem>::const_iterator src_it = src_ch.begin(), src_last = src_ch.end();
  typename QList<TrgItem>::const_iterator cur    = trg_ch.begin(), trg_last = trg_ch.end();

  for ( ; src_it != src_last; src_it++ ) {
    typename QList<TrgItem>::const_iterator f =
      findEqual<SrcItem, TrgItem, TreeData>( *src_it, cur, trg_last, td );
    if ( f != trg_last )  { 
      // target is found
      // mark all items before found one as "to be deleted"
      for ( typename QList<TrgItem>::const_iterator it = cur; it != f; it++ ) {
        DiffItem<SrcItem,TrgItem> ndiff;
        ndiff.mySrc = td.nullSrc();
        ndiff.myTrg = *it;        // delete item
        d.append( ndiff );
      }
      cur = f;
      DiffItem<SrcItem,TrgItem> ndiff;
      ndiff.mySrc = *src_it;
      ndiff.myTrg = *cur;         // update this (found) item
      d.append( ndiff );
      cur++;
    }
    else {
      // target is not found
      DiffItem<SrcItem,TrgItem> ndiff;
      ndiff.mySrc = *src_it;
      ndiff.myTrg = td.nullTrg(); // add item
      d.append( ndiff );
    }
  }
  // delete rest items
  for ( ; cur != trg_last; cur++ ) {
    DiffItem<SrcItem,TrgItem> ndiff;
    ndiff.mySrc = td.nullSrc();
    ndiff.myTrg = *cur;           // delete item
    d.append( ndiff );
  }
  
  return d;
}

Here is the caller graph for this function:

template<class SrcItem , class TrgItem , class TreeData >
const QList< TrgItem >::const_iterator findEqual ( const SrcItem &  it,
const typename QList< TrgItem >::const_iterator &  first,
const typename QList< TrgItem >::const_iterator &  last,
const TreeData &  td 
)

Find the item in the target tree which correspond to the specified source tree item.

Parameters:
itsource item for which correspondence is to be found
firstiterator pointing to the item in the list l from which search shoud be started
lastiterator pointing to the item in the list l the search to be finished at
tdfunctor class
Returns:
iterator pointing to the item in the list if the correspondence is found or iterator last if the correspondence is not found
See also:
synchronize()

Definition at line 180 of file SUIT_TreeSync.h.

{
  typename QList<TrgItem>::const_iterator cur = first;
  for ( ; cur != last; cur++ ) {
    if ( td.isEqual( it, *cur ) )
      return cur;
  }
  return last;
}
template<class SrcItem , class TrgItem , class TreeData >
TrgItem synchronize ( const SrcItem &  r1,
const TrgItem &  r2,
const TreeData &  td 
)

Synchronize two data trees by recurive comparing of the corresponding items.

Parameters:
r1starting item from the source data tree
r2starting item from the target data tree
tdfunctor class
Returns:
the target tree item (updated or just created) corresponding to the starting data object

Actual comparing of the items and the syncronization of the trees is performed by the functor class which is passed as the last parameter of the function. The functor class should implement the following methods:

  • bool isEqual( const SrcItem& src, const TrgItem& tgt ) const;
    • src source tree item
    • tgt target tree item
    • compares items and returns true if the items are equal (correspond to each other)
  • SrcItem nullSrc() const;
    • returns null source tree itemm
  • TrgItem nullTrg() const
    • returns null target tree item
  • TrgItem createItem( const SrcItem& src, const TrgItem& parent, const TrgItem& after ) const;
    • src source item
    • parent parent target item
    • after target tree item after which new item shoud be inserted (if null, the item is added to the end)
    • creates new ite in the target tree which correspond to the source item and returns created item
  • void updateItem( const SrcItem& src, const TrgItem& tgt ) const;
    • src source tree item
    • tgt the item in the target tree to be updated
    • updates target treeitem
  • void deleteItemWithChildren( const TrgItem& tgt ) const;
    • tgt the item in the target tree to be removed
    • deletes target tree item (recursively)
  • QList<SrcItem> children( const SrcItem& parent ) const;
    • parent the parent item in the source tree
    • returns the child items list
  • QList<TrgItem> children( const TrgItem& parent ) const;
    • parent the parent item in the target tree
    • returns the child items list
  • TrgItem parent( const TrgItem& tgt ) const;
    • tgt target tree item
    • returns the item which is parent for the specified source tree item

Definition at line 118 of file SUIT_TreeSync.h.

{
  if ( td.isEqual( r1, r2 ) ) {
    // update items themselves
    td.updateItem( r1, r2 );
    
    // iterate through children
    QList< DiffItem< SrcItem, TrgItem > > d =  diffSiblings( r1, r2, td );
    
    typename QList< DiffItem< SrcItem, TrgItem > >::const_iterator anIt = d.begin(), aLast = d.end();
    TrgItem lastItem = td.nullTrg();

    for ( ; anIt != aLast; anIt++ ) {
      const DiffItem<SrcItem,TrgItem>& item = *anIt;
      if ( item.mySrc == td.nullSrc() ) {
        if ( item.myTrg == td.nullTrg() )
          qDebug( "error: both null" );
        else
          // delete item
          td.deleteItemWithChildren( item.myTrg );
      }
      else {
        if ( item.myTrg == td.nullTrg() ) {
          // add item (recursively)
          TrgItem nitem = createSubTree( item.mySrc, r2, lastItem, td );
          if ( nitem != td.nullTrg() )
            lastItem = nitem;
        }
        else {
          // update item
          synchronize( item.mySrc, item.myTrg, td );
          lastItem = item.myTrg;
        }
      }
    }
    return r2;
  }
  else {
    TrgItem new_r2 = td.nullTrg();
    if ( r1 != td.nullSrc() ) {
      // add new item (recursively)
      new_r2 = createSubTree( r1, td.parent( r2 ), r2, td );
    }
    if ( r2 != td.nullTrg() ) {
      // delete old one (if it is not null)
      td.deleteItemWithChildren( r2 );
    }
    return new_r2;
  }
}

Here is the call graph for this function: