/Util/PySmallPtrSet.cc

http://unladen-swallow.googlecode.com/ · C++ · 68 lines · 53 code · 13 blank · 2 comment · 4 complexity · 40c81681ff834d8d5cc814dcead1e4f3 MD5 · raw file

  1. #include "Util/PySmallPtrSet.h"
  2. #include "llvm/ADT/SmallPtrSet.h"
  3. #include "llvm/ADT/SmallVector.h"
  4. typedef llvm::SmallPtrSet<PyObject *, 8> PySmallPtrSet_Impl;
  5. typedef struct PySmallPtrSet {
  6. PySmallPtrSet_Impl llvm_set;
  7. } PySmallPtrSet;
  8. // C'tors, d'tors
  9. PySmallPtrSet *
  10. PySmallPtrSet_New()
  11. {
  12. PySmallPtrSet *set = PyMem_New(PySmallPtrSet, 1);
  13. if (set == NULL)
  14. return NULL;
  15. new(set)PySmallPtrSet();
  16. return set;
  17. }
  18. void
  19. PySmallPtrSet_Del(PySmallPtrSet *set)
  20. {
  21. set->~PySmallPtrSet();
  22. PyMem_Free(set);
  23. }
  24. int
  25. PySmallPtrSet_Insert(PySmallPtrSet *set, PyObject *obj)
  26. {
  27. return set->llvm_set.insert(obj);
  28. }
  29. int
  30. PySmallPtrSet_Erase(PySmallPtrSet *set, PyObject *obj)
  31. {
  32. return set->llvm_set.erase(obj);
  33. }
  34. unsigned
  35. PySmallPtrSet_Size(PySmallPtrSet *set)
  36. {
  37. return set->llvm_set.size();
  38. }
  39. int
  40. PySmallPtrSet_Count(PySmallPtrSet *set, PyObject *obj)
  41. {
  42. return set->llvm_set.count(obj);
  43. }
  44. void
  45. PySmallPtrSet_ForEach(PySmallPtrSet *set, PySmallPtrSetCallback callback,
  46. void *callback_arg)
  47. {
  48. // Copy the original set in case the callback modifies the set.
  49. llvm::SmallVector<PyObject *, 8> contents(set->llvm_set.begin(),
  50. set->llvm_set.end());
  51. for (llvm::SmallVector<PyObject *, 8>::iterator i = contents.begin(),
  52. end = contents.end(); i != end; ++i) {
  53. callback(*i, callback_arg);
  54. }
  55. }