dlib C++ Library - Containers

The Library

- Algorithms
- API Wrappers
- Compression
- Containers
- Imaging
- Metaprogramming
- Miscellaneous
- Network
- Parsing

Help/Info

- Examples
  - Base64_Encoder
  - Bayesian_Network
  - Bayesian_Network_From_Disk
  - Bayesian_Network_GUI
  - Cmd_Line_Parser
  - Compress_Stream
  - Directory_Navigation
  - GUI
  - HTTP_Server
  - Image
  - Kernel_Centroid
  - Kernel_K-Means_Clustering
  - Kernel_RLS_Filtering
  - Kernel_RLS_Regression
  - Logger
  - Logger_Advanced
  - Matrix
  - Member_Function_Pointer
  - Multithreaded_Object
  - Neural_Network
  - Pipe
  - Queue
  - Rank_Features
  - Relevance_Vector_Classification
  - Relevance_Vector_Regression
  - Sockets
  - Sockets_2
  - Sockstreambuf
  - Std_C++_Allocator
  - Support_Vector_Machine
  - Threaded_Object
  - Threads
  - Thread_Function
  - Thread_Pool
  - Timer
  - XML_Parser
- Home
- How to compile
- Index
- Introduction
- License

Current Release

- Change Log
- Release Notes
- Version: 17.12

Last Modified: Nov 10, 2008

# Containers

Many of these containers were inspired by the RESOLVE/C++ course sequence at Ohio State. As such, most of the objects do not support copying in any form, only swapping is allowed. That is, when objects are added or removed from any of these containers they are swapped in and out, not copied. This allows you to do things like have containers of containers of containers without encountering the overhead of the massive copying that would likely result if you did the same thing with the STL.

To use any of these containers all you need to do is #include the file indicated in the short section about the component you would like to use. Then pick which implementation you would like and typedef it to something nice. Here is an example of creating a typedef for a set of integers using the first kernel implementation.
`typedef dlib::set<int>::kernel_1a set_of_ints;`

Note that it is assumed by these containers that swap() and operator< do not throw. They may not function correctly if this assumption is broken. Also note that the built in types (int, long, char, etc...) and std::string will not cause operator< or swap() to throw.

Note also that most of the containers inherit from the enumerable interface. Thus, all the member functions inherited from enumerable are defined in the enumerable class and their documentation is not repeated in each container's documentation. This includes the size() member function in each container.

Objects

- array
- array2d
- binary_search_tree
- directed_graph
- graph
- hash_map
- hash_set
- hash_table
- map
- matrix
- queue
- reference_counter
- sequence
- set
- sliding_buffer
- smart pointers
  - scoped_ptr
  - shared_ptr
  - shared_ptr_thread_safe
  - weak_ptr
- stack
- static_map
- static_set
- std_vector_c
- tuple

Interfaces

- enumerable
- map_pair
- remover
  - asc_pair_remover
  - asc_remover
  - pair_remover
  - remover

[top]

# array

This object is just like a C style array and the accessor functions operate in constant time.
Specification: dlib/array/array_kernel_abstract.h
File to include: dlib/array.h
Implementations:

> array_kernel_1:
> This implementation is done using an array of pointers, each of which point to small sections of the array. This implementation allows the array to use only about as much memory as it needs at any given time. It does not use the memory_manager at all.

> kernel_1a | is a typedef for array_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

> array_kernel_2:
> This implementation is done using a single array of max_size() elements. So this is just a simple layer on top of a C style array. It uses the memory_manager for all memory allocations.

> kernel_2a | is a typedef for array_kernel_2
> kernel_2a_c | is a typedef for kernel_2a that checks its preconditions.

## Extensions to array

array_expand:
This extension gives an array the ability to expand its size() beyond its max_size() without clearing out all its elements. It also adds a set of pop/push_back() functions similar to the ones in the std::vector object.
Specification: dlib/array/array_expand_abstract.h
Implementations:

> array_expand_1:
> This is implemented by creating a new bigger array if max_size() isn't big enough, swapping everything into that new array, and then swapping that array with *this.

> expand_1a | is a typedef for array_sort_1a extended by array_expand_1
> expand_1a_c | is a typedef for expand_1a that checks its preconditions.
> expand_1b | is a typedef for array_sort_1b extended by array_expand_1
> expand_1b_c | is a typedef for expand_1b that checks its preconditions.
> expand_1c | is a typedef for array_sort_2a extended by array_expand_1
> expand_1c_c | is a typedef for expand_1c that checks its preconditions.
> expand_1d | is a typedef for array_sort_2b extended by array_expand_1
> expand_1d_c | is a typedef for expand_1d that checks its preconditions.

