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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-09-2009, 06:30 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 48
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
Old 11-09-2009, 08:54 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 215
Default

How does the speed of this compare to using the fast enumeration of arrays? And what about the Core Foundation counterparts? I'm all for better performance when it is critical, so your work could benefit a lot of others as well.

Regarding adding support for other data types, for example NSString is implemented using class clusters, so maybe that would be an option for you if you want to create a generic vector container. I'm not really knowledgeable about class clusters, so my suggestion might be totally inappropriate.
fiftysixty is offline   Reply With Quote
Old 11-09-2009, 09:54 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 48
Thumbs up will find out, plus added remaining constructors

I'll check out the performance metrics you asked about and post the results.

Also, I added the remaining constructors. Here is all the constructor prototypes:

PHP Code:
- (id)init;
- (
id)initWithVSize:(NSInteger)vSize;
- (
id)initWithVSizeAndVal:(NSInteger)vSize val:(NSInteger)value;
- (
id)initCopyhVector:(Vector *)vec;
- (
id)initWithRange:(NSInteger *)first last:(NSInteger *)last
My class has 5 constructors whereas the C++ STL only has 4. This is because I am not sure how, if possible, to make the value parameter optional. Therefor, I have one that just requires a size and another that requires both size and value. So, it's really the same.

Thanks,

Nick Powers
encryption is offline   Reply With Quote
Reply

Bookmarks

Tags
array, stl`, vectors, wrapper

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 286
19 members and 267 guests
2WeeksToGo, ADY, Creativ, dacapo, Dani77, Fritzer, ghost, HDshot, headkaze, iDifferent, mer10, mystic.purple, Rudy, smethorst, stoneage, superg, tathaastu, Thompson22, Zool
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,878
Threads: 89,224
Posts: 380,732
Top Poster: BrianSlick (7,129)
Welcome to our newest member, olga2000
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:43 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0