Radix Class
Radix Class
- Subject: Radix Class
- From: Jordan Evans <email@hidden>
- Date: Sun, 8 Jan 2006 17:48:20 -0800 (PST)
Here is a cleaned up version of Radix. I was looking
over the last one and realized I should have waited to
post it. Sorry for that messiness. This one is alot
cleaner. Any suggestions welcomed. Wrong
conventions? Need other methods? Memory leak? Bad
variable names? Even just a one thing that could be
improved would help. Thank you.
//
// Radix.h
// RadixProject
//
// Created by Jordan Evans on 12/1/05.
// Copyright 2005 XRADIX All rights reserved.
//
//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 objects, which is
made up of one element from each set.
/*
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 number of objects in the NSArray
that forms the set is the radix of that set. Sets are
added to a arrayOfSets(NSArray). Once this is done,
an arrayOfSets may be queried for a sequence, all
sequences or a range of sequences. In all cases, a
NSArray is returned. If a single sequence is asked
for, the integer sent to -sequence: that corresponds
with that unique sequence of elements will be returned
as a NSArray. If all sequences are asked for or a
range of sequences is asked for, then a NSArray with
NSArrays is returned. Each sub-NSArray contains a
unique sequence of elements. In the case of
-allSequences, every possible element sequence is
returned in the sub-NSArrays. In the case of
-sequencesInRange: each sub-NSArray contains a unique
element sequence, where each sub-NSArray returned
correspond with values in the NSRange sent.
The memory of the NSArray returned is the
responsibility of the objects that request the
NSArray.
*/
#import <Foundation/Foundation.h>
@interface Radix : NSObject
{
id arraySource;
int possibilities;
struct radixSet
{
id setPtr;
int element;
int omegaElement;
int radix;
}radixSet;
struct radixSet *sets;
}
-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);
for(i=0;i<count;i++)
{
sets->element = 0;
sets->setPtr = [arrayOfSets objectAtIndex:i];
sets->radix = [[arrayOfSets objectAtIndex:i]
count];
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, v, count, sequence;
NSMutableArray *sequenceArray, *returnArray;
returnArray = [[NSMutableArray alloc] init];
count = [arraySource count];
v = 0;sequence = 0;
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;
count = [arraySource count];
i = 0;
while(requestedSequence)
{
sets[i].element = requestedSequence %
sets[i].radix;
requestedSequence /= sets[i].radix;
i++;
}
for( i=0; i<count; i++ )
[sequenceArray addObject:[sets[i].setPtr
objectAtIndex:sets[i].element] ];
return sequenceArray;
}
-(NSArray *)sequencesInRange:(NSRange)range
{
int i, v, count, sequence;
NSMutableArray *sequenceArray, *returnArray;
sequenceArray = [[NSMutableArray alloc] init];
returnArray = [[NSMutableArray alloc] init];
count = [arraySource count];
i = 0;v = 0;sequence = 0;
while(range.location)
{
sets[i].element = range.location % sets[i].radix;
range.location /= sets[i].radix;
i++;
}
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] ];
if(sequence == (range.location + range.length) )
{
[sequenceArray release];
break;
}
[returnArray addObject:sequenceArray];
[[returnArray lastObject] release];
}
while( sequence != (range.location + range.length) );
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]];
NSArray *recSeqArray;
NSRange seqRange;
seqRange.location = 2;
seqRange.length = 3;
/* THIS IS FOR -sequence:
recSeqArray = [myRadix sequence:3];
printf("One Sequence: \n%s\n", [[recSeqArray
description] cString] );
*/
/* THIS IS FOR -allSequences
recSeqArray = [myRadix allSequences];
printf("All Sequences: \n%s\n", [[recSeqArray
description] cString] );
*/
// THIS IS FOR -sequencesInRange:
recSeqArray = [myRadix sequencesInRange:seqRange];
printf("A Range of Sequences: \n%s\n", [[recSeqArray
description] cString] );
//
[myRadix release];
[recSeqArray release];
[pool release];
return 0;
}
__________________________________________
Yahoo! DSL Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
_______________________________________________
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