faqts : Computers : Programming : Languages : Python : Snippets : Lists

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 27 people (48%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

IDList

Jul 5th, 2000 10:02
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 248, dj trombley


"""
Packages: basic_datatypes.lists;new_datatypes
"""
"""
This new datatype is one I often find quite useful - a fast associative
similar to the base type that generates "Handles" for windows.  It is
simply a list which grows as needed, and whose element's IDs remain
constant until they are deleted, at which time they get reused.
It is more efficient than using a dictionary (i.e. faster).
-dj
"""
# ---------------------IDList.py------------------
# Lists with persistent unique IDs
# 1998 dj trombley <dtrom@building.clark.net>
# You are free to use and modify this code for
# any purpose with the restriction that any modifications
# including the author's name or contact information
# must include all of the original characters of the code.
# (you are free to comment out as you see fit, or to delete
# my contact information)
class IDList:
    def __init__(self):
        self.first_unused = 1
        self.data = [None]
        self.unused = []
    def add(self, o):
        # Don't reassign a None object.
        if (o == None):
            return(0)
        # If there is an empty space, use it.
        if (self.unused != []):
            id = self.unused[0]
            self.data[id] = o
            self.unused = self.unused[1:]
            return(id)
        # Otherwise, assign a new id.
        id = self.first_unused
        self.first_unused = self.first_unused + 1
        self.data = self.data + [o]
        return(id)
    def delete(self, idx):
        # Make sure this is a valid id.
        if (idx < 1):
            return(None)
        if (idx >= self.first_unused):
            return(None)
        if (self.data[idx] == None):
            return(None)
        # Delete the object from the list.
        obj = self.data[idx]
        self.data[idx] = None
        if (self.first_unused == idx + 1):
            while ((idx > 0) and (self.data[idx] == None)):
                self.first_unused = self.first_unused - 1
                self.data = self.data[:-1]
                idx = idx - 1
        else:
            self.unused = self.unused + [idx]
        return(obj)
# These should be obvious.
    def get(self, idx):
        if ((idx < 1) or (idx >= self.first_unused)):
            return(None)
        return(self.data[idx])
    def replace(self, idx, o):
        if ((idx < 1) or (idx >= self.first_unused)):
            return(None)
        if (o == None):
            return(self.delete(idx))
        obj = self.data[idx]
        self.data[idx] = o
        return(obj)
    def list(self):
        list = []
        idx = 0
        while (idx < self.first_unused):
            if (self.data[idx] != None):
                list = list + [self.data[idx]]
            idx = idx + 1
# Slicing interface
    def __getitem__(self, idx):
        return(self.data[idx])
    def __getslice__(self, a, b):
        return(self.data[a:b])
    def __setitem__(self, a = None, b = None):
        raise Exception("Cannot assign to a slice of an IDList")
    def __setslice__(self, a = None, b = None, c = None):
        raise Exception("Cannot assign to a slice of an IDList")