-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'heuristicsortingofchangessets' into develop
- Loading branch information
Showing
3 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
def tosortedlist(changeentrymap): | ||
sortedlist = [] | ||
expectedlistsize = len(__aslist(changeentrymap)) | ||
|
||
while len(sortedlist) < expectedlistsize: | ||
firstentryfromeachkey = __getfirstentryfromeachkeyasmap(changeentrymap) | ||
changesetwithearliestdate = __getchangeentrywithearliestdate(firstentryfromeachkey) | ||
__deleteentry(changeentrymap, changesetwithearliestdate) | ||
sortedlist.append(changesetwithearliestdate) | ||
|
||
return sortedlist | ||
|
||
|
||
def __getfirstentryfromeachkeyasmap(changeentrymap): | ||
firstentries = {} | ||
for key in changeentrymap.keys(): | ||
changeentries = changeentrymap.get(key) | ||
if changeentries: | ||
firstentries[key] = changeentries[0] | ||
return firstentries | ||
|
||
|
||
def __deleteentry(changeentrymap, changeentrytodelete): | ||
for key in changeentrymap.keys(): | ||
changeentries = changeentrymap.get(key) | ||
if changeentries and changeentrytodelete.revision is changeentries[0].revision: | ||
changeentries.remove(changeentrytodelete) | ||
break | ||
|
||
|
||
def __getchangeentrywithearliestdate(changeentries): | ||
changeentrywithearliestdate = None | ||
for key in changeentries.keys(): | ||
changeentry = changeentries.get(key) | ||
if not changeentrywithearliestdate or changeentry.date < changeentrywithearliestdate.date: | ||
changeentrywithearliestdate = changeentry | ||
return changeentrywithearliestdate | ||
|
||
|
||
def __aslist(anymap): | ||
resultlist = [] | ||
for key in anymap.keys(): | ||
for changeentry in anymap.get(key): | ||
resultlist.append(changeentry) | ||
return resultlist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
|
||
from rtcFunctions import ChangeEntry | ||
import sorter | ||
|
||
|
||
class SorterTest(unittest.TestCase): | ||
def setUp(self): | ||
self.changeentriesmap = {} | ||
self.changesetcounter = 0 | ||
|
||
def testSortingTwoComponents_ExpectKeepOrderingInsideSameKey(self): | ||
firstkey = "anyKey" | ||
secondkey = "anyOtherKey" | ||
self.put(firstkey, self.createchangeentry("1991-03-24")) | ||
self.put(firstkey, self.createchangeentry("1991-05-24")) | ||
self.put(firstkey, self.createchangeentry("1991-05-22")) | ||
self.put(secondkey, self.createchangeentry("1991-05-23")) | ||
self.put(secondkey, self.createchangeentry("1991-07-22")) | ||
sortedchangesets = sorter.tosortedlist(self.changeentriesmap) | ||
|
||
self.assertEqual("1991-03-24", sortedchangesets[0].date) # earliestEntry firstkey | ||
self.assertEqual("1991-05-23", sortedchangesets[1].date) # earliestEntry secondKey | ||
self.assertEqual("1991-05-24", sortedchangesets[2].date) # secondEarliestEntry firstKey | ||
self.assertEqual("1991-05-22", sortedchangesets[3].date) # lastEntry firstKey | ||
self.assertEqual("1991-07-22", sortedchangesets[4].date) # lastEntry secondKey | ||
|
||
def testSortingThreeComponents_ExpectDatesInOrder(self): | ||
earliestdate = "2015-01-30 09:00:15" | ||
dateinbetween = "2015-01-30 09:00:16" | ||
latestdate = "2015-02-30 09:00:00" | ||
self.put("justAnotherKey", self.createchangeentry(latestdate)) | ||
self.put("anyKey", self.createchangeentry(earliestdate)) | ||
self.put("anyOtherKey", self.createchangeentry(dateinbetween)) | ||
|
||
sortedchangesets = sorter.tosortedlist(self.changeentriesmap) | ||
self.assertEqual(earliestdate, sortedchangesets[0].date) | ||
self.assertEqual(dateinbetween, sortedchangesets[1].date) | ||
self.assertEqual(latestdate, sortedchangesets[2].date) | ||
|
||
def createchangeentry(self, date): | ||
self.changesetcounter += 1 | ||
return ChangeEntry(self.changesetcounter, "anyAuthor", "anyEmail", date, "") | ||
|
||
def put(self, key, changeentry): | ||
changeentries = self.changeentriesmap.get(key) | ||
if not changeentries: | ||
changeentries = [] | ||
changeentries.append(changeentry) | ||
self.changeentriesmap[key] = changeentries |