cdcontainers  0.1.1
Library of data containers and collections for C programming language.
Data Structures | Functions
map.h File Reference

The cdc_map is a struct and functions that provide a map. More...

#include <cdcontainers/common.h>
#include <cdcontainers/status.h>
#include <cdcontainers/tables/imap.h>
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  cdc_map
 The cdc_map is service struct. More...
 
struct  cdc_map_iter
 The cdc_map_iter is service struct. More...
 

Functions

enum cdc_stat cdc_map_ctor (const struct cdc_map_table *table, struct cdc_map **m, struct cdc_data_info *info)
 Constructs an empty map. More...
 
enum cdc_stat cdc_map_ctorl (const struct cdc_map_table *table, struct cdc_map **m, struct cdc_data_info *info,...)
 Constructs a map, initialized by an variable number of pointers on cdc_pair's(first - key, and the second - value). The last item must be CDC_END. More...
 
enum cdc_stat cdc_map_ctorv (const struct cdc_map_table *table, struct cdc_map **m, struct cdc_data_info *info, va_list args)
 Constructs a map, initialized by args. The last item must be CDC_END. More...
 
void cdc_map_dtor (struct cdc_map *m)
 Destroys the map. More...
 
static enum cdc_stat cdc_map_get (struct cdc_map *m, void *key, void **value)
 Returns a value that is mapped to a key. If the key does not exist, then NULL will return. More...
 
static size_t cdc_map_count (struct cdc_map *m, void *key)
 Returns the number of elements with key that compares equal to the specified argument key, which is either 1 or 0 since this container does not allow duplicates. More...
 
static void cdc_map_find (struct cdc_map *m, void *key, struct cdc_map_iter *it)
 Finds an element with key equivalent to key. More...
 
static size_t cdc_map_size (struct cdc_map *m)
 Returns the number of items in the map. More...
 
static bool cdc_map_empty (struct cdc_map *m)
 Checks if the map has no elements. More...
 
static void cdc_map_clear (struct cdc_map *m)
 Removes all the elements from the map. More...
 
static enum cdc_stat cdc_map_insert (struct cdc_map *m, void *key, void *value, struct cdc_map_iter *it, bool *inserted)
 Inserts an element into the container, if the container doesn't already contain an element with an equivalent key. More...
 
static enum cdc_stat cdc_map_insert_or_assign (struct cdc_map *m, void *key, void *value, struct cdc_map_iter *it, bool *inserted)
 Inserts an element or assigns to the current element if the key already exists. More...
 
static size_t cdc_map_erase (struct cdc_map *m, void *key)
 Removes the element (if one exists) with the key equivalent to key. More...
 
static void cdc_map_swap (struct cdc_map *a, struct cdc_map *b)
 Swaps maps a and b. This operation is very fast and never fails. More...
 
static void cdc_map_begin (struct cdc_map *m, struct cdc_map_iter *it)
 Initializes the iterator to the beginning. More...
 
static void cdc_map_end (struct cdc_map *m, struct cdc_map_iter *it)
 Initializes the iterator to the end. More...
 
enum cdc_stat cdc_map_iter_ctor (struct cdc_map *m, struct cdc_map_iter *it)
 Constructs a map iterator. Should be called for each new iterator. More...
 
static void cdc_map_iter_dtor (struct cdc_map_iter *it)
 Destroys the iterator. It should be called after the iterator is no longer needed. Releases resources. More...
 
static enum cdc_iterator_type cdc_map_iter_type (struct cdc_map_iter *it)
 Returns a type of iterator. More...
 
static void cdc_map_iter_next (struct cdc_map_iter *it)
 Advances the iterator to the next element in the map. More...
 
static void cdc_map_iter_prev (struct cdc_map_iter *it)
 Advances the iterator to the previous element in the map. More...
 
static bool cdc_map_iter_has_next (struct cdc_map_iter *it)
 Returns true if there is at least one element ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returns false. More...
 
