[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
stdlib.c File Reference
#include <ctype.h>
#include <debug.h>
#include <random.h>
#include <stdlib.h>
#include <stdbool.h>
Include dependency graph for stdlib.c:

Functions

int atoi (const char *s)
 
static int compare_thunk (const void *a, const void *b, void *aux)
 
void qsort (void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *))
 
static void do_swap (unsigned char *array, size_t a_idx, size_t b_idx, size_t size)
 
static int do_compare (unsigned char *array, size_t a_idx, size_t b_idx, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
 
static void heapify (unsigned char *array, size_t i, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
 
void sort (void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
 
void * bsearch (const void *key, const void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *))
 
void * binary_search (const void *key, const void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
 

Function Documentation

◆ atoi()

int atoi ( const char *  s)
11{
12 bool negative;
13 int value;
14
15 ASSERT (s != NULL);
16
17 /* Skip white space. */
18 while (isspace ((unsigned char) *s))
19 s++;
20
21 /* Parse sign. */
22 negative = false;
23 if (*s == '+')
24 s++;
25 else if (*s == '-')
26 {
27 negative = true;
28 s++;
29 }
30
31 /* Parse digits. We always initially parse the value as
32 negative, and then make it positive later, because the
33 negative range of an int is bigger than the positive range
34 on a 2's complement system. */
35 for (value = 0; isdigit (*s); s++)
36 value = value * 10 - (*s - '0');
37 if (!negative)
38 value = -value;
39
40 return value;
41}
static int isspace(int c)
Definition: ctype.h:12
static int isdigit(int c)
Definition: ctype.h:7
#define ASSERT(CONDITION)
Definition: debug.h:30
static uint8_t s[256]
Definition: random.c:17
#define NULL
Definition: stddef.h:4
Here is the call graph for this function:
Here is the caller graph for this function:

◆ binary_search()

void * binary_search ( const void *  key,
const void *  array,
size_t  cnt,
size_t  size,
int(*)(const void *, const void *, void *aux)  compare,
void *  aux 
)
188{
189 const unsigned char *first = array;
190 const unsigned char *last = array + size * cnt;
191
192 while (first < last)
193 {
194 size_t range = (last - first) / size;
195 const unsigned char *middle = first + (range / 2) * size;
196 int cmp = compare (key, middle, aux);
197
198 if (cmp < 0)
199 last = middle;
200 else if (cmp > 0)
201 first = middle + size;
202 else
203 return (void *) middle;
204 }
205
206 return NULL;
207}
uint16_t size
Definition: mmu.h:0
Here is the caller graph for this function:

◆ bsearch()

void * bsearch ( const void *  key,
const void *  array,
size_t  cnt,
size_t  size,
int(*)(const void *, const void *)  compare 
)
168{
169 return binary_search (key, array, cnt, size, compare_thunk, &compare);
170}
void * binary_search(const void *key, const void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
Definition: stdlib.c:185
static int compare_thunk(const void *a, const void *b, void *aux)
Definition: stdlib.c:45
Here is the call graph for this function:

◆ compare_thunk()

static int compare_thunk ( const void *  a,
const void *  b,
void *  aux 
)
static
46{
47 int (**compare) (const void *, const void *) = aux;
48 return (*compare) (a, b);
49}
Here is the caller graph for this function:

◆ do_compare()

static int do_compare ( unsigned char *  array,
size_t  a_idx,
size_t  b_idx,
size_t  size,
int(*)(const void *, const void *, void *aux)  compare,
void *  aux 
)
static
89{
90 return compare (array + (a_idx - 1) * size, array + (b_idx - 1) * size, aux);
91}
Here is the caller graph for this function:

◆ do_swap()

static void do_swap ( unsigned char *  array,
size_t  a_idx,
size_t  b_idx,
size_t  size 
)
static
68{
69 unsigned char *a = array + (a_idx - 1) * size;
70 unsigned char *b = array + (b_idx - 1) * size;
71 size_t i;
72
73 for (i = 0; i < size; i++)
74 {
75 unsigned char t = a[i];
76 a[i] = b[i];
77 b[i] = t;
78 }
79}
Here is the caller graph for this function:

◆ heapify()

static void heapify ( unsigned char *  array,
size_t  i,
size_t  cnt,
size_t  size,
int(*)(const void *, const void *, void *aux)  compare,
void *  aux 
)
static
100{
101 for (;;)
102 {
103 /* Set `max' to the index of the largest element among I
104 and its children (if any). */
105 size_t left = 2 * i;
106 size_t right = 2 * i + 1;
107 size_t max = i;
108 if (left <= cnt && do_compare (array, left, max, size, compare, aux) > 0)
109 max = left;
110 if (right <= cnt
111 && do_compare (array, right, max, size, compare, aux) > 0)
112 max = right;
113
114 /* If the maximum value is already in element I, we're
115 done. */
116 if (max == i)
117 break;
118
119 /* Swap and continue down the heap. */
120 do_swap (array, i, max, size);
121 i = max;
122 }
123}
static void do_swap(unsigned char *array, size_t a_idx, size_t b_idx, size_t size)
Definition: stdlib.c:67
static int do_compare(unsigned char *array, size_t a_idx, size_t b_idx, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
Definition: stdlib.c:86
Here is the call graph for this function:
Here is the caller graph for this function:

◆ qsort()

void qsort ( void *  array,
size_t  cnt,
size_t  size,
int(*)(const void *, const void *)  compare 
)
60{
61 sort (array, cnt, size, compare_thunk, &compare);
62}
void sort(void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
Definition: stdlib.c:132
Here is the call graph for this function:

◆ sort()

void sort ( void *  array,
size_t  cnt,
size_t  size,
int(*)(const void *, const void *, void *aux)  compare,
void *  aux 
)
135{
136 size_t i;
137
138 ASSERT (array != NULL || cnt == 0);
139 ASSERT (compare != NULL);
140 ASSERT (size > 0);
141
142 /* Build a heap. */
143 for (i = cnt / 2; i > 0; i--)
144 heapify (array, i, cnt, size, compare, aux);
145
146 /* Sort the heap. */
147 for (i = cnt; i > 1; i--)
148 {
149 do_swap (array, 1, i, size);
150 heapify (array, 1, i - 1, size, compare, aux);
151 }
152}
static void heapify(unsigned char *array, size_t i, size_t cnt, size_t size, int(*compare)(const void *, const void *, void *aux), void *aux)
Definition: stdlib.c:97
Here is the call graph for this function:
Here is the caller graph for this function: