Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

Thread: Vector Wapper
View Single Post
Old 11-09-2009, 05:30 AM   #1 (permalink)
encryption
Registered Member
 
Join Date: Sep 2009
Posts: 48
encryption is on a distinguished road
Cool Vector Wapper

Hello,

I have just finished the long process of porting a c++ application to objective-c / Cocoa.

To make it easier on me, yeah right, I created an objective-c vector class. It has the following:

Constructors
PHP Code:
[[v allocinit]; // Default constructor
// Can specify an initial capacity
[[v allocinitWithVSize:10]; 
Iterators
PHP Code:
// iterators are currently of all type NSInteger * 
for (int *ptr = [v begin]; ptr < [v end]; ++ptr)

start = [v2 begin];
end = [v2 end] - 3
Capacity
PHP Code:
// returns the current size of a vector
curSize = [v size];

// returns the current  capacity of a vector
curCap  = [v capacity];

// resizes a vector and fills it with value specified
[v resize:20 withFill:100];

[
v reserve:30]; // request a change in capacity

[empty] // returns BOOL value 
Element Access
PHP Code:
// Access value at specified element
value = [v at:2];  

// returns value of the front element
value = [v front];

// returns value of the back element
if ( [v back] > 10 
Modifiers
PHP Code:
// Assigns 15 elements the value 25
[v assign:15 value:25];

// iterators used to demonstrate assign
start = [v2 begin];
end = [v2 end] - 3;

// uses range for assignment
[v assign:start last:end];

// element added at end of vector with value
[v push_back:15];

// Removes the last element
[v pop_back];

// Inserts value 20 at loc
insertLoc = [v insert:loc value:20];

// Inserts 5 elements starting at loc with value
[v insert:loc num:5 value:20]; 

// Copies of the elements in the range are
// inserted at position loc
[v insert:loc first:[v2 begin] + 3 last:[v2 end]]; 

// Erase single element
// Returns loc of element that follows it
pos = [v erase:[v begin]];

// Erase the range specified
pos = [v erase:[v end] -3 last:[v end]];

// Exchanges the content of a vector
// with another (i.e. v with v2)
[v swap:v2]; 

// All the elements of the vector v are dropped
[v clear]; 

Observations / thoughts

This currently only handles storage for the primitive data type NSInteger. This is how the default constructor would look like in c++: Vector<NSInteger> v1;

I plan on creating code that allows for various other data types: the other primitive types, NSNumber, then code a vector based generic sequence object container (similar to NSMutable). But, likely each type will be a seprate class since I don't think it is possible to use templates for abstract data types like c++ does and if one cannot get that benefit then I think it is better to keep each class as small as possible so you can include only the specific variant you need.

Since this uses NSInteger pointers, for iterators, and dynamic c arrays, for storage, this class is vastly superior, in terms of speed, compared to any of the NSArray options. If you are dealing with numbers this might be an option for you.

I would like to see more of the STL, Standard Template Library, ported to objective-c. This way we can have the best of both worlds.

*Notes:
Operator overloading is not permited in objective-c. For this reason I cannot provide the following: Iterators (rbegin, rend) or Operators (=, []).

I couldn't determine how to compute max_size. Anyone knowing how this is done please let me know.

Any suggestions and / or comments would be appreciated. If anyone is interested in obtaining the code let me know and I'll make it available.

Thanks for reading,

Nick Powers

Below is the methods from my header file:

PHP Code:
// public methods prototypes
- (id)init;
- (
id)initWithVSize:(NSInteger)vSize;

// Iterators
- (NSInteger *)begin;
- (
NSInteger *)end;

// Capacity
- (NSInteger)size;
- (
void)resize:(NSInteger)num withFill:(NSInteger)fill;
- (
NSInteger)capacity;
- (BOOL)empty;
- (
void)reserve:(NSInteger)size;

// Element access
- (NSInteger)at:(NSInteger)loc;
- (
NSInteger)front;
- (
NSInteger)back;

// Modifiers
- (void)assign:(NSInteger *)first last:(NSInteger *)last;
- (
void)assign:(NSInteger)num value:(NSInteger)value;
- (
void)push_back:(NSInteger)value;
- (
void)pop_back;
- (
NSInteger *)insert:(NSInteger *)loc value:(NSInteger)value;
- (
void)insert:(NSInteger *)loc num:(NSInteger)num value:(NSInteger)value;
- (
void)insert:(NSInteger *)loc first:(NSInteger *)first last:(NSInteger *)last;
- (
NSInteger *)erase:(NSInteger *)loc;
- (
NSInteger *)erase:(NSInteger *)first last:(NSInteger *)last;
- (
void)swap:(Vector *)vec;
- (
void)clear
encryption is offline   Reply With Quote
 

» Advertisements
» Online Users: 565
9 members and 556 guests
Domele, matt2009, Rudy, skiril, skrew88, SLIC, st02197, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,319
Threads: 93,988
Posts: 402,409
Top Poster: BrianSlick (7,978)
Welcome to our newest member, st02197
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:33 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.