[llvm-commits] CVS: llvm/test/Programs/MultiSource/McCat-15-trie/Makefile trie.c trie.in1 trie.in2
Chris Lattner
lattner at cs.uiuc.edu
Mon May 12 13:34:38 PDT 2003
Changes in directory llvm/test/Programs/MultiSource/McCat-15-trie:
Makefile added (r1.1)
trie.c added (r1.1)
trie.in1 added (r1.1)
trie.in2 added (r1.1)
---
Log message:
Initial checkin
---
Diffs of the changes:
Index: llvm/test/Programs/MultiSource/McCat-15-trie/Makefile
diff -c /dev/null llvm/test/Programs/MultiSource/McCat-15-trie/Makefile:1.1
*** /dev/null Mon May 12 13:31:03 2003
--- llvm/test/Programs/MultiSource/McCat-15-trie/Makefile Mon May 12 13:30:53 2003
***************
*** 0 ****
--- 1,6 ----
+ LEVEL = ../../../..
+ PROG = trie
+ LDFLAGS = -lm
+ RUN_OPTIONS += trie.in1
+ include ../Makefile.multisrc
+
Index: llvm/test/Programs/MultiSource/McCat-15-trie/trie.c
diff -c /dev/null llvm/test/Programs/MultiSource/McCat-15-trie/trie.c:1.1
*** /dev/null Mon May 12 13:31:03 2003
--- llvm/test/Programs/MultiSource/McCat-15-trie/trie.c Mon May 12 13:30:53 2003
***************
*** 0 ****
--- 1,355 ----
+
+ /****
+ Copyright (C) 1996 McGill University.
+ Copyright (C) 1996 McCAT System Group.
+ Copyright (C) 1996 ACAPS Benchmark Administrator
+ benadmin at acaps.cs.mcgill.ca
+
+ This program is free software; you can redistribute it and/or modify
+ it provided this copyright notice is maintained.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ ****/
+
+ /* Implementation of the datastructure Trie
+ ) September 1996
+ Lars Hjorth Nielsen and Jacob Winstrup Schmidt */
+
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+
+ #define True -1
+ #define False 0
+ #define or ||
+ #define and &&
+ #define MAXSIZE 500
+ #define LINESIZE 1000
+
+ typedef struct trie *Trie;
+ typedef struct TrieRoot * TrieRoot;
+ typedef struct SString * SString;
+
+ typedef int bool;
+ typedef char * String;
+
+ void insertWord(TrieRoot tr,String w);
+ void insertW(Trie tr,String w);
+ Trie insertRestOfWord(String w);
+ Trie deleteWord(Trie tr,String w);
+ bool deleteW(Trie tr,String w);
+ void deleteNode(Trie tr);
+ Trie tNew(char c);
+ Trie tAlloc(void);
+ void insertChar(SString word, char c);
+ void deleteChar(SString word);
+ void printT(TrieRoot tr);
+ void printTheRest(Trie t, SString word);
+ SString ssInit(void);
+ TrieRoot trInit(void);
+
+
+ struct trie
+ {
+ char val;
+ bool word;
+ void * data;
+ Trie postfix;
+ Trie otherChar;
+ };
+
+ struct TrieRoot
+ {
+ Trie t;
+ };
+
+ struct SString
+ {
+ String s;
+ int max;
+ int len;
+ };
+
+
+ Trie tAlloc(void)
+ {
+ Trie t;
+
+ t = (Trie) calloc(1, sizeof(struct trie));
+ if (t == NULL)
+ {
+ abort();
+ }
+ return t;
+ };
+
+ Trie tNew(char c)
+ {
+ Trie t;
+ t = tAlloc();
+ t->val = c;
+ t->word = False;
+ t->data = (void *) NULL;
+ t->postfix = t->otherChar = (Trie) NULL;
+ return t;
+ };
+
+ /*Insert functions */
+ /*******************/
+
+ void insertWord(TrieRoot r,String w)
+ {
+ if (*w == '\0')
+ {
+ return;
+ }
+ else
+ {
+ if (r->t == (Trie) NULL)
+ {
+ r->t = insertRestOfWord(w);
+ }
+ else
+ {
+ insertW(r->t, w);
+ }
+ }
+ }
+
+
+ void insertW(Trie tr,String w)
+ /* Insert a word in a NON-EMPTY trie */
+ {
+ if (tr == (Trie) NULL)
+ {
+ abort();
+ /* error("Cannot insert in an empty Trie") */ ;
+ }
+ else
+ {
+ if (*w == '\0')
+ /* Should not happen but does not harm. */
+ {
+ return;
+ }
+ else
+ {
+ if (*w == tr->val)
+ {
+ if (*(w+1) == '\0')
+ {
+ tr->word = True;
+ return;
+ }
+ else
+ {
+ if (tr->postfix == (Trie) NULL)
+ {
+ tr->postfix = insertRestOfWord(++w);
+ return;
+ }
+ else
+ {
+ insertW(tr->postfix, ++w);
+ return;
+ }
+ }
+ }
+ else
+ {
+ if (tr->otherChar == (Trie) NULL)
+ {
+ tr->otherChar = insertRestOfWord(w);
+ return;
+ }
+ else
+ {
+ insertW(tr->otherChar, w);
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ Trie insertRestOfWord(String w)
+ /* Insert a NON-EMPTY word in a new trie */
+ {
+ Trie t;
+ if (*w == '\0')
+ /* error("Cannot insert the empty string. \0") */ ;
+ else
+ {
+ t = tNew(*w);
+ w++;
+ if (*w == '\0')
+ {
+ t->word = True;
+ return t;
+ }
+ else
+ {
+ t->postfix = insertRestOfWord(w);
+ return t;
+ }
+ }
+ }
+
+ /*End of insert functions */
+
+ void insertChar(SString word, char c)
+ {
+ if (word == NULL)
+ {
+ abort();
+ }
+ else
+ {
+ if (word->s == NULL)
+ {
+ String sTemp;
+ sTemp = (String) calloc(1, sizeof(char)+1);
+ if (sTemp == NULL) abort();
+ else
+ {
+ word->s = sTemp;
+ word->max = 1; /* don't count \0 */
+ word->len = 0;
+ }
+ }
+ else
+ {
+ if (word->len == word->max)
+ {
+ String tmp;
+ tmp = (String) calloc(1, 2 * word->max * sizeof(char) + 1);
+ if (tmp == NULL) abort();
+ else
+ {
+ tmp = strcpy(tmp, word->s);
+ free(word->s);
+ word->s = tmp;
+ word->max *= 2;
+ }
+ }
+ }
+ word->s[word->len++] = c;
+ word->s[word->len] = '\0';
+ }
+ }
+
+ void deleteChar(SString word)
+ /* word must be NON-NULL */
+ {
+ if (word == NULL) abort();
+ else
+ {
+ if (word->s == NULL) abort();
+ else
+ {
+ if (word->len > 0)
+ {
+ word->s[--word->len] = '\0';
+ }
+ else /* OK */ ;
+ }
+ }
+ }
+
+ /*Print function */
+ /****************/
+
+ void printT(TrieRoot tr)
+ {
+ SString word = ssInit();
+ if (tr == NULL)
+ abort();
+ else
+ {
+ printTheRest(tr->t, word);
+ }
+ }
+
+ void printTheRest(Trie t, SString word)
+ {
+ if (t == NULL)
+ {
+ return;
+ }
+ else
+ {
+ insertChar(word, t->val);
+ if (t->word == True)
+ {
+ printf("%s\n", word->s);
+ }
+ else
+ ;
+ printTheRest(t->postfix, word);
+ deleteChar(word);
+ printTheRest(t->otherChar, word);
+ }
+ }
+
+ SString ssInit(void)
+ {
+ SString word = (SString) calloc(1, sizeof(struct SString));
+ if (word == NULL)
+ {
+ abort();
+ }
+ else
+ {
+ word->s = (String) NULL;
+ word->max = 0;
+ word->len = 0;
+ return word;
+ }
+ }
+
+ TrieRoot trInit(void)
+ {
+ TrieRoot tr;
+
+ tr = (TrieRoot) calloc(1, sizeof(struct TrieRoot));
+ tr->t = (Trie) NULL;
+ return tr;
+ }
+
+ void main(int argc, char **argv)
+ {
+ FILE * file;
+ char word[LINESIZE];
+ char buffer[MAXSIZE];
+ int pos = 0;
+ TrieRoot tr;
+
+ tr = trInit();
+ if(argc != 2)
+ {
+ fprintf(stdout, "Usage: %s <sourcefile>\n",argv[0]);
+ exit(0);
+ }
+ if(!(file = fopen(argv[1], "r")))
+ {
+ perror(argv[1]);
+ exit(0);
+ }
+ while(!feof(file))
+ {
+ if (fscanf(file,"%s",word) < 0)
+ {
+ break;
+ }
+ else
+ {
+ pos = strlen(word) + pos + 1;
+ fseek(file, pos, 0);
+ insertWord(tr, word);
+ }
+ }
+ fclose(file);
+ printT(tr);
+ };
Index: llvm/test/Programs/MultiSource/McCat-15-trie/trie.in1
diff -c /dev/null llvm/test/Programs/MultiSource/McCat-15-trie/trie.in1:1.1
*** /dev/null Mon May 12 13:31:03 2003
--- llvm/test/Programs/MultiSource/McCat-15-trie/trie.in1 Mon May 12 13:30:53 2003
***************
*** 0 ****
--- 1,340 ----
+ /* Implementation of the datastructure Trie
+ ) September 1996
+ Lars Hjorth Nielsen and Jacob Winstrup Schmidt */
+
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+
+ #define True -1
+ #define False 0
+ #define or ||
+ #define and &&
+ #define MAXSIZE 500
+ #define LINESIZE 1000
+
+ typedef struct trie *Trie;
+ typedef struct TrieRoot * TrieRoot;
+ typedef struct SString * SString;
+
+ typedef int bool;
+ typedef char * String;
+
+ void insertWord(TrieRoot tr,String w);
+ void insertW(Trie tr,String w);
+ Trie insertRestOfWord(String w);
+ Trie deleteWord(Trie tr,String w);
+ bool deleteW(Trie tr,String w);
+ void deleteNode(Trie tr);
+ Trie tNew(char c);
+ Trie tAlloc(void);
+ void insertChar(SString word, char c);
+ void deleteChar(SString word);
+ void printT(TrieRoot tr);
+ void printTheRest(Trie t, SString word);
+ SString ssInit(void);
+ TrieRoot trInit(void);
+
+
+ struct trie
+ {
+ char val;
+ bool word;
+ void * data;
+ Trie postfix;
+ Trie otherChar;
+ };
+
+ struct TrieRoot
+ {
+ Trie t;
+ };
+
+ struct SString
+ {
+ String s;
+ int max;
+ int len;
+ };
+
+
+ Trie tAlloc(void)
+ {
+ Trie t;
+
+ t = (Trie) calloc(1, sizeof(struct trie));
+ if (t == NULL)
+ {
+ abort();
+ }
+ return t;
+ };
+
+ Trie tNew(char c)
+ {
+ Trie t;
+ t = tAlloc();
+ t->val = c;
+ t->word = False;
+ t->data = (void *) NULL;
+ t->postfix = t->otherChar = (Trie) NULL;
+ return t;
+ };
+
+ /*Insert functions */
+ /*******************/
+
+ void insertWord(TrieRoot r,String w)
+ {
+ if (*w == '\0')
+ {
+ return;
+ }
+ else
+ {
+ if (r->t == (Trie) NULL)
+ {
+ r->t = insertRestOfWord(w);
+ }
+ else
+ {
+ insertW(r->t, w);
+ }
+ }
+ }
+
+
+ void insertW(Trie tr,String w)
+ /* Insert a word in a NON-EMPTY trie */
+ {
+ if (tr == (Trie) NULL)
+ {
+ abort();
+ /* error("Cannot insert in an empty Trie") */ ;
+ }
+ else
+ {
+ if (*w == '\0')
+ /* Should not happen but does not harm. */
+ {
+ return;
+ }
+ else
+ {
+ if (*w == tr->val)
+ {
+ if (*(w+1) == '\0')
+ {
+ tr->word = True;
+ return;
+ }
+ else
+ {
+ if (tr->postfix == (Trie) NULL)
+ {
+ tr->postfix = insertRestOfWord(++w);
+ return;
+ }
+ else
+ {
+ insertW(tr->postfix, ++w);
+ return;
+ }
+ }
+ }
+ else
+ {
+ if (tr->otherChar == (Trie) NULL)
+ {
+ tr->otherChar = insertRestOfWord(w);
+ return;
+ }
+ else
+ {
+ insertW(tr->otherChar, w);
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ Trie insertRestOfWord(String w)
+ /* Insert a NON-EMPTY word in a new trie */
+ {
+ Trie t;
+ if (*w == '\0')
+ /* error("Cannot insert the empty string. \0") */ ;
+ else
+ {
+ t = tNew(*w);
+ w++;
+ if (*w == '\0')
+ {
+ t->word = True;
+ return t;
+ }
+ else
+ {
+ t->postfix = insertRestOfWord(w);
+ return t;
+ }
+ }
+ }
+
+ /*End of insert functions */
+
+ void insertChar(SString word, char c)
+ {
+ if (word == NULL)
+ {
+ abort();
+ }
+ else
+ {
+ if (word->s == NULL)
+ {
+ String sTemp;
+ sTemp = (String) calloc(1, sizeof(char)+1);
+ if (sTemp == NULL) abort();
+ else
+ {
+ word->s = sTemp;
+ word->max = 1; /* don't count \0 */
+ word->len = 0;
+ }
+ }
+ else
+ {
+ if (word->len == word->max)
+ {
+ String tmp;
+ tmp = (String) calloc(1, 2 * word->max * sizeof(char) + 1);
+ if (tmp == NULL) abort();
+ else
+ {
+ tmp = strcpy(tmp, word->s);
+ free(word->s);
+ word->s = tmp;
+ word->max *= 2;
+ }
+ }
+ }
+ word->s[word->len++] = c;
+ word->s[word->len] = '\0';
+ }
+ }
+
+ void deleteChar(SString word)
+ /* word must be NON-NULL */
+ {
+ if (word == NULL) abort();
+ else
+ {
+ if (word->s == NULL) abort();
+ else
+ {
+ if (word->len > 0)
+ {
+ word->s[--word->len] = '\0';
+ }
+ else /* OK */ ;
+ }
+ }
+ }
+
+ /*Print function */
+ /****************/
+
+ void printT(TrieRoot tr)
+ {
+ SString word = ssInit();
+ if (tr == NULL)
+ abort();
+ else
+ {
+ printTheRest(tr->t, word);
+ }
+ }
+
+ void printTheRest(Trie t, SString word)
+ {
+ if (t == NULL)
+ {
+ return;
+ }
+ else
+ {
+ insertChar(word, t->val);
+ if (t->word == True)
+ {
+ printf("%s\n", word->s);
+ }
+ else
+ ;
+ printTheRest(t->postfix, word);
+ deleteChar(word);
+ printTheRest(t->otherChar, word);
+ }
+ }
+
+ SString ssInit(void)
+ {
+ SString word = (SString) calloc(1, sizeof(struct SString));
+ if (word == NULL)
+ {
+ abort();
+ }
+ else
+ {
+ word->s = (String) NULL;
+ word->max = 0;
+ word->len = 0;
+ return word;
+ }
+ }
+
+ TrieRoot trInit(void)
+ {
+ TrieRoot tr;
+
+ tr = (TrieRoot) calloc(1, sizeof(struct TrieRoot));
+ tr->t = (Trie) NULL;
+ return tr;
+ }
+
+ void main(int argc, char **argv)
+ {
+ FILE * file;
+ char word[LINESIZE];
+ char buffer[MAXSIZE];
+ int pos = 0;
+ TrieRoot tr;
+
+ tr = trInit();
+ if(argc != 2)
+ {
+ fprintf(stdout, "Usage: %s <sourcefile>\n",argv[0]);
+ exit(0);
+ }
+ if(!(file = fopen(argv[1], "r")))
+ {
+ perror(argv[1]);
+ exit(0);
+ }
+ while(!feof(file))
+ {
+ if (fscanf(file,"%s",word) < 0)
+ {
+ break;
+ }
+ else
+ {
+ pos = strlen(word) + pos + 1;
+ fseek(file, pos, 0);
+ insertWord(tr, word);
+ }
+ }
+ fclose(file);
+ printT(tr);
+ };
Index: llvm/test/Programs/MultiSource/McCat-15-trie/trie.in2
diff -c /dev/null llvm/test/Programs/MultiSource/McCat-15-trie/trie.in2:1.1
*** /dev/null Mon May 12 13:31:03 2003
--- llvm/test/Programs/MultiSource/McCat-15-trie/trie.in2 Mon May 12 13:30:53 2003
***************
*** 0 ****
--- 1,57 ----
+
+
+
+ Postscript Assignments
+
+ Assignment 1, one page per sheet
+ Assignment 1, two pages per sheet
+ Project Descriptions, one page per sheet
+ Project Descriptions, two pages per sheet
+
+
+
+ Useful References
+
+ M.Brandis, H.Mvssenbvck: Single-Pass Generation of Static Single Assignment
+ Form for Structured Languages. ACM Trans. on Programming Languages and
+ Systems 16(6), Nov.1994, 1684-1698
+ Cliff Click, Combining Analyses, Combining Optimizations, Ph.D. Thesis, Rice
+ University. (One page per sheet) , (Two pages per sheet)
+ Preston Briggs, Register Allocation via Graph Coloring, Ph.D. Thesis, Rice
+ University. (One page per sheet) , (Two pages per sheet)
+
+
+
+ Java Links
+
+ Sun maintains a web site that contains considerable Java documentation. Its URL is
+ http://java.sun.com. This site contains considerable Java documentation, including (thanks
+ to Mark Hill and Jim Larus for putting this info on their CS838 page at Wisconsin):
+
+ The Java Language Specification.
+ The Virtual Machine Specification
+ The Java API (Application Programmer Interface, i.e., libraries) Specification
+ Java Language Tutorial
+
+ The web also can provide a Java Development Kit for writing and running Java
+ applications on a PC. You should be able to find it installed already on many UNIX
+ systems. You can also run Java applets in recent versions of Netscape and Microsoft's
+ Internet Explorer.
+
+ Sample Java applets are available from many places. Here are three examples:
+
+ Sun has a collection at: http://java.sun.com/java.sun.com/applets/index.html.
+ The largest collection of Java programs and applets appears to be at
+ http://www.gamelan.com.
+ Free tools for java appears at http://www.blackdown.org/~kbs/.
+
+ A really interesting site on optimizing Java programs and related tools has been developed
+ by Jonathan Hardwick at CMU. He develops his own benchmark and also lists several
+ other benchmarks on his resources page. I have found one other benchmark suite called
+ BenchBeans .
+
+ If you get tired waiting for bits to cross the ocean, you can find lots of interesting
+ information and mirror sites of important software and documentation via the home page
+ of The Danish Java Developer's Club .
+
+ September 1996
More information about the llvm-commits
mailing list