Radix Class
Radix Class
- Subject: Radix Class
- From: Jordan Evans <email@hidden>
- Date: Sun, 8 Jan 2006 21:52:48 -0800 (PST)
Lester wrote:
1. The extra effort to write clear documentation is
well rewarded. ...
2. You can keep the struct radixSet out of the
interface file and place
it in the implementation file. ...
3. Try to refactor the three methods allSequences,
sequencesInRange,
and sequence. Keeping your key algorithm in one place
helps enormously
with debugging. ...
Thank you Lester. I made those changes.
Any criticism of this Radix is welcomed from anyone
else. (This is not a homework assignment, but if
there is a Cocoa Objective-C College, where do I get a
student loan?)
//
// 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 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, 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 corresponds 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
{
int numOfSets;//Number of NSArrays in NSArray sent.
int possibilities; //This is the total number of
possible sequences that can be produced.
struct radixSet *sets; //This structure pointer is
used to point to an array of structs. Each struct
represents facts about each set(NSArray).
}
-initWithSets:(NSArray *)arrayOfSets;
-(int)possibilities;
-(NSArray *)allSequences;
-(NSArray *)sequence: (int)requestedSequence;
-(NSArray *)sequencesInRange:(NSRange)range;
@end
@implementation Radix
struct radixSet
{
id setPtr;//A pointer to an NSArray. This NSArray is
a set belonging to arrayOfSets.
int element;//Used to point to an element in a set.
Each element number is the postion it resides in the
array. This number is assinged to element, if that
particular element is under query.
int omegaElement;//Is the number of the postion of
the last element(object) in an array set. Is assigned
once. Is used for carry operations. I.e., in base
ten, when nine is hit, zero is replaced. Nine is
omega element for base ten.
int radix;//Each set has a base. This is based on
the number of elements(objects) in the set(NSArray).
};
-(int)possibilities
{
return possibilities;
}
-initWithSets:(NSArray *)arrayOfSets
{
self = [super init];
if (self != nil)
{
int i;
numOfSets = [arrayOfSets count];
possibilities = 1;
sets = (struct radixSet *)malloc (sizeof(struct
radixSet)*numOfSets);
for(i=0;i<numOfSets;i++)
{
//Each element is set to the alpha element, which
is the first object of a NSArray.
sets->element = 0;
//Each setPtr is set to it's respective NSArray.
sets->setPtr = [arrayOfSets objectAtIndex:i];
//Each base is set, based upon the NSArray count.
sets->radix = [[arrayOfSets objectAtIndex:i]
count];
//Each omega element is also set based on the
NSArray count.
sets->omegaElement = [sets->setPtr count]-1;
//possibilities is the product of all bases of the
sets(NSArrays).
possibilities *= (sets->omegaElement + 1);
sets++;
}
sets -= numOfSets;
}
return self;
}
- (void)dealloc;
{
free(sets);
[super dealloc];
}
-(NSArray *)allSequences
{
NSRange seqRange;
seqRange.location = 0;
seqRange.length = possibilities;
return [self sequencesInRange:seqRange];
}
-(NSArray *)sequence: (int)requestedSequence
{
NSMutableArray *sequenceArray = [[NSMutableArray
alloc] init];
int i = 0;
while(requestedSequence)
{
//Converts the requestedSequence integer into a
multi-base number. At the same time, advances the
element of each set. This advance of element is
determined by the varing radix.
sets[i].element = requestedSequence %
sets[i].radix;
requestedSequence /= sets[i].radix;
i++;
}
//Adds appropriate object to array, depending upon
where element was set by previous section.
for( i=0; i<numOfSets; i++ )
[sequenceArray addObject:[sets[i].setPtr
objectAtIndex:sets[i].element] ];
return sequenceArray;
}
-(NSArray *)sequencesInRange:(NSRange)range
{
int i, v, sequence;
NSMutableArray *sequenceArray, *returnArray;
sequenceArray = [[NSMutableArray alloc] init];
returnArray = [[NSMutableArray alloc] init];
i = 0;v = 0;sequence = 0;
//Does what -sequence does above, for the purpose of
getting sequence up to the starting line.
while(range.location)
{
sets[i].element = range.location % sets[i].radix;
range.location /= sets[i].radix;
i++;
}
for( i=0; i<numOfSets; i++ )
[sequenceArray addObject:[sets[i].setPtr
objectAtIndex:sets[i].element] ];
//Releases object so objects who request sequences
have responsibility for memory of object.
[returnArray addObject:sequenceArray];
[[returnArray lastObject] release];
do
{
do
{
//This is the carry implemention, as in math, carry
a one.
if( sets[v].element == sets[v].omegaElement )
{
//When a carry occurs, element must be set to
alpha element, which is zero.
sets[v].element = 0;
//The sets must be advanced, so the next set may
increment. This occurs in the next section.
v++;
}
//If a set is at it's omega element, go back and
carry.
}while( sets[v].element == sets[v].omegaElement );
//Increment the present set. Element will now refer
to next element(object).
sets[v].element += 1;
//Zero out the set. It will remain the zero set
around the outer-most do loop, unless a carry
operation needs to occur. If not, the zero set is
always incremented. If there is a carry, the last set
that is not at it's omega element will be incremented
and at that point the set needs to be zeroed, so
incrementation begins in the one's column again.
(One's column, as in math).
v = 0;
//Increase the number of sequences.
sequence++;
//All sequences have been added, break. This is
here because the next section would inappropriately
allocate an extra sequence.
if(sequence == (range.location + range.length) )
break;
//Allocate an array so a new sequence can be added.
sequenceArray = [[NSMutableArray alloc] init];
//Add appropriate objects based upon element
position in each set.
for( i=0; i<numOfSets; i++ )
[sequenceArray addObject: [sets[i].setPtr
objectAtIndex:sets[i].element] ];
[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 *string1 = [NSString stringWithString:@"0"];
NSString *string2 = [NSString stringWithString:@"1"];
NSString *string3 = [NSString stringWithString:@"2"];
NSArray *set1 = [NSArray arrayWithObjects: string1,
string2, string3, nil];
NSString *string4 = [NSString stringWithString:@"0"];
NSString *string5 = [NSString stringWithString:@"1"];
NSArray *set2 = [NSArray arrayWithObjects: string4,
string5, nil];
NSString *string6 = [NSString stringWithString:@"0"];
NSString *string7 = [NSString stringWithString:@"1"];
NSArray *set3 = [NSArray arrayWithObjects: string6,
string7, nil];
NSString *string8 = [NSString stringWithString:@"0"];
NSString *string9 = [NSString stringWithString:@"1"];
NSArray *set4 = [NSArray arrayWithObjects: string8,
string9, nil];
Radix *myRadix = [[Radix alloc] initWithSets:[NSArray
arrayWithObjects: set1, set2, set3, set4, nil]];
NSArray *recSeqArray;
NSRange seqRange;
seqRange.location = 9;//Location here is based on
array notation. Zero indicates first sequence.
seqRange.length = 10;
/* 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