Example #1
0
Link * Linked_List::Find(unsigned long ID)
{
  Link *lcur;
  Link *l;

  Link *tampoo;

  #ifdef DEBUGEMB
  lFindDebugEmbCount++;
  #endif

  // optimized by Emb 26 to 28/6/95 (sections optimize 1 and 2)
  // re-optimized by Emb 23/8/95    (other sections)

  // optimize 1: manage several calls on the same item
  if (lastfound)
    lcur = lastfound;
  else
    lcur = first;
  if (!lcur)
    return lcur;    // otherwise GPF on next line
  if (lcur->Item()==ID)
    return lcur;

  // optimize 1 bis 23/8/95 : manage "before the last" found
  if (beforelastfound)
    if (beforelastfound->Item()==ID) {
      // swap parties
      tampoo = beforelastfound;
      beforelastfound = lastfound;
      lastfound = tampoo;
      return lastfound;
    }

  // optimize 2: scan linked list from lastfound,
  // then only if not found, scan from lastfound until beginning
  if (lastfound)
    lcur = lastfound;
  else {
    lcur = first;
    if (lcur->Item()==ID) {
      beforelastfound = lastfound;
      lastfound = lcur;
      return lcur;
    }
  }

  // --- TRIAL : forward from beforelastfound
  // MASQUED - NOT SIGNIFICANT ENOUGH ( gain 11" on 1'31")
  // if (beforelastfound)
  //   lcur = beforelastfound;
  // else {
  //   lcur = first;
  //   if (lcur->Item()==ID) {
  //     beforelastfound = lastfound;
  //     lastfound = lcur;
  //     return lcur;
  //   }
  // }
  // --- TRIAL : forward from beforelastfound

  // forward, from lastfound
  l = lcur;
  #ifdef DEBUGEMB
  for (l=l->Next(); l; l=l->Next(), lNextDebugEmbCount++)
  #else
  for (l=l->Next(); l; l=l->Next())
  #endif
    if (l->Item()==ID) {
      beforelastfound = lastfound;
      lastfound = l;
      return l;
    }

  // backwards, from lastfound
  l = lcur;
  #ifdef DEBUGEMB
  for (l=l->Prev(); l; l=l->Prev(), lNextDebugEmbCount++)
  #else
  for (l=l->Prev(); l; l=l->Prev())
  #endif
    if (l->Item()==ID) {
      beforelastfound = lastfound;
      lastfound = l;
      return l;
    }
  return 0;

  // original code, masqued :
  //for (Link *l=first; l; l=l->Next())
  //  if (l->Item()==ID)
  //    return l;
  //return 0; 
}
Example #2
0
Link *  Linked_List::Add(unsigned long ID,unsigned long parentID,char *pStr, void * lpItemData,unsigned long uiInsertAfter,unsigned long uiInsertBefore)
{
  Link * insertAfter=last;
  Link * insertBefore=0;
  Link * parent=0;

  if (uiInsertAfter)
    {
    Link *l =Find(uiInsertAfter);
    if (l)
      insertAfter=l;
    }

  if (parentID)
    {
    if (lastparent && lastparent->Item()==parentID)
      {
      parent=insertAfter=lastparent;
      }
    else
      {
      // Emb 28/6/95 : performance improvement using Find()
      Link * l = Find(parentID);
      if (l)
        parent=lastparent=insertAfter=l;

      // original code, masqued :
      //for (Link *l=first; l && !parent;l=l->Next())
      //  {
      //  if (l->Item()==parentID)
      //    {
      //    parent=lastparent=insertAfter=l;
      //    }
      //  }
      }
    if (parent)
      {
      if (!parent->hasChild)
        {
        uiInsertAfter=parent->Item();
        parent->hasChild=1;
        }
      int   lev=parent->Level()+1;
      for (Link *l2=parent->Next();l2;l2=l2->Next())
        {
        if (l2->Parent()!=parent && l2->Level()<=lev)
          break;
        insertAfter=l2;
        if (uiInsertAfter && l2->Item()==uiInsertAfter)
          break;
        }
      }
    else
      {
      //for (LinkNonResolved *l =firstNonResolved;l && l->Next(); l= (LinkNonResolved *) l->Next());
      LinkNonResolved *ll =new LinkNonResolved(ID,firstNonResolved,pStr,parentID,lpItemData);
      if (ll && !firstNonResolved)
        firstNonResolved=ll;
      return 0;
      }
    }


  if (bSort && !uiInsertAfter && pStr)
    {
    for (Link *l = parent ? parent->Next():first ; l && !insertBefore; l=l->Next())
      {
      if ((l->Parent()==parent) && IsBefore(pStr,l->String()))
        {
        insertBefore=l;
        }
      }
    }

  if (uiInsertBefore)
    {
    Link *l =Find(uiInsertBefore);
    if (l && ((parent==l) || (parent==l->Parent())))
      {
      insertBefore=l;
      }
    }

  Link *l =new Link(ID,parent,insertAfter,insertBefore,pStr,lpItemData);
  if (l)
    {
    count++;
    current=l;
    if (!l->Prev())
      first=l;
    if (!l->Next())
      last=l;
    if (insertBefore && insertBefore==first)
      first=l;
    if (firstNonResolved)
      ResolveLink(ID);
    }
  return l;
}