blob: cbf431f28d4d9d1473912e7317d2c2f4ecad92be (
plain)
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
|
// Copyright (c) 2011 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ObjCVectorInternal.h"
#import "ObjCVector.h"
#include <vector>
@interface ObjCVector (Private)
- (std::vector<id>::iterator)makeIterator:(NSUInteger)index;
@end
@implementation ObjCVector
- (id)init {
if ((self = [super init])) {
imp_ = new ObjCVectorImp();
}
return self;
}
- (void)dealloc {
delete imp_;
[super dealloc];
}
- (void)addObject:(id)obj {
imp_->v.push_back([obj retain]);
}
- (void)addObject:(id)obj atIndex:(NSUInteger)index {
imp_->v.insert([self makeIterator:index], [obj retain]);
}
- (void)removeObject:(id)obj {
for (std::vector<id>::iterator it = imp_->v.begin();
it != imp_->v.end();
++it) {
if ([*it isEqual:obj]) {
[*it autorelease];
imp_->v.erase(it);
return;
}
}
}
- (void)removeObjectAtIndex:(NSUInteger)index {
[imp_->v[index] autorelease];
imp_->v.erase([self makeIterator:index]);
}
- (id)objectAtIndex:(NSUInteger)index {
return imp_->v[index];
}
- (std::vector<id>::iterator)makeIterator:(NSUInteger)index {
std::vector<id>::iterator it = imp_->v.begin();
it += index;
return it;
}
@end
|