RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Back to Basics: Manage Collections with a Custom Dictionary : Page 5

Dictionaries are an essential data structure in many of today's programming environments. Use this one in your next Brew application.

Deleting an Item
To delete an element, it first needs to be found. Dictionary_Delete does this using Dictionary_FindIndex, which you saw previously:

int Dictionary_Delete( IDictionary *p, const char *szKey )
  CDictionary *pMe = DICTIONARY_FROM_INTERFACE( p );
  uint32 index = 0;
  int result = EBADPARM;

  if ( !pMe || !szKey || STRLEN( szKey ) == 0 ) return result;

  result = Dictionary_FindIndex( pMe, szKey, &index );
  if ( result == SUCCESS )
    if ( pMe->pfnDeallocator && pMe->pElements[ index ].pValue ) 
      (pMe->pfnDeallocator)(pMe->pElements[ index ].pValue);
    pMe->pElements[ index ].pValue = NULL;
    FREE( pMe->pElements[ index ].szKey );
    pMe->pElements[ index ].szKey = NULL;
    pMe->pElements[ index ].hash = 0;
  return result;
After validating its arguments, the routine finds the index of the key in question, and if it's found, uses the dictionary's deallocator to free the item. Next, it nulls the pointer to the item and frees the key, nulling the key as well to indicate the slot is empty and available for reuse. Finally, the hash is set to zero. Dictionary_DeleteAll does the same, but across all set elements in the dictionary's collection.

Useful and Reusable
Using Brew's component-oriented approach makes it easy to create reusable data structures that you can share between your applications. Dictionaries and vectors benefit greatly from this approach because they can be foundation collection in many applications and share a common conceptual interface. Feel free to leverage these in your own applications!

Ray Rischpater is the chief architect at Rocket Mobile, Inc., specializing in the design and development of messaging and information access applications for today's wireless devices. Ray is the author of several books on software development including "eBay Application Development" and "Software Development for the QUALCOMM BREW Platform," both available from Apress, and is an active amateur radio operator.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date