SKILL Q: How To Sort List of Windows Based on a Property?

Discussion in 'Cadence' started by Edward, Aug 16, 2007.

  1. Edward

    Edward Guest

    Running the following code inside a function gets me screwy results.
    Some windows are dropped out of the list while others stay and no
    sorting occurs. I can't seem to run this code from the CIW, because
    I get the following error:

    *Error* get/getq: first arg must be either symbol, list, defstruct or
    user type - 4

    (setq thiswinList '(window:3 window:4 window:5 window:6 window:7))

    (sort thiswinList
    (lambda (x y)
    (alphalessp
    x~>cellView~>viewName
    y~>cellView~>viewName)))

    The results are even worse when I attempt to reverse the sort by
    transposing the arguments for alphalessp. Then the function returns a
    list with only one element, the first element.

    It would be nice to know why this is happening. I'm assuming it has
    to do with the recursive nature of the operation. But more to the
    point, what code can accomplish what I want to do? I'm trying to
    take a list of windows and sort based on the viewName such that all
    schematic views are at the beginning of the list and all layout views
    are at the end. Hopefully the answer would be something easily
    generalizable for any list of window-IDs or dbObjs on any property.

    Thanks in advance,

    Edward
     
    Edward, Aug 16, 2007
    #1
  2. Edward

    Trevor Bowen Guest

    I would try something more like:

    thiswinList = hiGetWindowList()

    It is very dangerous, if not impossible, to use the database or
    environment id's directly. If you know you want window:3 specifically,
    for example, use:

    window(3)

    The first function will return the list of all open, valid windows,
    which is probably what you really want. If you want a more select list,
    then perform a setof() and reduce the list before sorting.

    HTH,

    Trevor
     
    Trevor Bowen, Aug 21, 2007
    #2
  3. This is because sort() is a destructive function (see the documentation). This
    means it modifies the list in place for efficiency reasons.

    As a result, you should _always_ use the return value of sort. In other words,
    if you did:

    (setq thiswinList (sort thiswinList (lambda ....)))

    you'll be OK. So why is this? Well, the sort function can modify any (or all) of
    the pointers in the list cells making up the list - but it doesn't touch the
    values themselves. It can't alter what the variable "thiswinList" is pointing to
    - so that will always point to whatever was the original first entry in the list
    - and what follows will be whatever follows it after the sort. You'll be missing
    anything that precedes the original first value in the list.

    This is correct behaviour - you're just not using the sort() function properly.

    Regards,

    Andrew.
     
    Andrew Beckett, Sep 16, 2007
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.