array_sort:
This extension gives an array the ability to sort its contents.
Specification: dlib/array/array_sort_abstract.h
Implementations:

> array_sort_1:
> This is a version of the QuickSort algorithm. It swaps the entire array into a C style array, sorts it and then swaps it back into the array object.

> sort_1a | is a typedef for array_kernel_1a extended by array_sort_1
> sort_1a_c | is a typedef for sort_1a that checks its preconditions.
> sort_1b | is a typedef for array_kernel_2a extended by array_sort_1
> sort_1b_c | is a typedef for sort_1b that checks its preconditions.

> array_sort_2:
> This is a version of the QuickSort algorithm.

> sort_2a | is a typedef for array_kernel_1a extended by array_sort_2
> sort_2a_c | is a typedef for sort_2a that checks its preconditions.
> sort_2b | is a typedef for array_kernel_2a extended by array_sort_2
> sort_2b_c | is a typedef for sort_2b that checks its preconditions.

[top]

# array2d

This object represents a 2-Dimensional array of objects.
Specification: dlib/array2d/array2d_kernel_abstract.h
File to include: dlib/array2d.h
Code Examples: 1
Implementations:

> array2d_kernel_1:
> This is implemented in the obvious way. See the source for details. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for array2d_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

[top]

# binary_search_tree

This object represents a data dictionary that is built on top of some kind of binary search tree.
Specification: dlib/binary_search_tree/binary_search_tree_kernel_abstract.h
File to include: dlib/binary_search_tree.h
Implementations:

> binary_search_tree_kernel_1:
> This implementation is done using an AVL binary search tree. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for binary_search_tree_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

> binary_search_tree_kernel_2:
> This implementation is done using a red-black binary search tree. It uses the memory_manager for all memory allocations.

> kernel_2a | is a typedef for binary_search_tree_kernel_2
> kernel_2a_c | is a typedef for kernel_2a that checks its preconditions.

[top]

# directed_graph

This object represents a directed graph which is a set of nodes with directed edges connecting various nodes.
Specification: dlib/directed_graph/directed_graph_kernel_abstract.h
File to include: dlib/directed_graph.h
Implementations:

> directed_graph_kernel_1:
> This is implemented using std::vector to contain all the nodes and edges.

> kernel_1a | is a typedef for directed_graph_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

[top]

# enumerable

This object is an abstract class which represents an interface for iterating over all the elements of a container.
Specification: dlib/interfaces/enumerable.h
File to include: dlib/interfaces/enumerable.h
[top]

# graph

This object represents a graph which is a set of nodes with undirected edges connecting various nodes.
Specification: dlib/graph/graph_kernel_abstract.h
File to include: dlib/graph.h
Implementations:

> graph_kernel_1:
> This is implemented using std::vector to contain all the nodes and edges.

> kernel_1a | is a typedef for graph_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

[top]

# hash_map

This object represents a hashed mapping of items of type domain onto items of type range.
Specification: dlib/hash_map/hash_map_kernel_abstract.h
File to include: dlib/hash_map.h
Implementations:

> hash_map_kernel_1:
> This implementation is done using a hash_table object. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for hash_map_kernel_1 that uses hash_table_kernel_1a
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.
> kernel_1b | is a typedef for hash_map_kernel_1 that uses hash_table_kernel_2a
> kernel_1b_c | is a typedef for kernel_1b that checks its preconditions.
> kernel_1c | is a typedef for hash_map_kernel_1 that uses hash_table_kernel_2b
> kernel_1c_c | is a typedef for kernel_1c that checks its preconditions.

[top]

# hash_set

This object represents a hashed unordered and unaddressed collection of unique items.
Specification: dlib/hash_set/hash_set_kernel_abstract.h
File to include: dlib/hash_set.h
Implementations:

> hash_set_kernel_1:
> This implementation is done using a hash_table object. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for hash_set_kernel_1 that uses hash_table_kernel_1a
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.
> kernel_1b | is a typedef for hash_set_kernel_1 that uses hash_table_kernel_2a
> kernel_1b_c | is a typedef for kernel_1b that checks its preconditions.
> kernel_1c | is a typedef for hash_set_kernel_1 that uses hash_table_kernel_2b
> kernel_1c_c | is a typedef for kernel_1c that checks its preconditions.

[top]

# hash_table

This object represents a data dictionary that is built on top of some kind of hash table.
Specification: dlib/hash_table/hash_table_kernel_abstract.h
File to include: dlib/hash_table.h
Implementations:

