Home > Community > Forums > Custom IC SKILL > How do you take the cdr of a tconc list?

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 How do you take the cdr of a tconc list? 

Last post Fri, Jan 9 2009 10:04 AM by dmay. 2 replies.
Started by dmay 08 Jan 2009 02:49 PM. Topic has 2 replies and 1675 views
Page 1 of 1 (3 items)
Sort Posts:
  • Thu, Jan 8 2009 2:49 PM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 393
    • Points 7,415
    How do you take the cdr of a tconc list? Reply

    I am trying to do some efficient list processing and would like to use a tconc list. I need to build my list in the order that I find objects, so I cannot use the cons command. This list will grow quite large and as it is growing, I have a while loop processing the data on the front of the list. In the middle of the while loop, many items can be added to the end of the list. I can use the tconc command to efficiently add items to the end of the tconc list, but each time through the while loop, I want to remove the first item from the list with cdr. I don't know how to do this efficiently with a tconc list.

    There is a nice description of how tconc works on Sourcelink: http://sourcelink.cadence.com/docs/db/kdb/2002/Apr/11001694.html

    However, as you can see, the tconc structure is a list that contains your list and a pointer to the last item in your list. If you want to create a tconc list by other means, you would need to do it this way (note: lconc is the same as tconc, except lconc appends a list to the end of your list and tconc appends one item to the end of your list):

    a=list(1 2 3 4)
    a=cons(a last(a)) => ((1 2 3 4) 4)
    OR this way:
    a=list(1 2 3 4)
    a=lconc(nil a) => ((1 2 3 4) 4)

    First, I could take the cdr of the car of my tconc list, then I could use one of the above approaches to rebuild my tconc list. However, I think it would be expensive since last must traverse to the end of the list. Likewise, I assume that lconc has the same overhead.

    Anyone have any ideas?

    Derek

    • Post Points: 20
  • Fri, Jan 9 2009 9:50 AM

    • Tongju
    • Top 200 Contributor
    • Joined on Mon, Jul 14 2008
    • San Jose, CA
    • Posts 38
    • Points 505
    Re: How do you take the cdr of a tconc list? Reply

    Hi, Derek,

    If you don't worry about the memory usage (grow of tconc list) at all, then, I though I could suggest a work-around for the cdr operation on the first list of a tconc list.

    Suppose there is a tconc list, TCONC. We can use car(TCONC) to get the pointer to the first item of TCONC, which is the list that you like to do cdr on.  This new sub list WILL BE updated when you make any addition to the original TCONC if you use tconc function. So, you can do "cdr" on the sub list when you use tconc to operate the original TCONC.

     For example:

    i TCONC = nil

    \t nil

    \i TCONC = tconc(TCONC 1)

    \t ((1) 1)

    \i subList = (car TCONC)

    \t (1)

    \i TCONC = tconc(TCONC 2)

    \t ((1 2) 2)

    \i TCONC = tconc(TCONC 3)

    \t ((1 2 3) 3)

    \i TCONC = tconc(TCONC 4)

    \t ((1 2 3 4) 4)

    \i TCONC = tconc(TCONC "end")

    \t ((1 2 3 4 "end") "end")

    \i subList

    \t (1 2 3 4 "end")

    \i subList = (cdr subList)

    \t (2 3 4 "end")

    \i subList = (cdr subList)

    \t (3 4 "end")

    \i length(subList)

    \t 3

    \i TCONC = tconc(TCONC "extra")

    \t ((1 2 3 4 "end" "extra") "extra")

    \i subList

    \t (3 4 "end" "extra")

    ------------------------------------------------------------------------

    So, the cdr on the first element (a list by itself) of the tconc list is working fine.

    However, if you worry about the the memory usage, then, this trick will not be a successful work-around since front list elements will not be released for collection/reuse (TCONC is still lock them)

     Thanks,

     Tongju

    • Post Points: 20
  • Fri, Jan 9 2009 10:04 AM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 393
    • Points 7,415
    Re: How do you take the cdr of a tconc list? Reply

    Tongju,

    I like your thinking! I tried that in my simple testcase and was able to run in 0.12 seconds instead of 22 seconds. I don't think the memory is an issue since the tconc list will be freed when the routine finishes. I'll try it in my original code.

     Thanks!
    Derek

    • Post Points: 5
Page 1 of 1 (3 items)
Sort Posts:
Started by dmay at 08 Jan 2009 02:49 PM. Topic has 2 replies.