static bool cdc_map_iter_has_prev (struct cdc_map_iter *it)
 Returns true if there is at least one element behind the iterator, i.e. the iterator is not at the front of the container; otherwise returns false. More...
 
static void * cdc_map_iter_key (struct cdc_map_iter *it)
 Returns an item's key. More...
 
static void * cdc_map_iter_value (struct cdc_map_iter *it)
 Returns an item's value. More...
 
static struct cdc_pair cdc_map_iter_key_value (struct cdc_map_iter *it)
 Returns a pair, where first - key, second - value. More...
 
static bool cdc_map_iter_is_eq (struct cdc_map_iter *it1, struct cdc_map_iter *it2)
 Returns false if the iterator |it1| equal to the iterator |it2|, otherwise returns false. More...
 

Detailed Description

The cdc_map is a struct and functions that provide a map.

Example usage map:

// The MIT License (MIT)
// Copyright (c) 2018 Maksim Andrianov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// Program to emulates the phone book.
#define CDC_USE_SHORT_NAMES
#include <stdio.h>
#include <string.h>
int str_lt(const void *lhs, const void *rhs)
{
return strcmp((const char *)lhs, (const char *)rhs) < 0;
}
int handle_error(map_t *phone_book)
{
map_dtor(phone_book);
return EXIT_FAILURE;
}
void print_phone_book(map_t *phone_book)
{
map_iter_t iter = CDC_INIT_STRUCT;
if (map_iter_ctor(phone_book, &iter) != CDC_STATUS_OK) {
printf("Error map_iter_init.\n");
return;
}
map_begin(phone_book, &iter);
while (map_iter_has_next(&iter)) {
printf("%s - %s\n",(char *)map_iter_key(&iter), (char *)map_iter_value(&iter));
map_iter_next(&iter);
}
}
int main(int argc, char **argv)
{
CDC_UNUSED(argc);
CDC_UNUSED(argv);
map_t *phone_book = NULL;
data_info_t data_info = CDC_INIT_STRUCT;
data_info.cmp = str_lt;
pair_t Lilia_Friedman = {.first = "Lilia Friedman", .second = "(892) 670-4739"};
pair_t Tariq_Beltran = {.first = "Tariq Beltran", .second = "(489) 600-7575"};
pair_t Laiba_Juarez = {.first = "Laiba Juarez", .second = "(303) 885-5692"};
pair_t Elliott_Mooney = {.first = "Elliott Mooney", .second = "(945) 616-4482"};
// map based on avl tree
if (map_ctorl(cdc_map_avl, &phone_book, &data_info, &Lilia_Friedman, &Tariq_Beltran,
&Laiba_Juarez, &Elliott_Mooney, CDC_END) != CDC_STATUS_OK) {
return EXIT_FAILURE;
}
printf("Phone book:\n");
print_phone_book(phone_book);
if (map_insert(phone_book, "Zak Byers", "(551) 396-1880",
NULL /* iterator */, NULL /* is_inserted */) != CDC_STATUS_OK) {
return handle_error(phone_book);
}
printf("Phone book after adding Zak Byers:\n");
print_phone_book(phone_book);
if (map_count(phone_book, "Tariq Beltran") != 0)
map_erase(phone_book, "Tariq Beltran");
printf("Phone book after erasing Tariq Beltran:\n");
print_phone_book(phone_book);
if (map_insert_or_assign(phone_book, "Zak Byers", "(555) 396-188",
NULL /* iterator */, NULL /* is_inserted */) != CDC_STATUS_OK) {
return handle_error(phone_book);
}
printf("Phone book after update phone of Zak Byers:\n");
print_phone_book(phone_book);
return EXIT_SUCCESS;
}
Author
Maksim Andrianov maksi.nosp@m.mand.nosp@m.riano.nosp@m.v1@y.nosp@m.andex.nosp@m..ru