From e3b2d77ebffbb7abc679ddd74509797892f1f54b Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Fri, 26 Dec 2008 00:16:19 +0000 Subject: [PATCH] reuse existing TorrentGroup object instead of recreating constantly (fixes #1415) --- macosx/Controller.m | 35 +++++++++++++++++++++++++++-------- macosx/NSStringAdditions.m | 8 ++++---- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/macosx/Controller.m b/macosx/Controller.m index a6bbbff70..9ff971d73 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -2113,25 +2113,44 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy BOOL groupRows = [fDefaults boolForKey: @"SortByGroup"] && [NSApp isOnLeopardOrBetter]; if (groupRows) { + NSArray * oldTorrentGroups = nil; + if ([fDisplayedTorrents count] > 0 && [[fDisplayedTorrents objectAtIndex: 0] isKindOfClass: [TorrentGroup class]]) + oldTorrentGroups = [NSArray arrayWithArray: fDisplayedTorrents]; + [fDisplayedTorrents removeAllObjects]; NSSortDescriptor * groupDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"groupOrderValue" ascending: YES] autorelease]; allTorrents = [allTorrents sortedArrayUsingDescriptors: [NSArray arrayWithObject: groupDescriptor]]; NSMutableArray * groupTorrents; - for (NSInteger i = 0, oldGroupValue = -2; i < [allTorrents count]; i++) + NSInteger lastGroupValue = -2, currentOldGroupIndex = 0; + NSEnumerator * enumerator = [allTorrents objectEnumerator]; + while ((torrent = [enumerator nextObject])) { - torrent = [allTorrents objectAtIndex: i]; NSInteger groupValue = [torrent groupValue]; - if (groupValue != oldGroupValue) + if (groupValue != lastGroupValue) { - TorrentGroup * group = [[TorrentGroup alloc] initWithGroup: groupValue]; + TorrentGroup * group = nil; + + //try to see if the group already exists + for (; oldTorrentGroups && currentOldGroupIndex < [oldTorrentGroups count]; currentOldGroupIndex++) + { + TorrentGroup * currentGroup = [oldTorrentGroups objectAtIndex: currentOldGroupIndex]; + if ([currentGroup groupIndex] == groupValue) + { + group = currentGroup; + [[currentGroup torrents] removeAllObjects]; + break; + } + } + + if (!group) + group = [[[TorrentGroup alloc] initWithGroup: groupValue] autorelease]; + [fDisplayedTorrents addObject: group]; + groupTorrents = [group torrents]; - [fDisplayedTorrents addObject: group]; - [group release]; - - oldGroupValue = groupValue; + lastGroupValue = groupValue; } [groupTorrents addObject: torrent]; diff --git a/macosx/NSStringAdditions.m b/macosx/NSStringAdditions.m index 6dc944ce1..34abf31a7 100644 --- a/macosx/NSStringAdditions.m +++ b/macosx/NSStringAdditions.m @@ -147,15 +147,15 @@ - (NSComparisonResult) compareFinder: (NSString *) string { - NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSCaseInsensitiveSearch | NSNumericSearch - | NSWidthInsensitiveSearch | NSForcedOrderingSearch) - : (NSCaseInsensitiveSearch | NSNumericSearch); + const NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] + ? (NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch) + : (NSCaseInsensitiveSearch | NSNumericSearch); return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]]; } - (NSComparisonResult) compareNumeric: (NSString *) string { - NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSNumericSearch | NSForcedOrderingSearch) : NSNumericSearch; + const NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSNumericSearch | NSForcedOrderingSearch) : NSNumericSearch; return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]]; }