Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1 | // Copyright (c) 2011 Google Inc. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #import "ObjCVectorInternal.h" |
| 6 | #import "ObjCVector.h" |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | @interface ObjCVector (Private) |
| 11 | - (std::vector<id>::iterator)makeIterator:(NSUInteger)index; |
| 12 | @end |
| 13 | |
| 14 | @implementation ObjCVector |
| 15 | |
| 16 | - (id)init { |
| 17 | if ((self = [super init])) { |
| 18 | imp_ = new ObjCVectorImp(); |
| 19 | } |
| 20 | return self; |
| 21 | } |
| 22 | |
| 23 | - (void)dealloc { |
| 24 | delete imp_; |
| 25 | [super dealloc]; |
| 26 | } |
| 27 | |
| 28 | - (void)addObject:(id)obj { |
| 29 | imp_->v.push_back([obj retain]); |
| 30 | } |
| 31 | |
| 32 | - (void)addObject:(id)obj atIndex:(NSUInteger)index { |
| 33 | imp_->v.insert([self makeIterator:index], [obj retain]); |
| 34 | } |
| 35 | |
| 36 | - (void)removeObject:(id)obj { |
| 37 | for (std::vector<id>::iterator it = imp_->v.begin(); |
| 38 | it != imp_->v.end(); |
| 39 | ++it) { |
| 40 | if ([*it isEqual:obj]) { |
| 41 | [*it autorelease]; |
| 42 | imp_->v.erase(it); |
| 43 | return; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | - (void)removeObjectAtIndex:(NSUInteger)index { |
| 49 | [imp_->v[index] autorelease]; |
| 50 | imp_->v.erase([self makeIterator:index]); |
| 51 | } |
| 52 | |
| 53 | - (id)objectAtIndex:(NSUInteger)index { |
| 54 | return imp_->v[index]; |
| 55 | } |
| 56 | |
| 57 | - (std::vector<id>::iterator)makeIterator:(NSUInteger)index { |
| 58 | std::vector<id>::iterator it = imp_->v.begin(); |
| 59 | it += index; |
| 60 | return it; |
| 61 | } |
| 62 | |
| 63 | @end |