[Sumover-dev] [svn commit] r4307 - vic/branches/cc/cc/test

sumover-dev at cs.ucl.ac.uk sumover-dev at cs.ucl.ac.uk
Mon Nov 10 18:13:58 GMT 2008


Author: soohyunc
Date: Mon Nov 10 18:13:58 2008
New Revision: 4307

Modified:
   vic/branches/cc/cc/test/buffer.cpp
   vic/branches/cc/cc/test/buffer.h

Log:
o  implemented Data constructor
o  added Buffer::display() method


Modified: vic/branches/cc/cc/test/buffer.cpp
==============================================================================
--- vic/branches/cc/cc/test/buffer.cpp	(original)
+++ vic/branches/cc/cc/test/buffer.cpp	Mon Nov 10 18:13:58 2008
@@ -1,6 +1,6 @@
 /*
  * buffer.cpp
- * (implementation for buffer - a simple c++ linked list)
+ * (implementation for Buffer - a simple c++ linked list)
  */
 
 /* $Id$ */
@@ -8,18 +8,18 @@
 #include "buffer.h"
 
 // insert
-void buffer::insert (int val, data *ptr) {
+void Buffer::insert (int val, Data *ptr) {
 	if (!ptr) {
 		insert_front (val);
 	} else {
-		new data (val, ptr);
+		new Data (val, ptr);
 		size_up();
 	}
 }
 
 // insert front
-void buffer::insert_front (int val) {
-	data *ptr = new data(val);
+void Buffer::insert_front (int val) {
+	Data *ptr = new Data(val);
 
 	if (!head_) {
 		head_ = tail_ = ptr;
@@ -32,19 +32,19 @@
 }
 
 // insert end
-void buffer::insert_end (int val) {
+void Buffer::insert_end (int val) {
 	if (tail_) {
-		tail_ = head_ = new data (val);
+		tail_ = head_ = new Data (val);
 	} else {
-		tail_ = new data (val, tail_);
+		tail_ = new Data (val, tail_);
 	}
 
 	size_up();
 }
 
 // find
-void buffer::find (int val) {
-	data* ptr = head_;
+Data* Buffer::find (int val) {
+	Data* ptr = head_;
 
 	while (ptr) {
 		if (ptr->get_val() == val)
@@ -55,9 +55,9 @@
 }
 
 // remove front
-void buffer::remove_front() {
+void Buffer::remove_front() {
 	if (head_) {
-		data *ptr = head_;
+		Data *ptr = head_;
 		head_ = head_->next();
 
 		size_down();
@@ -66,10 +66,33 @@
 }
 
 // purge
-void buffer::purge() {
+void Buffer::purge() {
 	while (head_)
 		remove_front();
 
 	size_ = 0;
 	head_ = tail_ = 0;
 }
+
+// display
+void Buffer::display (std::ostream &out) {
+	out << "\n( " << size_ << " ) ( ";
+
+	Data *ptr = head_;
+	while (ptr) {
+		out << ptr->get_val() << " ";
+		ptr = ptr->next();
+	}
+
+	out << ")\n\n";
+}
+
+// Data constructor
+Data::Data (int val, Data *ptr) : val_(val) {
+	if (!ptr) {
+		next_ = NULL;
+	} else {
+		next_ = ptr->next_;
+		ptr->next_ = this;
+	}
+}

Modified: vic/branches/cc/cc/test/buffer.h
==============================================================================
--- vic/branches/cc/cc/test/buffer.h	(original)
+++ vic/branches/cc/cc/test/buffer.h	Mon Nov 10 18:13:58 2008
@@ -5,15 +5,22 @@
 
 /* $Id$ */
 
+#ifndef vic_cc_test_buffer_h
+#define vic_cc_test_buffer_h
+
+#include <iostream>
 #include "config.h"
 
-// buffer is a linked list
-class data;
+// Buffer is a linked list
+class Data;
 
-class buffer {
+class Buffer {
 public:
 	// default constructor
-	buffer() : head_(0), tail_(0), size_(0) {};
+	Buffer() : head_(NULL), tail_(NULL), size_(0) {};
+
+	// destructor
+	~Buffer() {};
 
 	// return list size
 	inline int size() { return size_; }
@@ -25,7 +32,7 @@
 	inline void size_down() { --size_; }
 
 	// insert
-	void insert (int val, data *ptr);
+	void insert (int val, Data *ptr);
 
 	// insert front
 	void insert_front (int val);
@@ -34,7 +41,7 @@
 	void insert_end (int val);
 
 	// find
-	data* find (int val);
+	Data* find (int val);
 
 	// remove
 	int remove (int val);
@@ -45,28 +52,37 @@
 	// purge list
 	void purge();
 
+	// display
+	void display (std::ostream &out = std::cout);
+
 private:
-	data* head_;
-	data* tail_;
+	Data* head_;
+	Data* tail_;
 	int size_;
 };
 
-// buffer contents 
-class data {
+// Buffer contents 
+class Data {
 public:
-	friend class buffer;
-
 	// default constructor
-	data() : val_(0), next_(0) {};
-	data (int val, data *loc = 0);
+	Data() : val_(0), next_(NULL) {};
+	Data (int val, Data *ptr = NULL);
+
+	// destructor
+	~Data() {};
 
 	// value
 	int get_val() { return val_; }
 
 	// next
-	data* next() { return next_; }
+	Data* next() { return next_; }
+
+	// set to next
+	void next (Data *ptr) { next_ = ptr; }
 
 private:
 	int val_;		// hold a number 
-	data *next_;	// hold the address of the next item on the list
+	Data *next_;	// hold the address of the next item on the list
 };
+
+#endif /* vic_cc_test_buffer_h */



More information about the Sumover-dev mailing list