Author Topic: LispFunction SubList  (Read 6688 times)

0 Members and 2 Guests are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: LispFunction SubList
« Reply #15 on: October 05, 2010, 11:13:34 AM »
Good idea; i just created a generic List class in C++. Very good practice for me. Thanx guys.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8718
  • AKA Daniel
Re: LispFunction SubList
« Reply #16 on: October 05, 2010, 01:42:22 PM »
here is the resbuf version   :-o

Code: [Select]
tatic resbuf* cloneNode( const resbuf* pRb )
  {
    if (pRb == NULL)
      return NULL;

    struct resbuf* pRbTmp = NULL;

    if(pRb->restype > 5000)
    {
      if(pRb->restype == RTSTR)
      {
        pRbTmp = acutNewRb(pRb->restype);
        if(pRbTmp)
          pRbTmp->resval.rstring = _tcsdup(pRb->resval.rstring);
        return pRbTmp;
      }
      else
      {
        pRbTmp = acutNewRb(pRb->restype);
        if(pRbTmp)
          memcpy(&pRbTmp->resval,&pRb->resval, sizeof(pRbTmp->resval));
        return pRbTmp;
      }
    }
    return pRbTmp;
  }



  //(SubList '(0 1 2 3 4 5) 2 3)
  static int ads_sublist(void)
  {
    struct resbuf *pRbOutHead = NULL;
    struct resbuf *pRbOutTail = NULL;
    struct resbuf *pRbInHead = acedGetArgs();

    INT_PTR rtlb = 0;
    INT_PTR rtle = 0;
    INT_PTR startPos = 0;
    INT_PTR length = 0;
    INT_PTR listLength = 0;
    INT_PTR inIdx = 0;
    INT_PTR outIdx = 0;

    for(const struct resbuf *piter = pRbInHead; piter!=NULL; piter=piter->rbnext)
    {
      if(piter->restype==RTLB)
      {
        rtlb = inIdx;
      }
      else if (piter->restype==RTLE)
      {
        rtle = inIdx;
        if(piter->rbnext)
        {
          if(piter->rbnext->restype == RTSHORT ||
             piter->rbnext->restype == RTLONG)
          {
            startPos = piter->rbnext->resval.rlong;
          }
        }
        if(piter->rbnext->rbnext)
        {
          if(piter->rbnext->rbnext->restype == RTSHORT ||
             piter->rbnext->rbnext->restype == RTLONG)
          {
            length = piter->rbnext->rbnext->resval.rlong;
          }
        }
      }
      inIdx++;
    }

    if(inIdx<4)
      return (RSERR);

    if(length == 0 || length > (rtle-1 - startPos) || length > (rtle-1+rtlb+1))
      length = rtle-1 - startPos;

    for(const struct resbuf *piter = pRbInHead; piter!=NULL; piter=piter->rbnext)
    {
      if(outIdx == startPos+1)
        pRbOutTail= pRbOutHead = cloneNode(piter);
      if(outIdx > startPos+1 && length + startPos+1 > outIdx && outIdx < rtle)
        pRbOutTail = pRbOutTail->rbnext = cloneNode(piter);
      outIdx++;
    }
    acedRetList(pRbOutHead);
    acutRelRb(pRbOutHead);
    return (RSRSLT) ;
  }

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8718
  • AKA Daniel
Re: LispFunction SubList
« Reply #17 on: October 06, 2010, 03:55:40 AM »
This one uses the same memory received from lisp

Code: [Select]
//(SubList '(0 1 2 3 4 5) 2 3)
  static int ads_sublist(void)
  {
    struct resbuf *pRbTmp = NULL;
    struct resbuf *pRbOutHead = NULL;
    struct resbuf *pRbOutTail = NULL;
    struct resbuf *pRbInHead = acedGetArgs();

    INT_PTR rtlb = 0;
    INT_PTR rtle = 0;
    INT_PTR startPos = 0;
    INT_PTR length = 0;
    INT_PTR listLength = 0;
    INT_PTR inIdx = 0;
    INT_PTR outIdx = 0;

    for(const struct resbuf *piter = pRbInHead; piter!=NULL; piter=piter->rbnext)
    {
      if(piter->restype==RTLB)
      {
        rtlb = inIdx;
      }
      else if (piter->restype==RTLE)
      {
        rtle = inIdx;
        if(piter->rbnext)
        {
          if(piter->rbnext->restype == RTSHORT ||
             piter->rbnext->restype == RTLONG)
          {
            startPos = piter->rbnext->resval.rlong;
          }
        }
        if(piter->rbnext->rbnext)
        {
          if(piter->rbnext->rbnext->restype == RTSHORT ||
             piter->rbnext->rbnext->restype == RTLONG)
          {
            length = piter->rbnext->rbnext->resval.rlong;
          }
        }
      }
      inIdx++;
    }

    if(inIdx<4)
      return (RSERR);

    if(length == 0 || length > (rtle-1 - startPos) || length > (rtle-1+rtlb+1))
      length = rtle-1 - startPos;

    for(struct resbuf *piter = pRbInHead; piter!=NULL; piter=piter->rbnext)
    {
      if(outIdx == startPos+1)
        pRbOutTail= pRbOutHead = piter;
      if(outIdx > startPos+1 && length + startPos+1 > outIdx && outIdx < rtle)
        pRbOutTail = pRbOutTail->rbnext = piter;
      outIdx++;
    }

    if(pRbOutTail->rbnext)
    {
      pRbTmp = pRbOutTail->rbnext;
      pRbOutTail->rbnext = NULL;
    }
    acedRetList(pRbOutHead);
    pRbOutTail->rbnext =pRbTmp;
    return (RSRSLT) ;
  }