Sunday, July 29, 2012

The Generic Data Structures Library

The purpose of the GDS (Generic Data Structures) Library is to provide type-independent data structures such as lists, sets, trees or arrays for the C programming language.

A GDS object contains 2 data structures:
-A interface data structure which contains the object's generic behaviors:
      -Copy (Used for creating a hard copy of the object)
      -Destroy (Used for freeing the memory occupied by the object)
      -Compare (Used for comparing 2 objects)
-A generic pointer to another structure which should hold the object's attributes.

The implementation of the GDS object:
/*Contains the attributes and the behaviors of the object*/
typedef struct
{
   /*Contains the object's data*/
   void    *data;
   /*Contains pointers to the object's function*/
   Interface  *interface;
}Object;
The interface data structure is defined in the following way:
/*Contains pointers to the object's functions*/
typedef struct
{
   /*A pointer to a hard copy function*/
   void* (*Copy)(const void*);
   /*A pointer to a destructor-like function*/
   void* (*Destroy)(void*);
   /*A pointer to a compare function*/
   int   (*Compare)(const void*,const void*);
}Interface;
The object data structure uses the following functions:
/*
 * Description:
 *  Creates an Object structure
 * Parameters:
 *  value - The value used for initializing an object
 * Returns:
 *  A pointer to the object OR
 *  NULL if there is not enough memory
 */
Object* Object_Create(void* value, void* interface);
/*
 * Description:
 *  Compares two objects
 * Parameters:
 *  object1, object2 - the objects who are going to be compared
 * Returns:
 *  The difference between the values of the objects
 *  0  - the objects are equal
 *  >0 - object1 is greater than object2
 *  <0 - object2 is greater than object1
 */
int Object_Compare(const Object* element1, const Object* element2);
/*
 * Description:
 *  Creates a hard copy of an object structure
 * Parameters:
 *  value - A pointer to object who is going to be copied
 * Returns:
 *  A pointer to the copy OR
 *  NULL if there is not enough memory
 */
Object* Object_Copy(const Object* element);
/*
 * Description:
 *  Destroys an object structure
 * Parameters:
 *  object - The object who is going to be destroyed
 * Returns:
 * NULL
 */
Object* Object_Destroy(Object* element);
The interface data structure has the following functions:
/*
 * Description:
 *  Creates an Interface structure
 * Parameters:
 *  Copy - a pointer to the object copy function
 *  Destroy - a pointer to the object destructor
 *  Compare - a pointer to the object compare function
 *  GetType - reserved for further use
 * Returns:
 * NULL
 */
Interface* Interface_Create(void* (*Copy)(const void*),
                            void* (*Destroy)(void*),
                            int (*Compare)(const void*,const void*));
/*
 * Description:
 *  Destroys an Interface structure
 * Parameters:
 *  interface - a pointer to a interface structure
 * Returns:
 *  NULL
 */
Interface* Interface_Destroy(Interface* interface);

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger