-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbWeakArray.m
More file actions
96 lines (79 loc) · 1.62 KB
/
DbWeakArray.m
File metadata and controls
96 lines (79 loc) · 1.62 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
//
// DbWeakArray.m
// DbBaseUtils
//
// Created by Qiang Huang on 6/2/15.
// Copyright (c) 2015 Sudobility. All rights reserved.
//
#import "DbWeakArray.h"
@interface DbWeakArray()
{
NSPointerArray * _array;
}
@end
@implementation DbWeakArray
+ (instancetype)array
{
return [[DbWeakArray alloc] init];
}
- (id)init
{
if (self = [super init])
{
_array = [NSPointerArray weakObjectsPointerArray];
return self;
}
return nil;
}
- (void)compact
{
for (NSInteger i = self.count - 1; i >= 0; i --)
{
id obj = [self objectAtIndex:i];
if (!obj)
{
[self removeObjectAtIndex:i];
}
}
}
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
{
[_array insertPointer:(__bridge void *)anObject atIndex:index];
}
- (void)removeObjectAtIndex:(NSUInteger)index
{
[_array removePointerAtIndex:index];
}
- (void)addObject:(id)anObject
{
[_array addPointer:(__bridge void *)anObject];
}
- (void)removeLastObject
{
[self removeObjectAtIndex:self.count - 1];
}
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{
[_array replacePointerAtIndex:index withPointer:(__bridge void *)anObject];
}
- (NSUInteger)count
{
return [_array count];
}
- (id)objectAtIndex:(NSUInteger)index
{
return (__bridge id)[_array pointerAtIndex:index];
}
- (NSUInteger)indexOfObject:(id)anObject
{
NSUInteger found = NSNotFound;
for (NSInteger i = 0; found == NSNotFound && i < self.count; i ++)
{
if ([self objectAtIndex:i] == anObject)
{
found = i;
}
}
return found;
}
@end