Radix Class, A Request for Advice
Radix Class, A Request for Advice
- Subject: Radix Class, A Request for Advice
- From: Jordan Evans <email@hidden>
- Date: Sat, 7 Jan 2006 22:34:04 -0800 (PST)
I'm new to Cocoa. I'm trying to keep all the
conventions. But for a beginner, this isn't always
easy. This is not a homework assignment, I actually
like programming and have some high ideals, but I do
have to master the basics. I want to thank everyone
who helped me on the Engine Class. I would have paid
money for that help, but you guys are so cool, you
didn't even ask.
This is my first serious class. It's in the making.
I've pushed it this far. Anyone who has the time and
interest, please take a look and give me some input.
I think it will be a great utility class when it's all
done. But of course, I'm new, so at finishing it,
I'll need some direction. Even just a small detail
of input on any particular area would be greatly
appreciated. Thanks guys.
I pasted it here, but the files are also attached for
a foundation project.
//
// Radix.h
// RadixProject
//
// Created by Jordan Evans on 12/1/05.
// Copyright 2005 XRADIX All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Radix : NSObject
{
id arraySource;
int omegaElement;
int sequence;
int possibilities;
int currentSet;
int v;
char *sequenceString;
struct radixSet
{
int element;
int omegaElement;
int radix;
BOOL setIsString;
id setPtr;
};
struct radixSet *sets;
}
//To help describe the purpose of this class, these
terms are defined and used consistently in the
program:
//element - an object
//set- a group of elements
//arrayOfSets - a group of sets
//sequence - a unique combination of one element from
each set.
/*
THIS IS NOT A HOMEWORK ASSIGNMENT. I LOVE THIS DOING
THIS STUFF.
The purpose of the Radix class is to allow the
programmer to create every possible combination of a
sequence of objects. This could be used for
debugging, creativity, logic analysis, decryption and
encryption, etc.
A programmer must create their own sets and put those
sets into a NSArray. Sets are made by putting objects
into an NSArray. The NSArray is a set in terms of
this class. The number of objects in the NSArray that
forms the set is the base or radix of that set. Sets
are added to a NSArray, which is termed above the
arrayOfSets. Once this is done, a arrayOfSets NSArray
may be queried for a sequence, all sequences or a
range of sequences. In all cases, an NSArray is
returned. If a single sequence is asked for, the
integer sent to -sequence: that corresponds with that
unique sequence of elements(object pointers) will be
returned as an NSArray. If all sequences are asked
for or a range of sequences, then a NSArray with
NSArray's is returned. In Each sub-NSArray contains a
unique sequence of elements. In the case of a
-allSequences, every possible element sequence is
returned in the sub-NSArray's. In the case of
-sequenceInRange: each sub-NSArray contains a unique
element sequence which corresponds with values in the
NSRange sent.
The memory of the NSArray returned are the
responsibility of the objects that request the
NSArray.
*/
-initWithSets:(NSArray *)arrayOfSets;
-(int)possibilities;
-(NSArray *)allSequences;
-(NSArray *)sequence: (int)requestedSequence;
-(NSArray *)sequencesInRange:(NSRange)range;
@end
@implementation Radix
-(int)possibilities
{
return possibilities;
}
-initWithSets:(NSArray *)arrayOfSets
{
self = [super init];
if (self != nil)
{
int i, count;
possibilities = 1;
arraySource = arrayOfSets;
count = [arrayOfSets count];
sets = (struct radixSet *)malloc (sizeof(struct
radixSet)*count);
id lastObject;
for(i=0;i<count;i++)
{
sets->element = 0;
sets->setPtr = [arrayOfSets objectAtIndex:i];
sets->radix = [[arrayOfSets objectAtIndex:i]
count];
lastObject = [sets->setPtr lastObject];
if([sets->setPtr isKindOfClass:[NSString class]])
sets->omegaElement = [sets->setPtr count]-1;
else
sets->omegaElement = [sets->setPtr count]-1;
possibilities *= (sets->omegaElement + 1);
sets++;
}
sets -= count;
}
return self;
}
- (void)dealloc;
{
free(sets);
[super dealloc];
}
-(NSArray *)allSequences
{
int i, count;
NSMutableArray *sequenceArray;
NSMutableArray *returnArray = [[NSMutableArray alloc]
init];
count = [arraySource count];
if(possibilities)
{
do
{
do
{
if( sets[v].element == sets[v].omegaElement )
{
sets[v].element = 0;
v++;
}
}while( sets[v].element == sets[v].omegaElement );
sets[v].element += 1;
v = 0;
sequence++;
sequenceArray = [[NSMutableArray alloc] init];
for( i=0; i<count; i++ )
[sequenceArray addObject: [sets[i].setPtr
objectAtIndex:sets[i].element] ];
[returnArray addObject:sequenceArray];
[[returnArray lastObject] release];
if(sequence == possibilities)
break;
}
while( sequence != possibilities );
}
return returnArray;
}
-(NSArray *)sequence: (int)requestedSequence
{
NSMutableArray *sequenceArray = [[NSMutableArray
alloc] init];
int count, i;
i = 0;
while(requestedSequence)
{
sets[i].element = requestedSequence %
sets[i].radix;
requestedSequence /= sets[i].radix;
i++;
}
count = [arraySource count];
for( i=0; i<count; i++ )
[sequenceArray addObject:[sets[i].setPtr
objectAtIndex:sets[i].element] ];
return sequenceArray;
}
-(NSArray *)sequencesInRange:(NSRange)range
{
int i, count;
NSMutableArray *sequenceArray = [[NSMutableArray
alloc] init];
NSMutableArray *returnArray = [[NSMutableArray alloc]
init];
count = [arraySource count];
i = 0;
while(range.location)
{
sets[i].element = range.location % sets[i].radix;
range.location /= sets[i].radix;
i++;
}
count = [arraySource count];
for( i=0; i<count; i++ )
[sequenceArray addObject:[sets[i].setPtr
objectAtIndex:sets[i].element] ];
[returnArray addObject:sequenceArray];
[[returnArray lastObject] release];
do
{
do
{
if( sets[v].element == sets[v].omegaElement )
{
sets[v].element = 0;
v++;
}
}while( sets[v].element == sets[v].omegaElement );
sets[v].element += 1;
v = 0;
sequence++;
sequenceArray = [[NSMutableArray alloc] init];
for( i=0; i<count; i++ )
[sequenceArray addObject: [sets[i].setPtr
objectAtIndex:sets[i].element] ];
[returnArray addObject:sequenceArray];
[[returnArray lastObject] release];
if(sequence == possibilities)
break;
}
while( sequence != possibilities );
return returnArray;
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]
init];
NSString *num1 = [NSString stringWithString:@"0"];
NSString *num2 = [NSString stringWithString:@"1"];
NSArray *set1 = [NSArray arrayWithObjects: num1,
num2, nil];
NSString *num4 = [NSString stringWithString:@"0"];
NSString *num5 = [NSString stringWithString:@"1"];
NSArray *set2 = [NSArray arrayWithObjects: num4,
num5, nil];
NSString *num7 = [NSString stringWithString:@"0"];
NSString *num8 = [NSString stringWithString:@"1"];
NSArray *set3 = [NSArray arrayWithObjects: num7,
num8, nil];
Radix *myRadix = [[Radix alloc] initWithSets:[NSArray
arrayWithObjects: set1, set2, set3, nil]];
int i, j;
NSArray *recSeqArray;
int count = 3 - 1; //three sets
int max = [myRadix possibilities];
id firstArray;
// THIS IS FOR -sequence:
recSeqArray = [myRadix sequence:5];
[myRadix release];
for( j=count; j>=0; j-- )
printf("%s", [[recSeqArray objectAtIndex:j]
cString]);
//
/* THIS IS FOR -allSequences
recSeqArray = [myRadix allSequences];
firstArray = [recSeqArray objectAtIndex:0];
for( i=1; i<max+1; i++ )
{
printf("seq# %i: ", i);
for( j=count; j>=0; j-- )
printf("%s",[[firstArray objectAtIndex:j]
cString]);
printf("\n");
if( i < (max) )
firstArray = [recSeqArray objectAtIndex:i];
}
*/
/* THIS IS FOR -sequencesInRange:
NSRange seqRange;
seqRange.location = 2;
seqRange.length = 4;
recSeqArray = [myRadix sequencesInRange:seqRange];
firstArray = [recSeqArray objectAtIndex:0];
int sequ = seqRange.location;
for( i=1; i<seqRange.length+1; i++ )
{
printf("seq# %i: ", sequ);
for( j=count; j>=0; j-- )
printf("%s",[[firstArray objectAtIndex:j]
cString]);
printf("\n");
sequ++;
if( i < (max) )
firstArray = [recSeqArray objectAtIndex:i];
}
*/
[recSeqArray release];
[pool release];
return 0;
}
__________________________________________
Yahoo! DSL Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
Attachment:
Radix.h
Description: 2882819253-Radix.h
Attachment:
Radix.m
Description: 490977933-Radix.m
Attachment:
Radix2.0.m
Description: 249233820-Radix2.0.m
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden