Skip to content

Commit

Permalink
Add code to deduplicate objects going into the dictionary when flattened
Browse files Browse the repository at this point in the history
  • Loading branch information
gcasa committed Sep 14, 2024
1 parent 80e4641 commit 79dd1da
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion XCode/NSObject+KeyExtraction.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,45 @@
#import "NSObject+KeyExtraction.h"
#import "NSString+PBXAdditions.h"

//
// Check dictionary to see if it's equvalent...
//
@interface NSDictionary (Private)
- (BOOL) isEqualDictionary: (NSDictionary *)dict;
@end

@implementation NSDictionary (Private)
- (BOOL) isEqualDictionary: (NSDictionary *)dict
{
NSEnumerator *en = [self keyEnumerator];
id k = nil;
BOOL result = YES;

while ((k = [en nextObject]) != nil)
{
id v1 = [self objectForKey: k];
id v2 = [dict objectForKey: k];

if ([v1 isKindOfClass: [NSDictionary class]]
&& [v2 isKindOfClass: [NSDictionary class]])
{
if ([v1 isEqualToDictionary: v2] == NO)
{
result = NO;
break;
}
}
else if ([v1 isEqual: v2] == NO)
{
result = NO;
break;
}
}

return result;
}
@end

// Function to generate a 24-character GUID (uppercase, alphanumeric, no dashes)
NSString *generateGUID()
{
Expand Down Expand Up @@ -83,6 +122,26 @@ id moveContainerProperties(NSDictionary *input)
return result;
}

NSString *guidInCachedObjects(NSDictionary *objects, NSDictionary *dict)
{
NSString *guid = nil;
NSEnumerator *en = [objects keyEnumerator];
NSString *g = nil;

while ((g = [en nextObject]) != nil)
{
NSDictionary *d = [objects objectForKey: g];

if ([dict isEqualToDictionary: d])
{
guid = g;
break;
}
}

return guid;
}

// Recursive function to flatten the property list
id flattenPropertyList(id propertyList, NSMutableDictionary *objects, NSString **rootObjectGUID)
{
Expand Down Expand Up @@ -112,7 +171,16 @@ id flattenPropertyList(id propertyList, NSMutableDictionary *objects, NSString *
[flattenedDict setObject: flattenPropertyList([dict objectForKey:key], objects, rootObjectGUID)
forKey: key];
}
[objects setObject:flattenedDict forKey:guid];

NSString *existingGuid = guidInCachedObjects(objects, flattenedDict);
if (existingGuid != nil)
{
guid = existingGuid;
}
else
{
[objects setObject:flattenedDict forKey:guid];
}

// Return the GUID to replace the dictionary
return guid;
Expand Down

0 comments on commit 79dd1da

Please sign in to comment.