OLMap Class Reference
[Containers]

A collection of key-value pairs. More...

#import <ObjectiveLib/Map.h>

Inheritance diagram for OLMap:

Inheritance graph
[legend]
List of all members.

Public Member Functions

(void) - assignKey:value:
 Assign a value to a key.
(OLAssociativeIterator *) - begin
 Return an iterator that points to the first element in the map.
(void) - clear
 Remove all elements.
(int) - compare:
 Compare this map to another object.
(id) - copy
 Make a copy of this map.
(unsigned) - count:
 Return the number of instances of key in the map.
(BOOL) - empty
 Return whether the map is empty.
(OLAssociativeIterator *) - end
 Return an iterator that points to one position beyond the last element.
(OLPair *) - equalRange:
 Return a range whose elements are equal to key.
(void) - erase:
 Remove the element designated by where.
(void) - eraseFrom:to:
 Remove a range of elements.
(unsigned) - eraseKey:
 Erase all instances of key.
(OLAssociativeIterator *) - find:
 Find an element.
(id) - insert:
 Insert a key-value pair into the map.
(OLAssociativeIterator *) - insertAt:value:
 Insert a key-value pair into the map.
(void) - insertFrom:to:
 Insert a range of key-value pairs into the map.
(id) - insertKey:value:
 Insert a key-value pair.
(BOOL) - isEqual:
 Test whether another map is equal to this one.
(OLStreamableFunctor< OLBoolBinaryFunction > *) - keyComp
 Return the comparison function for keys.
(OLAssociativeIterator *) - lowerBound:
 Find the lower bound of a given key.
(unsigned) - maxSize
 Return the maxiumum number of objects that can be stored in a map.
(OLReverseBidiIterator *) - rbegin
 Return a reverse iterator pointing to the end of the sequence.
(OLReverseBidiIterator *) - rend
 Return a reverse iterator pointing to the beginning of the sequence.
(unsigned) - size
 Return the number of elements in the map.
(void) - swap:
 Swap this map with another one.
(OLAssociativeIterator *) - upperBound:
 Find the upper bound of a given key.
(OLStreamableFunctor< OLBoolBinaryFunction > *) - valueComp
 Return the function used to compare values.
(id) - valueForKey:
 Find the value of a given key.
(void) - writeSelfToStream:
 Write the object to a stream.
Initializers and Deallocators
(id) - free
 Finalize the map and deallocate any allocated memory.
(id) - init
 Initialize the map.
(id) - initFrom:to:
 Initialize the map.
(id) - initFrom:to:compare:
 Initialize the map.
(id) - initWithCompare:
 Initialize the map.
(id) - initWithMap:
 Initialize the map.
(id) - initWithObjectInStream:
 Initialize the object.

Static Public Member Functions

(id) + map
 Create and return a new map.
(id) + mapFrom:to:
 Create and return a new map.
(id) + mapWithCompare:
 Create and return a new map.
(id) + mapWithMap:
 Create and return a new map.

Protected Attributes

OLTreeMap * tree
 The red-black tree that provides the underlying data structure.

Detailed Description

A collection of key-value pairs.

A map is a sorted associative container of elements. Keys are associated with values, and the keys can be used to look up and gain access to values. Each key is unique. If a key-value pair is inserted into the map and the key already exists, then the insertion will fail. Maps are sorted by definition and the sorting order is determined by a comparison function that is passed at initialization. If no comparison function is specified, then OLLess is used, thus sorting the map in ascending order.

The object to which a map's iterators point is an instance of OLPair. The first value of the pair is the key, and the second value of the pair is the key's value.

See also:
OLAssociativeIterator


Member Function Documentation

- (void) assignKey: (id)  key
value: (id)  value 

Assign a value to a key.

If the key exists in the map, then value is assigned to the key and the old value is dropped. If the key does not exist in the map, then the key-value pair is inserted.

Parameters:
key the key to which to assign
value the value to assign

Reimplemented in OLMultiMap.

- (OLAssociativeIterator*) begin  

Return an iterator that points to the first element in the map.

The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Returns:
the first iterator for the map

- (void) clear  

Remove all elements.

- (int) compare: (id)  other  

Compare this map to another object.

If the other object is of type OLMap, each of the contained objects is compared to the corresponding object in other by calling the compare: method.

Parameters:
other the object with which to compare this one
Returns:
a value greater than, equal to, or less than zero accoringly as this object is greater than, equal to, or less than other

- (id) copy  

Make a copy of this map.

Returns:
the copy

- (unsigned) count: (id)  key  

Return the number of instances of key in the map.

Parameters:
key the key for which to search
Returns:
the number of values that exist for key

- (BOOL) empty  

Return whether the map is empty.

Returns:
YES if the map is empty, NO otherwise

- (OLAssociativeIterator*) end  

Return an iterator that points to one position beyond the last element.

The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Returns:
an iterator one position beyond the last element in the map

- (OLPair*) equalRange: (id)  key  

Return a range whose elements are equal to key.

