209 __attribute__((__nonnull__)) |
210 __attribute__((__nonnull__)) |
210 static inline void cxMapClear(CxMap *map) { |
211 static inline void cxMapClear(CxMap *map) { |
211 map->cl->clear(map); |
212 map->cl->clear(map); |
212 } |
213 } |
213 |
214 |
|
215 |
|
216 // TODO: set-like map operations (union, intersect, difference) |
|
217 |
|
218 /** |
|
219 * Creates a value iterator for a map. |
|
220 * |
|
221 * \note An iterator iterates over all elements successively. Therefore the order |
|
222 * highly depends on the map implementation and may change arbitrarily when the contents change. |
|
223 * |
|
224 * @param map the map to create the iterator for |
|
225 * @return an iterator for the currently stored values |
|
226 */ |
|
227 __attribute__((__nonnull__, __warn_unused_result__)) |
|
228 static inline CxIterator cxMapIteratorValues(CxMap *map) { |
|
229 return map->cl->iterator_values(map); |
|
230 } |
|
231 |
|
232 /** |
|
233 * Creates a key iterator for a map. |
|
234 * |
|
235 * The elements of the iterator are keys of type CxHashKey. |
|
236 * |
|
237 * \note An iterator iterates over all elements successively. Therefore the order |
|
238 * highly depends on the map implementation and may change arbitrarily when the contents change. |
|
239 * |
|
240 * @param map the map to create the iterator for |
|
241 * @return an iterator for the currently stored keys |
|
242 */ |
|
243 __attribute__((__nonnull__, __warn_unused_result__)) |
|
244 static inline CxIterator cxMapIteratorKeys(CxMap *map) { |
|
245 return map->cl->iterator_keys(map); |
|
246 } |
|
247 |
|
248 /** |
|
249 * Creates an iterator for a map. |
|
250 * |
|
251 * The elements of the iterator are key/value pairs of type CxMapEntry. |
|
252 * |
|
253 * \note An iterator iterates over all elements successively. Therefore the order |
|
254 * highly depends on the map implementation and may change arbitrarily when the contents change. |
|
255 * |
|
256 * @param map the map to create the iterator for |
|
257 * @return an iterator for the currently stored entries |
|
258 * @see cxMapIteratorKeys() |
|
259 * @see cxMapIteratorValues() |
|
260 */ |
|
261 __attribute__((__nonnull__, __warn_unused_result__)) |
|
262 static inline CxIterator cxMapIterator(CxMap *map) { |
|
263 return map->cl->iterator(map); |
|
264 } |
|
265 |
|
266 |
|
267 /** |
|
268 * Creates a mutating iterator over the values of a map. |
|
269 * |
|
270 * \note An iterator iterates over all elements successively. Therefore the order |
|
271 * highly depends on the map implementation and may change arbitrarily when the contents change. |
|
272 * |
|
273 * @param map the map to create the iterator for |
|
274 * @return an iterator for the currently stored values |
|
275 */ |
|
276 __attribute__((__nonnull__, __warn_unused_result__)) |
|
277 static inline CxMutIterator cxMapMutIteratorValues(CxMap *map) { |
|
278 return map->cl->mut_iterator_values(map); |
|
279 } |
|
280 |
|
281 /** |
|
282 * Creates a mutating iterator over the keys of a map. |
|
283 * |
|
284 * The elements of the iterator are keys of type CxHashKey. |
|
285 * |
|
286 * \note An iterator iterates over all elements successively. Therefore the order |
|
287 * highly depends on the map implementation and may change arbitrarily when the contents change. |
|
288 * |
|
289 * @param map the map to create the iterator for |
|
290 * @return an iterator for the currently stored keys |
|
291 */ |
|
292 __attribute__((__nonnull__, __warn_unused_result__)) |
|
293 static inline CxMutIterator cxMapMutIteratorKeys(CxMap *map) { |
|
294 return map->cl->mut_iterator_keys(map); |
|
295 } |
|
296 |
|
297 /** |
|
298 * Creates a mutating iterator for a map. |
|
299 * |
|
300 * The elements of the iterator are key/value pairs of type CxMapEntry. |
|
301 * |
|
302 * \note An iterator iterates over all elements successively. Therefore the order |
|
303 * highly depends on the map implementation and may change arbitrarily when the contents change. |
|
304 * |
|
305 * @param map the map to create the iterator for |
|
306 * @return an iterator for the currently stored entries |
|
307 * @see cxMapMutIteratorKeys() |
|
308 * @see cxMapMutIteratorValues() |
|
309 */ |
|
310 __attribute__((__nonnull__, __warn_unused_result__)) |
|
311 static inline CxMutIterator cxMapMutIterator(CxMap *map) { |
|
312 return map->cl->mut_iterator(map); |
|
313 } |
|
314 |
|
315 #ifdef __cplusplus |
|
316 } // end the extern "C" block here, because we want to start overloading |
|
317 |
214 /** |
318 /** |
215 * Puts a key/value-pair into the map. |
319 * Puts a key/value-pair into the map. |
216 * |
320 * |
217 * @param map the map |
321 * @param map the map |
218 * @param key the key |
322 * @param key the key |
312 CxHashKey key |
569 CxHashKey key |
313 ) { |
570 ) { |
314 return map->cl->remove(map, key, !map->store_pointer); |
571 return map->cl->remove(map, key, !map->store_pointer); |
315 } |
572 } |
316 |
573 |
317 // TODO: set-like map operations (union, intersect, difference) |
574 /** |
318 |
575 * Removes a key/value-pair from the map by using the key. |
319 /** |
576 * |
320 * Creates a value iterator for a map. |
577 * This function can be used when the map is storing pointers, |
321 * |
578 * in order to retrieve the pointer from the map without invoking |
322 * \note An iterator iterates over all elements successively. Therefore the order |
579 * any destructor function. Sometimes you do not want the pointer |
323 * highly depends on the map implementation and may change arbitrarily when the contents change. |
580 * to be returned - in that case (instead of suppressing the "unused |
324 * |
581 * result" warning) you can use cxMapDetach(). |
325 * @param map the map to create the iterator for |
582 * |
326 * @return an iterator for the currently stored values |
583 * If this map is not storing pointers, this function behaves like |
327 */ |
584 * cxMapRemove() and returns \c NULL. |
328 __attribute__((__nonnull__, __warn_unused_result__)) |
585 * |
329 static inline CxIterator cxMapIteratorValues(CxMap *map) { |
586 * @param map the map |
330 return map->cl->iterator_values(map); |
587 * @param key the key |
331 } |
588 * @return the stored pointer or \c NULL if either the key is not present |
332 |
589 * in the map or the map is not storing pointers |
333 /** |
590 * @see cxMapStorePointers() |
334 * Creates a key iterator for a map. |
591 * @see cxMapDetach() |
335 * |
592 */ |
336 * The elements of the iterator are keys of type CxHashKey. |
593 __attribute__((__nonnull__, __warn_unused_result__)) |
337 * |
594 static inline void *cxMapRemoveAndGet( |
338 * \note An iterator iterates over all elements successively. Therefore the order |
595 CxMap *map, |
339 * highly depends on the map implementation and may change arbitrarily when the contents change. |
596 cxstring key |
340 * |
597 ) { |
341 * @param map the map to create the iterator for |
598 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); |
342 * @return an iterator for the currently stored keys |
599 } |
343 */ |
600 |
344 __attribute__((__nonnull__, __warn_unused_result__)) |
601 /** |
345 static inline CxIterator cxMapIteratorKeys(CxMap *map) { |
602 * Removes a key/value-pair from the map by using the key. |
346 return map->cl->iterator_keys(map); |
603 * |
347 } |
604 * This function can be used when the map is storing pointers, |
348 |
605 * in order to retrieve the pointer from the map without invoking |
349 /** |
606 * any destructor function. Sometimes you do not want the pointer |
350 * Creates an iterator for a map. |
607 * to be returned - in that case (instead of suppressing the "unused |
351 * |
608 * result" warning) you can use cxMapDetach(). |
352 * The elements of the iterator are key/value pairs of type CxMapEntry. |
609 * |
353 * |
610 * If this map is not storing pointers, this function behaves like |
354 * \note An iterator iterates over all elements successively. Therefore the order |
611 * cxMapRemove() and returns \c NULL. |
355 * highly depends on the map implementation and may change arbitrarily when the contents change. |
612 * |
356 * |
613 * @param map the map |
357 * @param map the map to create the iterator for |
614 * @param key the key |
358 * @return an iterator for the currently stored entries |
615 * @return the stored pointer or \c NULL if either the key is not present |
359 * @see cxMapIteratorKeys() |
616 * in the map or the map is not storing pointers |
360 * @see cxMapIteratorValues() |
617 * @see cxMapStorePointers() |
361 */ |
618 * @see cxMapDetach() |
362 __attribute__((__nonnull__, __warn_unused_result__)) |
619 */ |
363 static inline CxIterator cxMapIterator(CxMap *map) { |
620 __attribute__((__nonnull__, __warn_unused_result__)) |
364 return map->cl->iterator(map); |
621 static inline void *cxMapRemoveAndGet( |
365 } |
622 CxMap *map, |
366 |
623 char const *key |
367 |
624 ) { |
368 /** |
625 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); |
369 * Creates a mutating iterator over the values of a map. |
626 } |
370 * |
627 |
371 * \note An iterator iterates over all elements successively. Therefore the order |
628 #else // __cplusplus |
372 * highly depends on the map implementation and may change arbitrarily when the contents change. |
629 |
373 * |
630 /** |
374 * @param map the map to create the iterator for |
631 * Puts a key/value-pair into the map. |
375 * @return an iterator for the currently stored values |
632 * |
376 */ |
633 * @param map the map |
377 __attribute__((__nonnull__, __warn_unused_result__)) |
634 * @param key the key |
378 static inline CxMutIterator cxMapMutIteratorValues(CxMap *map) { |
635 * @param value the value |
379 return map->cl->mut_iterator_values(map); |
636 * @return 0 on success, non-zero value on failure |
380 } |
637 */ |
381 |
638 __attribute__((__nonnull__)) |
382 /** |
639 static inline int cx_map_put( |
383 * Creates a mutating iterator over the keys of a map. |
640 CxMap *map, |
384 * |
641 CxHashKey key, |
385 * The elements of the iterator are keys of type CxHashKey. |
642 void *value |
386 * |
643 ) { |
387 * \note An iterator iterates over all elements successively. Therefore the order |
644 return map->cl->put(map, key, value); |
388 * highly depends on the map implementation and may change arbitrarily when the contents change. |
645 } |
389 * |
646 |
390 * @param map the map to create the iterator for |
647 /** |
391 * @return an iterator for the currently stored keys |
648 * Puts a key/value-pair into the map. |
392 */ |
649 * |
393 __attribute__((__nonnull__, __warn_unused_result__)) |
650 * @param map the map |
394 static inline CxMutIterator cxMapMutIteratorKeys(CxMap *map) { |
651 * @param key the key |
395 return map->cl->mut_iterator_keys(map); |
652 * @param value the value |
396 } |
653 * @return 0 on success, non-zero value on failure |
397 |
654 */ |
398 /** |
655 __attribute__((__nonnull__)) |
399 * Creates a mutating iterator for a map. |
656 static inline int cx_map_put_cxstr( |
400 * |
657 CxMap *map, |
401 * The elements of the iterator are key/value pairs of type CxMapEntry. |
658 cxstring key, |
402 * |
659 void *value |
403 * \note An iterator iterates over all elements successively. Therefore the order |
660 ) { |
404 * highly depends on the map implementation and may change arbitrarily when the contents change. |
661 return map->cl->put(map, cx_hash_key_cxstr(key), value); |
405 * |
662 } |
406 * @param map the map to create the iterator for |
663 |
407 * @return an iterator for the currently stored entries |
664 /** |
408 * @see cxMapMutIteratorKeys() |
665 * Puts a key/value-pair into the map. |
409 * @see cxMapMutIteratorValues() |
666 * |
410 */ |
667 * @param map the map |
411 __attribute__((__nonnull__, __warn_unused_result__)) |
668 * @param key the key |
412 static inline CxMutIterator cxMapMutIterator(CxMap *map) { |
669 * @param value the value |
413 return map->cl->mut_iterator(map); |
670 * @return 0 on success, non-zero value on failure |
414 } |
671 */ |
415 |
672 __attribute__((__nonnull__)) |
416 #ifdef __cplusplus |
673 static inline int cx_map_put_str( |
417 } |
674 CxMap *map, |
418 #endif |
675 char const *key, |
|
676 void *value |
|
677 ) { |
|
678 return map->cl->put(map, cx_hash_key_str(key), value); |
|
679 } |
|
680 |
|
681 /** |
|
682 * Puts a key/value-pair into the map. |
|
683 * |
|
684 * @param map the map |
|
685 * @param key the key |
|
686 * @param value the value |
|
687 * @return 0 on success, non-zero value on failure |
|
688 */ |
|
689 #define cxMapPut(map, key, value) _Generic((key), \ |
|
690 CxHashKey: cx_map_put, \ |
|
691 cxstring: cx_map_put_cxstr, \ |
|
692 char*: cx_map_put_str) \ |
|
693 (map, key, value) |
|
694 |
|
695 /** |
|
696 * Retrieves a value by using a key. |
|
697 * |
|
698 * @param map the map |
|
699 * @param key the key |
|
700 * @return the value |
|
701 */ |
|
702 __attribute__((__nonnull__, __warn_unused_result__)) |
|
703 static inline void *cx_map_get( |
|
704 CxMap const *map, |
|
705 CxHashKey key |
|
706 ) { |
|
707 return map->cl->get(map, key); |
|
708 } |
|
709 |
|
710 /** |
|
711 * Retrieves a value by using a key. |
|
712 * |
|
713 * @param map the map |
|
714 * @param key the key |
|
715 * @return the value |
|
716 */ |
|
717 __attribute__((__nonnull__, __warn_unused_result__)) |
|
718 static inline void *cx_map_get_cxstr( |
|
719 CxMap const *map, |
|
720 cxstring key |
|
721 ) { |
|
722 return map->cl->get(map, cx_hash_key_cxstr(key)); |
|
723 } |
|
724 |
|
725 /** |
|
726 * Retrieves a value by using a key. |
|
727 * |
|
728 * @param map the map |
|
729 * @param key the key |
|
730 * @return the value |
|
731 */ |
|
732 __attribute__((__nonnull__, __warn_unused_result__)) |
|
733 static inline void *cx_map_get_str( |
|
734 CxMap const *map, |
|
735 char const *key |
|
736 ) { |
|
737 return map->cl->get(map, cx_hash_key_str(key)); |
|
738 } |
|
739 |
|
740 /** |
|
741 * Retrieves a value by using a key. |
|
742 * |
|
743 * @param map the map |
|
744 * @param key the key |
|
745 * @return the value |
|
746 */ |
|
747 #define cxMapGet(map, key) _Generic((key), \ |
|
748 CxHashKey: cx_map_get, \ |
|
749 cxstring: cx_map_get_cxstr, \ |
|
750 char*: cx_map_get_str) \ |
|
751 (map, key) |
|
752 |
|
753 /** |
|
754 * Removes a key/value-pair from the map by using the key. |
|
755 * |
|
756 * @param map the map |
|
757 * @param key the key |
|
758 */ |
|
759 __attribute__((__nonnull__)) |
|
760 static inline void cx_map_remove( |
|
761 CxMap *map, |
|
762 CxHashKey key |
|
763 ) { |
|
764 (void) map->cl->remove(map, key, true); |
|
765 } |
|
766 |
|
767 /** |
|
768 * Removes a key/value-pair from the map by using the key. |
|
769 * |
|
770 * @param map the map |
|
771 * @param key the key |
|
772 */ |
|
773 __attribute__((__nonnull__)) |
|
774 static inline void cx_map_remove_cxstr( |
|
775 CxMap *map, |
|
776 cxstring key |
|
777 ) { |
|
778 (void) map->cl->remove(map, cx_hash_key_cxstr(key), true); |
|
779 } |
|
780 |
|
781 /** |
|
782 * Removes a key/value-pair from the map by using the key. |
|
783 * |
|
784 * @param map the map |
|
785 * @param key the key |
|
786 */ |
|
787 __attribute__((__nonnull__)) |
|
788 static inline void cx_map_remove_str( |
|
789 CxMap *map, |
|
790 char const *key |
|
791 ) { |
|
792 (void) map->cl->remove(map, cx_hash_key_str(key), true); |
|
793 } |
|
794 |
|
795 /** |
|
796 * Removes a key/value-pair from the map by using the key. |
|
797 * |
|
798 * Always invokes the destructor function, if any, on the removed element. |
|
799 * If this map is storing pointers and you just want to retrieve the pointer |
|
800 * without invoking the destructor, use cxMapRemoveAndGet(). |
|
801 * If you just want to detach the element from the map without invoking the |
|
802 * destructor or returning the element, use cxMapDetach(). |
|
803 * |
|
804 * @param map the map |
|
805 * @param key the key |
|
806 * @see cxMapRemoveAndGet() |
|
807 * @see cxMapDetach() |
|
808 */ |
|
809 #define cxMapRemove(map, key) _Generic((key), \ |
|
810 CxHashKey: cx_map_remove, \ |
|
811 cxstring: cx_map_remove_cxstr, \ |
|
812 char*: cx_map_remove_str) \ |
|
813 (map, key) |
|
814 |
|
815 /** |
|
816 * Detaches a key/value-pair from the map by using the key |
|
817 * without invoking the destructor. |
|
818 * |
|
819 * @param map the map |
|
820 * @param key the key |
|
821 */ |
|
822 __attribute__((__nonnull__)) |
|
823 static inline void cx_map_detach( |
|
824 CxMap *map, |
|
825 CxHashKey key |
|
826 ) { |
|
827 (void) map->cl->remove(map, key, false); |
|
828 } |
|
829 |
|
830 /** |
|
831 * Detaches a key/value-pair from the map by using the key |
|
832 * without invoking the destructor. |
|
833 * |
|
834 * @param map the map |
|
835 * @param key the key |
|
836 */ |
|
837 __attribute__((__nonnull__)) |
|
838 static inline void cx_map_detach_cxstr( |
|
839 CxMap *map, |
|
840 cxstring key |
|
841 ) { |
|
842 (void) map->cl->remove(map, cx_hash_key_cxstr(key), false); |
|
843 } |
|
844 |
|
845 /** |
|
846 * Detaches a key/value-pair from the map by using the key |
|
847 * without invoking the destructor. |
|
848 * |
|
849 * @param map the map |
|
850 * @param key the key |
|
851 */ |
|
852 __attribute__((__nonnull__)) |
|
853 static inline void cx_map_detach_str( |
|
854 CxMap *map, |
|
855 char const *key |
|
856 ) { |
|
857 (void) map->cl->remove(map, cx_hash_key_str(key), false); |
|
858 } |
|
859 |
|
860 /** |
|
861 * Detaches a key/value-pair from the map by using the key |
|
862 * without invoking the destructor. |
|
863 * |
|
864 * In general, you should only use this function if the map does not own |
|
865 * the data and there is a valid reference to the data somewhere else |
|
866 * in the program. In all other cases it is preferable to use |
|
867 * cxMapRemove() or cxMapRemoveAndGet(). |
|
868 * |
|
869 * @param map the map |
|
870 * @param key the key |
|
871 * @see cxMapRemove() |
|
872 * @see cxMapRemoveAndGet() |
|
873 */ |
|
874 #define cxMapDetach(map, key) _Generic((key), \ |
|
875 CxHashKey: cx_map_detach, \ |
|
876 cxstring: cx_map_detach_cxstr, \ |
|
877 char*: cx_map_detach_str) \ |
|
878 (map, key) |
|
879 |
|
880 /** |
|
881 * Removes a key/value-pair from the map by using the key. |
|
882 * |
|
883 * @param map the map |
|
884 * @param key the key |
|
885 * @return the stored pointer or \c NULL if either the key is not present |
|
886 * in the map or the map is not storing pointers |
|
887 */ |
|
888 __attribute__((__nonnull__, __warn_unused_result__)) |
|
889 static inline void *cx_map_remove_and_get( |
|
890 CxMap *map, |
|
891 CxHashKey key |
|
892 ) { |
|
893 return map->cl->remove(map, key, !map->store_pointer); |
|
894 } |
|
895 |
|
896 /** |
|
897 * Removes a key/value-pair from the map by using the key. |
|
898 * |
|
899 * @param map the map |
|
900 * @param key the key |
|
901 * @return the stored pointer or \c NULL if either the key is not present |
|
902 * in the map or the map is not storing pointers |
|
903 */ |
|
904 __attribute__((__nonnull__, __warn_unused_result__)) |
|
905 static inline void *cx_map_remove_and_get_cxstr( |
|
906 CxMap *map, |
|
907 cxstring key |
|
908 ) { |
|
909 return map->cl->remove(map, cx_hash_key_cxstr(key), !map->store_pointer); |
|
910 } |
|
911 |
|
912 /** |
|
913 * Removes a key/value-pair from the map by using the key. |
|
914 * |
|
915 * @param map the map |
|
916 * @param key the key |
|
917 * @return the stored pointer or \c NULL if either the key is not present |
|
918 * in the map or the map is not storing pointers |
|
919 */ |
|
920 __attribute__((__nonnull__, __warn_unused_result__)) |
|
921 static inline void *cx_map_remove_and_get_str( |
|
922 CxMap *map, |
|
923 char const *key |
|
924 ) { |
|
925 return map->cl->remove(map, cx_hash_key_str(key), !map->store_pointer); |
|
926 } |
|
927 |
|
928 /** |
|
929 * Removes a key/value-pair from the map by using the key. |
|
930 * |
|
931 * This function can be used when the map is storing pointers, |
|
932 * in order to retrieve the pointer from the map without invoking |
|
933 * any destructor function. Sometimes you do not want the pointer |
|
934 * to be returned - in that case (instead of suppressing the "unused |
|
935 * result" warning) you can use cxMapDetach(). |
|
936 * |
|
937 * If this map is not storing pointers, this function behaves like |
|
938 * cxMapRemove() and returns \c NULL. |
|
939 * |
|
940 * @param map the map |
|
941 * @param key the key |
|
942 * @return the stored pointer or \c NULL if either the key is not present |
|
943 * in the map or the map is not storing pointers |
|
944 * @see cxMapStorePointers() |
|
945 * @see cxMapDetach() |
|
946 */ |
|
947 #define cxMapRemoveAndGet(map, key) _Generic((key), \ |
|
948 CxHashKey: cx_map_remove_and_get, \ |
|
949 cxstring: cx_map_remove_and_get_cxstr, \ |
|
950 char*: cx_map_remove_and_get_str) \ |
|
951 (map, key) |
|
952 |
|
953 #endif // __cplusplus |
419 |
954 |
420 #endif // UCX_MAP_H |
955 #endif // UCX_MAP_H |