The purpose of this assignment is to help you learn how to
implement common data structures in C and how to exploit them to
achieve modularity in a real-world application. It also will give you
the opportunity to gain more experience with the GNU/Linux programming
tools, especially bash
,
vscode
(or the editor of your choice), and gdb
.
A data structure is a way of organizing data for efficient operation. In this assignment, you will implement the required functionalities (register, unregister, and find) of a customer management program using the following data structures.
You will implement and improve the customer data management API using various data structures. Your tasks in this assignment are below:
README.md
.
customer_manager
Interfacecustomer_manager
is an API library for the customer data management, where the users can register the customer information and perform lookup operations to retrieve the purchase amount information.
The customer_manager
interface introduces a structure type definition, DB_T
:
DB_T
structure is used for saving
the pointer(s) to the entire customer data.The customer_manager
interface is described in a file named customer_manager.h
, and it contains these function declarations:
What each function does is as follows:
CreateCustomerDB
should allocate memory for a new DB_T
object and any underlying objects.Return value | On success, it should return a pointer to the memory block for the new DB_T object. |
---|---|
If the function fails to allocate the memory, it should return NULL . |
DestroyCustomerDB
should free all memory occupied by the DB_T
object and all the underlying objects.Requirements | If the given pointer to the DB_T object is NULL , it should do nothing. |
---|
RegisterCustomer
registers a new item, that is, a new
user whose ID is id
, name is name
, and
purchase amount is purchase
to the DB_T
object d
.
Objective | d . |
---|---|
Return value |
On success, it should return 0. Otherwise (e.g., on failure), it should return -1. |
1) If any of d , id , or name is NULL , it is a failure.2) If purchase is zero or a negative number, it is a failure.3) If an item with the same id or with the same name already exists, it is a failure. More about rule3: You should not modify the existing item in this case, and should not leak memory for the new item. A good strategy is to check if such an item already exists, and allocate the new item only if it does not exist. |
|
Requirements | The new item should own the id and name strings by copying them to new buffers. Consider using strdup() . If you use strdup() , please add #define _GNU_SOURCE 1 in your C files |
UnregisterCustomerByID
unregisters a user whose ID is id
from the DB_T
object, d
.Return value |
On success, it should return 0. Otherwise, it should return -1. |
---|---|
1) If d or id is NULL , it is a failure. 2) If no such item exists, it is a failure. |
|
Requirements | Make sure that you free all the memory allocated for the item being unregistered. |
UnregisterCustomerByName
unregisters a user whose name is name
from the DB_T
object, d
.Return value |
On success, it should return 0. Otherwise, it should return -1. |
---|---|
1) If d or name is NULL , it is a failure. 2) If no such item exists, it is a failure. |
|
Requirements | Make sure that you free all the memory allocated for the item being unregistered. |
Caution | It is identical to UnregisterCustomerByID except that it matches name instead of id . |
GetPurchaseByID
searches for the purchase amount of the customer whose ID is id
from a DB_T
object d
.Return value |
On success, it should return the purchase amount. Otherwise, it should return -1 |
---|---|
1) If d or id is NULL , it is a failure. 2) If there is no customer whose ID matches the given one, it is a failure. |
GetPurchaseByName
searches for the purchase amount of the customer whose name is name
from a DB_T
object d
.Objective | It is identical to GetPurchaseByID except that it matches name instead of id . |
---|---|
Return value |
On success, it should return the purchase amount. Otherwise, it should return -1 |
1) If d or name is NULL , it is a failure. 2) If there is no customer whose name matches the given one, it is a failure. |
GetSumCustomerPurchase
calculates the sum of the numbers returned by fp
by calling fp
for each user item. That is, this function iterates every user item in d
, calls fp
once for each user item, and returns the sum of the numbers returned by fp
. Return value |
On success, GetCustomerPurchase should return the sum of all numbers returned by fp by iterating each user item in d .
|
---|---|
If d or fp is NULL, it should return -1. |
|
Requirements | Note that fp is provided by the caller
of GetSumCustomerPurchase .
fp is a function
pointer that takes user's id, name, and purchase as parameters,
evaluates a specific condition on it, and returns a non-negative
number.
For example, the following code snippet shows the example of
the function pointed by fp , which returns the half of the
purchase amount of the user whose name contains with "Gorilla". |
customer_manager
Array ImplementationThe goal of the first task is to implement
the
Your first customer_manager
implementation should be as follows:
customer_manager.h
.customer_manager1.c
(skeleton code
).
customer_manager
implementation should use a dynamically-allocated memory (i.e.,
in CreateCustomerDB()
and DestroyCustomerDB()
). It should make sure to free all
dynamically-allocated memory when the memory is no longer needed.assert
macro. Determine which invariant should be
maintained is a part of your task. CreateCustomerDB
is
called. You may start with the initial array size as UNIT_ARRAY_SIZE
. CreteCustomerDB
is as follows. You can use the
following code or define your own structure differently.
calloc()
to allocate the array is often helpful
since it initializes all elements (and their fields)
to 0
.
RegisterCustomer
, find an empty element in the
array, and store the new user data in it. Make sure that you copy id
and name strings instead of only their pointers. If there is no empty
element, expand the array by calling realloc()
. The
increment amount should be UNIT_ARRAY_SIZE
elements each time you expand.
UnregisterCustomerByID
or UnregisterCustomerByName
, you search for the matching
item, and deallocate the name and id. Optionally, you can set the name
to NULL
. This way, you can easily know which element is
empty by checking each element's name
with NULL
. GetSumCustomerPurchase
, scan the array from
index 0 till the max index, and call fp
for each valid
element. customer_manager
Hash Table ImplementationUnfortunately, using an array is slow when you deal with a large number of user items. Frequent reqistration and unregisteration of a user item creates many holes (empty elements) scattered across the array, which, in turn, makes these operations slow. Adding, deleting, and searching of a user item would eventually depend on linear search (unless you take extra measures to manage the holes separately).
We improve the performance of customer_manager operations with a hash table in this task. Actually, you would need two hash tables. One is for looking up a user item with ID as a key, and the other is for a lookup with a name as a key.
Your hash table-based customer_manager
implementation
should:
customer_manager.h
.customer_manager2.c
.malloc
or calloc
in any object, eventually there should be
exactly one call of free
.murmurhash.c
from Task 1-2, make sure you copy the code into your customer_manager2.c.
Unlike Task1-2, the Makefile used for grading won't be linking the two files.readme
file.
customer_manager
implementation
(it uses the hash_function
mentioned above).
hash_function("ygmoon", n)
and hash_function
("ch.hwang128", n)
are 0.hash_function("baesangwook89", n)
is 4.hash_function("Changho Hwang", n)
and hash_function
("Sangwook Bae", n)
are 3.hash_function("YoungGyoun Moon", n)
is 5.
We
provide client.c
to test your implementations. It first checks the correctness of your
library functions and measures the performance over various user
items. Note that we may use other programs for grading.
To compile your code, do the following:
(Extra credit: 15% ) We will give an extra credit to the students whose implementation is the fastest among all students. Note that only assignments whose basic functionality is implemented without problems deserve the extra credit. We may use our own program to measure the performance.
Rank # | Extra credit |
---|---|
1 | 15% |
2-3 | 10% |
4-6 | 5% |
7-10 | 3% |
Develop in your own environment using vscode
to create
source code and gdb
to debug. Make sure to compile
with gcc209
and test your code on lab machine before
submission.
Please follow the steps through Task 1 to Task 3 to complete
the customer_manager
API, and test your libraries.
Create a readme
text file that contains:
customer_manager
using array and the one using hash table
respectively. Please try to provide a reasonable explanation for the pros and cons of each implementation.
Use KLMS submission link to submit your assignments. Your submission should
be one gzipped tar file whose name is
YourStudentID_assign3.tar.gz
For example, if your student ID is 20231234,
please name the file as
20231234_assign3.tar.gz
Create a local directory named 'YourStudentID_assign3
' and place all your files in it. Then, tar
your submission file.
Please refer to here for how to archive your assignment.
Your submission need to include the following files:
customer_manager1.c
customer_manager2.c
no file is needed.
readme
text fileYour submission file should look like this:
Makefile
to make your submission file.
We will grade your work on quality from the user's point of view and from the programmer's point of view. To encourage good coding practices, we will deduct points if gcc209
generates warning messages.
From the user's point of view, your module has quality if it behaves as it should.
In part, style is defined by the rules given in The Practice of Programming (Kernighan and Pike), as summarized by the Rules of Programming Style document. These additional rules apply:
Names: You should use a clear and consistent style for variable and function names. One example of such a style is to prefix each variable name with characters that indicate its type. For example, the prefix c
might indicate that the variable is of type char
, i
might indicate int
, pc
might mean char*
, ui
might mean unsigned int
, etc. But it is fine to use another style -- a style which does not include the type of a variable in its name -- as long as the result is a readable program.
Line lengths: Limit line lengths in your source code to 72 characters. Doing so allows us to print your work in two columns, thus saving paper.
Comments: Each source code file should begin with a comment that includes your name, student ID, and the description of the file.
Comments: Each function should begin with a comment that describes what the function does from the caller's point of view. The function comment should:
Comments: Each structure type definition and each structure field definition should have a comment that describes it. Comments should be written in English.