The returned pair contains two instances of OLAssociativeIterator delimiting the range of elements. The objects to which the iterators point are instances of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
key the value for which to search
Returns:
a pair of OLAssociativeIterator instances that define the range of elements in the map equal to key

- (void) erase: (OLAssociativeIterator *)  where  

Remove the element designated by where.

Precondition:
where must point to an element in this map.
Parameters:
where an iterator designating the element to remove

- (void) eraseFrom: (OLAssociativeIterator *)  first
to: (OLAssociativeIterator *)  last 

Remove a range of elements.

All elements in the range [first, last) will be removed from the map.

Precondition:
first and last must refer to elements in this map.
Parameters:
first the first in the range of elements to remove
last one position beyond the last in the range of elements to remove

- (unsigned) eraseKey: (id)  key  

Erase all instances of key.

The map is searched for key, all instances are removed from the map and the number removed is returned.

Parameters:
key the key to remove from the map
Returns:
the number of elements removed

- (OLAssociativeIterator*) find: (id)  key  

Find an element.

The element key is searched for in the map and an iterator to its location is returned. If the element does not exist in the map, then the returned iterator will be equal to the iterator returned by end.

The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
key the key for which to search
Returns:
an iterator pointing to the location of key, or an iterator equal to that returned by end if key does not exist

- (id) free  

Finalize the map and deallocate any allocated memory.

- (id) init  

Initialize the map.

The map is empty and OLLess is used as the comparison function.

Returns:
a reference to this map

- (id) initFrom: (OLForwardIterator *)  first
to: (OLForwardIterator *)  last 

Initialize the map.

All elements in the range [first, last) are inserted into the map. OLLess is used as the comparison function. If any duplicate keys exist in the range [first, last), then only the first instance is inserted.

Precondition:
Each element in the range [first, last) must be an instance of OLPair. The first element of the pair is the key and the second element is the key's value.
Parameters:
first the first in the range of elements to insert
last one beyond the last in the range of elements to insert
Returns:
a reference to this map

- (id) initFrom: (OLForwardIterator *)  first
to: (OLForwardIterator *)  last
compare: (OLStreamableFunctor< OLBoolBinaryFunction > *)  comp 

Initialize the map.

All elements in the range [first, last) are inserted into the map. The comparison function comp is used.

Precondition:
Each element in the range [first, last) must be an instance of OLPair. The first element of the pair is the key and the second element is the key's value.
Parameters:
first the first in the range of elements to insert
last one beyond the last in the range of elements to insert
comp the comparison function used to sort keys
Returns:
a reference to this map

- (id) initWithCompare: (OLStreamableFunctor< OLBoolBinaryFunction > *)  comp  

Initialize the map.

The map is empty and the comparison function comp is used to sort keys.

Parameters:
comp the comparison function used to sort keys
Returns:
a reference to this map

- (id) initWithMap: (OLMap *)  right  

Initialize the map.

The comparison function and all elements are copied from right into this map.

Parameters:
right the map with which to initialize this one
Returns:
a reference to this map

- (id) initWithObjectInStream: (OLObjectInStream *)  stream  

Initialize the object.

Each instance variable is read from stream and all other initialization is performed.

Parameters:
stream the stream from which to read
Returns:
a reference to this object

Reimplemented from < OLStreamable >.

- (id) insert: (OLPair *)  keyValue  

Insert a key-value pair into the map.

An attempt is made to insert keyValue into the map, and an instance of OLPair is returned indicating the state of the insertion. The first element in the returned pair is an instance of OLAssociativeIterator indicating the position of keyValue in the map, and the second element of the returned pair is an object that responds to the message boolValue. The message boolValue will return YES if the insertion was successful, or NO if not.

Precondition:
The first element of keyValue must be the key and the second element must be the key's value.
Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
keyValue the key-value pair to insert
Returns:
an instance of OLPair indicating the position of keyVal in the map and whether the insertion succeeded or failed

Reimplemented in OLMultiMap.

- (OLAssociativeIterator*) insertAt: (OLAssociativeIterator *)  where
value: (OLPair *)  keyVal 

Insert a key-value pair into the map.

The keyVal is inserted and the position indicated by where is used as a hint about where in the map the keyVal should be placed. There is no guarantee that the keyVal will ultimately placed anywhere close to where. Note that the insertion may fail if the key is already in the map. An iterator is returned that points to object. The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
where a hint as to where in the map the keyVal should be located
keyVal the key-value pair to insert
Returns:
the iterator indicating where keyVal is now located in the map

Reimplemented in OLMultiMap.

- (void) insertFrom: (OLForwardIterator *)  first
to: (OLForwardIterator *)  last 

Insert a range of key-value pairs into the map.

An attempt is made to insert all pairs in the range [first, last), however there is no guarantee that any of the elements in the range will actually be inserted if they already exist in the map.

Precondition:
All elements in the range [first, last) must be instances of OLPair where the first element of the pair is the key and the second element is the key's value.
Parameters:
first the first in the range of pairs to insert
last one position beyond the last in the range of pairs to insert

