summaryrefslogtreecommitdiff
path: root/source/kit/array_ref.c
blob: 4c1b1a4f738776e315dfac4c8c5b9a6c14be4718 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "array_ref.h"

#include <string.h>

int kit_ar_equal_bytes(ptrdiff_t left_element_size,
                       ptrdiff_t left_size, void *left_data,
                       ptrdiff_t right_element_size,
                       ptrdiff_t right_size, void *right_data) {
  ptrdiff_t i;
  if (left_element_size != right_element_size)
    return 0;
  if (left_size != right_size)
    return 0;
  for (i = 0; i < left_size; i++)
    if (memcmp((char *) left_data + i * left_element_size,
               (char *) right_data + i * left_element_size,
               left_element_size) != 0)
      return 0;
  return 1;
}

int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size,
                   void *left_data, ptrdiff_t right_element_size,
                   ptrdiff_t right_size, void *right_data,
                   kit_ar_compare_fn compare) {
  ptrdiff_t i;
  if (left_element_size < right_element_size)
    return -1;
  if (left_element_size > right_element_size)
    return 1;
  for (i = 0; i < left_size && i < right_size; i++) {
    int c = compare((char *) left_data + i * left_element_size,
                    (char *) right_data + i * left_element_size);
    if (c != 0)
      return c;
  }
  if (left_size < right_size)
    return -1;
  if (left_size > right_size)
    return 1;
  return 0;
}