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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
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
135
136
137
138
139
|
-
+
-
-
+
-
+
-
+
-
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
+
-
+
-
-
-
-
-
+
-
-
-
-
-
+
-
-
+
-
-
+
-
-
-
-
-
+
+
-
-
-
-
-
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
|
/*
* Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
* Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#import "OFMutableSet.h"
#import "OFConcreteMutableSet.h"
#import "OFMutableMapTableSet.h"
#import "OFString.h"
static struct {
Class isa;
} placeholder;
@interface OFMutableSetPlaceholder: OFMutableSet
@interface OFPlaceholderMutableSet: OFMutableSet
@end
@implementation OFMutableSetPlaceholder
@implementation OFPlaceholderMutableSet
#ifdef __clang__
/* We intentionally don't call into super, so silence the warning. */
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-pragmas"
# pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#endif
- (instancetype)init
{
return (id)[[OFMutableMapTableSet alloc] init];
return (id)[[OFConcreteMutableSet alloc] init];
}
- (instancetype)initWithSet: (OFSet *)set
{
return (id)[[OFMutableMapTableSet alloc] initWithSet: set];
return (id)[[OFConcreteMutableSet alloc] initWithSet: set];
}
- (instancetype)initWithArray: (OFArray *)array
{
return (id)[[OFMutableMapTableSet alloc] initWithArray: array];
return (id)[[OFConcreteMutableSet alloc] initWithArray: array];
}
- (instancetype)initWithObjects: (id)firstObject, ...
{
id ret;
va_list arguments;
va_start(arguments, firstObject);
ret = [[OFMutableMapTableSet alloc] initWithObject: firstObject
ret = [[OFConcreteMutableSet alloc] initWithObject: firstObject
arguments: arguments];
va_end(arguments);
return ret;
}
- (instancetype)initWithObjects: (id const *)objects count: (size_t)count
{
return (id)[[OFMutableMapTableSet alloc] initWithObjects: objects
return (id)[[OFConcreteMutableSet alloc] initWithObjects: objects
count: count];
}
- (instancetype)initWithObject: (id)firstObject arguments: (va_list)arguments
{
return (id)[[OFMutableMapTableSet alloc] initWithObject: firstObject
return (id)[[OFConcreteMutableSet alloc] initWithObject: firstObject
arguments: arguments];
}
- (instancetype)initWithSerialization: (OFXMLElement *)element
{
return (id)[[OFMutableMapTableSet alloc]
initWithSerialization: element];
}
- (instancetype)initWithCapacity: (size_t)capacity
{
return (id)[[OFMutableMapTableSet alloc] initWithCapacity: capacity];
return (id)[[OFConcreteMutableSet alloc] initWithCapacity: capacity];
}
#ifdef __clang__
- (instancetype)retain
{
return self;
}
# pragma clang diagnostic pop
- (instancetype)autorelease
{
return self;
}
#endif
- (void)release
{
}
OF_SINGLETON_METHODS
- (void)dealloc
{
OF_DEALLOC_UNSUPPORTED
}
@end
@implementation OFMutableSet
+ (void)initialize
{
if (self == [OFMutableSet class])
placeholder.isa = [OFMutableSetPlaceholder class];
object_setClass((id)&placeholder,
[OFPlaceholderMutableSet class]);
}
+ (instancetype)alloc
{
if (self == [OFMutableSet class])
return (id)&placeholder;
return [super alloc];
}
+ (instancetype)setWithCapacity: (size_t)capacity
{
return [[[self alloc] initWithCapacity: capacity] autorelease];
}
- (instancetype)init
{
if ([self isMemberOfClass: [OFMutableSet class]]) {
@try {
[self doesNotRecognizeSelector: _cmd];
abort();
} @catch (id e) {
[self release];
return [super init];
@throw e;
}
}
}
#ifdef __clang__
/* We intentionally don't call into super, so silence the warning. */
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-pragmas"
# pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#endif
- (instancetype)initWithObjects: (id const *)objects count: (size_t)count
{
return [super init];
OF_INVALID_INIT_METHOD
}
- (instancetype)initWithCapacity: (size_t)capacity
{
OF_INVALID_INIT_METHOD
}
#ifdef __clang__
# pragma clang diagnostic pop
#endif
- (id)copy
{
return [[OFSet alloc] initWithSet: self];
}
- (void)addObject: (id)object
|
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
-
+
|
cArray = OFAllocMemory(count, sizeof(id));
@try {
size_t i;
i = 0;
for (id object in self) {
assert(i < count);
OFAssert(i < count);
cArray[i++] = object;
}
for (i = 0; i < count; i++)
if (![set containsObject: cArray[i]])
[self removeObject: cArray[i]];
} @finally {
|