erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
923 B

5 years ago
  1. #ifndef C_SRC_PRIORITY_QUEUE_H_
  2. #define C_SRC_PRIORITY_QUEUE_H_
  3. #include "macros.h"
  4. typedef bool(*LessFun)(void*, void*);
  5. typedef void(*UpdatePositionFun)(void*, int);
  6. typedef void(*DestroyElementFun)(void*);
  7. class PriorityQueue
  8. {
  9. public:
  10. PriorityQueue(LessFun ls, UpdatePositionFun upd, DestroyElementFun dtor);
  11. ~PriorityQueue();
  12. bool insert(void* item);
  13. bool remove(void* item, int pos);
  14. void* remove(int pos);
  15. void* pop() {return remove(0);}
  16. void* peek();
  17. int size() const { return length_;}
  18. private:
  19. inline void set(int pos, void* item);
  20. inline void pos_swap(int pos1, int pos2);
  21. void bubble_down(int pos);
  22. void bubble_up(int pos);
  23. int capacity_;
  24. int length_;
  25. void** heap_;
  26. LessFun less_;
  27. UpdatePositionFun update_pos_fun_;
  28. DestroyElementFun item_dtor_;
  29. DISALLOW_COPY_AND_ASSIGN(PriorityQueue);
  30. };
  31. #endif // C_SRC_PRIORITY_QUEUE_H_