[llvm-commits] CVS: llvm-test/MultiSource/Benchmarks/Prolangs-C++/primes/primes.cpp

Chris Lattner lattner at cs.uiuc.edu
Mon Oct 4 13:01:30 PDT 2004



Changes in directory llvm-test/MultiSource/Benchmarks/Prolangs-C++/primes:

primes.cpp added (r1.1)
---
Log message:

Initial checkin of all of the source


---
Diffs of the changes:  (+48 -0)

Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/primes/primes.cpp
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/primes/primes.cpp:1.1
*** /dev/null	Mon Oct  4 15:01:25 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/primes/primes.cpp	Mon Oct  4 15:01:15 2004
***************
*** 0 ****
--- 1,48 ----
+ // From Ravi Sethi's book on Programming Languages.
+ 
+ #include <stdio.h>
+ class item {
+ public:
+ 	item *source;
+ 	item(item *src) {source = src;}
+ 	virtual int out() {return 0;}
+ };
+ class counter : public item {
+ 	int value;
+ public:
+ 	int out() {return value++;}
+ 	counter(int v) : item(0) {value = v;}
+ };
+ class sieve : public item {
+ public:
+ 	int out();
+ 	sieve(item *src) : item(src) {};
+ };
+ class filter : public item {
+ 	int factor;
+ public:
+ 	int out();
+ 	filter(item *src, int f) : item (src) {factor = f;}
+ };
+ main() {
+ 	counter c(2);
+ 	sieve s(&c);
+ 	int next;
+ 	do {
+ 		next = s.out();
+ 		printf("%d ",next);
+ 	} while (next < 61);
+ 	printf ("\n");
+ }
+ int sieve::out() {
+ 	int n = source->out();
+ 	source = new filter(source,n);
+ 	return n;
+ }
+ int filter::out() {
+ 	while (1) {
+ 		int n = source->out();
+ 		if (n % factor)
+ 			return n;
+ 	}
+ }






More information about the llvm-commits mailing list