array.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // C source file with the function definitions to handle generic arrays
  2. // check_array()
  3. // function to check the paramaters of a raw array
  4. bool check_array(byte * arr, umax asize, umax var_size)
  5. {
  6. return (check_byte_array(arr, asize) && var_size != 0);
  7. }
  8. // print_arr_elems() function
  9. // function to print the elements of a raw array. It needs the array size, the
  10. // size of an individual element and also a function that can "print" the element
  11. umax print_arr_elems(byte * arr, umax asize, umax var_size, umax (* print_elem) (byte *, umax))
  12. {
  13. // check function parameters
  14. if (check_array(arr, asize, var_size) == false || print_elem == NULL)
  15. return 0;
  16. // print the array elements
  17. for (umax i = 0; i < asize; i++) {
  18. arr += print_elem(arr, var_size);
  19. putchar(' ');
  20. }
  21. // end the line and return the number of bytes read
  22. putchar('\n');
  23. return asize * var_size;
  24. }
  25. // reverse_arr() function
  26. // function to reverse the order of the variables of an array
  27. // it is a raw byte mevement basically, considering variable size
  28. umax reverse_arr(byte * arr, umax asize, umax var_size)
  29. {
  30. // check function parameters
  31. if (check_array(arr, asize, var_size) == false)
  32. return false;
  33. // calculate the middle position (truncated)
  34. umax mid_pos = (asize / 2);
  35. // create temp array to hold the array variable's bytes when doing the data movement
  36. mem_space * elem_tmp = create_mem_space(var_size);
  37. // cast the array pointer conveniently (pointer to arrays of size var_size that contain bytes)
  38. byte (* ptr)[var_size] = (void *) arr;
  39. // swap array elements positions
  40. for (umax i = 0; i < mid_pos; i++) {
  41. // swap element i with element arr_size - i - 1
  42. cp_mem_bytes(ptr[i], var_size, elem_tmp -> ptr);
  43. cp_mem_bytes(ptr[asize - i - 1], var_size, ptr[i]);
  44. cp_mem_bytes(elem_tmp -> ptr, var_size, ptr[asize - i - 1]);
  45. }
  46. // free memory used
  47. free_mem_space(elem_tmp);
  48. return true;
  49. }
  50. // find_arr_elem() function
  51. // function to find for an element of in an array.
  52. // it will return the first occurrence of the element. Needs a function to make the comparison.
  53. byte * find_arr_elem(byte * elem, umax esize, byte * arr, umax asize, bool (* comp_elems) (byte *, byte *))
  54. {
  55. // check function parameters
  56. if (check_byte_array(elem, esize) == false || check_byte_array(arr, asize) == false || comp_elems == NULL)
  57. goto err;
  58. // search in array for said element
  59. for (umax i = 0; i < asize; i++, arr += esize)
  60. if (comp_elems(elem, arr))
  61. return arr;
  62. err: // error/element not found
  63. return NULL;
  64. }
  65. // sort_arr() function
  66. // function to sort the contents of an array
  67. // this function will order elements from "lower" to "larger"
  68. umax sort_arr(byte * arr, umax asize, umax var_size, bool (* comp_elems) (byte *, byte *))
  69. {
  70. // check function parameters
  71. if (check_array(arr, asize, var_size) == false || comp_elems == NULL)
  72. return false;
  73. // create temporal array to hold the array data to be moved
  74. mem_space * elem_tmp = create_mem_space(var_size);
  75. // cast the array pointer conveniently (pointer to arrays of size var_size that contain bytes)
  76. byte (* ptr)[var_size] = (void *) arr;
  77. // compare each array elements
  78. for (umax i = 0; i < asize; i++)
  79. for (umax j = i + 1; j < asize; j++)
  80. // swap element i and element j positions
  81. if (comp_elems(ptr[i], ptr[j])) {
  82. cp_mem_bytes(ptr[j], var_size, elem_tmp -> ptr);
  83. cp_mem_bytes(ptr[i], var_size, ptr[j]);
  84. cp_mem_bytes(elem_tmp -> ptr, var_size, ptr[i]);
  85. }
  86. // free memory used and return the byte size of the array
  87. free_mem_space(elem_tmp);
  88. return asize * var_size;
  89. }