2 * This file is part of DisOrder
3 * Copyright (C) 2006, 2007 Richard Kettlewell
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.
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.
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/>.
18 /** @file lib/selection.c
19 * @brief Select management for Disobedience
26 #include "selection.h"
28 /** @brief Create a new selection manager
29 * @return Pointer to @ref hash used to manage the selection
31 hash *selection_new(void) {
32 return hash_new(sizeof (int));
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
40 * @p key is copied so the pointer need not remain valid. Newly selected keys
41 * are not marked as live.
43 void selection_set(hash *h, const char *key, int selected) {
45 int *const liveness = xmalloc_noptr(sizeof (int));
47 hash_add(h, key, liveness, HASH_INSERT_OR_REPLACE);
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
57 int selection_selected(hash *h, const char *key) {
58 return hash_find(h, key) != 0;
61 /** @brief Invert a key's selection status
62 * @param h Hash representing selection
63 * @param key Key to flip
65 * If the key is selected as a result it is not marked as live.
67 void selection_flip(hash *h, const char *key) {
68 selection_set(h, key, !selection_selected(h, key));
71 /** @brief Mark a selection key as live
72 * @param h Hash representing selection
73 * @param key Key to mark as live
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).
78 void selection_live(hash *h, const char *key) {
79 int *ptr = hash_find(h, key);
85 static int selection_cleanup_callback(const char *key,
91 hash_remove((hash *)v, key);
95 /** @brief Delete all non-live keys from a selection
96 * @param h Hash representing selection
98 * After cleanup, no keys are marked as live.
100 void selection_cleanup(hash *h) {
101 hash_foreach(h, selection_cleanup_callback, h);
104 static int selection_empty_callback(const char *key,
105 void attribute((unused)) *value,
107 hash_remove((hash *)v, key);
111 /** @brief Remove all keys from a selection
112 * @param h Hash representing selection
114 void selection_empty(hash *h) {
115 hash_foreach(h, selection_empty_callback, h);