utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. class FileRegionsDisposableGetter(object):
  20. def __init__(self, file_data):
  21. self.checksums = {}
  22. self.names = {}
  23. for each in file_data.iterate_regions():
  24. if each.get_checksum() not in self.checksums:
  25. self.checksums[each.get_checksum()] = []
  26. self.checksums[each.get_checksum()].append((each.get_id(), each.get_name()))
  27. if each.get_name() not in self.names:
  28. self.names[each.get_name()] = []
  29. self.names[each.get_name()].append((each.get_id(), each.get_checksum()))
  30. def get_next_id_once_by_checksum(self, checksum):
  31. if checksum not in self.checksums.keys():
  32. return None
  33. if len(self.checksums[checksum]) == 0:
  34. return None
  35. elem = self.checksums[checksum].pop(0)
  36. next_id = elem[0]
  37. next_name = elem[1]
  38. self.names[next_name].remove((next_id, checksum))
  39. return next_id
  40. def get_next_id_once_by_name(self, name):
  41. if name not in self.names.keys():
  42. return None
  43. if len(self.names[name]) == 0:
  44. return None
  45. elem = self.names[name].pop(0)
  46. next_id = elem[0]
  47. next_checksum = elem[1]
  48. self.checksums[next_checksum].remove((next_id, name))
  49. return next_id
  50. class FileRegionsMatcher(object):
  51. def __init__(self, file_data, prev_file_data):
  52. self.ids = [None] # add one to shift id from zero
  53. once_filter = FileRegionsDisposableGetter(prev_file_data)
  54. unmatched_region_ids = []
  55. for (ind, region) in enumerate(file_data.iterate_regions()):
  56. assert(ind + 1 == region.id)
  57. # Identify corresponding region in previous database (attempt by checksum)
  58. prev_id = once_filter.get_next_id_once_by_checksum(region.checksum)
  59. if prev_id != None:
  60. self.ids.append((prev_id, False))
  61. else:
  62. unmatched_region_ids.append(region.id)
  63. self.ids.append((None, True))
  64. # Identify corresponding region in previous database (attempt by name)
  65. for region_id in unmatched_region_ids:
  66. prev_id = once_filter.get_next_id_once_by_name(file_data.get_region(region_id).name)
  67. if prev_id != None:
  68. self.ids[region_id] = (prev_id, True)
  69. def get_prev_id(self, curr_id):
  70. return self.ids[curr_id][0]
  71. def is_matched(self, curr_id):
  72. return (self.ids[curr_id][0] != None)
  73. def is_modified(self, curr_id):
  74. return self.ids[curr_id][1]