| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2006, 2007 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file lib/selection.c |
| 19 | * @brief Select management for Disobedience |
| 20 | */ |
| 21 | |
| 22 | #include "common.h" |
| 23 | |
| 24 | #include "mem.h" |
| 25 | #include "hash.h" |
| 26 | #include "selection.h" |
| 27 | |
| 28 | /** @brief Create a new selection manager |
| 29 | * @return Pointer to @ref hash used to manage the selection |
| 30 | */ |
| 31 | hash *selection_new(void) { |
| 32 | return hash_new(sizeof (int)); |
| 33 | } |
| 34 | |
| 35 | /** @brief Add or remove a key in a selection |
| 36 | * @param h Hash representing selection |
| 37 | * @param key Key to insert |
| 38 | * @param selected non-0 if key is selected, 0 if it is not |
| 39 | * |
| 40 | * @p key is copied so the pointer need not remain valid. Newly selected keys |
| 41 | * are not marked as live. |
| 42 | */ |
| 43 | void selection_set(hash *h, const char *key, int selected) { |
| 44 | if(selected) { |
| 45 | int *const liveness = xmalloc_noptr(sizeof (int)); |
| 46 | *liveness = 0; |
| 47 | hash_add(h, key, liveness, HASH_INSERT_OR_REPLACE); |
| 48 | } else |
| 49 | hash_remove(h, key); |
| 50 | } |
| 51 | |
| 52 | /** @brief Test whether a key is set in a selection |
| 53 | * @param h Hash representing selection |
| 54 | * @param key Key to check |
| 55 | * @return non-0 if key is present, 0 if it is not |
| 56 | */ |
| 57 | int selection_selected(hash *h, const char *key) { |
| 58 | return hash_find(h, key) != 0; |
| 59 | } |
| 60 | |
| 61 | /** @brief Invert a key's selection status |
| 62 | * @param h Hash representing selection |
| 63 | * @param key Key to flip |
| 64 | * |
| 65 | * If the key is selected as a result it is not marked as live. |
| 66 | */ |
| 67 | void selection_flip(hash *h, const char *key) { |
| 68 | selection_set(h, key, !selection_selected(h, key)); |
| 69 | } |
| 70 | |
| 71 | /** @brief Mark a selection key as live |
| 72 | * @param h Hash representing selection |
| 73 | * @param key Key to mark as live |
| 74 | * |
| 75 | * Live keys will survive a call to selection_cleanup(). @p need not be in the |
| 76 | * selection (if it is not then the call will be ignored). |
| 77 | */ |
| 78 | void selection_live(hash *h, const char *key) { |
| 79 | int *ptr = hash_find(h, key); |
| 80 | |
| 81 | if(ptr) |
| 82 | *ptr = 1; |
| 83 | } |
| 84 | |
| 85 | static int selection_cleanup_callback(const char *key, |
| 86 | void *value, |
| 87 | void *v) { |
| 88 | if(*(int *)value) |
| 89 | *(int *)value = 0; |
| 90 | else |
| 91 | hash_remove((hash *)v, key); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /** @brief Delete all non-live keys from a selection |
| 96 | * @param h Hash representing selection |
| 97 | * |
| 98 | * After cleanup, no keys are marked as live. |
| 99 | */ |
| 100 | void selection_cleanup(hash *h) { |
| 101 | hash_foreach(h, selection_cleanup_callback, h); |
| 102 | } |
| 103 | |
| 104 | static int selection_empty_callback(const char *key, |
| 105 | void attribute((unused)) *value, |
| 106 | void *v) { |
| 107 | hash_remove((hash *)v, key); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /** @brief Remove all keys from a selection |
| 112 | * @param h Hash representing selection |
| 113 | */ |
| 114 | void selection_empty(hash *h) { |
| 115 | hash_foreach(h, selection_empty_callback, h); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | Local Variables: |
| 120 | c-basic-offset:2 |
| 121 | comment-column:40 |
| 122 | fill-column:79 |
| 123 | indent-tabs-mode:nil |
| 124 | End: |
| 125 | */ |