* Please refer to this document for reference from blog.csdn.net/wtz1985
I believe everyone is familiar with the container, especially Java or C #. It is just an intermediate interface, that is, a pure virtual class. Its implementation can be achieved through a linked list or array structure. Its appearance is to allow other function bodies to freely choose their own implementation structure. For example, hash tables, stacks, and other structures can be implemented by using linked lists or dynamic arrays. If there is no intermediate interface and you want to implement it separately, this is a waste of time and space.
1. interface definition.
- /* ----- Container. h -------*/
- # Ifndef _ container_h
- # DEFINE _ container_h
- Struct _ container;
- Typedef struct _ container;
- Yptedef void (* foreachfunc) (void * data, void * CTX );
- Typedef void * (* containerappendfunc) (container * thiz, void * data );
- Typedef void * (* containerinsertfunc) (container * thiz, void * data, size * index );
- Typedef void * (* containerdeletefunc) (container * thiz, size_t index );
- Typedef void (* containerdestroyfunc) (container * thiz );
- Typedef void (* containerforeachfunc) (container * thiz, foreachfunc print, void * CTX );
- Struct _ container
- {
- Containerappendfunc container_append;
- Containerinsertfunc container_insert;
- Containerdeletefunc container_delete;
- Containerdestroyfunc container_func;
- Containrforeachfunc container_foreach;
- Char priv [0];
- };
- Static inline void * container_append (container * thiz, void * Data)
- {
- Assert (thiz! = NULL & thiz-> container_append! = NULL );
- Return thiz-> container_append (thiz, data );
- }
- Static inline void * container_insert (container * thiz, void * data, size_t index)
- {
- Assert (thiz! = NULL & thiz-> container_insert! = NULL );
- Return thiz-> container_insert (thiz, Data, index );
- }
- Static inline void * container_delete (container * thiz, size_t index)
- {
- Assert (thiz! = NULL & thiz-> container_delete! = NULL );
- Return thiz-> container_delete (thiz, index );
- }
- Static inline void container_destroy (container * thiz)
- {
- Assert (thiz! = NULL & thiz-> container_destroy! = NULL );
- Return thiz-> container_destroy (thiz );
- }
- Static inline void container_foreach (container * thiz, foreachfunc print, void * CTX)
- {
- Assert (thiz! = NULL & thiz-> container_foreach! = NULL );
- Return thiz-> container_foreach (thiz, print, CTX );
- }
- # Endif/_ container_h/
2. Different interface implementations:
Linked List:
- /* ------- Container_list.h ---------*/
- # Include "container. H"
- # Include "Dlist. H"
- # Ifndef _ container_list_h
- # DEFINE _ container_list_h
- Container * container_list_create (Dlist * List );
- # Endif/* _ container_h */
Dynamic Array:
- /* --------- Container_vector.h -----------*/
- # Include "vector. H"
- # Include "container. H"
- # Ifndef _ container_vector_h
- # DEFINE _ container_vector_h
- Container * container_vector_create (vector * thiz );
- # Endif/* container_vector_h */
With these interfaces, you don't have to write any more for specific implementation. I believe everyone should know about them. If there is something bad to write, please advise.
~~ End ~~