paul@92 | 1 | #!/usr/bin/env python |
paul@92 | 2 | |
paul@92 | 3 | """ |
paul@92 | 4 | Optimise object layouts. |
paul@92 | 5 | |
paul@92 | 6 | Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@92 | 7 | |
paul@92 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@92 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@92 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@92 | 11 | version. |
paul@92 | 12 | |
paul@92 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@92 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@92 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@92 | 16 | details. |
paul@92 | 17 | |
paul@92 | 18 | You should have received a copy of the GNU General Public License along with |
paul@92 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@92 | 20 | """ |
paul@92 | 21 | |
paul@92 | 22 | from common import add_counter_item, get_attrname_from_location, init_item, \ |
paul@92 | 23 | sorted_output |
paul@92 | 24 | from encoders import encode_access_location, get_kinds |
paul@92 | 25 | from os.path import exists, join |
paul@92 | 26 | from os import makedirs |
paul@92 | 27 | from referencing import Reference |
paul@92 | 28 | |
paul@92 | 29 | class Optimiser: |
paul@92 | 30 | |
paul@92 | 31 | "Optimise objects in a program." |
paul@92 | 32 | |
paul@92 | 33 | def __init__(self, importer, deducer, output): |
paul@92 | 34 | |
paul@92 | 35 | """ |
paul@92 | 36 | Initialise an instance using the given 'importer' and 'deducer' that |
paul@92 | 37 | will perform the arrangement of attributes for program objects, writing |
paul@92 | 38 | the results to the given 'output' directory. |
paul@92 | 39 | """ |
paul@92 | 40 | |
paul@92 | 41 | self.importer = importer |
paul@92 | 42 | self.deducer = deducer |
paul@92 | 43 | self.output = output |
paul@92 | 44 | |
paul@92 | 45 | # Locations/offsets of attributes in objects. |
paul@92 | 46 | |
paul@92 | 47 | self.locations = None |
paul@92 | 48 | self.attr_locations = None |
paul@92 | 49 | self.all_attrnames = None |
paul@92 | 50 | |
paul@92 | 51 | # Locations of parameters in parameter tables. |
paul@92 | 52 | |
paul@92 | 53 | self.arg_locations = None |
paul@92 | 54 | self.param_locations = None |
paul@92 | 55 | self.all_paramnames = None |
paul@92 | 56 | |
paul@92 | 57 | # Specific attribute access information. |
paul@92 | 58 | |
paul@92 | 59 | self.access_plans = {} |
paul@92 | 60 | |
paul@92 | 61 | # Object structure information. |
paul@92 | 62 | |
paul@92 | 63 | self.structures = {} |
paul@92 | 64 | self.attr_table = {} |
paul@92 | 65 | |
paul@92 | 66 | # Parameter list information. |
paul@92 | 67 | |
paul@92 | 68 | self.parameters = {} |
paul@92 | 69 | self.param_table = {} |
paul@92 | 70 | |
paul@92 | 71 | # Constant literal information. |
paul@92 | 72 | |
paul@92 | 73 | self.constants = [] |
paul@92 | 74 | self.constant_numbers = {} |
paul@92 | 75 | |
paul@92 | 76 | # Optimiser activities. |
paul@92 | 77 | |
paul@92 | 78 | self.populate_objects() |
paul@92 | 79 | self.position_attributes() |
paul@92 | 80 | self.populate_parameters() |
paul@92 | 81 | self.position_parameters() |
paul@92 | 82 | self.populate_tables() |
paul@92 | 83 | self.populate_constants() |
paul@92 | 84 | self.refine_access_plans() |
paul@92 | 85 | |
paul@92 | 86 | def to_output(self): |
paul@92 | 87 | |
paul@92 | 88 | "Write the output files using optimisation information." |
paul@92 | 89 | |
paul@92 | 90 | if not exists(self.output): |
paul@92 | 91 | makedirs(self.output) |
paul@92 | 92 | |
paul@92 | 93 | self.write_objects() |
paul@92 | 94 | |
paul@92 | 95 | def write_objects(self): |
paul@92 | 96 | |
paul@92 | 97 | """ |
paul@92 | 98 | Write object-related output. |
paul@92 | 99 | |
paul@92 | 100 | The locations are a list of positions indicating the attributes residing |
paul@92 | 101 | at each position in the different structures in a program. |
paul@92 | 102 | |
paul@92 | 103 | ---- |
paul@92 | 104 | |
paul@92 | 105 | The parameter locations are a list of positions indicating the parameters |
paul@92 | 106 | residing at each position in the different parameter lists in a program. |
paul@92 | 107 | |
paul@92 | 108 | ---- |
paul@92 | 109 | |
paul@92 | 110 | Each attribute plan provides attribute details in the following format: |
paul@92 | 111 | |
paul@92 | 112 | location " " name " " test " " test type " " base |
paul@92 | 113 | " " traversed attributes " " traversed attribute ambiguity |
paul@92 | 114 | " " attributes to traverse " " attribute ambiguity |
paul@92 | 115 | " " context " " access method " " static attribute |
paul@92 | 116 | |
paul@92 | 117 | Locations have the following format: |
paul@92 | 118 | |
paul@92 | 119 | qualified name of scope "." local name ":" name version |
paul@92 | 120 | |
paul@92 | 121 | ---- |
paul@92 | 122 | |
paul@92 | 123 | The structures are presented as a table in the following format: |
paul@92 | 124 | |
paul@92 | 125 | qualified name " " attribute names |
paul@92 | 126 | |
paul@92 | 127 | The attribute names are separated by ", " characters and indicate the |
paul@92 | 128 | attribute provided at each position in the structure associated with the |
paul@92 | 129 | given type. Where no attribute is provided at a particular location |
paul@92 | 130 | within a structure, "-" is given. |
paul@92 | 131 | |
paul@92 | 132 | ---- |
paul@92 | 133 | |
paul@92 | 134 | The parameters are presented as a table in the following format: |
paul@92 | 135 | |
paul@92 | 136 | qualified name " " parameter details |
paul@92 | 137 | |
paul@92 | 138 | The parameter details are separated by ", " characters and indicate |
paul@92 | 139 | the parameter name and list position for each parameter described at |
paul@92 | 140 | each location in the parameter table associated with the given |
paul@92 | 141 | function. Where no parameter details are provided at a particular |
paul@92 | 142 | location within a parameter table, "-" is given. The name and list |
paul@92 | 143 | position are separated by a colon (":"). |
paul@92 | 144 | |
paul@92 | 145 | ---- |
paul@92 | 146 | |
paul@92 | 147 | The attribute table is presented as a table in the following format: |
paul@92 | 148 | |
paul@92 | 149 | qualified name " " attribute identifiers |
paul@92 | 150 | |
paul@92 | 151 | Instead of attribute names, identifiers defined according to the order |
paul@92 | 152 | given in the "attrnames" file are employed to denote the attributes |
paul@92 | 153 | featured in each type's structure. Where no attribute is provided at a |
paul@92 | 154 | particular location within a structure, "-" is given. |
paul@92 | 155 | |
paul@92 | 156 | ---- |
paul@92 | 157 | |
paul@92 | 158 | The parameter table is presented as a table in the following format: |
paul@92 | 159 | |
paul@92 | 160 | qualified name " " parameter details |
paul@92 | 161 | |
paul@92 | 162 | Instead of parameter names, identifiers defined according to the order |
paul@92 | 163 | given in the "paramnames" file are employed to denote the parameters |
paul@92 | 164 | featured in each function's parameter table. Where no parameter is |
paul@92 | 165 | provided at a particular location within a table, "-" is given. |
paul@92 | 166 | |
paul@92 | 167 | ---- |
paul@92 | 168 | |
paul@92 | 169 | The ordered list of attribute names is given in the "attrnames" file. |
paul@92 | 170 | |
paul@92 | 171 | ---- |
paul@92 | 172 | |
paul@92 | 173 | The ordered list of parameter names is given in the "paramnames" file. |
paul@92 | 174 | |
paul@92 | 175 | ---- |
paul@92 | 176 | |
paul@92 | 177 | The ordered list of constant literals is given in the "constants" file. |
paul@92 | 178 | """ |
paul@92 | 179 | |
paul@92 | 180 | f = open(join(self.output, "locations"), "w") |
paul@92 | 181 | try: |
paul@92 | 182 | for attrnames in self.locations: |
paul@92 | 183 | print >>f, sorted_output(attrnames) |
paul@92 | 184 | |
paul@92 | 185 | finally: |
paul@92 | 186 | f.close() |
paul@92 | 187 | |
paul@92 | 188 | f = open(join(self.output, "parameter_locations"), "w") |
paul@92 | 189 | try: |
paul@92 | 190 | for argnames in self.arg_locations: |
paul@92 | 191 | print >>f, sorted_output(argnames) |
paul@92 | 192 | |
paul@92 | 193 | finally: |
paul@92 | 194 | f.close() |
paul@92 | 195 | |
paul@92 | 196 | f = open(join(self.output, "attribute_plans"), "w") |
paul@92 | 197 | try: |
paul@92 | 198 | access_plans = self.access_plans.items() |
paul@92 | 199 | access_plans.sort() |
paul@92 | 200 | |
paul@92 | 201 | for location, (name, test, test_type, base, |
paul@92 | 202 | traversed, traversed_ambiguity, |
paul@92 | 203 | attrnames, attrnames_ambiguity, context, method, attr) in access_plans: |
paul@92 | 204 | |
paul@92 | 205 | print >>f, encode_access_location(location), \ |
paul@92 | 206 | name, test, test_type or "{}", \ |
paul@92 | 207 | base or "{}", \ |
paul@92 | 208 | ".".join(traversed) or "{}", \ |
paul@92 | 209 | ".".join(map(str, traversed_ambiguity)) or "{}", \ |
paul@92 | 210 | ".".join(map(str, attrnames_ambiguity)) or "{}", \ |
paul@92 | 211 | ".".join(attrnames) or "{}", \ |
paul@92 | 212 | context, method, attr or "{}" |
paul@92 | 213 | |
paul@92 | 214 | finally: |
paul@92 | 215 | f.close() |
paul@92 | 216 | |
paul@92 | 217 | f = open(join(self.output, "structures"), "w") |
paul@92 | 218 | try: |
paul@92 | 219 | structures = self.structures.items() |
paul@92 | 220 | structures.sort() |
paul@92 | 221 | |
paul@92 | 222 | for name, attrnames in structures: |
paul@92 | 223 | print >>f, name, ", ".join([s or "-" for s in attrnames]) |
paul@92 | 224 | |
paul@92 | 225 | finally: |
paul@92 | 226 | f.close() |
paul@92 | 227 | |
paul@92 | 228 | f = open(join(self.output, "parameters"), "w") |
paul@92 | 229 | try: |
paul@92 | 230 | parameters = self.parameters.items() |
paul@92 | 231 | parameters.sort() |
paul@92 | 232 | |
paul@92 | 233 | for name, argnames in parameters: |
paul@92 | 234 | print >>f, name, ", ".join([s and ("%s:%d" % s) or "-" for s in argnames]) |
paul@92 | 235 | |
paul@92 | 236 | finally: |
paul@92 | 237 | f.close() |
paul@92 | 238 | |
paul@92 | 239 | f = open(join(self.output, "attrtable"), "w") |
paul@92 | 240 | try: |
paul@92 | 241 | attr_table = self.attr_table.items() |
paul@92 | 242 | attr_table.sort() |
paul@92 | 243 | |
paul@92 | 244 | for name, attrcodes in attr_table: |
paul@92 | 245 | print >>f, name, ", ".join([i is not None and str(i) or "-" for i in attrcodes]) |
paul@92 | 246 | |
paul@92 | 247 | finally: |
paul@92 | 248 | f.close() |
paul@92 | 249 | |
paul@92 | 250 | f = open(join(self.output, "paramtable"), "w") |
paul@92 | 251 | try: |
paul@92 | 252 | param_table = self.param_table.items() |
paul@92 | 253 | param_table.sort() |
paul@92 | 254 | |
paul@92 | 255 | for name, paramcodes in param_table: |
paul@92 | 256 | print >>f, name, ", ".join([s and ("%d:%d" % s) or "-" for s in paramcodes]) |
paul@92 | 257 | |
paul@92 | 258 | finally: |
paul@92 | 259 | f.close() |
paul@92 | 260 | |
paul@92 | 261 | f = open(join(self.output, "attrnames"), "w") |
paul@92 | 262 | try: |
paul@92 | 263 | for name in self.all_attrnames: |
paul@92 | 264 | print >>f, name |
paul@92 | 265 | |
paul@92 | 266 | finally: |
paul@92 | 267 | f.close() |
paul@92 | 268 | |
paul@92 | 269 | f = open(join(self.output, "paramnames"), "w") |
paul@92 | 270 | try: |
paul@92 | 271 | for name in self.all_paramnames: |
paul@92 | 272 | print >>f, name |
paul@92 | 273 | |
paul@92 | 274 | finally: |
paul@92 | 275 | f.close() |
paul@92 | 276 | |
paul@92 | 277 | f = open(join(self.output, "constants"), "w") |
paul@92 | 278 | try: |
paul@92 | 279 | constants = [(n, value) for (value, n) in self.constants.items()] |
paul@92 | 280 | constants.sort() |
paul@92 | 281 | for n, value in constants: |
paul@92 | 282 | print >>f, repr(value) |
paul@92 | 283 | |
paul@92 | 284 | finally: |
paul@92 | 285 | f.close() |
paul@92 | 286 | |
paul@92 | 287 | def populate_objects(self): |
paul@92 | 288 | |
paul@92 | 289 | "Populate objects using attribute and usage information." |
paul@92 | 290 | |
paul@92 | 291 | all_attrs = {} |
paul@92 | 292 | |
paul@92 | 293 | # Partition attributes into separate sections so that class and instance |
paul@92 | 294 | # attributes are treated separately. |
paul@92 | 295 | |
paul@92 | 296 | for source, objtype in [ |
paul@92 | 297 | (self.importer.all_class_attrs, "<class>"), |
paul@92 | 298 | (self.importer.all_instance_attrs, "<instance>"), |
paul@92 | 299 | (self.importer.all_module_attrs, "<module>") |
paul@92 | 300 | ]: |
paul@92 | 301 | for name, attrs in source.items(): |
paul@92 | 302 | all_attrs[(objtype, name)] = attrs |
paul@92 | 303 | |
paul@92 | 304 | self.locations = get_allocated_locations(all_attrs, get_attributes_and_sizes) |
paul@92 | 305 | |
paul@92 | 306 | def populate_parameters(self): |
paul@92 | 307 | |
paul@92 | 308 | "Populate parameter tables using parameter information." |
paul@92 | 309 | |
paul@92 | 310 | self.arg_locations = get_allocated_locations(self.importer.function_parameters, get_parameters_and_sizes) |
paul@92 | 311 | |
paul@92 | 312 | def position_attributes(self): |
paul@92 | 313 | |
paul@92 | 314 | "Position specific attribute references." |
paul@92 | 315 | |
paul@92 | 316 | # Reverse the location mappings. |
paul@92 | 317 | |
paul@92 | 318 | attr_locations = self.attr_locations = {} |
paul@92 | 319 | |
paul@92 | 320 | for i, attrnames in enumerate(self.locations): |
paul@92 | 321 | for attrname in attrnames: |
paul@92 | 322 | attr_locations[attrname] = i |
paul@92 | 323 | |
paul@92 | 324 | # Record the structures. |
paul@92 | 325 | |
paul@92 | 326 | for source, objtype in [ |
paul@92 | 327 | (self.importer.all_class_attrs, "<class>"), |
paul@92 | 328 | (self.importer.all_instance_attrs, "<instance>"), |
paul@92 | 329 | (self.importer.all_module_attrs, "<module>") |
paul@92 | 330 | ]: |
paul@92 | 331 | |
paul@92 | 332 | for name, attrnames in source.items(): |
paul@92 | 333 | key = Reference(objtype, name) |
paul@92 | 334 | l = self.structures[key] = [None] * len(attrnames) |
paul@92 | 335 | for attrname in attrnames: |
paul@92 | 336 | position = attr_locations[attrname] |
paul@92 | 337 | if position >= len(l): |
paul@92 | 338 | l.extend([None] * (position - len(l) + 1)) |
paul@92 | 339 | l[position] = attrname |
paul@92 | 340 | |
paul@92 | 341 | def refine_access_plans(self): |
paul@92 | 342 | |
paul@92 | 343 | "Augment deduced access plans with attribute position information." |
paul@92 | 344 | |
paul@92 | 345 | for access_location, access_plan in self.deducer.access_plans.items(): |
paul@92 | 346 | |
paul@92 | 347 | # Obtain the access details. |
paul@92 | 348 | |
paul@92 | 349 | name, test, test_type, base, traversed, attrnames, \ |
paul@92 | 350 | context, method, attr = access_plan |
paul@92 | 351 | |
paul@92 | 352 | traversed_ambiguity = self.get_ambiguity_for_attributes(traversed) |
paul@92 | 353 | attrnames_ambiguity = self.get_ambiguity_for_attributes(attrnames) |
paul@92 | 354 | |
paul@92 | 355 | self.access_plans[access_location] = \ |
paul@92 | 356 | name, test, test_type, base, traversed, traversed_ambiguity, \ |
paul@92 | 357 | attrnames, attrnames_ambiguity, context, method, attr |
paul@92 | 358 | |
paul@92 | 359 | def get_ambiguity_for_attributes(self, attrnames): |
paul@92 | 360 | |
paul@92 | 361 | """ |
paul@92 | 362 | Return a list of attribute position alternatives corresponding to each |
paul@92 | 363 | of the given 'attrnames'. |
paul@92 | 364 | """ |
paul@92 | 365 | |
paul@92 | 366 | ambiguity = [] |
paul@92 | 367 | |
paul@92 | 368 | for attrname in attrnames: |
paul@92 | 369 | position = self.attr_locations[attrname] |
paul@92 | 370 | ambiguity.append(len(self.locations[position])) |
paul@92 | 371 | |
paul@92 | 372 | return ambiguity |
paul@92 | 373 | |
paul@92 | 374 | def position_parameters(self): |
paul@92 | 375 | |
paul@92 | 376 | "Position the parameters for each function's parameter table." |
paul@92 | 377 | |
paul@92 | 378 | # Reverse the location mappings. |
paul@92 | 379 | |
paul@92 | 380 | param_locations = self.param_locations = {} |
paul@92 | 381 | |
paul@92 | 382 | for i, argnames in enumerate(self.arg_locations): |
paul@92 | 383 | for argname in argnames: |
paul@92 | 384 | param_locations[argname] = i |
paul@92 | 385 | |
paul@92 | 386 | for name, argnames in self.importer.function_parameters.items(): |
paul@92 | 387 | l = self.parameters[name] = [None] * len(argnames) |
paul@92 | 388 | |
paul@92 | 389 | # Store an entry for the name along with the name's position in the |
paul@92 | 390 | # parameter list. |
paul@92 | 391 | |
paul@92 | 392 | for pos, argname in enumerate(argnames): |
paul@92 | 393 | position = param_locations[argname] |
paul@92 | 394 | if position >= len(l): |
paul@92 | 395 | l.extend([None] * (position - len(l) + 1)) |
paul@92 | 396 | l[position] = (argname, pos) |
paul@92 | 397 | |
paul@92 | 398 | def populate_tables(self): |
paul@92 | 399 | |
paul@92 | 400 | """ |
paul@92 | 401 | Assign identifiers to attributes and encode structure information using |
paul@92 | 402 | these identifiers. |
paul@92 | 403 | """ |
paul@92 | 404 | |
paul@92 | 405 | self.all_attrnames, d = self._get_name_mapping(self.attr_locations) |
paul@92 | 406 | |
paul@92 | 407 | # Record the numbers indicating the locations of the names. |
paul@92 | 408 | |
paul@92 | 409 | for key, attrnames in self.structures.items(): |
paul@92 | 410 | l = self.attr_table[key] = [] |
paul@92 | 411 | for attrname in attrnames: |
paul@92 | 412 | if attrname is None: |
paul@92 | 413 | l.append(None) |
paul@92 | 414 | else: |
paul@92 | 415 | l.append(d[attrname]) |
paul@92 | 416 | |
paul@92 | 417 | self.all_paramnames, d = self._get_name_mapping(self.param_locations) |
paul@92 | 418 | |
paul@92 | 419 | # Record the numbers indicating the locations of the names. |
paul@92 | 420 | |
paul@92 | 421 | for key, values in self.parameters.items(): |
paul@92 | 422 | l = self.param_table[key] = [] |
paul@92 | 423 | for value in values: |
paul@92 | 424 | if value is None: |
paul@92 | 425 | l.append(None) |
paul@92 | 426 | else: |
paul@92 | 427 | name, pos = value |
paul@92 | 428 | l.append((d[name], pos)) |
paul@92 | 429 | |
paul@92 | 430 | def _get_name_mapping(self, locations): |
paul@92 | 431 | |
paul@92 | 432 | """ |
paul@92 | 433 | Get a sorted list of names from 'locations', then map them to |
paul@92 | 434 | identifying numbers. Return the list and the mapping. |
paul@92 | 435 | """ |
paul@92 | 436 | |
paul@92 | 437 | all_names = locations.keys() |
paul@92 | 438 | all_names.sort() |
paul@92 | 439 | return all_names, dict([(name, i) for i, name in enumerate(all_names)]) |
paul@92 | 440 | |
paul@92 | 441 | def populate_constants(self): |
paul@92 | 442 | |
paul@92 | 443 | """ |
paul@92 | 444 | Obtain a collection of distinct constant literals, building a mapping |
paul@92 | 445 | from constant references to those in this collection. |
paul@92 | 446 | """ |
paul@92 | 447 | |
paul@92 | 448 | # Obtain mappings from constant values to identifiers. |
paul@92 | 449 | |
paul@92 | 450 | self.constants = {} |
paul@92 | 451 | |
paul@92 | 452 | for path, constants in self.importer.all_constants.items(): |
paul@92 | 453 | for constant, n in constants.items(): |
paul@92 | 454 | |
paul@92 | 455 | # Record constants and obtain a number for them. |
paul@92 | 456 | |
paul@92 | 457 | add_counter_item(self.constants, constant) |
paul@92 | 458 | |
paul@92 | 459 | self.constant_numbers = {} |
paul@92 | 460 | |
paul@92 | 461 | for name, (value, value_type) in self.importer.all_constant_values.items(): |
paul@92 | 462 | self.constant_numbers[name] = self.constants[value] |
paul@92 | 463 | |
paul@92 | 464 | def combine_rows(a, b): |
paul@92 | 465 | c = [] |
paul@92 | 466 | for i, j in zip(a, b): |
paul@92 | 467 | if i is None or j is None: |
paul@92 | 468 | c.append(i or j) |
paul@92 | 469 | else: |
paul@92 | 470 | return None |
paul@92 | 471 | return c |
paul@92 | 472 | |
paul@92 | 473 | def get_attributes_and_sizes(d): |
paul@92 | 474 | |
paul@92 | 475 | """ |
paul@92 | 476 | Return a matrix of attributes, a list of type names corresponding to columns |
paul@92 | 477 | in the matrix, and a list of ranked sizes each indicating... |
paul@92 | 478 | |
paul@92 | 479 | * a weighted size depending on the kind of object |
paul@92 | 480 | * the minimum size of an object employing an attribute |
paul@92 | 481 | * the number of free columns in the matrix for the attribute |
paul@92 | 482 | * the attribute name itself |
paul@92 | 483 | """ |
paul@92 | 484 | |
paul@92 | 485 | attrs = {} |
paul@92 | 486 | sizes = {} |
paul@92 | 487 | objtypes = {} |
paul@92 | 488 | |
paul@92 | 489 | for name, attrnames in d.items(): |
paul@92 | 490 | objtype, _name = name |
paul@92 | 491 | |
paul@92 | 492 | for attrname in attrnames: |
paul@92 | 493 | |
paul@92 | 494 | # Record each type supporting the attribute. |
paul@92 | 495 | |
paul@92 | 496 | init_item(attrs, attrname, set) |
paul@92 | 497 | attrs[attrname].add(name) |
paul@92 | 498 | |
paul@92 | 499 | # Maintain a record of the smallest object size supporting the given |
paul@92 | 500 | # attribute. |
paul@92 | 501 | |
paul@92 | 502 | if not sizes.has_key(attrname): |
paul@92 | 503 | sizes[attrname] = len(attrnames) |
paul@92 | 504 | else: |
paul@92 | 505 | sizes[attrname] = min(sizes[attrname], len(attrnames)) |
paul@92 | 506 | |
paul@92 | 507 | # Record the object types/kinds supporting the attribute. |
paul@92 | 508 | |
paul@92 | 509 | init_item(objtypes, attrname, set) |
paul@92 | 510 | objtypes[attrname].add(objtype) |
paul@92 | 511 | |
paul@92 | 512 | # Obtain attribute details in order of size and occupancy. |
paul@92 | 513 | |
paul@92 | 514 | names = d.keys() |
paul@92 | 515 | |
paul@92 | 516 | rsizes = [] |
paul@92 | 517 | for attrname, size in sizes.items(): |
paul@92 | 518 | priority = "<instance>" in objtypes[attrname] and 0.5 or 1 |
paul@92 | 519 | occupied = len(attrs[attrname]) |
paul@92 | 520 | key = (priority * size, size, len(names) - occupied, attrname) |
paul@92 | 521 | rsizes.append(key) |
paul@92 | 522 | |
paul@92 | 523 | rsizes.sort() |
paul@92 | 524 | |
paul@92 | 525 | # Make a matrix of attributes. |
paul@92 | 526 | |
paul@92 | 527 | matrix = {} |
paul@92 | 528 | for attrname, types in attrs.items(): |
paul@92 | 529 | row = [] |
paul@92 | 530 | for name in names: |
paul@92 | 531 | if name in types: |
paul@92 | 532 | row.append(attrname) |
paul@92 | 533 | else: |
paul@92 | 534 | row.append(None) |
paul@92 | 535 | matrix[attrname] = row |
paul@92 | 536 | |
paul@92 | 537 | return matrix, names, rsizes |
paul@92 | 538 | |
paul@92 | 539 | def get_parameters_and_sizes(d): |
paul@92 | 540 | |
paul@92 | 541 | """ |
paul@92 | 542 | Return a matrix of parameters, a list of functions corresponding to columns |
paul@92 | 543 | in the matrix, and a list of ranked sizes each indicating... |
paul@92 | 544 | |
paul@92 | 545 | * a weighted size depending on the kind of object |
paul@92 | 546 | * the minimum size of a parameter list employing a parameter |
paul@92 | 547 | * the number of free columns in the matrix for the parameter |
paul@92 | 548 | * the parameter name itself |
paul@92 | 549 | |
paul@92 | 550 | This is a slightly simpler version of the above 'get_attributes_and_sizes' |
paul@92 | 551 | function. |
paul@92 | 552 | """ |
paul@92 | 553 | |
paul@92 | 554 | params = {} |
paul@92 | 555 | sizes = {} |
paul@92 | 556 | |
paul@92 | 557 | for name, argnames in d.items(): |
paul@92 | 558 | for argname in argnames: |
paul@92 | 559 | |
paul@92 | 560 | # Record each function supporting the parameter. |
paul@92 | 561 | |
paul@92 | 562 | init_item(params, argname, set) |
paul@92 | 563 | params[argname].add(name) |
paul@92 | 564 | |
paul@92 | 565 | # Maintain a record of the smallest parameter list supporting the |
paul@92 | 566 | # given parameter. |
paul@92 | 567 | |
paul@92 | 568 | if not sizes.has_key(argname): |
paul@92 | 569 | sizes[argname] = len(argnames) |
paul@92 | 570 | else: |
paul@92 | 571 | sizes[argname] = min(sizes[argname], len(argnames)) |
paul@92 | 572 | |
paul@92 | 573 | # Obtain attribute details in order of size and occupancy. |
paul@92 | 574 | |
paul@92 | 575 | names = d.keys() |
paul@92 | 576 | |
paul@92 | 577 | rsizes = [] |
paul@92 | 578 | for argname, size in sizes.items(): |
paul@92 | 579 | occupied = len(params[argname]) |
paul@92 | 580 | key = (size, size, len(names) - occupied, argname) |
paul@92 | 581 | rsizes.append(key) |
paul@92 | 582 | |
paul@92 | 583 | rsizes.sort() |
paul@92 | 584 | |
paul@92 | 585 | # Make a matrix of parameters. |
paul@92 | 586 | |
paul@92 | 587 | matrix = {} |
paul@92 | 588 | for argname, types in params.items(): |
paul@92 | 589 | row = [] |
paul@92 | 590 | for name in names: |
paul@92 | 591 | if name in types: |
paul@92 | 592 | row.append(argname) |
paul@92 | 593 | else: |
paul@92 | 594 | row.append(None) |
paul@92 | 595 | matrix[argname] = row |
paul@92 | 596 | |
paul@92 | 597 | return matrix, names, rsizes |
paul@92 | 598 | |
paul@92 | 599 | def get_allocated_locations(d, fn): |
paul@92 | 600 | |
paul@92 | 601 | """ |
paul@92 | 602 | Return a list where each element corresponds to a structure location and |
paul@92 | 603 | contains a set of attribute names that may be stored at that location, given |
paul@92 | 604 | a mapping 'd' whose keys are (object type, object name) tuples and whose |
paul@92 | 605 | values are collections of attributes. |
paul@92 | 606 | """ |
paul@92 | 607 | |
paul@92 | 608 | matrix, names, rsizes = fn(d) |
paul@92 | 609 | allocated = [] |
paul@92 | 610 | |
paul@92 | 611 | x = 0 |
paul@92 | 612 | while x < len(rsizes): |
paul@92 | 613 | weight, size, free, attrname = rsizes[x] |
paul@92 | 614 | base = matrix[attrname] |
paul@92 | 615 | y = x + 1 |
paul@92 | 616 | while y < len(rsizes): |
paul@92 | 617 | _weight, _size, _free, _attrname = rsizes[y] |
paul@92 | 618 | occupied = len(names) - _free |
paul@92 | 619 | if occupied > free: |
paul@92 | 620 | break |
paul@92 | 621 | new = combine_rows(base, matrix[_attrname]) |
paul@92 | 622 | if new: |
paul@92 | 623 | del matrix[_attrname] |
paul@92 | 624 | del rsizes[y] |
paul@92 | 625 | base = new |
paul@92 | 626 | free -= occupied |
paul@92 | 627 | else: |
paul@92 | 628 | y += 1 |
paul@92 | 629 | allocated.append(base) |
paul@92 | 630 | x += 1 |
paul@92 | 631 | |
paul@92 | 632 | # Return the list of attribute names from each row of the allocated |
paul@92 | 633 | # attributes table. |
paul@92 | 634 | |
paul@92 | 635 | return [set([attrname for attrname in attrnames if attrname]) for attrnames in allocated] |
paul@92 | 636 | |
paul@92 | 637 | # vim: tabstop=4 expandtab shiftwidth=4 |