add single instance mode
[uwplayer.git] / ucx / map.c
1 /*\r
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.\r
3  *\r
4  * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.\r
5  *\r
6  * Redistribution and use in source and binary forms, with or without\r
7  * modification, are permitted provided that the following conditions are met:\r
8  *\r
9  *   1. Redistributions of source code must retain the above copyright\r
10  *      notice, this list of conditions and the following disclaimer.\r
11  *\r
12  *   2. Redistributions in binary form must reproduce the above copyright\r
13  *      notice, this list of conditions and the following disclaimer in the\r
14  *      documentation and/or other materials provided with the distribution.\r
15  *\r
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE\r
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
26  * POSSIBILITY OF SUCH DAMAGE.\r
27  */\r
28 \r
29 #include "cx/map.h"\r
30 #include <string.h>\r
31 \r
32 // <editor-fold desc="empty map implementation">\r
33 \r
34 static void cx_empty_map_noop(__attribute__((__unused__)) CxMap *map) {\r
35     // this is a noop, but MUST be implemented\r
36 }\r
37 \r
38 static void *cx_empty_map_get(\r
39         __attribute__((__unused__)) CxMap const *map,\r
40         __attribute__((__unused__)) CxHashKey key\r
41 ) {\r
42     return NULL;\r
43 }\r
44 \r
45 static bool cx_empty_map_iter_valid(__attribute__((__unused__)) void const *iter) {\r
46     return false;\r
47 }\r
48 \r
49 static CxIterator cx_empty_map_iterator(\r
50         struct cx_map_s const *map,\r
51         __attribute__((__unused__)) enum cx_map_iterator_type type\r
52 ) {\r
53     CxIterator iter = {0};\r
54     iter.src_handle = map;\r
55     iter.base.valid = cx_empty_map_iter_valid;\r
56     return iter;\r
57 }\r
58 \r
59 static struct cx_map_class_s cx_empty_map_class = {\r
60         cx_empty_map_noop,\r
61         cx_empty_map_noop,\r
62         NULL,\r
63         cx_empty_map_get,\r
64         NULL,\r
65         cx_empty_map_iterator\r
66 };\r
67 \r
68 CxMap cx_empty_map = {\r
69         NULL,\r
70         NULL,\r
71         0,\r
72         0,\r
73         NULL,\r
74         NULL,\r
75         NULL,\r
76         false,\r
77         &cx_empty_map_class\r
78 };\r
79 \r
80 CxMap *const cxEmptyMap = &cx_empty_map;\r
81 \r
82 // </editor-fold>\r
83 \r
84 CxMutIterator cxMapMutIteratorValues(CxMap *map) {\r
85     CxIterator it = map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);\r
86     it.base.mutating = true;\r
87 \r
88     // we know the iterators share the same memory layout\r
89     CxMutIterator iter;\r
90     memcpy(&iter, &it, sizeof(CxMutIterator));\r
91     return iter;\r
92 }\r
93 \r
94 CxMutIterator cxMapMutIteratorKeys(CxMap *map) {\r
95     CxIterator it = map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);\r
96     it.base.mutating = true;\r
97 \r
98     // we know the iterators share the same memory layout\r
99     CxMutIterator iter;\r
100     memcpy(&iter, &it, sizeof(CxMutIterator));\r
101     return iter;\r
102 }\r
103 \r
104 CxMutIterator cxMapMutIterator(CxMap *map) {\r
105     CxIterator it = map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);\r
106     it.base.mutating = true;\r
107 \r
108     // we know the iterators share the same memory layout\r
109     CxMutIterator iter;\r
110     memcpy(&iter, &it, sizeof(CxMutIterator));\r
111     return iter;\r
112 }\r