Code:

//
// Radix.h
// RadixProject
//
// Created by Jordan Evans on 12/1/05.
// Copyright 2005 XRADIX All rights reserved.
//
#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).
}
-(id)initWithSets:(NSArray *)arrayOfSets;
-(void)zeroElements;
-(void)initRadixVariables:(NSArray *)arrayOfSets;
-(int)possibilities;
-(void)freeSets;
-(void)reInitWithArray:(NSArray *)arrayOfSets;
-(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;
}
-(id)initWithSets:(NSArray *)arrayOfSets
{
self = [super init];
if (self != nil)
{
[self initRadixVariables:arrayOfSets];
}
return self;
}
-(void)zeroElements
{
//Reinitialize all elements to alpha position.
int i;
for(i=0;i<numOfSets;i++)
{
sets->element = 0;
sets++;
}
sets -= numOfSets;
}
-(void)freeSets
{
free(sets);
}
-(void)initRadixVariables:(NSArray *)arrayOfSets
{
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;
}
-(void)reInitWithArray:(NSArray *)arrayOfSets
{
[self freeSets];
[self initRadixVariables:arrayOfSets];
}
- (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] ];
[self zeroElements];
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) );
[self zeroElements];
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]];
NSString *string10 = [NSString stringWithString:@"0"];
NSString *string11 = [NSString stringWithString:@"1"];
NSArray *set5 = [NSArray arrayWithObjects: string10, string11, nil];
[set5 retain];
NSString *string12 = [NSString stringWithString:@"0"];
NSString *string13 = [NSString stringWithString:@"1"];
NSArray *set6 = [NSArray arrayWithObjects: string12, string13, nil];
[set6 retain];
NSMutableArray *newDataSource = [[NSMutableArray alloc] initWithObjects: set5, set6, nil];
NSArray *recSeqArray;
NSRange seqRange;
seqRange.location = 9;//Location here is based on array notation. Zero indicates first sequence.
seqRange.length = 6;//Length here is not in array notation. Is number of sequences wanted.
// THIS IS FOR -sequence:
recSeqArray = [myRadix sequence:3];
printf("One Sequence: \n%s\n", [[recSeqArray description] cString] );
[recSeqArray release];
// THIS IS FOR -allSequences
recSeqArray = [myRadix allSequences];
printf("All Sequences: \n%s\n", [[recSeqArray description] cString] );
[recSeqArray release];
// THIS IS FOR -sequencesInRange:
recSeqArray = [myRadix sequencesInRange:seqRange];
printf("A Range of Sequences: \n%s\n", [[recSeqArray description] cString] );
[recSeqArray release];
// THIS IS FOR -reInitWithArray:, This gives capacity for real-time encryption to occur.
[myRadix reInitWithArray:newDataSource];
recSeqArray = [myRadix allSequences];
printf("New Source -allSequences: \n%s\n", [[recSeqArray description] cString] );
[recSeqArray release];
recSeqArray = [myRadix sequence:1];
printf("One Sequence: \n%s\n", [[recSeqArray description] cString] );
[recSeqArray release];
recSeqArray = [myRadix sequence:0];
printf("One Sequence: \n%s\n", [[recSeqArray description] cString] );
[recSeqArray release];
[newDataSource release];
[myRadix release];
[pool release];
return 0;
}