From 7b18182e472a6cccd96819579d6059db00ccb2e7 Mon Sep 17 00:00:00 2001 From: Bailey Thompson Date: Mon, 1 Jan 2018 18:42:30 -0500 Subject: [PATCH] Add function to vector and array to get data Add a function "get_data" to vector and array which lets the user access the data pointer which the vector and array operate on. --- src/array.c | 19 +++++++++++++++++-- src/array.h | 5 +++-- src/deque.c | 4 ++-- src/deque.h | 4 ++-- src/forward_list.c | 4 ++-- src/forward_list.h | 4 ++-- src/list.c | 4 ++-- src/list.h | 4 ++-- src/priority_queue.c | 14 ++------------ src/queue.c | 4 ++-- src/queue.h | 4 ++-- src/stack.c | 4 ++-- src/stack.h | 4 ++-- src/vector.c | 19 +++++++++++++++++-- src/vector.h | 5 +++-- tst/array.c | 6 +++++- tst/deque.c | 8 ++++---- tst/forward_list.c | 8 ++++---- tst/list.c | 8 ++++---- tst/priority_queue.c | 12 +----------- tst/queue.c | 2 +- tst/stack.c | 2 +- tst/vector.c | 12 ++++++++---- 23 files changed, 90 insertions(+), 70 deletions(-) diff --git a/src/array.c b/src/array.c index f40c7d6..f9140eb 100644 --- a/src/array.c +++ b/src/array.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -73,11 +73,26 @@ int array_size(array me) * @param arr The array to copy to. * @param me The array to copy from. */ -void array_to_array(void *const arr, array me) +void array_copy_to_array(void *const arr, array me) { memcpy(arr, me->data, me->element_count * me->data_size); } +/** + * Gets the storage element of the array which is contiguous in memory. If the + * data is modified, the data in the array is modified. Also, any array + * operation may invalidate this data pointer. The array owns the data pointer, + * thus it must not be freed. + * + * @param me The array to get the storage element from. + * + * @return The storage element of the array. + */ +void *array_get_data(array me) +{ + return me->data; +} + /* * Determines if the input is illegal. */ diff --git a/src/array.h b/src/array.h index e29f1ba..1e339d2 100644 --- a/src/array.h +++ b/src/array.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -32,7 +32,8 @@ array array_init(int element_count, size_t data_size); // Utility int array_size(array me); -void array_to_array(void *arr, array me); +void array_copy_to_array(void *arr, array me); +void *array_get_data(array me); // Accessing int array_set(array me, int index, void *data); diff --git a/src/deque.c b/src/deque.c index 16c70ce..32a981f 100644 --- a/src/deque.c +++ b/src/deque.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -135,7 +135,7 @@ int deque_trim(deque me) * @param arr The array to copy the deque to. * @param me The deque to copy from. */ -void deque_to_array(void *const arr, deque me) +void deque_copy_to_array(void *const arr, deque me) { for (int i = 0; i < deque_size(me); i++) { deque_get_at(arr + i * me->data_size, me, i); diff --git a/src/deque.h b/src/deque.h index 984ba9c..613683c 100644 --- a/src/deque.h +++ b/src/deque.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,7 +34,7 @@ deque deque_init(size_t data_size); int deque_size(deque me); bool deque_is_empty(deque me); int deque_trim(deque me); -void deque_to_array(void *arr, deque me); +void deque_copy_to_array(void *arr, deque me); // Adding int deque_push_front(deque me, void *data); diff --git a/src/forward_list.c b/src/forward_list.c index f790703..5fe4e39 100644 --- a/src/forward_list.c +++ b/src/forward_list.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -85,7 +85,7 @@ bool forward_list_is_empty(forward_list me) * @param arr The array to copy the list to. * @param me The list to copy to the array. */ -void forward_list_to_array(void *const arr, forward_list me) +void forward_list_copy_to_array(void *const arr, forward_list me) { struct node *traverse = me->head; int offset = 0; diff --git a/src/forward_list.h b/src/forward_list.h index fa314b7..2c75b3e 100644 --- a/src/forward_list.h +++ b/src/forward_list.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -33,7 +33,7 @@ forward_list forward_list_init(size_t data_size); // Utility int forward_list_size(forward_list me); bool forward_list_is_empty(forward_list me); -void forward_list_to_array(void *arr, forward_list me); +void forward_list_copy_to_array(void *arr, forward_list me); // Adding int forward_list_add_first(forward_list me, void *data); diff --git a/src/list.c b/src/list.c index 429ad52..ad80d07 100644 --- a/src/list.c +++ b/src/list.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -88,7 +88,7 @@ bool list_is_empty(list me) * @param arr The array to copy the list to. * @param me The list to copy to the array. */ -void list_to_array(void *const arr, list me) +void list_copy_to_array(void *const arr, list me) { struct node *traverse = me->head; int offset = 0; diff --git a/src/list.h b/src/list.h index b46128d..137aba7 100644 --- a/src/list.h +++ b/src/list.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -33,7 +33,7 @@ list list_init(size_t data_size); // Utility int list_size(list me); bool list_is_empty(list me); -void list_to_array(void *arr, list me); +void list_copy_to_array(void *arr, list me); // Adding int list_add_first(list me, void *data); diff --git a/src/priority_queue.c b/src/priority_queue.c index a482f06..1cf9968 100644 --- a/src/priority_queue.c +++ b/src/priority_queue.c @@ -26,16 +26,6 @@ #include "vector.h" #include "priority_queue.h" -/* - * Must exactly match the declaration in vector.c - */ -struct _vector { - size_t data_size; - int offset; - int space; - void *storage; -}; - struct _priority_queue { vector data; size_t data_size; @@ -114,7 +104,7 @@ int priority_queue_push(priority_queue me, void *const data) free(temp); return rc; } - void *const vector_storage = me->data->storage; + void *const vector_storage = vector_get_data(me->data); int index = vector_size(me->data) - 1; int parent_index = (index - 1) / 2; void *data_index = vector_storage + index * me->data_size; @@ -146,7 +136,7 @@ bool priority_queue_pop(void *const data, priority_queue me) if (rc != 0) { return false; } - void *const vector_storage = me->data->storage; + void *const vector_storage = vector_get_data(me->data); const int size = vector_size(me->data) - 1; void *const temp = vector_storage + size * me->data_size; memcpy(vector_storage, temp, me->data_size); diff --git a/src/queue.c b/src/queue.c index 71ae9c8..9ccaeb2 100644 --- a/src/queue.c +++ b/src/queue.c @@ -98,9 +98,9 @@ int queue_trim(queue me) * @param arr The array to have copied from the queue. * @param me The queue to copy to the array. */ -void queue_to_array(void *const arr, queue me) +void queue_copy_to_array(void *const arr, queue me) { - deque_to_array(arr, me->deque_data); + deque_copy_to_array(arr, me->deque_data); } /** diff --git a/src/queue.h b/src/queue.h index a08312b..450888d 100644 --- a/src/queue.h +++ b/src/queue.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,7 +34,7 @@ queue queue_init(size_t data_size); int queue_size(queue me); bool queue_is_empty(queue me); int queue_trim(queue me); -void queue_to_array(void *arr, queue me); +void queue_copy_to_array(void *arr, queue me); // Adding int queue_push(queue me, void *data); diff --git a/src/stack.c b/src/stack.c index 0a302cc..53a6ad5 100644 --- a/src/stack.c +++ b/src/stack.c @@ -93,9 +93,9 @@ int stack_trim(stack me) * @param arr The array to copy to. * @param me The stack to copy from. */ -void stack_to_array(void *const arr, stack me) +void stack_copy_to_array(void *const arr, stack me) { - deque_to_array(arr, me->deque_data); + deque_copy_to_array(arr, me->deque_data); } /** diff --git a/src/stack.h b/src/stack.h index 1f3e541..05f4b51 100644 --- a/src/stack.h +++ b/src/stack.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,7 +34,7 @@ stack stack_init(size_t data_size); int stack_size(stack me); bool stack_is_empty(stack me); int stack_trim(stack me); -void stack_to_array(void *arr, stack me); +void stack_copy_to_array(void *arr, stack me); // Adding int stack_push(stack me, void *data); diff --git a/src/vector.c b/src/vector.c index f3e8f5b..5fd5332 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -137,11 +137,26 @@ int vector_trim(vector me) * @param arr The array to copy to. * @param me The vector to copy from. */ -void vector_to_array(void *const arr, vector me) +void vector_copy_to_array(void *const arr, vector me) { memcpy(arr, me->storage, me->offset * me->data_size); } +/** + * Gets the storage element of the vector which is contiguous in memory. If the + * data is modified, the data in the vector is modified. Also, any vector + * operation may invalidate this data pointer. The vector owns the data pointer, + * thus it must not be freed. + * + * @param me The vector to get the storage element from. + * + * @return The storage element of the vector. + */ +void *vector_get_data(vector me) +{ + return me->storage; +} + /** * Adds an element to the start of the vector. * diff --git a/src/vector.h b/src/vector.h index 246bd53..0175517 100644 --- a/src/vector.h +++ b/src/vector.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Bailey Thompson + * Copyright (c) 2017-2018 Bailey Thompson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -35,7 +35,8 @@ int vector_size(vector me); bool vector_is_empty(vector me); int vector_reserve(vector me, int size); int vector_trim(vector me); -void vector_to_array(void *arr, vector me); +void vector_copy_to_array(void *arr, vector me); +void *vector_get_data(vector me); // Adding int vector_add_first(vector me, void *data); diff --git a/tst/array.c b/tst/array.c index cabbc05..f8db23b 100644 --- a/tst/array.c +++ b/tst/array.c @@ -23,10 +23,14 @@ void test_array(void) assert(g == i); } int arr[10] = {0}; - array_to_array(arr, a); + array_copy_to_array(arr, a); for (int i = 0; i < 10; i++) { assert(arr[i] == i); } + int *data = array_get_data(a); + for (int i = 0; i < 10; i++) { + assert(data[i] == i); + } a = array_destroy(a); assert(a == NULL); } diff --git a/tst/deque.c b/tst/deque.c index 796d758..a564cf8 100644 --- a/tst/deque.c +++ b/tst/deque.c @@ -17,7 +17,7 @@ void test_deque(void) assert(deque_size(me) == 10); assert(!deque_is_empty(me)); int get_arr[10] = {0}; - deque_to_array(get_arr, me); + deque_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { int get = 0; deque_get_at(&get, me, i); @@ -31,7 +31,7 @@ void test_deque(void) deque_pop_back(&stuff, me); assert(stuff == val[i]); } - deque_to_array(trimmed, me); + deque_copy_to_array(trimmed, me); assert(deque_size(me) == 3); for (int i = 0; i < 3; i++) { assert(10 - i == trimmed[i]); @@ -88,7 +88,7 @@ void test_deque(void) set = 14; deque_set_last(me, &set); int arr[3] = {0}; - deque_to_array(arr, me); + deque_copy_to_array(arr, me); assert(arr[0] == 12); assert(arr[1] == 13); assert(arr[2] == 14); @@ -98,7 +98,7 @@ void test_deque(void) deque_set_at(me, 1, &set); set = -7; deque_set_at(me, 2, &set); - deque_to_array(arr, me); + deque_copy_to_array(arr, me); assert(arr[0] == -5); assert(arr[1] == -6); assert(arr[2] == -7); diff --git a/tst/forward_list.c b/tst/forward_list.c index 6337663..171c645 100644 --- a/tst/forward_list.c +++ b/tst/forward_list.c @@ -18,7 +18,7 @@ void test_forward_list(void) assert(forward_list_size(me) == 10); assert(!forward_list_is_empty(me)); int get_arr[10] = {0}; - forward_list_to_array(get_arr, me); + forward_list_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { int get = 0; forward_list_get_at(&get, me, i); @@ -29,7 +29,7 @@ void test_forward_list(void) forward_list_remove_last(me); } int trimmed[5] = {0}; - forward_list_to_array(trimmed, me); + forward_list_copy_to_array(trimmed, me); assert(forward_list_size(me) == 3); for (int i = 0; i < 3; i++) { assert(10 - i == trimmed[i]); @@ -83,7 +83,7 @@ void test_forward_list(void) set = 14; forward_list_set_last(me, &set); int arr[3] = {0}; - forward_list_to_array(arr, me); + forward_list_copy_to_array(arr, me); assert(arr[0] == 12); assert(arr[1] == 13); assert(arr[2] == 14); @@ -93,7 +93,7 @@ void test_forward_list(void) forward_list_set_at(me, 1, &set); set = -7; forward_list_set_at(me, 2, &set); - forward_list_to_array(arr, me); + forward_list_copy_to_array(arr, me); assert(arr[0] == -5); assert(arr[1] == -6); assert(arr[2] == -7); diff --git a/tst/list.c b/tst/list.c index 038bb33..3b873d2 100644 --- a/tst/list.c +++ b/tst/list.c @@ -18,7 +18,7 @@ void test_list(void) assert(list_size(me) == 10); assert(!list_is_empty(me)); int get_arr[10] = {0}; - list_to_array(get_arr, me); + list_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { int get = 0; list_get_at(&get, me, i); @@ -29,7 +29,7 @@ void test_list(void) list_remove_last(me); } int trimmed[5] = {0}; - list_to_array(trimmed, me); + list_copy_to_array(trimmed, me); assert(list_size(me) == 3); for (int i = 0; i < 3; i++) { assert(10 - i == trimmed[i]); @@ -83,7 +83,7 @@ void test_list(void) set = 14; list_set_last(me, &set); int arr[3] = {0}; - list_to_array(arr, me); + list_copy_to_array(arr, me); assert(arr[0] == 12); assert(arr[1] == 13); assert(arr[2] == 14); @@ -93,7 +93,7 @@ void test_list(void) list_set_at(me, 1, &set); set = -7; list_set_at(me, 2, &set); - list_to_array(arr, me); + list_copy_to_array(arr, me); assert(arr[0] == -5); assert(arr[1] == -6); assert(arr[2] == -7); diff --git a/tst/priority_queue.c b/tst/priority_queue.c index d1c7bab..4b022d2 100644 --- a/tst/priority_queue.c +++ b/tst/priority_queue.c @@ -2,16 +2,6 @@ #include "../src/vector.h" #include "../src/priority_queue.h" -/* - * Include this for the stubs. - */ -struct _vector { - size_t data_size; - int offset; - int space; - void *storage; -}; - /* * Include this for the stubs. */ @@ -23,7 +13,7 @@ struct _priority_queue { static void priority_queue_verify(priority_queue me) { - void *const vector_storage = me->data->storage; + void *const vector_storage = vector_get_data(me->data); const int size = vector_size(me->data); for (int i = 0; i < size; i++) { const int val = *(int *) (vector_storage + i * me->data_size); diff --git a/tst/queue.c b/tst/queue.c index e543962..0ebd690 100644 --- a/tst/queue.c +++ b/tst/queue.c @@ -20,7 +20,7 @@ void test_queue(void) assert(queue_size(me) == 10); assert(!queue_is_empty(me)); int get_arr[10] = {0}; - queue_to_array(get_arr, me); + queue_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { assert(get_arr[i] == i + 1); } diff --git a/tst/stack.c b/tst/stack.c index 7bc7d96..98a075b 100644 --- a/tst/stack.c +++ b/tst/stack.c @@ -17,7 +17,7 @@ void test_stack(void) assert(stack_size(me) == 10); assert(!stack_is_empty(me)); int get_arr[10] = {0}; - stack_to_array(get_arr, me); + stack_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { assert(get_arr[i] == i + 1); } diff --git a/tst/vector.c b/tst/vector.c index 2ba0aa8..89c3b58 100644 --- a/tst/vector.c +++ b/tst/vector.c @@ -45,20 +45,24 @@ void test_vector(void) assert(vector_size(me) == 10); assert(!vector_is_empty(me)); int get_arr[10] = {0}; - vector_to_array(get_arr, me); + vector_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { int get = 0; vector_get_at(&get, me, i); assert(get == val[9 - i]); assert(get_arr[i] == val[9 - i]); } + int *data = vector_get_data(me); + for (int i = 0; i < 10; i++) { + assert(data[i] == val[9 - i]); + } int trimmed[5] = {0}; vector_trim(me); vector_reserve(me, 3); for (int i = 0; i < 7; i++) { vector_remove_last(me); } - vector_to_array(trimmed, me); + vector_copy_to_array(trimmed, me); assert(vector_size(me) == 3); for (int i = 0; i < 3; i++) { assert(10 - i == trimmed[i]); @@ -112,7 +116,7 @@ void test_vector(void) set = 14; vector_set_last(me, &set); int arr[3] = {0}; - vector_to_array(arr, me); + vector_copy_to_array(arr, me); assert(arr[0] == 12); assert(arr[1] == 13); assert(arr[2] == 14); @@ -122,7 +126,7 @@ void test_vector(void) vector_set_at(me, 1, &set); set = -7; vector_set_at(me, 2, &set); - vector_to_array(arr, me); + vector_copy_to_array(arr, me); assert(arr[0] == -5); assert(arr[1] == -6); assert(arr[2] == -7);