ppos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit c970639b21bb134457a151b15832e1f008dd1e52
parent a47d3a7d0a81f33d791519131faed4f0f2a02e68
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date:   Thu,  6 Aug 2026 17:16:39 -0300

Map.

Diffstat:
Mlib/map.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mtad-vazio/map.c | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 197 insertions(+), 2 deletions(-)

diff --git a/lib/map.c b/lib/map.c @@ -1,8 +1,106 @@ // PingPongOS - PingPong Operating System -// © Prof. Carlos A. Maziero, DINF UFPR -// Versão 2.1 -- 06/2026 +// Prof. Carlos A. Maziero, DINF UFPR +// Versão 2.1 -- 07/2026 // Este arquivo PODE/DEVE ser alterado. // Implementação do TAD Mapa de objetos +#include "map.h" + +#include <stdio.h> +#include <stdlib.h> + +struct map_t { + int size; + void* items[]; +}; + +struct map_t* map_create(int size) +{ + struct map_t* map = calloc(1, sizeof(*map) + sizeof(void*) * size); + map->size = size; + return map; +} + +int map_destroy(struct map_t* map) +{ + if (map == NULL) + return ERROR; + + free(map); + + return NOERROR; +} + +int map_put(struct map_t* map, void* object) +{ + if (map == NULL) + return ERROR; + + for (int i = 0; i < map->size; i += 1) { + if (map->items[i] == NULL) { + map->items[i] = object; + return i; + } + } + + return ERROR; +} + +void* map_get(struct map_t* map, int id) +{ + if (map == NULL || id >= map->size) + return NULL; + return map->items[id]; +} + +void* map_del(struct map_t* map, int id) +{ + if (map == NULL || id >= map->size) + return NULL; + + void* p = map->items[id]; + map->items[id] = NULL; + return p; +} + +int map_items(struct map_t* map) +{ + if (map == NULL) + return ERROR; + + int c = 0; + for (int i = 0; i < map->size; i += 1) + c += map->items[i] != NULL; + + return c; +} + +int map_size(struct map_t* map) +{ + if (map == NULL) + return ERROR; + return map->size; +} + +void map_print(char* name, struct map_t* map) +{ + printf("%s: ", name); + if (map == NULL) { + printf("undef\n"); + return; + } + printf("[ "); + + int c = 0; + for (int i = 0; i < map->size; i += 1) { + if (map->items[i] == NULL) { + printf("- "); + } else { + printf("* "); + c += 1; + } + } + printf("] (%d/%d)\n", c, map->size); +} diff --git a/tad-vazio/map.c b/tad-vazio/map.c @@ -6,4 +6,101 @@ // Implementação do TAD Mapa de objetos +#include "map.h" +#include <stdio.h> +#include <stdlib.h> + +struct map_t { + int size; + void* items[]; +}; + +struct map_t* map_create(int size) +{ + struct map_t* map = calloc(1, sizeof(*map) + sizeof(void*) * size); + map->size = size; + return map; +} + +int map_destroy(struct map_t* map) +{ + if (map == NULL) + return ERROR; + + free(map); + + return NOERROR; +} + +int map_put(struct map_t* map, void* object) +{ + if (map == NULL) + return ERROR; + + for (int i = 0; i < map->size; i += 1) { + if (map->items[i] == NULL) { + map->items[i] = object; + return i; + } + } + + return ERROR; +} + +void* map_get(struct map_t* map, int id) +{ + if (map == NULL || id >= map->size) + return NULL; + return map->items[id]; +} + +void* map_del(struct map_t* map, int id) +{ + if (map == NULL || id >= map->size) + return NULL; + + void* p = map->items[id]; + map->items[id] = NULL; + return p; +} + +int map_items(struct map_t* map) +{ + if (map == NULL) + return ERROR; + + int c = 0; + for (int i = 0; i < map->size; i += 1) + c += map->items[i] != NULL; + + return c; +} + +int map_size(struct map_t* map) +{ + if (map == NULL) + return ERROR; + return map->size; +} + +void map_print(char* name, struct map_t* map) +{ + printf("%s: ", name); + if (map == NULL) { + printf("undef\n"); + return; + } + printf("[ "); + + int c = 0; + for (int i = 0; i < map->size; i += 1) { + if (map->items[i] == NULL) { + printf("- "); + } else { + printf("* "); + c += 1; + } + } + printf("] (%d/%d)\n", c, map->size); +}