選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

108 行
2.5 KiB

  1. #ifndef IMMUTABLES_MAP_H
  2. #define IMMUTABLES_MAP_H
  3. #include <stdint.h>
  4. #include "Python.h"
  5. #define _Py_HAMT_MAX_TREE_DEPTH 7
  6. #define Map_Check(o) (Py_TYPE(o) == &_Map_Type)
  7. #define MapMutation_Check(o) (Py_TYPE(o) == &_MapMutation_Type)
  8. /* Abstract tree node. */
  9. typedef struct {
  10. PyObject_HEAD
  11. } MapNode;
  12. #define _MapCommonFields(pref) \
  13. PyObject_HEAD \
  14. MapNode *pref##_root; \
  15. PyObject *pref##_weakreflist; \
  16. Py_ssize_t pref##_count;
  17. /* Base mapping struct; used in methods shared between
  18. MapObject and MapMutationObject types. */
  19. typedef struct {
  20. _MapCommonFields(b)
  21. } BaseMapObject;
  22. /* An HAMT immutable mapping collection. */
  23. typedef struct {
  24. _MapCommonFields(h)
  25. Py_hash_t h_hash;
  26. } MapObject;
  27. /* MapMutation object (returned from `map.mutate()`.) */
  28. typedef struct {
  29. _MapCommonFields(m)
  30. uint64_t m_mutid;
  31. } MapMutationObject;
  32. /* A struct to hold the state of depth-first traverse of the tree.
  33. HAMT is an immutable collection. Iterators will hold a strong reference
  34. to it, and every node in the HAMT has strong references to its children.
  35. So for iterators, we can implement zero allocations and zero reference
  36. inc/dec depth-first iteration.
  37. - i_nodes: an array of seven pointers to tree nodes
  38. - i_level: the current node in i_nodes
  39. - i_pos: an array of positions within nodes in i_nodes.
  40. */
  41. typedef struct {
  42. MapNode *i_nodes[_Py_HAMT_MAX_TREE_DEPTH];
  43. Py_ssize_t i_pos[_Py_HAMT_MAX_TREE_DEPTH];
  44. int8_t i_level;
  45. } MapIteratorState;
  46. /* Base iterator object.
  47. Contains the iteration state, a pointer to the HAMT tree,
  48. and a pointer to the 'yield function'. The latter is a simple
  49. function that returns a key/value tuple for the 'Items' iterator,
  50. just a key for the 'Keys' iterator, and a value for the 'Values'
  51. iterator.
  52. */
  53. typedef struct {
  54. PyObject_HEAD
  55. MapObject *mv_obj;
  56. binaryfunc mv_yield;
  57. PyTypeObject *mv_itertype;
  58. } MapView;
  59. typedef struct {
  60. PyObject_HEAD
  61. MapObject *mi_obj;
  62. binaryfunc mi_yield;
  63. MapIteratorState mi_iter;
  64. } MapIterator;
  65. /* PyTypes */
  66. PyTypeObject _Map_Type;
  67. PyTypeObject _MapMutation_Type;
  68. PyTypeObject _Map_ArrayNode_Type;
  69. PyTypeObject _Map_BitmapNode_Type;
  70. PyTypeObject _Map_CollisionNode_Type;
  71. PyTypeObject _MapKeys_Type;
  72. PyTypeObject _MapValues_Type;
  73. PyTypeObject _MapItems_Type;
  74. PyTypeObject _MapKeysIter_Type;
  75. PyTypeObject _MapValuesIter_Type;
  76. PyTypeObject _MapItemsIter_Type;
  77. #endif