C language-Implementation of the iterator.

Source: Internet
Author: User

* Please refer to this document for reference from blog.csdn.net/wtz1985

In the previous article, I have already introduced why container implementation and how to implement container. This chapter will introduce how to separate algorithms from containers and in containers, the basic interface definition is implemented, but the container does not implement specific operations, such as moving to a node or comparing the values of two nodes. Separating these algorithms from containers is the so-called iterator ). The following is the implementation of some code interfaces:

1. interface definition.

  1. /* -------- Iterator. h --------*/
  2. # Ifndef _ iterator_h
  3. # DEFINE _ iterator_h
  4. Struct _ iterator;
  5. Typedef struct _ iterator;
  6. Typedef void (* iteratornextfunc) (iterator * thiz );
  7. Typedef void (* iteratorprevfunc) (iterator * thiz );
  8. Typedef void (* iteratoradvancefunc) (iterator * thiz, size_t off_set );
  9. Typedef void (* iteratorbeginfunc) (iterator * thiz );
  10. Typedef void (* iteratorendfunc) (iterator * thiz );
  11. Typedef int (* iteratorcomparefunc) (iterator * X, iterator * y );
  12. Typedef void (* iteratorsetfunc) (iterator * thiz, void * data );
  13. Typedef void * (* iteratorgetfunc) (iterator * thiz );
  14. Typedef void (* iteratordestroyfunc) (iterator * thiz );
  15. Struct _ iterator
  16. {
  17. Iteratornextfunc next;
  18. Iteratorprevfunc Prev;
  19. Iteratoradvancefunc advance;
  20. Iteratorbeginfunc begin;
  21. Iteratorendfunc end;
  22. Iteratorcomparefunc compare;
  23. Iteratorsetfunc set;
  24. Iteratorgetfunc get;
  25. Iteratordestroyfunc destroy;
  26. };
  27. Static inline void iterator_next (iterator * thiz)
  28. {
  29. Assert (thiz! = NULL & thiz-> next! = NULL );
  30. Thiz-> next (thiz );
  31. Return;
  32. }
  33. Static inline void iterator_prev (iterator * thiz)
  34. {
  35. Assert (thiz! = NULL & thiz-> Prev! = NULL );
  36. Thiz-> Prev (thiz );
  37. Return;
  38. }
  39. Static inline void iterator_advance (iterator * thiz, size_t off_set)
  40. {
  41. Assert (thiz! = NULL & thiz-> advance! = NULL );
  42. Thiz-> advance (thiz, off_set );
  43. Return;
  44. }
  45. .......
  46. # Endif

2. Different interface implementations:

Linked List:

  1. /* -------- Iterator_list.h ---------*/
  2. # Ifndef _ iterator_list_h
  3. # DEFINE _ iterator_list_h
  4. # Include "iterator. H"
  5. Iterator * iterator_list_create (Dlist * thiz );
  6. # Endif

Dynamic Array mode:

  1. /* -------- Iterator_vector.h ---------*/
  2. # Ifndef _ iterator_vector_h
  3. # DEFINE _ iterator_vector_h
  4. # Include "iterator. H"
  5. Iterator * iterator_vector_create (vector * thiz );
  6. # Endif

The above is the implementation of the iterator interface, and the specific operation is not implemented here. With the container and iterator intermediate interfaces, You can freely choose the appropriate structure to implement your own functions. I hope you can give me more advice on some bad things.

 

~~ End ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.