No! No! No! It's not fashion!

Source: Internet
Author: User

Do you still remember the strange hold sister Miss Lin? for people's regular behavior, Miss Lin will pick up exaggerated eyebrows and say, "Oh my God, it's not fashion! ". IfProgramThere is a Miss Lin in the member circle. For some functions, she thinks which coding methods are not fashion and which are fashion?

The following example shows how to implement the coding function. If you prefer to use the conventional method, hold your sister to implement the function while not forgetting fashion.

Question 1: The Event trigger library function supports three types of events: Io events (including read/write), signals, and timeout events. encode the above event types and describe how to use them.

Mr.:

1. Event Type Definition

 
# DefineEv_timeout 1
# DefineEv_read 2
# DefineEv_write 3
# DefineEv_signal 4

2. Usage

Validity judgment:

 
If(Ev_events> = ev_timeout & ev_events <= ev_signal ){......}

Handle events based on the event type:

 
If(Ev_events = ev_read ){......}


No! No! No! It's not fashion!

** Miss Lin **:

1. Define the event type:

 
# DefineEv_timeout 0x01
# DefineEv_read 0x02
# DefineEv_write 0x04
# DefineEv_signal 0x08

Validity judgment:

 
If(Ev-> ev_events & (ev_read | ev_write | ev_signal )){......}

Handle events based on the event type:

 
If(Ev-> ev_events & ev_read ){......}


Miss Lin:

The method of mr. Well, well, well. Follow the rules is very sample, let people understand at a glance, but it's not fashion!

If an event contains both "read" and "write", do you want to add an ev_read_write macro? Think about that!

But my method is different. ev_read | ev_write indicates a read/write event. This method also applies to other eventsCombinationFor example, a time-limited read event can be defined as: ev_read | ev_timeout, so easy!

In addition, through bitwise operations, you can easily perform operations on event types:

 
//Add timeout type
Ev_events | = ev_timeout;
//Deletion timeout type
Ev_events & = ~ Ev_timeout;

Use bit to indicate the status, and use bit operation to operate the status value-you must know, fashion also stresses on saving!

Question 2: Define the data structure of the linked list and write related operation functions.

Mr.:

 
// Node Structure Definition
Typedef struct _ list_node {
Void * pdata;
Struct _ list_node * next;
} List_node;
// Chain table structure definition
Typedef struct _ list_head {
List_node * first;
} List_head;
// Header insertion Node Function
Void list_insert_head (list_head * head, list_node * node ){
Node-> next = head-> first;
Head-> first = node;
}


No! No! No! It's not fashion!

** Miss Lin **:

 // The definition macro of the linked list Header
# Define Slist_head (name, type )\
Struct Name {\
Struct Type * slh_first; /* First element */ \
}
// List element definition macro
# Define Slist_entry (type )\
Struct {\
Struct Type * sle_next; /* Next Element */ \
}
// Initialize the linked list
# Define slist_init (head) do {\
(Head)-> slh_first = NULL ;\
} While (/* constcond */0)
// Insert node macro in the header
# Define slist_insert_head (Head, elm, field) do {\
(ELM)-> field. sle_next = (head)-> slh_first ;\
(Head)-> slh_first = (ELM );\
} While (/* constcond */0)

Miss Lin:

What is fashion? To be different! When it comes to the use of macros, many programmers can list a bunch of shortcomings, such as the error-prone and inconvenient debugging, but the proper use of macros can bring many benefits. Compared with the implementation of functions, higher running efficiency (even a little improvement ). The most critical point is it's cool!
The usage of the macro definition of the linked list is as follows:

// Define the linked list Node
StructSlist_item {
IntValue;
Slist_entry (slist_item) entries;
};
Slist_head (, slist_item) slist_head; // declare the head node of the linked list
StructSlist_item * item;
Slist_init (& slist_head); // initialize the linked list
Item = malloc (Sizeof(StructSlist_item ));
Item-> value =10;
Slist_insert_head (& slist_head, item, entries); // insert an element in the header

Question 3: Write interface functions to insert and delete dynamic arrays and queues respectively.

Mr.:

 // list. h  
struct _ list;
typedef struct _ list;
// linked list interface function
void list_insert (void * thiz, size_t index, void * data );
void list_delete (void * thiz, size_t index);
// darray. h
struct _ darray;
typedef struct _ darray;
/// dynamic array interface functions
void darray_insert (void * thiz, size_t index, void * data );
void darray_delete (void * thiz, size_t index);


No! No! No! It's not fashion!

** Miss Lin **:

 //  Container. h 
Struct Container {
Void (* Insert )( Void *, Size_t index, Void * Data );
Void (* Delete )( Void *, Size_t index );
};

// List. h
Static Void List_insert ( Void * Thiz, size_t index, Void * Data );
Static Void List_delete ( Void * Thiz, size_t index );
Struct Container list_container = {list_insert, list_delete };

// Darray. h
Static Void Darray_insert ( Void * Thiz, size_t index, Void * Data );
Static Void Darray_delete ( Void * Thiz, size_t index );
Struct Container darray_container = {darray_insert, darray_delete };

Miss Lin:

Fashion! Can you see that ?! The container structure is added and the callback function is used to implement a unified interface. The static keyword is hidden. The usage is as follows:

StructContainer * containerp = & list_container;
Containerp-> insert (......);
Containerp-> Delete (......);

 
The above describes the following content in a tone that imitates Miss Lin:

    1. Use binary and bitwise operations to define types;
    2. Use macros to define structures and functions;
    3. Provides unified interfaces using callback functions.

We should learn from Miss Lin and learn from her spirit of pursuing uniqueness. For programming, in addition to the conventional coding implementation methods, we should also think more about methods that can achieve the same purpose. Gradually improve programming skills through continuous thinking and summarization.

 

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.