Derive-C
Loading...
Searching...
No Matches
employees.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/alloc/std.h>
#include <derive-c/core/prelude.h>
#include <derive-c/container/arena/basic/template.h>
#include <derive-c/container/vector/dynamic/template.h>
#include <derive-c/container/map/decomposed/template.h>
Include dependency graph for employees.c:

Go to the source code of this file.

Classes

struct  name
struct  age
struct  employee
struct  hr_system

Macros

#define INDEX_BITS   16
#define VALUE   employee
#define NAME   employees
#define ITEM   employees_index_t
#define NAME   same_age_employees
#define KEY   age
#define KEY_EQ   age_eq
#define KEY_HASH   age_hash
#define VALUE   same_age_employees
#define NAME   employees_by_age

Functions

bool name_eq (const name *name_1, const name *name_2)
bool age_eq (age const *age_1, age const *age_2)
size_t age_hash (age const *age)
hr_system hr_system_new ()
void hr_system_new_employee (hr_system *self, employee emp)
employee const * hr_system_newest_of_age (hr_system const *self, age age)
void hr_system_delete (hr_system *self)
int main ()

Macro Definition Documentation

◆ INDEX_BITS

#define INDEX_BITS   16

Definition at line 44 of file employees.c.

◆ ITEM

#define ITEM   employees_index_t

Definition at line 49 of file employees.c.

◆ KEY

#define KEY   age

Definition at line 53 of file employees.c.

◆ KEY_EQ

#define KEY_EQ   age_eq

Definition at line 54 of file employees.c.

◆ KEY_HASH

#define KEY_HASH   age_hash

Definition at line 55 of file employees.c.

◆ NAME [1/3]

#define NAME   employees

Definition at line 46 of file employees.c.

◆ NAME [2/3]

#define NAME   same_age_employees

Definition at line 46 of file employees.c.

◆ NAME [3/3]

#define NAME   employees_by_age

Definition at line 46 of file employees.c.

◆ VALUE [1/2]

#define VALUE   employee

Definition at line 45 of file employees.c.

◆ VALUE [2/2]

#define VALUE   same_age_employees

Definition at line 45 of file employees.c.

Function Documentation

◆ age_eq()

bool age_eq ( age const * age_1,
age const * age_2 )
Examples
complex/employees.c.

Definition at line 41 of file employees.c.

41{ return age_1->value == age_2->value; }

◆ age_hash()

size_t age_hash ( age const * age)
Examples
complex/employees.c.

Definition at line 42 of file employees.c.

42{ return age->value; }
int value
Definition employees.c:32

◆ hr_system_delete()

void hr_system_delete ( hr_system * self)
Examples
complex/employees.c.

Definition at line 96 of file employees.c.

96 {
97 employees_delete(&self->data);
98
99 employees_by_age_iter iter = employees_by_age_get_iter(&self->by_age);
100 employees_by_age_kv const* entry = NULL;
101 while ((entry = employees_by_age_iter_next(&iter))) {
102 same_age_employees_delete(entry->value);
103 }
104
105 employees_by_age_delete(&self->by_age);
106}
employees data
Definition employees.c:61
employees_by_age by_age
Definition employees.c:62
Here is the caller graph for this function:

◆ hr_system_new()

hr_system hr_system_new ( )
Examples
complex/employees.c.

Definition at line 65 of file employees.c.

65 {
66 return (hr_system){
67 .data = employees_new_with_capacity_for(1000, stdalloc_get()),
68 .by_age = employees_by_age_new(stdalloc_get()),
69 };
70}
Here is the caller graph for this function:

◆ hr_system_new_employee()

void hr_system_new_employee ( hr_system * self,
employee emp )
Examples
complex/employees.c.

Definition at line 72 of file employees.c.

72 {
73 printf("Adding employee %s %s\n", emp.name.forename, emp.name.surname);
74 employees_index_t idx = employees_insert(&self->data, emp);
75 same_age_employees* idxes = employees_by_age_try_write(&self->by_age, emp.age);
76 if (!idxes) {
77 idxes =
78 employees_by_age_insert(&self->by_age, emp.age, same_age_employees_new(stdalloc_get()));
79 }
80 same_age_employees_push(idxes, idx);
81}
age age
Definition employees.c:38
name name
Definition employees.c:36
char const * forename
Definition employees.c:15
char const * surname
Definition employees.c:16
Here is the caller graph for this function:

◆ hr_system_newest_of_age()

employee const * hr_system_newest_of_age ( hr_system const * self,
age age )
Examples
complex/employees.c.

Definition at line 83 of file employees.c.

83 {
84 same_age_employees const* idxes = employees_by_age_try_read(&self->by_age, age);
85 if (!idxes) {
86 return NULL;
87 }
88 if (same_age_employees_size(idxes) == 0) {
89 return NULL;
90 }
91 employees_index_t const* idx =
92 same_age_employees_read(idxes, same_age_employees_size(idxes) - 1);
93 return employees_read(&self->data, *idx);
94}
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 108 of file employees.c.

108 {
110
111 employee frank = {
112 .age = (age){.value = 22},
113 .email = "veryverylongemail@someprovider.net",
114 .name =
115 (name){
116 .forename = "Frank",
117 .surname = "Lee",
118 },
119 };
120 hr_system_new_employee(&hr, frank);
121
122 name bob_name = {
123 .forename = "Bob",
124 .surname = "Mike",
125 };
126 employee bob = {
127 .age = (age){.value = 22},
128 .email = "bib@cool.org",
129 .name = bob_name,
130 };
131 hr_system_new_employee(&hr, bob);
132
133 employee const* newest_22 = hr_system_newest_of_age(&hr, (age){.value = 22});
134 ASSERT(newest_22);
135 ASSERT(name_eq(&newest_22->name, &bob_name));
136
137 hr_system_delete(&hr);
138}
employee const * hr_system_newest_of_age(hr_system const *self, age age)
Definition employees.c:83
bool name_eq(const name *name_1, const name *name_2)
Definition employees.c:19
void hr_system_delete(hr_system *self)
Definition employees.c:96
hr_system hr_system_new()
Definition employees.c:65
void hr_system_new_employee(hr_system *self, employee emp)
Definition employees.c:72
#define ASSERT(expr,...)
Definition macros.h:42
Here is the call graph for this function:

◆ name_eq()

bool name_eq ( const name * name_1,
const name * name_2 )
Examples
complex/employees.c.

Definition at line 19 of file employees.c.

19 {
20 if (!name_1 || !name_2)
21 return false;
22 if (!name_1->forename || !name_2->forename)
23 return false;
24 if (!name_1->surname || !name_2->surname)
25 return false;
26
27 return strcmp(name_1->forename, name_2->forename) == 0 &&
28 strcmp(name_1->surname, name_2->surname) == 0;
29}
Here is the caller graph for this function: