• 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. Alternative way of getting the column in a list

Stats

  • Locked Locked
  • Replies 16
  • Subscribers 144
  • Views 19936
  • 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

Alternative way of getting the column in a list

essej
essej over 11 years ago
I have a function that gets the columns in an assoc list. Its working fine except for the very large size of list around 10 million. I gets a huge memory allocated when I profiled it.

procedure(getColSub(col lList)
  if(onep(length(col)) then
    case(car(col)
      (0 mapcar('car lList))
      (1 mapcar('cadr lList))
      (2 mapcar('caddr lList))
      (3 mapcar('cadddr lList))
      (t mapcar('nth vectorToList(makeVector(length(lList) car(col))) lList))
    )
  else
    mapcar('nth col vectorToList(makeVector(length(col) lList)))
  )
)

Example:
lList=list(list("a" 1) list("b" 2))
col=list(1 0) , gives ((1 "a")(2 "b"))
col=list(1) , gives (1 2)
  • Cancel
  • tweeks
    tweeks over 11 years ago

    I'm confused about what exactly you're trying to do here.  An assoc list is usually a list of pairs, so you can ask for the keys (the first element), or the values (the second element), but asking for the 3rd, 4th, nth element doesn't make sense, because there are only two (right?).

     It looks like what you really want to do is get the nth elements in each sublist in a list of lists, which is very easy to do:

    > lists = '(("a" 1) ("b" 2))
    (("a" 1) 
        ("b" 2)
    )
    > column = 0
    0
    > mapcar(lambda((list) nth(column list)) lists)
    ("a" "b")
    > column = 1
    1
    > mapcar(lambda((list) nth(column list)) lists)
    (1 2)
    > lists = '(("a" 1) ("b" 2) ("c" 3 "see" 'more (columns)))
    (("a" 1) 
        ("b" 2) 
        ("c" 3 "see" 
    	(quote more) 
    	(columns)
        )
    )
    > column = 2
    2
    > mapcar(lambda((list) nth(column list)) lists)
    (nil nil "see")
    > 
     
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 11 years ago

    If you want to pass a list of columns to project out of a list of n-tuples, that's also very easy:

    > lists = '((1 2 3 4 5) 
              (a b c d e)
              (hearts diamonds clubs spades)
              (monday tuesday wednesday thursday friday))
    
    ((1 2 3 4 5) 
        (a b c d e) 
        (hearts diamonds clubs spades) 
        (monday tuesday wednesday thursday friday)
    )
    > columns = '(1 3 2)
    (1 3 2)
    > foreach(mapcar column columns
        foreach(mapcar list lists
            nthelem(column list)
        )
    )
    ((1 a hearts monday) 
        (3 c clubs wednesday) 
        (2 b diamonds tuesday)
    )
    
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • essej
    essej over 11 years ago

    Sorry for making you confused. My existing function is applicable to an assoc list or for more than 2 elements in a list.

    I have a case where I have more than 2 elements in a list, which I think is valid reason for me to have that. Your solution and my function I think gives the same result.

    The problem is the very big memory or time allocated when running it for a million size list. Both my function & your suggestion I think would face the same problem.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • essej
    essej over 11 years ago

    Thanks for your suggestion 'tweeks' (dont know your real name).

    Again the problem is how to make it faster and less memory in a bigger size of list.

    mapcar alone takes a lot of memory and time

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 11 years ago

    essej said:

    My existing function is applicable to an assoc list or for more than 2 elements in a list.

    OK, then you want this:

    procedure(getColumns(columns lists "ll")
        foreach(mapcar column columns
            foreach(mapcar list lists
                nthelem(column list)
            )
        )
    )
    

    This is the most efficient way to make a list of the nth elements of a list of lists (without destroying the originals).

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 11 years ago

    essej said:

    Again the problem is how to make it faster and less memory in a bigger size of list.

    mapcar alone takes a lot of memory and time

    Your version wastes considerable memory and time converting back and forth between vectors in the case where "col" is not a 1-element list.

    If you know the max number of columns in advance, just use vectors instead of lists.  You can get the nth element of a vector in constant time (while it's linear in the length of a list), and the memory usage should be smaller.

    If your data is sparse, use a table instead of a list or vector.

    What are you trying to do, anyway?  What do you have ten million of?

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • essej
    essej over 11 years ago

    I tested it and its wrong

    a=list(list("a" 1) list("b" 2))

    getColumns(list(1 0) a) => (("a" "b") (nil nil))

    Output should be ((1 "a")(2 "b)).

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 11 years ago

    I used nthelem instead of nth, which is 1-based rather than 0-based, so you would write:

    getColumns(list(2 1) a)
    

    which returns

    ((1 2) ("a" "b"))
    

    If you want zero-based, replace nthelem with nth in the definition.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 11 years ago

    essej said:

    Output should be ((1 "a")(2 "b)).

    Ok, so you're not slicing across lists, you're re-arranging elements within a list.

    That code is also simple:

     

    > procedure(getColumns(columns lists "ll")
        foreach(mapcar list lists
            foreach(mapcan column columns
                list(nth(column list))
            )
        )
    )
    getColumns
    > a = '(("a" 1) ("b" 2))
    (("a" 1) 
        ("b" 2)
    )
    > getColumns('(1 0) a)
    ((1 "a") 
        (2 "b")
    )
    > 
    
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • essej
    essej over 11 years ago

    I always got warning that I am using bigger swap memory when running my script. I profiled it and gives me huge size on a list.

    I'm trying to improve every list manipulation so I can solve this issue.

    Snapshot

    Function Name                        Total   Inside
    -------------                        -----   ------
    TOTAL (Memory Allocated)           555464224  555464224 bytes
    toplevel                           555464224      912
    sgFldMacroEditGenerateMacro_CB     555437784        0
    sgAMacSkillCode                    418113912       72
    sgAMacExportToSkill                417927416       24
    sgAMacGetVar                       405785688     3000
    sgPMPropInfoRead                   367713440        0
    sgPMPropInfoList                   365481504        0
    list                               248371656  248371656
    sgPMPropRead                       228395024        0
    sgCompileAMacCode                  136996552        0
    load                               126692360     5616
    getSGq                             24713440  24713440
    parseString                        21260016  21260016
    sgSortPcellVarList                 12103016      136
    mapcan                             11564608        0
    sgPMCellSave                       11258952        0
    /* funobj@0x26d07868 */            11200992        0
    sgGetVar                           11200992        0
    sgPMCVSave                         10514840        0
    sgRunSkLint                        10241584        0
    tableToList                        9979200  9979200
    sklint                             9547536        0
    sklint1                            9540048     1872
    apply                              9540048        0
    /* funobj@0x1b250e30 */            8791984        0
    skLintFile                         8791984        0
    skCheckFile                        8747264     3120
    sgPMPropValueRead                  8658168        0
    sgPMObjectList                     8401528        0
    cons                               6906504  6906504
    vectorToList                       5282160  5282160
    dbSave                             5038352  1053696
    /* funobj@0x1b24b6b0 */            4957296        0
    lineread                           4957296        0
    skReadLine                         4957296        0
    linereadstring                     4808544        0

    • 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