Reimplemented in OLMultiMap.

- (id) insertKey: (id)  key
value: (id)  val 

Insert a key-value pair.

This message is simply a wrapper for insert:, provided as a convenience. It elminiates the need to create an instance of OLPair before performing insertion. As with insert: the message returns an instance of OLPair indicating the state of the insertion. The first element of the pair returned is an instance of OLAssociativeIterator indicating the position of key in the map, and the second element of the pair is an object that responds to the message boolValue. The message boolValue will return YES if the insertion succeeded, or NO if it did not.

Parameters:
key the key to insert
val the value of the key
Returns:
an instance of OLPair indicating the position of key in the map and whether the insertion succeeded or failed

- (BOOL) isEqual: (id)  object  

Test whether another map is equal to this one.

Two maps are considered equal if they contain the same number of objects and the objects are in the same order and each object is equal to the corresponding object in the other map.

Parameters:
object the object to test
Returns:
YES if object is equal to this map, NO otherwise

Reimplemented from < OLInserter >.

Reimplemented in OLMultiMap.

- (OLStreamableFunctor<OLBoolBinaryFunction>*) keyComp  

Return the comparison function for keys.

This function ultimately determines the sorting order of the map.

Returns:
the function object used to compare keys

- (OLAssociativeIterator*) lowerBound: (id)  key  

Find the lower bound of a given key.

The lower bound is the first position in the map at which key can be inserted without disturbing the sorting order. An iterator pointing to the lower bound is returned. The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
key the key for which to find the lower bound
Returns:
an iterator indicating the lower bound

+ (id) map  

Create and return a new map.

An instance of OLLess is used to compare keys for sorting.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Returns:
a new map

+ (id) mapFrom: (OLForwardIterator *)  first
to: (OLForwardIterator *)  last 

Create and return a new map.

The map is initialized with the contents of the range [first, last). An instance of OLLess is used to compare keys for sorting.

Precondition:
Each element in the range [first, last) must be an instance of OLPair. The first element of the pair is the key and the second element is the key's value.
Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
first the first in the range of elements to insert
last one beyond the last in the range of elements to insert
Returns:
a new map

+ (id) mapWithCompare: (OLStreamableFunctor< OLBoolBinaryFunction > *)  comp  

Create and return a new map.

The map is empty and the comparison function comp is used to sort keys.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
comp the comparison function used to sort keys
Returns:
a new map

+ (id) mapWithMap: (OLMap *)  right  

Create and return a new map.

The map is initialized with the contents of right.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
right the map to copy
Returns:
a new map

- (unsigned) maxSize  

Return the maxiumum number of objects that can be stored in a map.

This limit is theoretical, meaning that there is no guarantee that you can insert this many objects into any given map. The memory conditions of the run-time system play an important role.

Returns:
the maximum number of objects for a map

- (OLReverseBidiIterator*) rbegin  

Return a reverse iterator pointing to the end of the sequence.

Advancing the returned iterator will move backwards through the sequence to the point indicated by the iterator returned by rend. The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Returns:
a reverse iterator for the end of the sequence

- (OLReverseBidiIterator*) rend  

Return a reverse iterator pointing to the beginning of the sequence.

This iterator indicates one position beyond the last position that may be referenced by a reverse iterator. The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Returns:
a reverse iterator for the beginning of the sequence

- (unsigned) size  

Return the number of elements in the map.

Returns:
the number of elements

- (void) swap: (OLMap *)  right  

Swap this map with another one.

All elements in right will be placed into this map, and all elements in this map will be placed in right.

Parameters:
right the map with which to swap this one

- (OLAssociativeIterator*) upperBound: (id)  key  

Find the upper bound of a given key.

The upper bound is the last position in the map at which key can be inserted without disturbing the sorting order. An iterator pointing to the upper bound is returned. The object to which the iterator points is an instance of OLPair. The first element of the pair is the key and the second element of the pair is the key's value.

Note:
If OpenStep is present the returned object will be autoreleased before being returned.
Parameters:
key the key for which to find the upper bound
Returns:
an iterator indicating the upper bound

- (OLStreamableFunctor<OLBoolBinaryFunction>*) valueComp  

Return the function used to compare values.

This returns the same function as the message keyComp.

Returns:
the function object for comparing values

- (id) valueForKey: (id)  key  

Find the value of a given key.

The map is searched for key, and if it is found its value is returned. Otherwise, the message returns nil.

Parameters:
key the key for which to search
Returns:
the key's value or nil if the key does not exist in the map

Reimplemented in OLMultiMap.

- (void) writeSelfToStream: (OLObjectOutStream *)  stream  

Write the object to a stream.

All instance variables are written to stream.

Parameters:
stream the stream to which to write.

Reimplemented from < OLStreamable >.


Member Data Documentation

- (OLTreeMap*) tree [protected]

The red-black tree that provides the underlying data structure.


The documentation for this class was generated from the following file:
ObjectiveLibGenerated Sun Apr 22 15:18:49 2007, © 2004-2007 Will Mason
SourceForge.net Logo