-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGRKArrayDiffInfo.m
More file actions
134 lines (108 loc) · 3.49 KB
/
Copy pathGRKArrayDiffInfo.m
File metadata and controls
134 lines (108 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// GRKArrayDiffInfo.m
//
// Created by Levi Brown on June 23, 2015.
// Copyright (c) 2015 Levi Brown <mailto:[email protected]>
// This work is licensed under the Creative Commons Attribution 3.0
// Unported License. To view a copy of this license, visit
// http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
// Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041,
// USA.
//
// The above attribution and the included license must accompany any version
// of the source code. Visible attribution in any binary distributable
// including this work (or derivatives) is not required, but would be
// appreciated.
//
#import "GRKArrayDiffInfo.h"
#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))
@implementation GRKArrayDiffInfo
#pragma mark - Lifecycle
- (instancetype)initWithIdentity:(NSString *)identity previousIndex:(NSNumber *)previousIndex currentIndex:(NSNumber *)currentIndex
{
if ((self = [super init]))
{
_identity = [identity copy];
_previousIndex = previousIndex;
_currentIndex = currentIndex;
}
return self;
}
#pragma mark - Implementation
- (NSNumber *)valueForIndexType:(GRKArrayDiffInfoIndexType)type
{
NSNumber *retVal = nil;
switch (type) {
case GRKArrayDiffInfoIndexTypePrevious:
retVal = self.previousIndex;
break;
case GRKArrayDiffInfoIndexTypeCurrent:
retVal = self.currentIndex;
break;
default:
NSLog(@"[ERROR] GRKArrayDiffInfo: Unhandled case (%d) for `valueForIndexType:`", (int)type);
break;
}
return retVal;
}
- (NSIndexPath *)indexPathForIndexType:(GRKArrayDiffInfoIndexType)type withSection:(NSInteger)section;
{
NSIndexPath *retVal = nil;
NSNumber *index = [self valueForIndexType:type];
if (index)
{
NSUInteger indicies[] = {section, index.integerValue};
retVal = [NSIndexPath indexPathWithIndexes:indicies length:2];
}
return retVal;
}
- (BOOL)isEqualToArrayDiffInfo:(GRKArrayDiffInfo *)diffInfo
{
BOOL retVal = NO;
if ([diffInfo isKindOfClass:GRKArrayDiffInfo.class])
{
NSUInteger ourHash = self.hash;
NSUInteger theirHash = diffInfo.hash;
retVal = ourHash == theirHash;
}
return retVal;
}
#pragma mark - Overrides
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p, identity '%@', previousIndex '%@', currentIndex '%@'>", NSStringFromClass([self class]), self, self.identity, self.previousIndex, self.currentIndex];
}
- (NSUInteger)hash
{
__block NSUInteger contributionCount = 0;
__block NSUInteger builtHash = 0;
void(^buildHash)(id obj) = ^(id obj) {
NSUInteger hash = [obj hash];
if (hash == 0)
{
//Account for nil values as well.
hash = 31;
}
if (builtHash == 0)
{
builtHash = hash;
contributionCount = 1;
}
else
{
++contributionCount;
builtHash = NSUINTROTATE(hash, NSUINT_BIT / contributionCount) ^ builtHash;
}
};
buildHash(self.identity);
buildHash(self.previousIndex);
buildHash(self.currentIndex);
return builtHash;
}
- (BOOL)isEqual:(id)object
{
BOOL retVal = [self isEqualToArrayDiffInfo:object];
return retVal;
}
@end