13-Assignment-3-vector

13-Assignment-3-vector - CS107 Spring 2007 Handout 13 April...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 13 Spring 2007 April 18, 2007 Assignment 3: vector and hashset Assignment written by Julie Zelenski, and revised by me. Assignment 2 operates as a warm-up for the more intense Assignment 3 experience. Now that you’ve had some practice with void * s, pointer arithmetic, and casting, you’re in a position to write a few generic container data structures to imitate the functionality of the STL—without using templates. This requires some very careful programming, and mandates a full understanding of the more difficult C library functions: malloc , realloc , free , memcpy , memmove , qsort , and bsearch . You’re expected to use each and every one of these, relying on the man pages to get the documentation. This assignment is based on an assignment given out in previous quarters. Most students consistently identified that assignment to be the most demanding, so I can only assume it will be challenging for everyone here as well. In fact, students used to get much more practice with the void * and raw dynamic memory allocation when our introductory programming classes were taught in C without the added C++. You’re at something of a disadvantage when compared to those students who complained even then. While I have scaled the assignment back a little, it’s still a bear of a project by any measure. Note: you’ll be coding in straight C. There are no classes, no methods, no templates, no references, no operator new , no string s or streams. You define new types as exposed struct s (C’s best imitation of the class ), but all routines that initialize, destroy, and otherwise manipulate those struct s must be implemented as functions. Due: Wednesday, April 25 th at 11:59 p.m. The C vector The C vector is a more flexible extension of C's built-in array type. It has some of the same basic properties: it is managed as a contiguous region of memory, all the elements in any one array must be the same size, and element indexing starts at 0, not 1. But this C vector differs in that it can automatically grow when elements are appended, and it can splice elements in and out of the middle. Through the use of client-supplied function pointers, it can sort itself, iterate over its elements, and search for an element of interest.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 The specific requirements of the vector are detailed in the interface file attached to the end of this handout. The short summary: typedef int (*VectorCompareFunction)(const void *elemAddr1, const void *elemAddr2); typedef void (*VectorMapFunction)(void *elemAddr, void *auxData); typedef void (*VectorFreeFunction)(void *elemAddr); typedef struct { // implementation specific… you decide this } vector; void VectorNew(vector *v, int elemSize, VectorFreeFunction freefn, int initialAllocation); void VectorDispose(vector *v); int VectorLength(const vector *v); void *VectorNth(const vector *v, int position); void VectorInsert(vector *v, const void *elemAddr, int position); void VectorAppend(vector *v, const void *elemAddr); void VectorReplace(vector *v, const void *elemAddr, int position); void VectorDelete(vector *v, int position); int
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 17

13-Assignment-3-vector - CS107 Spring 2007 Handout 13 April...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online