> hash_table_kernel_1:
> This implementation is done using singly linked lists as hashing buckets. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for hash_table_kernel_1.
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

> hash_table_kernel_2:
> This implementation is done using binary_search_tree objects as hashing buckets. It uses the memory_manager for all memory allocations.

> kernel_2a | is a typedef for hash_table_kernel_2 that uses binary_search_tree_kernel_1
> kernel_2a_c | is a typedef for kernel_2a that checks its preconditions.
> kernel_2b | is a typedef for hash_table_kernel_2 that uses binary_search_tree_kernel_2
> kernel_2b_c | is a typedef for kernel_2b that checks its preconditions.

[top]

# map

This object represents a mapping of items of type domain onto items of type range.
Specification: dlib/map/map_kernel_abstract.h
File to include: dlib/map.h
Implementations:

> map_kernel_1:
> This is implemented using the binary_search_tree component. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for map_kernel_1 that uses binary_search_tree_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.
> kernel_1b | is a typedef for map_kernel_1 that uses binary_search_tree_kernel_2
> kernel_1b_c | is a typedef for kernel_1b that checks its preconditions.

[top]

# map_pair

This object is an abstract class which represents an interface for accessing a pair from a container such as the map, hash_table, etc...
Specification: dlib/interfaces/map_pair.h
File to include: dlib/interfaces/map_pair.h
[top]

# matrix

This is a 2D matrix object. It is implemented using the expression templates technique which allows us to eliminate the temporary matrix objects that would normally be returned from expressions such as M = A+B+C+D; Normally each invocation of the + operator would construct and return a temporary matrix object but using this technique we can avoid creating all of these temporary objects and receive a large speed boost.

Note that there is only one implementation of this object so there aren't any different kernels to choose from when you create instances of the matrix object. So for example, you could declare a matrix of 2 rows and 3 columns using the following statement: `dlib::matrix<float,2,3> m;`

It should also be noted that matrix multiplication is fastest when the two matrices being multiplied are not complex matrix_exp objects returned from other expressions (such as other matrix multiplies). This is because the matrix multiply operator will evaluate each element of the matrices it is multiplying many times, and a matrix_exp computes its elements' values each time they are queried. However, the matrix multiply operator is the only one that evaluates its argument's elements multiple times so you can stack up all the other operators however you want without any performance penalty. If you want to multiply two complex matrix_exp expressions together you can easily convert them into fully evaluated temporary matrix objects by using the tmp() function. For example, to multiply four matrices together you should use an expression such as `result = tmp(a*b)*tmp(c*d);`

Specification: dlib/matrix/matrix_abstract.h
File to include: dlib/matrix.h
Code Examples: 1

## Extensions to matrix

matrix_math_functions:
This extension contains mathematical functions that operate on each element of a matrix independently. Note that you don't need to #include anything to get them. They are included by the dlib/matrix.h file for you.
Specification: dlib/matrix/matrix_math_functions_abstract.h

matrix_utilities:
This extension contains miscellaneous utility functions for manipulating matrix objects. Note that you don't need to #include anything to get them. They are included by the dlib/matrix.h file for you.
Specification: dlib/matrix/matrix_utilities_abstract.h
[top]

# queue

This object represents a first in first out queue.
Specification: dlib/queue/queue_kernel_abstract.h
File to include: dlib/queue.h
Code Examples: 1
Implementations:

> queue_kernel_1:
> This is implemented in the obvious way using a singly linked list. It does not use the memory_manager at all.

> kernel_1a | is a typedef for queue_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

> queue_kernel_2:
> This is implemented using a singly linked list and each node in the list contains block_size (a template parameter) elements. It uses the memory_manager for all memory allocations.

> kernel_2a | is a typedef for queue_kernel_2 with a block_size of 20
> kernel_2a_c | is a typedef for kernel_2a that checks its preconditions.
> kernel_2b | is a typedef for queue_kernel_2 with a block_size of 100
> kernel_2b_c | is a typedef for kernel_2b that checks its preconditions.

## Extensions to queue

queue_sort:
This extension gives a queue the ability to sort its contents.
Specification: dlib/queue/queue_sort_abstract.h
Implementations:

> queue_sort_1:
> This is a version of the QuickSort algorithm.

> sort_1a | is a typedef for queue_kernel_1a extended by queue_sort_1
> sort_1a_c | is a typedef for sort_1a that checks its preconditions.
> sort_1b | is a typedef for queue_kernel_2a extended by queue_sort_1
> sort_1b_c | is a typedef for sort_1b that checks its preconditions.
> sort_1c | is a typedef for queue_kernel_2b extended by queue_sort_1
> sort_1c_c | is a typedef for sort_1c that checks its preconditions.

