Skip to content

Instantly share code, notes, and snippets.

@atr000
Created August 19, 2010 15:36
Show Gist options
  • Save atr000/538174 to your computer and use it in GitHub Desktop.
Save atr000/538174 to your computer and use it in GitHub Desktop.
wordPerms.c
#import <CoreFoundation/CoreFoundation.h>
#import <stdlib.h>
void PermutationsOfWord (CFMutableArrayRef output, CFStringRef prefix, CFStringRef letters);
CFStringRef CFStringCreateWithContentsOfFile (const char *path);
int main (int argc, const char *argv[]) {
if (argc != 3) {
printf("Usage: scrabblegen dictionary.txt setofletters\n");
return EXIT_SUCCESS;
}
char dPath[PATH_MAX + 1];
char *res = realpath(argv[1], dPath);
if (!res) {
printf("Invalid dictionary path!\n");
return EXIT_FAILURE;
}
CFStringRef letters = CFStringCreateWithCString(NULL, argv[2], kCFStringEncodingUTF8);
CFMutableArrayRef combinations = CFArrayCreateMutable(NULL, 0, NULL);
PermutationsOfWord(combinations, CFSTR(""), letters);
CFRelease(letters);
// Sort the combinations so a parallel linear search can be done
CFRange wholeRange = CFRangeMake(0, CFArrayGetCount(combinations));
CFArraySortValues(combinations, wholeRange, (CFComparatorFunction) CFStringCompare, NULL);
CFStringRef dictionary = CFStringCreateWithContentsOfFile(dPath);
CFArrayRef lines = CFStringCreateArrayBySeparatingStrings(NULL, dictionary, CFSTR("\n"));
CFRelease(dictionary);
CFIndex currCombIdx = 0;
CFIndex currLineIdx = 0;
CFIndex linesLen = CFArrayGetCount(lines);
CFIndex combsLen = CFArrayGetCount(combinations);
printf("--\n");
while (1) {
CFStringRef line = CFArrayGetValueAtIndex(lines, currLineIdx);
CFStringRef comb = CFArrayGetValueAtIndex(combinations, currCombIdx);
CFComparisonResult res = CFStringCompare(line, comb, kCFCompareCaseInsensitive);
if (res == kCFCompareLessThan) {
currLineIdx++;
}
else if (res == kCFCompareEqualTo) {
CFShow(line);
currLineIdx++;
currCombIdx++;
}
else {
currCombIdx++;
}
if (currLineIdx == linesLen || currCombIdx == combsLen)
break;
}
printf("--\n");
CFRelease(lines);
CFRelease(combinations);
return 0;
}
void PermutationsOfWord (CFMutableArrayRef output, CFStringRef prefix, CFStringRef letters) {
CFIndex len = CFStringGetLength(letters);
for (int n = 0; n < len; n++) {
UniChar c = CFStringGetCharacterAtIndex(letters, n);
CFIndex pLen = CFStringGetLength(prefix);
CFMutableStringRef nPrefix = CFStringCreateMutableCopy(NULL, pLen + 1, prefix);
CFStringAppendCharacters(nPrefix, &c, 1);
CFArrayAppendValue(output, nPrefix);
if (len > 1) {
CFMutableStringRef nLetters = CFStringCreateMutableCopy(NULL, len, letters);
CFStringDelete(nLetters, CFRangeMake(n, 1));
PermutationsOfWord(output, nPrefix, nLetters);
CFRelease(nLetters);
}
}
}
/* Returns NULL if an error occured */
CFStringRef CFStringCreateWithContentsOfFile (const char *path) {
FILE *file = fopen(path, "r");
if (!file)
return NULL;
fseek(file, 0, SEEK_END);
long size = ftell(file);
rewind(file);
char *buffer = (char *) malloc(sizeof(char) * size);
if (!fread(buffer, 1, size, file))
return NULL;
fclose(file);
CFStringRef str = CFStringCreateWithCString(NULL, buffer, kCFStringEncodingUTF8);
free(buffer);
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment