diff --git a/dna.xcodeproj/project.pbxproj b/dna.xcodeproj/project.pbxproj new file mode 100644 index 00000000..0a07ddcc --- /dev/null +++ b/dna.xcodeproj/project.pbxproj @@ -0,0 +1,249 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + FF03F3F016542F9B007FDAA5 /* NSMutableArray (Shuffling).m in Sources */ = {isa = PBXBuildFile; fileRef = FF03F3EF16542F9B007FDAA5 /* NSMutableArray (Shuffling).m */; }; + FFCA88A316502BB9009AB98C /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FFCA88A216502BB9009AB98C /* Foundation.framework */; }; + FFCA88A616502BB9009AB98C /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = FFCA88A516502BB9009AB98C /* main.m */; }; + FFCA88AA16502BB9009AB98C /* dna.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = FFCA88A916502BB9009AB98C /* dna.1 */; }; + FFCA88B216502C2F009AB98C /* Cell.m in Sources */ = {isa = PBXBuildFile; fileRef = FFCA88B116502C2F009AB98C /* Cell.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + FFCA889C16502BB9009AB98C /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + FFCA88AA16502BB9009AB98C /* dna.1 in CopyFiles */, + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + FF03F3EE16542F9B007FDAA5 /* NSMutableArray (Shuffling).h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableArray (Shuffling).h"; sourceTree = ""; }; + FF03F3EF16542F9B007FDAA5 /* NSMutableArray (Shuffling).m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableArray (Shuffling).m"; sourceTree = ""; }; + FFCA889E16502BB9009AB98C /* dna */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = dna; sourceTree = BUILT_PRODUCTS_DIR; }; + FFCA88A216502BB9009AB98C /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + FFCA88A516502BB9009AB98C /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + FFCA88A816502BB9009AB98C /* dna-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "dna-Prefix.pch"; sourceTree = ""; }; + FFCA88A916502BB9009AB98C /* dna.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = dna.1; sourceTree = ""; }; + FFCA88B016502C2F009AB98C /* Cell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cell.h; sourceTree = ""; }; + FFCA88B116502C2F009AB98C /* Cell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Cell.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + FFCA889B16502BB9009AB98C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + FFCA88A316502BB9009AB98C /* Foundation.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + FFCA889316502BB9009AB98C = { + isa = PBXGroup; + children = ( + FFCA88A416502BB9009AB98C /* dna */, + FFCA88A116502BB9009AB98C /* Frameworks */, + FFCA889F16502BB9009AB98C /* Products */, + ); + sourceTree = ""; + }; + FFCA889F16502BB9009AB98C /* Products */ = { + isa = PBXGroup; + children = ( + FFCA889E16502BB9009AB98C /* dna */, + ); + name = Products; + sourceTree = ""; + }; + FFCA88A116502BB9009AB98C /* Frameworks */ = { + isa = PBXGroup; + children = ( + FFCA88A216502BB9009AB98C /* Foundation.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + FFCA88A416502BB9009AB98C /* dna */ = { + isa = PBXGroup; + children = ( + FFCA88A516502BB9009AB98C /* main.m */, + FFCA88B016502C2F009AB98C /* Cell.h */, + FF03F3EE16542F9B007FDAA5 /* NSMutableArray (Shuffling).h */, + FF03F3EF16542F9B007FDAA5 /* NSMutableArray (Shuffling).m */, + FFCA88B116502C2F009AB98C /* Cell.m */, + FFCA88A916502BB9009AB98C /* dna.1 */, + FFCA88A716502BB9009AB98C /* Supporting Files */, + ); + path = dna; + sourceTree = ""; + }; + FFCA88A716502BB9009AB98C /* Supporting Files */ = { + isa = PBXGroup; + children = ( + FFCA88A816502BB9009AB98C /* dna-Prefix.pch */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + FFCA889D16502BB9009AB98C /* dna */ = { + isa = PBXNativeTarget; + buildConfigurationList = FFCA88AD16502BB9009AB98C /* Build configuration list for PBXNativeTarget "dna" */; + buildPhases = ( + FFCA889A16502BB9009AB98C /* Sources */, + FFCA889B16502BB9009AB98C /* Frameworks */, + FFCA889C16502BB9009AB98C /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = dna; + productName = dna; + productReference = FFCA889E16502BB9009AB98C /* dna */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + FFCA889516502BB9009AB98C /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0430; + }; + buildConfigurationList = FFCA889816502BB9009AB98C /* Build configuration list for PBXProject "dna" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = FFCA889316502BB9009AB98C; + productRefGroup = FFCA889F16502BB9009AB98C /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + FFCA889D16502BB9009AB98C /* dna */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + FFCA889A16502BB9009AB98C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + FFCA88A616502BB9009AB98C /* main.m in Sources */, + FFCA88B216502C2F009AB98C /* Cell.m in Sources */, + FF03F3F016542F9B007FDAA5 /* NSMutableArray (Shuffling).m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + FFCA88AB16502BB9009AB98C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + CLANG_ENABLE_OBJC_ARC = YES; + COPY_PHASE_STRIP = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.7; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + FFCA88AC16502BB9009AB98C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + CLANG_ENABLE_OBJC_ARC = YES; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.7; + SDKROOT = macosx; + }; + name = Release; + }; + FFCA88AE16502BB9009AB98C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "dna/dna-Prefix.pch"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + FFCA88AF16502BB9009AB98C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "dna/dna-Prefix.pch"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + FFCA889816502BB9009AB98C /* Build configuration list for PBXProject "dna" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + FFCA88AB16502BB9009AB98C /* Debug */, + FFCA88AC16502BB9009AB98C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + FFCA88AD16502BB9009AB98C /* Build configuration list for PBXNativeTarget "dna" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + FFCA88AE16502BB9009AB98C /* Debug */, + FFCA88AF16502BB9009AB98C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = FFCA889516502BB9009AB98C /* Project object */; +} diff --git a/dna.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/dna.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..0c6b448a --- /dev/null +++ b/dna.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/dna.xcodeproj/project.xcworkspace/xcuserdata/vasiliy.xcuserdatad/UserInterfaceState.xcuserstate b/dna.xcodeproj/project.xcworkspace/xcuserdata/vasiliy.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 00000000..ce346800 Binary files /dev/null and b/dna.xcodeproj/project.xcworkspace/xcuserdata/vasiliy.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist new file mode 100644 index 00000000..05301bc2 --- /dev/null +++ b/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcschemes/dna.xcscheme b/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcschemes/dna.xcscheme new file mode 100644 index 00000000..6643b168 --- /dev/null +++ b/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcschemes/dna.xcscheme @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcschemes/xcschememanagement.plist b/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 00000000..5975800d --- /dev/null +++ b/dna.xcodeproj/xcuserdata/vasiliy.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + dna.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + FFCA889D16502BB9009AB98C + + primary + + + + + diff --git a/dna/Cell+Mutator.h b/dna/Cell+Mutator.h new file mode 100644 index 00000000..f84b0d07 --- /dev/null +++ b/dna/Cell+Mutator.h @@ -0,0 +1,7 @@ +#import "Cell.h" + +@interface Cell (Mutator) + +-(void)mutate:(int)percentage; + +@end diff --git a/dna/Cell+Mutator.m b/dna/Cell+Mutator.m new file mode 100644 index 00000000..4bb67b0d --- /dev/null +++ b/dna/Cell+Mutator.m @@ -0,0 +1,19 @@ +#import "NSMutableArray (Shuffling).h" +#import "Cell+Mutator.h" + +@implementation Cell (Mutator) + +-(void)mutate:(int)percentage { + NSMutableArray *indices = [NSMutableArray arrayWithCapacity:100]; + for (int i = 0; i < 100; i++) { + [indices addObject:[NSNumber numberWithInt:i]]; + } + + [indices shuffle]; + + for (int i = 0; i < percentage; i++) { + [self.DNA replaceObjectAtIndex:i withObject:[self getRandomElement]]; + } +} + +@end \ No newline at end of file diff --git a/dna/Cell.h b/dna/Cell.h new file mode 100644 index 00000000..983610cb --- /dev/null +++ b/dna/Cell.h @@ -0,0 +1,18 @@ +#import +#import "NSMutableArray (Shuffling).h" + +@interface Cell : NSObject + +@property NSMutableArray *DNA; + +-(id)init; +-(id)insertRandomData; +-(id)getRandomElement; +-(void)print; +-(int)hammingDistance:(Cell *)cellObject; +-(void)mutate:(int)percentage; + +@end + + + diff --git a/dna/Cell.m b/dna/Cell.m new file mode 100644 index 00000000..7c41c41b --- /dev/null +++ b/dna/Cell.m @@ -0,0 +1,73 @@ +#import "Cell.h" + +@implementation Cell { + int CAPACITY; +} + +-(id) init { + self = [super init]; + CAPACITY = 100; + + // init Array + _DNA = [NSMutableArray arrayWithCapacity:CAPACITY]; + + return self; +} + +-(id) insertRandomData { + for (int i = 0; i < CAPACITY; i++) { + [_DNA addObject:[self getRandomElement]]; + } + + return self; +} + +-(id) getRandomElement { + srandomdev(); + + NSArray *values = [[NSArray alloc] initWithObjects: @"A", @"T", @"G", @"C", nil]; + NSUInteger randomIndex = arc4random() % [values count]; + + + return [values objectAtIndex:randomIndex]; +} + +-(void) print { + NSString *str = [[NSString alloc] init]; + + for (int i = 0; i < CAPACITY; i++) { + str = [str stringByAppendingString:[_DNA objectAtIndex:i]]; + } + + NSLog(@"%@", str); +} + +-(int) hammingDistance:(Cell *)cellObject { + int distance = 0, arrayLength; + + arrayLength = (int)[self.DNA count]; + + for (int i = 0; i < arrayLength; i++) { + if ( [self.DNA objectAtIndex:i] != [cellObject.DNA objectAtIndex:i]) { + distance++; + } + + } + + return distance; +} + +-(void)mutate:(int)percentage { + NSMutableArray *indices = [NSMutableArray arrayWithCapacity:100]; + for (int i = 0; i < 100; i++) { + [indices addObject:[NSNumber numberWithInt:i]]; + } + + [indices shuffle]; + + for (int i = 0; i < percentage; i++) { + [self.DNA replaceObjectAtIndex:i withObject:[self getRandomElement]]; + } +} + +@end diff --git a/dna/NSMutableArray (Shuffling).h b/dna/NSMutableArray (Shuffling).h new file mode 100644 index 00000000..e41f567d --- /dev/null +++ b/dna/NSMutableArray (Shuffling).h @@ -0,0 +1,5 @@ +#import + +@interface NSMutableArray (Shuffling) +- (void)shuffle; +@end diff --git a/dna/NSMutableArray (Shuffling).m b/dna/NSMutableArray (Shuffling).m new file mode 100644 index 00000000..838c411e --- /dev/null +++ b/dna/NSMutableArray (Shuffling).m @@ -0,0 +1,16 @@ +#import "NSMutableArray (Shuffling).h" + +@implementation NSMutableArray (Shuffling) + +- (void)shuffle +{ + NSUInteger count = [self count]; + for (NSUInteger i = 0; i < count; ++i) { + // Select a random element between i and end of array to swap with. + NSInteger nElements = count - i; + NSInteger n = (arc4random() % nElements) + i; + [self exchangeObjectAtIndex:i withObjectAtIndex:n]; + } +} + +@end diff --git a/dna/dna-Prefix.pch b/dna/dna-Prefix.pch new file mode 100644 index 00000000..d6034006 --- /dev/null +++ b/dna/dna-Prefix.pch @@ -0,0 +1,7 @@ +// +// Prefix header for all source files of the 'dna' target in the 'dna' project +// + +#ifdef __OBJC__ + #import +#endif diff --git a/dna/dna.1 b/dna/dna.1 new file mode 100644 index 00000000..ea863684 --- /dev/null +++ b/dna/dna.1 @@ -0,0 +1,79 @@ +.\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples. +.\"See Also: +.\"man mdoc.samples for a complete listing of options +.\"man mdoc for the short list of editing options +.\"/usr/share/misc/mdoc.template +.Dd 11/11/12 \" DATE +.Dt dna 1 \" Program name and manual section number +.Os Darwin +.Sh NAME \" Section Header - required - don't modify +.Nm dna, +.\" The following lines are read in generating the apropos(man -k) database. Use only key +.\" words here as the database is built based on the words here and in the .ND line. +.Nm Other_name_for_same_program(), +.Nm Yet another name for the same program. +.\" Use .Nm macro to designate other names for the documented program. +.Nd This line parsed for whatis database. +.Sh SYNOPSIS \" Section Header - required - don't modify +.Nm +.Op Fl abcd \" [-abcd] +.Op Fl a Ar path \" [-a path] +.Op Ar file \" [file] +.Op Ar \" [file ...] +.Ar arg0 \" Underlined argument - use .Ar anywhere to underline +arg2 ... \" Arguments +.Sh DESCRIPTION \" Section Header - required - don't modify +Use the .Nm macro to refer to your program throughout the man page like such: +.Nm +Underlining is accomplished with the .Ar macro like this: +.Ar underlined text . +.Pp \" Inserts a space +A list of items with descriptions: +.Bl -tag -width -indent \" Begins a tagged list +.It item a \" Each item preceded by .It macro +Description of item a +.It item b +Description of item b +.El \" Ends the list +.Pp +A list of flags and their descriptions: +.Bl -tag -width -indent \" Differs from above in tag removed +.It Fl a \"-a flag as a list item +Description of -a flag +.It Fl b +Description of -b flag +.El \" Ends the list +.Pp +.\" .Sh ENVIRONMENT \" May not be needed +.\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1 +.\" .It Ev ENV_VAR_1 +.\" Description of ENV_VAR_1 +.\" .It Ev ENV_VAR_2 +.\" Description of ENV_VAR_2 +.\" .El +.Sh FILES \" File used or created by the topic of the man page +.Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact +.It Pa /usr/share/file_name +FILE_1 description +.It Pa /Users/joeuser/Library/really_long_file_name +FILE_2 description +.El \" Ends the list +.\" .Sh DIAGNOSTICS \" May not be needed +.\" .Bl -diag +.\" .It Diagnostic Tag +.\" Diagnostic informtion here. +.\" .It Diagnostic Tag +.\" Diagnostic informtion here. +.\" .El +.Sh SEE ALSO +.\" List links in ascending order by section, alphabetically within a section. +.\" Please do not reference files that do not exist without filing a bug report +.Xr a 1 , +.Xr b 1 , +.Xr c 1 , +.Xr a 2 , +.Xr b 2 , +.Xr a 3 , +.Xr b 3 +.\" .Sh BUGS \" Document known, unremedied bugs +.\" .Sh HISTORY \" Document history if command behaves in a unique manner \ No newline at end of file diff --git a/dna/main.m b/dna/main.m new file mode 100644 index 00000000..9433279a --- /dev/null +++ b/dna/main.m @@ -0,0 +1,39 @@ +#import +#import "Cell.h" + +int main(int argc, const char * argv[]) +{ + + @autoreleasepool { + + Cell *myCell1, *myCell2; + + // Init first + myCell1 = [[Cell alloc] init]; + [myCell1 insertRandomData]; + + // Init second + myCell2 = [[Cell alloc] init]; + [myCell2 insertRandomData]; + + // Print + NSLog(@"Initial sequences:"); + [myCell1 print]; + [myCell2 print]; + NSLog(@"Hamming Distance: %i\n\n", [myCell1 hammingDistance:myCell2]); + + // Mutation: + NSLog(@"Sequences after mutation:"); + [myCell1 mutate:12]; + [myCell2 mutate:22]; + + // Print: + [myCell1 print]; + [myCell2 print]; + NSLog(@"Hamming Distance: %i", [myCell1 hammingDistance:myCell2]); + + } + + return 0; +} +