-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
ConcurrentMutableSet.m
103 lines (77 loc) · 1.91 KB
/
ConcurrentMutableSet.m
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
//
// ConcurrentMutableSet.m
// Strongbox
//
// Created by Strongbox on 02/05/2020.
// Copyright © 2014-2021 Mark McGuill. All rights reserved.
//
#import "ConcurrentMutableSet.h"
@interface ConcurrentMutableSet ()
@property (strong, nonatomic) NSMutableSet *data;
@property (strong, nonatomic) dispatch_queue_t dataQueue;
@end
@implementation ConcurrentMutableSet
+ (instancetype)mutableSet {
return [[ConcurrentMutableSet alloc] init];
}
- (instancetype)init {
self = [super init];
if (self) {
self.data = NSMutableSet.set;
self.dataQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT);
}
return self;
}
- (void)addObject:(id)object {
dispatch_barrier_async(self.dataQueue, ^{
[self.data addObject:object];
});
}
- (void)addObjectsFromArray:(NSArray*)array {
dispatch_barrier_async(self.dataQueue, ^{
[self.data addObjectsFromArray:array];
});
}
- (void)removeObject:(id)object {
dispatch_barrier_async(self.dataQueue, ^{
if ( object ) {
[self.data removeObject:object];
}
});
}
- (NSSet*)snapshot {
__block NSSet *result;
dispatch_sync(self.dataQueue, ^{
result = self.data.copy;
});
return result;
}
- (NSArray*)arraySnapshot {
__block NSArray *result;
dispatch_sync(self.dataQueue, ^{
result = self.data.allObjects.copy;
});
return result;
}
- (NSUInteger)count {
__block NSUInteger result;
dispatch_sync(self.dataQueue, ^{
result = self.data.count;
});
return result;
}
- (id)anyObject {
__block id result;
dispatch_sync(self.dataQueue, ^{
result = self.data.anyObject;
});
return result;
}
- (BOOL)containsObject:(id)object {
__block BOOL result;
dispatch_sync(self.dataQueue, ^{
result = [self.data containsObject:object];
});
return result;
}
@end