• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Merging Lists

Stats

  • Locked Locked
  • Replies 2
  • Subscribers 143
  • Views 15559
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Merging Lists

SBrickles
SBrickles over 16 years ago

I am writing some code which uses lists.
I need a way to be able merge two lists into one such that

list1:

( x "foo"
     (
        ( a "a" "func1()" )
        ( b "b" "func2()" )
        ( c "c" (
                    ( d "d" "func3()" )
                  )
     )
)

and list2: 

( x "foo"
     (
        ( e "e" "func4()" )
        ( c "c" (
                    ( f "f" "func5" )
                  )
     )
)

merge so that you end up with:

( x "foo"
     (
        ( a "a" "func1()" )
        ( b "b" "func2()" )
        ( c "c" (
                    ( d "d" "func3()" )
                    ( f "f" "func5" )
                  )
        ( e "e" "func4()" )
     )
)

The code needs to be able to merge at multiple levels depending on
whether the  'key' element (the car of the list at any level) is the same value or not.

If the item in the second list is of the form (a "a" "func()" ) then it should overwrite the
value when it gets merged with the first list.  So in the example above if the second list
had  ( a "a" "func8()" ) in it then it would have overwritten the ( a "a" "func1()" ) in the first list
- but only if it was at the same level of hierachy  as in the first list.

Any ideas ?

Stephen 

  • Cancel
  • Andrew Beckett
    Andrew Beckett over 16 years ago

    Stephen,

    Try the code below. In the comments at the top, I corrected your examples - because there were some missing close parentheses. Note that in order to make the top level look the same as the lists further down in the list hierarchy, I added a call to list() around each of list1 and list2 - that keeps them the same.

    If you use the destructive version (the one without the copy) then you can simply look at list1 once the function has finished, and it will be updated. If you use the version with the SBcopyDeep(), you can just take the car() of the result of SBmergeLists(). In fact you can always take the car() of the SBmergeLists.

    The code works by using a recursive function - I think it captures your requirements.

    /*
    list1=
    '( x "foo"
         (
            ( a "a" "func1()" )
            ( b "b" "func2()" )
            ( c "c" (
                        ( d "d" "func3()" )
                      )
               )
         )
    )
    
    list2=
    '( x "foo"
         (
            ( a "a" "func8()" )
            ( e "e" "func4()" )
            ( c "c" (
                        ( f "f" "func5" )
                      )
               )
         )
    )
    
    ; if you don't mind a destructive change
    SBmergeLists(list(list1) list(list2))
    ; if you want to protect against side effects caused by doing
    ; destructive change
    SBmergeLists(SBcopyDeep(list(list1)) list(list2))
    
    */
    
    procedure(SBcopyDeep(l)
      foreach(mapcar el l
        if(listp(el) SBcopyDeep(el) el)
      )
    )
    
    procedure(SBmergeLists(l1 l2)
      let((match)
        foreach(l2el l2
          match=assoc(car(l2el) l1)
          if(match then
    	if(listp(caddr(match)) then
    	  SBmergeLists(caddr(match) caddr(l2el))
            else
              rplaca(cddr(match) caddr(l2el))
            )
          else
            rplacd(last(l1) list(l2el))
          )
        )
        l1
      )
    )
    

    Best Regards,

    Andrew.

     

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • SBrickles
    SBrickles over 16 years ago

    Andrew - Many many thanks for your help and prompt response on this.
    It completely solved my problem !!

    I made the following modification to the SBmergeLists procedure to allow a list
    to be replaced by an item:

    procedure( SBmergeLists(l1 l2)
    let( ( match )
    foreach( l2el l2
    match = assoc( car( l2el ) l1 )
    if( match then
    if( listp( caddr( match ) ) then
              if( listp( nth( 2 l2el ) ) then
    SBmergeLists( caddr( match ) caddr( l2el ) )
    else
    rplaca( member( match l1 ) l2el )
    )
    else
    rplaca( cddr( match ) caddr( l2el ) )
    )
    else
    rplacd(last( l1 ) list( l2el ) )
    )
    )
    l1
    )
    )

    This allows the case below to work as well:

    list1=
    '( x "foo"
    (
    ( a "a" "func1()" )
    ( b "b" "func2()" )
    ( c "c" (
    ( d "d" "func3()" )
    )
    )
    )
    )

    list2=
    '( x "foo"
    (
    ( c "c" "func4()" )
    )
    ) 

    Resulting in:

    pp( list1 )
    ( x "foo"
    (
    ( a "a" "func1()" )
    ( b "b" "func2()" )
    ( c "c" "func4()" )
    )
    )

    The opposite case of turning an item into a list works fine as well.

    Again, many thanks,

    Stephen.

     
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information