[top]

# reference_counter

This object represents a container for an object and provides reference counting capabilities for the object it contains.
Specification: dlib/reference_counter/reference_counter_kernel_abstract.h
File to include: dlib/reference_counter.h
Implementations:

> reference_counter_kernel_1:
> This implementation is done using pointers in the obvious way.

> kernel_1a | is a typedef for reference_counter_kernel_1

[top]

# remover

This is a set of interfaces which gives the ability to remove all the items in a container without actually knowing what kind of container contains them.
Specification: dlib/interfaces/remover.h
File to include: dlib/interfaces/remover.h
[top]

# scoped_ptr

This is a implementation of the scoped_ptr class found in the Boost C++ library. It is a simple smart pointer class which guarantees that the pointer contained within it will always be deleted. The class does not permit copying and so does not do any kind of reference counting. Thus it is very simple and quite fast.
Specification: dlib/smart_pointers/scoped_ptr_abstract.h
File to include: dlib/smart_pointers.h
[top]

# sequence

This object represents an ordered sequence of items, each item is associated with an integer value. The items are numbered from 0 to the number of items in the sequence minus 1.
Specification: dlib/sequence/sequence_kernel_abstract.h
File to include: dlib/sequence.h
Implementations:

> sequence_kernel_1:
> This is implemented as an AVL binary search tree. Accessing(or adding or removing) an element always takes O(log n) time. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for sequence_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

> sequence_kernel_2:
> This implementation is done using a doubly linked list in the shape of a ring. It will remember the last element accessed(or added or removed) and give O(1) access time to the elements just left and right of it. Aside from that, accessing(or adding or removing) a random element will take O(n) and in the worst case it will take time proportional to the size of the sequence/2.

> It does not use the memory_manager at all.

> kernel_2a | is a typedef for sequence_kernel_2
> kernel_2a_c | is a typedef for kernel_2a that checks its preconditions.

## Extensions to sequence

sequence_compare:
This extension gives sequences the ability to compare themselves using operator< and operator==. Thus they can be used in the other container classes that require this ability. (maps, sets, etc...)
Specification: dlib/sequence/sequence_compare_abstract.h
Implementations:

> sequence_compare_1:
> The implementation is obvious. Click on the sequence_compare_1 link if you want to see.

> compare_1a | is a typedef for sequence_kernel_1a extended by sequence_compare_1
> compare_1a_c | is a typedef for compare_1a that checks its preconditions.
> compare_1b | is a typedef for sequence_kernel_2a extended by sequence_compare_1
> compare_1b_c | is a typedef for compare_1b that checks its preconditions.

sequence_sort:
This extension gives a sequence the ability to sort its contents.
Specification: dlib/sequence/sequence_sort_abstract.h
Implementations:

> sequence_sort_1:
> This is a version of the QuickSort algorithm and it sorts sequences of less than 30 elements with a selection sort. This implementation is fastest when used with sequence_kernel_2 and fairly slow when used with sequence_kernel_1

> sort_1a | is a typedef for sequence_kernel_2a extended by sequence_sort_1
> sort_1a_c | is a typedef for sort_1a that checks its preconditions.

> sequence_sort_2:
> This is a version of the QuickSort algorithm. This implementation of sort is the best to use with sequence_kernel_1 objects but gives extremely poor performance with sequence_kernel_2 objects.

> sort_2a | is a typedef for sequence_kernel_1a extended by sequence_sort_2
> sort_2a_c | is a typedef for sort_2a that checks its preconditions.

[top]

# set

This object represents an unordered and unaddressed collection of unique items.
Specification: dlib/set/set_kernel_abstract.h
File to include: dlib/set.h
Implementations:

> set_kernel_1:
> This is implemented using the binary_search_tree component. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for set_kernel_1 that uses binary_search_tree_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.
> kernel_1b | is a typedef for set_kernel_1 that uses binary_search_tree_kernel_2
> kernel_1b_c | is a typedef for kernel_1b that checks its preconditions.

## Extensions to set

set_compare:
This extension gives sets the ability to compare themselves using operator< and operator==. Thus they can be used in the other container classes that require this ability. (maps, sets, etc...)
Specification: dlib/set/set_compare_abstract.h
Implementations:

> set_compare_1:
> The implementation is obvious. Click on the set_compare_1 link if you want to see.

> compare_1a | is a typedef for set_kernel_1a extended by set_compare_1
> compare_1a_c | is a typedef for compare_1a that checks its preconditions.
> compare_1b | is a typedef for set_kernel_1b extended by set_compare_1
> compare_1b_c | is a typedef for compare_1b that checks its preconditions.

