paul@5 | 1 | #!/usr/bin/env python |
paul@5 | 2 | |
paul@5 | 3 | """ |
paul@5 | 4 | Preparation of run-time attribute lookup tables. |
paul@8 | 5 | |
paul@63 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@8 | 7 | |
paul@8 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@8 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@8 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@8 | 11 | version. |
paul@8 | 12 | |
paul@8 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@8 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@8 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@8 | 16 | details. |
paul@8 | 17 | |
paul@8 | 18 | You should have received a copy of the GNU General Public License along with |
paul@8 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@5 | 20 | """ |
paul@5 | 21 | |
paul@63 | 22 | from micropython.common import * |
paul@5 | 23 | try: |
paul@5 | 24 | set |
paul@5 | 25 | except NameError: |
paul@5 | 26 | from sets import Set as set |
paul@5 | 27 | |
paul@63 | 28 | class TableError(ProcessingError): pass |
paul@63 | 29 | |
paul@6 | 30 | class List: |
paul@6 | 31 | |
paul@6 | 32 | """ |
paul@6 | 33 | A displaced list containing attribute details and an index mapping names to |
paul@63 | 34 | offsets in the list. This is the optimised form of the table initially |
paul@63 | 35 | constructed to record object attributes. |
paul@6 | 36 | """ |
paul@6 | 37 | |
paul@7 | 38 | def __init__(self, names): |
paul@6 | 39 | self.names = names |
paul@7 | 40 | self.displaced = [] |
paul@7 | 41 | self.offsets = {} |
paul@7 | 42 | self.offsets_used = set() |
paul@7 | 43 | |
paul@6 | 44 | self.names_index = {} |
paul@6 | 45 | for i, name in enumerate(self.names): |
paul@6 | 46 | self.names_index[name] = i |
paul@6 | 47 | |
paul@7 | 48 | def __len__(self): |
paul@7 | 49 | return len(self.displaced) |
paul@7 | 50 | |
paul@7 | 51 | # Simulation methods. |
paul@7 | 52 | |
paul@6 | 53 | def access(self, objname, attrname): |
paul@6 | 54 | obj_offset = self.offsets.get(objname) |
paul@6 | 55 | attr_index = self.names_index.get(attrname) |
paul@6 | 56 | if obj_offset is None or attr_index is None: |
paul@6 | 57 | return None |
paul@6 | 58 | |
paul@6 | 59 | element = self.displaced[obj_offset + attr_index] |
paul@6 | 60 | if element is not None: |
paul@6 | 61 | offset, details = element |
paul@6 | 62 | if offset == obj_offset: |
paul@6 | 63 | return details |
paul@6 | 64 | |
paul@6 | 65 | return None |
paul@6 | 66 | |
paul@7 | 67 | def dir(self, objname): |
paul@7 | 68 | attributes = {} |
paul@7 | 69 | for name in self.names: |
paul@7 | 70 | attr = self.access(objname, name) |
paul@7 | 71 | if attr is not None: |
paul@7 | 72 | attributes[name] = attr |
paul@7 | 73 | return attributes |
paul@7 | 74 | |
paul@7 | 75 | # Update methods. |
paul@7 | 76 | |
paul@7 | 77 | def add(self, objname, attributes): |
paul@7 | 78 | |
paul@7 | 79 | "Add an entry for 'objname' with the given 'attributes'." |
paul@7 | 80 | |
paul@7 | 81 | len_displaced = len(self.displaced) |
paul@7 | 82 | len_attributes = len(attributes) |
paul@7 | 83 | |
paul@7 | 84 | # Try to fit the row into the list, starting from the beginning and |
paul@7 | 85 | # skipping places where an existing row has been fitted. |
paul@7 | 86 | |
paul@7 | 87 | for offset in xrange(0, len_displaced): |
paul@7 | 88 | if offset in self.offsets_used: |
paul@7 | 89 | continue |
paul@7 | 90 | if self._fit_row(offset, attributes, len_displaced): |
paul@7 | 91 | self.offsets_used.add(offset) |
paul@7 | 92 | self.offsets[objname] = offset |
paul@7 | 93 | self._add_row(offset, attributes, len_displaced) |
paul@7 | 94 | break |
paul@7 | 95 | |
paul@7 | 96 | # Where a row could not be fitted within the list, add it to the |
paul@7 | 97 | # end. |
paul@7 | 98 | |
paul@7 | 99 | else: |
paul@7 | 100 | self.offsets_used.add(len_displaced) |
paul@7 | 101 | self.offsets[objname] = len_displaced |
paul@7 | 102 | self._add_row(len_displaced, attributes, len_displaced) |
paul@7 | 103 | |
paul@7 | 104 | def _fit_row(self, offset, attributes, len_displaced): |
paul@7 | 105 | |
paul@7 | 106 | """ |
paul@7 | 107 | Fit, at the given 'offset', the row of 'attributes' in the displaced |
paul@7 | 108 | list having length 'len_displaced'. |
paul@7 | 109 | Return a true value if this succeeded. |
paul@7 | 110 | """ |
paul@7 | 111 | |
paul@7 | 112 | for i, attr in enumerate(attributes): |
paul@7 | 113 | if i + offset >= len_displaced: |
paul@7 | 114 | break |
paul@7 | 115 | element = self.displaced[i + offset] |
paul@7 | 116 | if attr is not None and element is not None: |
paul@7 | 117 | return 0 |
paul@7 | 118 | return 1 |
paul@7 | 119 | |
paul@7 | 120 | def _add_row(self, offset, attributes, len_displaced): |
paul@7 | 121 | |
paul@7 | 122 | """ |
paul@7 | 123 | Add, at the given 'offset', the row of 'attributes' in the displaced |
paul@7 | 124 | list having length 'len_displaced'. |
paul@7 | 125 | """ |
paul@7 | 126 | |
paul@7 | 127 | # Extend the list if necessary. |
paul@7 | 128 | |
paul@7 | 129 | for i in xrange(0, offset + len(attributes) - len_displaced): |
paul@7 | 130 | self.displaced.append(None) |
paul@7 | 131 | |
paul@7 | 132 | # Record the offset and attribute details in the list. |
paul@7 | 133 | |
paul@7 | 134 | for i, attr in enumerate(attributes): |
paul@7 | 135 | if attr is not None: |
paul@7 | 136 | self.displaced[offset+i] = offset, attr |
paul@7 | 137 | |
paul@5 | 138 | class Table: |
paul@5 | 139 | |
paul@5 | 140 | "A lookup table." |
paul@5 | 141 | |
paul@63 | 142 | TableError = TableError |
paul@63 | 143 | |
paul@5 | 144 | def __init__(self): |
paul@5 | 145 | self.attributes = set() |
paul@5 | 146 | self.table = {} |
paul@22 | 147 | self.objnames = [] |
paul@5 | 148 | self.names = [] |
paul@7 | 149 | self.displaced_list = None |
paul@5 | 150 | |
paul@6 | 151 | def add(self, objname, attributes): |
paul@6 | 152 | |
paul@6 | 153 | "For the given 'objname' add the given 'attributes' to the table." |
paul@6 | 154 | |
paul@6 | 155 | self.table[objname] = attributes |
paul@5 | 156 | for name, origin in attributes.items(): |
paul@5 | 157 | self.attributes.add(name) |
paul@5 | 158 | |
paul@7 | 159 | if self.displaced_list is not None: |
paul@7 | 160 | self.displaced_list.add(objname, self.matrix_row(attributes)) |
paul@7 | 161 | |
paul@22 | 162 | def object_names(self): |
paul@22 | 163 | |
paul@22 | 164 | "Return the object names used in the table." |
paul@22 | 165 | |
paul@22 | 166 | self.objnames = self.objnames or list(self.table.keys()) |
paul@22 | 167 | return self.objnames |
paul@22 | 168 | |
paul@5 | 169 | def attribute_names(self): |
paul@6 | 170 | |
paul@6 | 171 | "Return the attribute names used in the table." |
paul@6 | 172 | |
paul@5 | 173 | self.names = self.names or list(self.attributes) |
paul@5 | 174 | return self.names |
paul@5 | 175 | |
paul@22 | 176 | def get_code(self, name): |
paul@22 | 177 | |
paul@22 | 178 | "Return the code of the given 'name'." |
paul@22 | 179 | |
paul@63 | 180 | try: |
paul@63 | 181 | return self.object_names().index(name) |
paul@63 | 182 | except ValueError: |
paul@63 | 183 | raise TableError, "Name %r is not registered as an object in the table." % name |
paul@22 | 184 | |
paul@21 | 185 | def get_index(self, name): |
paul@21 | 186 | |
paul@21 | 187 | "Return the index of the given 'name'." |
paul@21 | 188 | |
paul@63 | 189 | try: |
paul@63 | 190 | return self.attribute_names().index(name) |
paul@63 | 191 | except ValueError: |
paul@63 | 192 | raise TableError, "Name %r is not registered as an attribute in the table." % name |
paul@21 | 193 | |
paul@5 | 194 | def as_matrix(self): |
paul@6 | 195 | |
paul@6 | 196 | """ |
paul@6 | 197 | Return the table as a matrix mapping object names to lists of attributes |
paul@6 | 198 | arranged in the order of the recorded attribute names. |
paul@6 | 199 | """ |
paul@6 | 200 | |
paul@5 | 201 | matrix = {} |
paul@6 | 202 | for objname, attributes in self.table.items(): |
paul@7 | 203 | matrix[objname] = self.matrix_row(attributes) |
paul@5 | 204 | return matrix |
paul@5 | 205 | |
paul@7 | 206 | def matrix_row(self, attributes): |
paul@7 | 207 | |
paul@7 | 208 | "Return a matrix row for the given 'attributes'." |
paul@7 | 209 | |
paul@7 | 210 | row = [] |
paul@7 | 211 | for name in self.attribute_names(): |
paul@7 | 212 | row.append(attributes.get(name)) |
paul@7 | 213 | return row |
paul@7 | 214 | |
paul@6 | 215 | def as_list(self): |
paul@6 | 216 | |
paul@6 | 217 | """ |
paul@6 | 218 | Return a displaced list object encoding the table in a more compact form |
paul@7 | 219 | than that provided by the matrix representation. This list will be |
paul@7 | 220 | updated with new entries if added to this object using the 'add' method. |
paul@6 | 221 | """ |
paul@6 | 222 | |
paul@7 | 223 | if self.displaced_list is None: |
paul@7 | 224 | self.displaced_list = List(self.attribute_names()) |
paul@6 | 225 | |
paul@7 | 226 | # Visit each row of the matrix. |
paul@6 | 227 | |
paul@7 | 228 | for objname, attributes in self.as_matrix().items(): |
paul@7 | 229 | self.displaced_list.add(objname, attributes) |
paul@6 | 230 | |
paul@7 | 231 | return self.displaced_list |
paul@6 | 232 | |
paul@5 | 233 | # vim: tabstop=4 expandtab shiftwidth=4 |