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@135 | 23 | from micropython.data import * |
paul@63 | 24 | |
paul@6 | 25 | class List: |
paul@6 | 26 | |
paul@6 | 27 | """ |
paul@6 | 28 | A displaced list containing attribute details and an index mapping names to |
paul@63 | 29 | offsets in the list. This is the optimised form of the table initially |
paul@63 | 30 | constructed to record object attributes. |
paul@6 | 31 | """ |
paul@6 | 32 | |
paul@7 | 33 | def __init__(self, names): |
paul@6 | 34 | self.names = names |
paul@7 | 35 | self.displaced = [] |
paul@7 | 36 | self.offsets = {} |
paul@166 | 37 | self.offsets_used = {} |
paul@7 | 38 | |
paul@6 | 39 | self.names_index = {} |
paul@6 | 40 | for i, name in enumerate(self.names): |
paul@6 | 41 | self.names_index[name] = i |
paul@6 | 42 | |
paul@7 | 43 | def __len__(self): |
paul@7 | 44 | return len(self.displaced) |
paul@7 | 45 | |
paul@119 | 46 | def __getitem__(self, i): |
paul@119 | 47 | return self.displaced[i] |
paul@119 | 48 | |
paul@137 | 49 | def get_code(self, name): |
paul@137 | 50 | |
paul@137 | 51 | "Return the code/offset of the given 'name'." |
paul@137 | 52 | |
paul@137 | 53 | return self.offsets.get(name) |
paul@137 | 54 | |
paul@166 | 55 | def get_name(self, code): |
paul@166 | 56 | |
paul@166 | 57 | "Return the name for the given 'code' or offset." |
paul@166 | 58 | |
paul@166 | 59 | return self.offsets_used[code] |
paul@166 | 60 | |
paul@7 | 61 | # Simulation methods. |
paul@7 | 62 | |
paul@6 | 63 | def access(self, objname, attrname): |
paul@6 | 64 | obj_offset = self.offsets.get(objname) |
paul@6 | 65 | attr_index = self.names_index.get(attrname) |
paul@6 | 66 | if obj_offset is None or attr_index is None: |
paul@6 | 67 | return None |
paul@6 | 68 | |
paul@6 | 69 | element = self.displaced[obj_offset + attr_index] |
paul@6 | 70 | if element is not None: |
paul@135 | 71 | index, details = element |
paul@135 | 72 | if index == attr_index: |
paul@6 | 73 | return details |
paul@6 | 74 | |
paul@6 | 75 | return None |
paul@6 | 76 | |
paul@7 | 77 | def dir(self, objname): |
paul@7 | 78 | attributes = {} |
paul@7 | 79 | for name in self.names: |
paul@7 | 80 | attr = self.access(objname, name) |
paul@7 | 81 | if attr is not None: |
paul@7 | 82 | attributes[name] = attr |
paul@7 | 83 | return attributes |
paul@7 | 84 | |
paul@7 | 85 | # Update methods. |
paul@7 | 86 | |
paul@7 | 87 | def add(self, objname, attributes): |
paul@7 | 88 | |
paul@7 | 89 | "Add an entry for 'objname' with the given 'attributes'." |
paul@7 | 90 | |
paul@7 | 91 | len_displaced = len(self.displaced) |
paul@7 | 92 | len_attributes = len(attributes) |
paul@7 | 93 | |
paul@7 | 94 | # Try to fit the row into the list, starting from the beginning and |
paul@7 | 95 | # skipping places where an existing row has been fitted. |
paul@7 | 96 | |
paul@7 | 97 | for offset in xrange(0, len_displaced): |
paul@166 | 98 | if self.offsets_used.has_key(offset): |
paul@7 | 99 | continue |
paul@7 | 100 | if self._fit_row(offset, attributes, len_displaced): |
paul@166 | 101 | self.offsets_used[offset] = objname |
paul@7 | 102 | self.offsets[objname] = offset |
paul@7 | 103 | self._add_row(offset, attributes, len_displaced) |
paul@7 | 104 | break |
paul@7 | 105 | |
paul@7 | 106 | # Where a row could not be fitted within the list, add it to the |
paul@7 | 107 | # end. |
paul@7 | 108 | |
paul@7 | 109 | else: |
paul@166 | 110 | self.offsets_used[len_displaced] = objname |
paul@7 | 111 | self.offsets[objname] = len_displaced |
paul@7 | 112 | self._add_row(len_displaced, attributes, len_displaced) |
paul@7 | 113 | |
paul@7 | 114 | def _fit_row(self, offset, attributes, len_displaced): |
paul@7 | 115 | |
paul@7 | 116 | """ |
paul@7 | 117 | Fit, at the given 'offset', the row of 'attributes' in the displaced |
paul@7 | 118 | list having length 'len_displaced'. |
paul@7 | 119 | Return a true value if this succeeded. |
paul@7 | 120 | """ |
paul@7 | 121 | |
paul@7 | 122 | for i, attr in enumerate(attributes): |
paul@7 | 123 | if i + offset >= len_displaced: |
paul@7 | 124 | break |
paul@7 | 125 | element = self.displaced[i + offset] |
paul@7 | 126 | if attr is not None and element is not None: |
paul@7 | 127 | return 0 |
paul@7 | 128 | return 1 |
paul@7 | 129 | |
paul@7 | 130 | def _add_row(self, offset, attributes, len_displaced): |
paul@7 | 131 | |
paul@7 | 132 | """ |
paul@7 | 133 | Add, at the given 'offset', the row of 'attributes' in the displaced |
paul@7 | 134 | list having length 'len_displaced'. |
paul@7 | 135 | """ |
paul@7 | 136 | |
paul@7 | 137 | # Extend the list if necessary. |
paul@7 | 138 | |
paul@7 | 139 | for i in xrange(0, offset + len(attributes) - len_displaced): |
paul@7 | 140 | self.displaced.append(None) |
paul@7 | 141 | |
paul@135 | 142 | # Record the attribute details in the list. |
paul@7 | 143 | |
paul@7 | 144 | for i, attr in enumerate(attributes): |
paul@7 | 145 | if attr is not None: |
paul@135 | 146 | |
paul@135 | 147 | # Each entry is of the form (attribute number, attribute). |
paul@135 | 148 | |
paul@135 | 149 | self.displaced[offset+i] = i, attr |
paul@7 | 150 | |
paul@126 | 151 | # Image production. |
paul@126 | 152 | |
paul@126 | 153 | def as_raw(self): |
paul@126 | 154 | |
paul@126 | 155 | "Return the raw contents of the table as a list of values." |
paul@126 | 156 | |
paul@126 | 157 | result = [] |
paul@126 | 158 | for entry in self.displaced: |
paul@126 | 159 | if entry is None: |
paul@126 | 160 | result.append(None) |
paul@126 | 161 | else: |
paul@132 | 162 | result.append(self.entry_as_raw(entry)) |
paul@126 | 163 | |
paul@126 | 164 | return result |
paul@126 | 165 | |
paul@132 | 166 | class ObjectList(List): |
paul@132 | 167 | |
paul@132 | 168 | "An object list." |
paul@132 | 169 | |
paul@132 | 170 | def entry_as_raw(self, entry): |
paul@135 | 171 | attr_index, attr = entry |
paul@135 | 172 | |
paul@135 | 173 | # Support descendants. |
paul@135 | 174 | |
paul@135 | 175 | if isinstance(attr, Class): |
paul@194 | 176 | return (attr_index, None, None) |
paul@132 | 177 | |
paul@132 | 178 | if attr.parent is not None: |
paul@132 | 179 | location = attr.parent.location or 0 |
paul@132 | 180 | else: |
paul@132 | 181 | location = 0 |
paul@132 | 182 | if attr.position is not None: |
paul@132 | 183 | position = attr.position + location + 1 # skip structure header |
paul@132 | 184 | else: |
paul@132 | 185 | position = None # NOTE: Should fix unpositioned attributes. |
paul@132 | 186 | |
paul@194 | 187 | # Attribute index/code, attribute type, location/position. |
paul@132 | 188 | |
paul@242 | 189 | return (attr_index, attr.is_static_attribute(), position) |
paul@132 | 190 | |
paul@132 | 191 | class ParameterList(List): |
paul@132 | 192 | |
paul@132 | 193 | "A parameter list." |
paul@132 | 194 | |
paul@132 | 195 | def entry_as_raw(self, entry): |
paul@190 | 196 | # param_index, position = entry |
paul@132 | 197 | return entry |
paul@132 | 198 | |
paul@5 | 199 | class Table: |
paul@5 | 200 | |
paul@5 | 201 | "A lookup table." |
paul@5 | 202 | |
paul@63 | 203 | TableError = TableError |
paul@132 | 204 | list_class = None # overridden |
paul@63 | 205 | |
paul@5 | 206 | def __init__(self): |
paul@5 | 207 | self.attributes = set() |
paul@5 | 208 | self.table = {} |
paul@22 | 209 | self.objnames = [] |
paul@5 | 210 | self.names = [] |
paul@7 | 211 | self.displaced_list = None |
paul@138 | 212 | self.raw = None |
paul@5 | 213 | |
paul@268 | 214 | def access(self, objname, attrname): |
paul@268 | 215 | |
paul@268 | 216 | "Return the attribute entry for the given 'objname' and 'attrname'." |
paul@268 | 217 | |
paul@268 | 218 | try: |
paul@268 | 219 | entry = self.table[objname] |
paul@268 | 220 | except KeyError: |
paul@268 | 221 | raise TableError, "Name %r is not registered as an object in the table." % objname |
paul@268 | 222 | |
paul@268 | 223 | try: |
paul@268 | 224 | return entry[attrname] |
paul@268 | 225 | except KeyError: |
paul@268 | 226 | raise TableError, "Name %r is not registered as an attribute in the table." % attrname |
paul@268 | 227 | |
paul@6 | 228 | def add(self, objname, attributes): |
paul@6 | 229 | |
paul@6 | 230 | "For the given 'objname' add the given 'attributes' to the table." |
paul@6 | 231 | |
paul@6 | 232 | self.table[objname] = attributes |
paul@5 | 233 | for name, origin in attributes.items(): |
paul@5 | 234 | self.attributes.add(name) |
paul@5 | 235 | |
paul@7 | 236 | if self.displaced_list is not None: |
paul@7 | 237 | self.displaced_list.add(objname, self.matrix_row(attributes)) |
paul@7 | 238 | |
paul@22 | 239 | def object_names(self): |
paul@22 | 240 | |
paul@22 | 241 | "Return the object names used in the table." |
paul@22 | 242 | |
paul@22 | 243 | self.objnames = self.objnames or list(self.table.keys()) |
paul@22 | 244 | return self.objnames |
paul@22 | 245 | |
paul@5 | 246 | def attribute_names(self): |
paul@6 | 247 | |
paul@6 | 248 | "Return the attribute names used in the table." |
paul@6 | 249 | |
paul@5 | 250 | self.names = self.names or list(self.attributes) |
paul@5 | 251 | return self.names |
paul@5 | 252 | |
paul@21 | 253 | def get_index(self, name): |
paul@21 | 254 | |
paul@21 | 255 | "Return the index of the given 'name'." |
paul@21 | 256 | |
paul@63 | 257 | try: |
paul@63 | 258 | return self.attribute_names().index(name) |
paul@63 | 259 | except ValueError: |
paul@63 | 260 | raise TableError, "Name %r is not registered as an attribute in the table." % name |
paul@21 | 261 | |
paul@5 | 262 | def as_matrix(self): |
paul@6 | 263 | |
paul@6 | 264 | """ |
paul@6 | 265 | Return the table as a matrix mapping object names to lists of attributes |
paul@6 | 266 | arranged in the order of the recorded attribute names. |
paul@6 | 267 | """ |
paul@6 | 268 | |
paul@5 | 269 | matrix = {} |
paul@6 | 270 | for objname, attributes in self.table.items(): |
paul@7 | 271 | matrix[objname] = self.matrix_row(attributes) |
paul@5 | 272 | return matrix |
paul@5 | 273 | |
paul@7 | 274 | def matrix_row(self, attributes): |
paul@7 | 275 | |
paul@7 | 276 | "Return a matrix row for the given 'attributes'." |
paul@7 | 277 | |
paul@7 | 278 | row = [] |
paul@7 | 279 | for name in self.attribute_names(): |
paul@7 | 280 | row.append(attributes.get(name)) |
paul@7 | 281 | return row |
paul@7 | 282 | |
paul@111 | 283 | def all_attribute_positions(self, name): |
paul@111 | 284 | |
paul@111 | 285 | """ |
paul@111 | 286 | Return a list of positions for the attribute with the given 'name' from |
paul@111 | 287 | all known objects. |
paul@111 | 288 | """ |
paul@111 | 289 | |
paul@111 | 290 | all = set() |
paul@111 | 291 | for attributes in self.table.values(): |
paul@111 | 292 | if attributes.has_key(name): |
paul@111 | 293 | all.add(attributes[name]) |
paul@111 | 294 | return all |
paul@111 | 295 | |
paul@111 | 296 | def all_possible_objects(self, names): |
paul@111 | 297 | |
paul@111 | 298 | """ |
paul@111 | 299 | Return a list of object names supporting the given attribute 'names'. |
paul@111 | 300 | """ |
paul@111 | 301 | |
paul@111 | 302 | possible = [] |
paul@111 | 303 | for objname, attributes in self.table.items(): |
paul@111 | 304 | found = 1 |
paul@111 | 305 | for name in names: |
paul@111 | 306 | if not attributes.has_key(name): |
paul@111 | 307 | found = 0 |
paul@111 | 308 | break |
paul@111 | 309 | if found: |
paul@111 | 310 | possible.append(objname) |
paul@111 | 311 | return possible |
paul@111 | 312 | |
paul@6 | 313 | def as_list(self): |
paul@6 | 314 | |
paul@6 | 315 | """ |
paul@6 | 316 | Return a displaced list object encoding the table in a more compact form |
paul@7 | 317 | than that provided by the matrix representation. This list will be |
paul@7 | 318 | updated with new entries if added to this object using the 'add' method. |
paul@6 | 319 | """ |
paul@6 | 320 | |
paul@7 | 321 | if self.displaced_list is None: |
paul@132 | 322 | self.displaced_list = self.list_class(self.attribute_names()) |
paul@6 | 323 | |
paul@7 | 324 | # Visit each row of the matrix. |
paul@6 | 325 | |
paul@135 | 326 | matrix_by_usage = [] |
paul@135 | 327 | size = len(self.attributes) |
paul@135 | 328 | |
paul@135 | 329 | # Add rows in descending order of utilisation. |
paul@135 | 330 | |
paul@7 | 331 | for objname, attributes in self.as_matrix().items(): |
paul@135 | 332 | matrix_by_usage.append((size - attributes.count(None), objname, attributes)) |
paul@135 | 333 | |
paul@135 | 334 | matrix_by_usage.sort() |
paul@135 | 335 | matrix_by_usage.reverse() |
paul@135 | 336 | |
paul@135 | 337 | for usage, objname, attributes in matrix_by_usage: |
paul@7 | 338 | self.displaced_list.add(objname, attributes) |
paul@6 | 339 | |
paul@7 | 340 | return self.displaced_list |
paul@6 | 341 | |
paul@137 | 342 | def as_raw(self): |
paul@137 | 343 | |
paul@137 | 344 | "Return the raw contents of the table as a list of values." |
paul@137 | 345 | |
paul@138 | 346 | if self.raw is None: |
paul@138 | 347 | self.raw = self.as_list().as_raw() |
paul@138 | 348 | return self.raw |
paul@137 | 349 | |
paul@132 | 350 | class ObjectTable(Table): |
paul@132 | 351 | |
paul@132 | 352 | "An object table." |
paul@132 | 353 | |
paul@132 | 354 | list_class = ObjectList |
paul@132 | 355 | |
paul@132 | 356 | class ParameterTable(Table): |
paul@132 | 357 | |
paul@132 | 358 | "A parameter table." |
paul@132 | 359 | |
paul@132 | 360 | list_class = ParameterList |
paul@132 | 361 | |
paul@5 | 362 | # vim: tabstop=4 expandtab shiftwidth=4 |