[top]

# shared_ptr

This object represents a reference counted smart pointer. Each shared_ptr contains a pointer to some object and when the last shared_ptr that points to the object is destructed or reset() then the object is guaranteed to be deleted.

This is an implementation of the std::tr1::shared_ptr template from the document ISO/IEC PDTR 19768, Proposed Draft Technical Report on C++ Library Extensions. The only deviation from that document is that this shared_ptr is declared inside the dlib namespace rather than std::tr1.

Specification: dlib/smart_pointers/shared_ptr_abstract.h
File to include: dlib/smart_pointers.h
[top]

# shared_ptr_thread_safe

This object represents a reference counted smart pointer just like shared_ptr except that it is threadsafe.

Specification: dlib/smart_pointers/shared_ptr_thread_safe_abstract.h
File to include: dlib/smart_pointers.h
[top]

# sliding_buffer

This object represents an array with the ability to rotate its contents left or right.
Specification: dlib/sliding_buffer/sliding_buffer_kernel_abstract.h
File to include: dlib/sliding_buffer.h
Implementations:

> sliding_buffer_kernel_1:
> This object is implemented using a C style array in the obvious way. See the code for details.

> kernel_1a | is a typedef for sliding_buffer_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

[top]

# stack

This object represents a last in first out stack.
Specification: dlib/stack/stack_kernel_abstract.h
File to include: dlib/stack.h
Implementations:

> stack_kernel_1:
> This implementation is done in the obvious way using a singly linked list. It uses the memory_manager for all memory allocations.

> kernel_1a | is a typedef for stack_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

[top]

# static_map

This object represents a mapping of items of type domain onto items of type range. The difference between this object and the normal map object is that it does not support adding or removing individual objects from itself. This allows implementations to focus on using less memory and achieving faster searching.
Specification: dlib/static_map/static_map_kernel_abstract.h
File to include: dlib/static_map.h
Implementations:

> static_map_kernel_1:
> This implementation is just a sorted array which can be searched using a binary search.

> kernel_1a | is a typedef for static_map_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

[top]

# static_set

This object represents an unordered and unaddressed collection of items. The difference between this object and the normal set object is that it does not support adding or removing individual objects from itself. This allows implementations to focus on using less memory and achieving faster searching.
Specification: dlib/static_set/static_set_kernel_abstract.h
File to include: dlib/static_set.h
Code Examples: 1
Implementations:

> static_set_kernel_1:
> This implementation is just a sorted array which can be searched using a binary search.

> kernel_1a | is a typedef for static_set_kernel_1
> kernel_1a_c | is a typedef for kernel_1a that checks its preconditions.

## Extensions to static_set

static_set_compare:
This extension gives static_sets the ability to compare themselves using operator< and operator==. Thus they can be used in the other container classes that require this ability. (maps, static_sets, etc...)
Specification: dlib/static_set/static_set_compare_abstract.h
Implementations:

> static_set_compare_1:
> The implementation is obvious. Click on the static_set_compare_1 link if you want to see.

> compare_1a | is a typedef for static_set_kernel_1a extended by static_set_compare_1
> compare_1a_c | is a typedef for compare_1a that checks its preconditions.

[top]

# std_vector_c

This object is a simple wrapper around the std::vector object. It provides an identical interface but also checks the preconditions of each member function. That is, if you violate a requires clause the dlib::fatal_error exception is thrown.
Specification: dlib/stl_checked/std_vector_c_abstract.h
File to include: dlib/stl_checked.h
[top]

# tuple

This is an implementation of a very simple templated container object. It contains between 0 and 31 objects where each object is listed explicity in the tuple's template arguments.

Note that there is only one implementation of this object so there aren't any different kernels to choose from when you create instances of the matrix object. So for example, you could declare a tuple of 3 ints using the following statement: `dlib::tuple<int,int,int> t;`

Specification: dlib/tuple/tuple_abstract.h
File to include: dlib/tuple.h
[top]

# weak_ptr

The weak_ptr class template stores a weak reference to an object that is already managed by a shared_ptr. To access the object, a weak_ptr can be converted to a shared_ptr using the member function lock().

This is an implementation of the std::tr1::weak_ptr template from the document ISO/IEC PDTR 19768, Proposed Draft Technical Report on C++ Library Extensions. The only deviation from that document is that this shared_ptr is declared inside the dlib namespace rather than std::tr1.

Specification: dlib/smart_pointers/weak_ptr_abstract.h
File to include: dlib/smart_pointers.h