paul@5 | 1 | #!/usr/bin/env python |
paul@5 | 2 | |
paul@5 | 3 | """ |
paul@5 | 4 | Preparation of run-time attribute lookup tables. |
paul@5 | 5 | """ |
paul@5 | 6 | |
paul@5 | 7 | try: |
paul@5 | 8 | set |
paul@5 | 9 | except NameError: |
paul@5 | 10 | from sets import Set as set |
paul@5 | 11 | |
paul@6 | 12 | class List: |
paul@6 | 13 | |
paul@6 | 14 | """ |
paul@6 | 15 | A displaced list containing attribute details and an index mapping names to |
paul@6 | 16 | offsets in the list. |
paul@6 | 17 | """ |
paul@6 | 18 | |
paul@7 | 19 | def __init__(self, names): |
paul@6 | 20 | self.names = names |
paul@7 | 21 | self.displaced = [] |
paul@7 | 22 | self.offsets = {} |
paul@7 | 23 | self.offsets_used = set() |
paul@7 | 24 | |
paul@6 | 25 | self.names_index = {} |
paul@6 | 26 | for i, name in enumerate(self.names): |
paul@6 | 27 | self.names_index[name] = i |
paul@6 | 28 | |
paul@7 | 29 | def __len__(self): |
paul@7 | 30 | return len(self.displaced) |
paul@7 | 31 | |
paul@7 | 32 | # Simulation methods. |
paul@7 | 33 | |
paul@6 | 34 | def access(self, objname, attrname): |
paul@6 | 35 | obj_offset = self.offsets.get(objname) |
paul@6 | 36 | attr_index = self.names_index.get(attrname) |
paul@6 | 37 | if obj_offset is None or attr_index is None: |
paul@6 | 38 | return None |
paul@6 | 39 | |
paul@6 | 40 | element = self.displaced[obj_offset + attr_index] |
paul@6 | 41 | if element is not None: |
paul@6 | 42 | offset, details = element |
paul@6 | 43 | if offset == obj_offset: |
paul@6 | 44 | return details |
paul@6 | 45 | |
paul@6 | 46 | return None |
paul@6 | 47 | |
paul@7 | 48 | def dir(self, objname): |
paul@7 | 49 | attributes = {} |
paul@7 | 50 | for name in self.names: |
paul@7 | 51 | attr = self.access(objname, name) |
paul@7 | 52 | if attr is not None: |
paul@7 | 53 | attributes[name] = attr |
paul@7 | 54 | return attributes |
paul@7 | 55 | |
paul@7 | 56 | # Update methods. |
paul@7 | 57 | |
paul@7 | 58 | def add(self, objname, attributes): |
paul@7 | 59 | |
paul@7 | 60 | "Add an entry for 'objname' with the given 'attributes'." |
paul@7 | 61 | |
paul@7 | 62 | len_displaced = len(self.displaced) |
paul@7 | 63 | len_attributes = len(attributes) |
paul@7 | 64 | |
paul@7 | 65 | # Try to fit the row into the list, starting from the beginning and |
paul@7 | 66 | # skipping places where an existing row has been fitted. |
paul@7 | 67 | |
paul@7 | 68 | for offset in xrange(0, len_displaced): |
paul@7 | 69 | if offset in self.offsets_used: |
paul@7 | 70 | continue |
paul@7 | 71 | if self._fit_row(offset, attributes, len_displaced): |
paul@7 | 72 | self.offsets_used.add(offset) |
paul@7 | 73 | self.offsets[objname] = offset |
paul@7 | 74 | self._add_row(offset, attributes, len_displaced) |
paul@7 | 75 | break |
paul@7 | 76 | |
paul@7 | 77 | # Where a row could not be fitted within the list, add it to the |
paul@7 | 78 | # end. |
paul@7 | 79 | |
paul@7 | 80 | else: |
paul@7 | 81 | self.offsets_used.add(len_displaced) |
paul@7 | 82 | self.offsets[objname] = len_displaced |
paul@7 | 83 | self._add_row(len_displaced, attributes, len_displaced) |
paul@7 | 84 | |
paul@7 | 85 | def _fit_row(self, offset, attributes, len_displaced): |
paul@7 | 86 | |
paul@7 | 87 | """ |
paul@7 | 88 | Fit, at the given 'offset', the row of 'attributes' in the displaced |
paul@7 | 89 | list having length 'len_displaced'. |
paul@7 | 90 | Return a true value if this succeeded. |
paul@7 | 91 | """ |
paul@7 | 92 | |
paul@7 | 93 | for i, attr in enumerate(attributes): |
paul@7 | 94 | if i + offset >= len_displaced: |
paul@7 | 95 | break |
paul@7 | 96 | element = self.displaced[i + offset] |
paul@7 | 97 | if attr is not None and element is not None: |
paul@7 | 98 | return 0 |
paul@7 | 99 | return 1 |
paul@7 | 100 | |
paul@7 | 101 | def _add_row(self, offset, attributes, len_displaced): |
paul@7 | 102 | |
paul@7 | 103 | """ |
paul@7 | 104 | Add, at the given 'offset', the row of 'attributes' in the displaced |
paul@7 | 105 | list having length 'len_displaced'. |
paul@7 | 106 | """ |
paul@7 | 107 | |
paul@7 | 108 | # Extend the list if necessary. |
paul@7 | 109 | |
paul@7 | 110 | for i in xrange(0, offset + len(attributes) - len_displaced): |
paul@7 | 111 | self.displaced.append(None) |
paul@7 | 112 | |
paul@7 | 113 | # Record the offset and attribute details in the list. |
paul@7 | 114 | |
paul@7 | 115 | for i, attr in enumerate(attributes): |
paul@7 | 116 | if attr is not None: |
paul@7 | 117 | self.displaced[offset+i] = offset, attr |
paul@7 | 118 | |
paul@5 | 119 | class Table: |
paul@5 | 120 | |
paul@5 | 121 | "A lookup table." |
paul@5 | 122 | |
paul@5 | 123 | def __init__(self): |
paul@5 | 124 | self.attributes = set() |
paul@5 | 125 | self.table = {} |
paul@5 | 126 | self.names = [] |
paul@7 | 127 | self.displaced_list = None |
paul@5 | 128 | |
paul@6 | 129 | def add(self, objname, attributes): |
paul@6 | 130 | |
paul@6 | 131 | "For the given 'objname' add the given 'attributes' to the table." |
paul@6 | 132 | |
paul@6 | 133 | self.table[objname] = attributes |
paul@5 | 134 | for name, origin in attributes.items(): |
paul@5 | 135 | self.attributes.add(name) |
paul@5 | 136 | |
paul@7 | 137 | if self.displaced_list is not None: |
paul@7 | 138 | self.displaced_list.add(objname, self.matrix_row(attributes)) |
paul@7 | 139 | |
paul@5 | 140 | def attribute_names(self): |
paul@6 | 141 | |
paul@6 | 142 | "Return the attribute names used in the table." |
paul@6 | 143 | |
paul@5 | 144 | self.names = self.names or list(self.attributes) |
paul@5 | 145 | return self.names |
paul@5 | 146 | |
paul@5 | 147 | def as_matrix(self): |
paul@6 | 148 | |
paul@6 | 149 | """ |
paul@6 | 150 | Return the table as a matrix mapping object names to lists of attributes |
paul@6 | 151 | arranged in the order of the recorded attribute names. |
paul@6 | 152 | """ |
paul@6 | 153 | |
paul@5 | 154 | matrix = {} |
paul@6 | 155 | for objname, attributes in self.table.items(): |
paul@7 | 156 | matrix[objname] = self.matrix_row(attributes) |
paul@5 | 157 | return matrix |
paul@5 | 158 | |
paul@7 | 159 | def matrix_row(self, attributes): |
paul@7 | 160 | |
paul@7 | 161 | "Return a matrix row for the given 'attributes'." |
paul@7 | 162 | |
paul@7 | 163 | row = [] |
paul@7 | 164 | for name in self.attribute_names(): |
paul@7 | 165 | row.append(attributes.get(name)) |
paul@7 | 166 | return row |
paul@7 | 167 | |
paul@6 | 168 | def as_list(self): |
paul@6 | 169 | |
paul@6 | 170 | """ |
paul@6 | 171 | Return a displaced list object encoding the table in a more compact form |
paul@7 | 172 | than that provided by the matrix representation. This list will be |
paul@7 | 173 | updated with new entries if added to this object using the 'add' method. |
paul@6 | 174 | """ |
paul@6 | 175 | |
paul@7 | 176 | if self.displaced_list is None: |
paul@7 | 177 | self.displaced_list = List(self.attribute_names()) |
paul@6 | 178 | |
paul@7 | 179 | # Visit each row of the matrix. |
paul@6 | 180 | |
paul@7 | 181 | for objname, attributes in self.as_matrix().items(): |
paul@7 | 182 | self.displaced_list.add(objname, attributes) |
paul@6 | 183 | |
paul@7 | 184 | return self.displaced_list |
paul@6 | 185 | |
paul@5 | 186 | # vim: tabstop=4 expandtab shiftwidth=4 |