map.goc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package runtime
  5. #include "runtime.h"
  6. #include "map.h"
  7. typedef struct __go_map Hmap;
  8. typedef struct __go_hash_iter hiter;
  9. /* Access a value in a map, returning a value and a presence indicator. */
  10. func mapaccess2(t *MapType, h *Hmap, key *byte, val *byte) (present bool) {
  11. byte *mapval;
  12. size_t valsize;
  13. mapval = __go_map_index(h, key, 0);
  14. valsize = t->__val_type->__size;
  15. if (mapval == nil) {
  16. __builtin_memset(val, 0, valsize);
  17. present = 0;
  18. } else {
  19. __builtin_memcpy(val, mapval, valsize);
  20. present = 1;
  21. }
  22. }
  23. /* Optionally assign a value to a map (m[k] = v, p). */
  24. func mapassign2(h *Hmap, key *byte, val *byte, p bool) {
  25. if (!p) {
  26. __go_map_delete(h, key);
  27. } else {
  28. byte *mapval;
  29. size_t valsize;
  30. mapval = __go_map_index(h, key, 1);
  31. valsize = h->__descriptor->__map_descriptor->__val_type->__size;
  32. __builtin_memcpy(mapval, val, valsize);
  33. }
  34. }
  35. /* Delete a key from a map. */
  36. func mapdelete(h *Hmap, key *byte) {
  37. __go_map_delete(h, key);
  38. }
  39. /* Initialize a range over a map. */
  40. func mapiterinit(h *Hmap, it *hiter) {
  41. __go_mapiterinit(h, it);
  42. }
  43. /* Move to the next iteration, updating *HITER. */
  44. func mapiternext(it *hiter) {
  45. __go_mapiternext(it);
  46. }
  47. /* Get the key of the current iteration. */
  48. func mapiter1(it *hiter, key *byte) {
  49. __go_mapiter1(it, key);
  50. }
  51. /* Get the key and value of the current iteration. */
  52. func mapiter2(it *hiter, key *byte, val *byte) {
  53. __go_mapiter2(it, key, val);
  54. }