Changeset 9649


Ignore:
Timestamp:
Dec 2, 2009, 1:35:48 AM (12 years ago)
Author:
livings124
Message:

significantly cleaner code (and more than likely just-as or more efficient) for the Mac message log cap - avoids sorting the whole thing, which is, I'm sure, more expensive than the search that it's being replaced with

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/MessageWindowController.m

    r9648 r9649  
    190190    }
    191191   
     192    if ([fMessages count] > TR_MAX_MSG_LOG)
     193    {
     194        NSIndexSet * removeIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fMessages count]-TR_MAX_MSG_LOG)];
     195        NSArray * itemsToRemove = [fMessages objectsAtIndexes: removeIndexes];
     196       
     197        [fMessages removeObjectsAtIndexes: removeIndexes];
     198        [fDisplayedMessages removeObjectsInArray: itemsToRemove];
     199        changed = YES;
     200    }
     201   
    192202    if (changed)
    193203    {
    194         if ([fMessages count] > TR_MAX_MSG_LOG)
    195         {
    196             const NSUInteger removeCount = [fMessages count] - TR_MAX_MSG_LOG;
    197            
    198             //find the latest message to removed that is also displayed
    199             NSDictionary * latestMessageToRemove = nil;
    200             for (NSInteger i = removeCount-1; i >= 0; --i)
    201             {
    202                 NSDictionary * message = [fMessages objectAtIndex: i];
    203                 if ([[message objectForKey: @"Level"] integerValue] <= maxLevel)
    204                 {
    205                     latestMessageToRemove = message;
    206                     break;
    207                 }
    208             }
    209            
    210             if (latestMessageToRemove)
    211             {
    212                 NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Index" ascending: NO] autorelease];
    213                 [fDisplayedMessages sortUsingDescriptors: [NSArray arrayWithObject: descriptor]];
    214                
    215                 //sort in reverse and find the message within the bounds of the number of messages to remove
    216                 const NSUInteger displayedCount = [fDisplayedMessages count];
    217                 const NSUInteger removeDisplayedCount = MIN(removeCount, displayedCount);
    218                 const NSUInteger lastIndex = [fDisplayedMessages indexOfObject: latestMessageToRemove inRange:
    219                                                 NSMakeRange(displayedCount-removeDisplayedCount, removeDisplayedCount)];
    220                 NSAssert(lastIndex != NSNotFound, @"message to trim not found when it should be");
    221                
    222                 NSIndexSet * removeIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(lastIndex, displayedCount-lastIndex)];
    223                 [fDisplayedMessages removeObjectsAtIndexes: removeIndexes];
    224             }
    225            
    226             NSIndexSet * removeIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, removeCount)];
    227             [fMessages removeObjectsAtIndexes: removeIndexes];
    228         }
    229        
    230204        [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
    231205       
Note: See TracChangeset for help on using the changeset viewer.