2 * Implementation of Data Structures for GenericStorageContainerTemplateImpl
3 * Copyright (c) 2014 TJ <hacker@iam.tj>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * see ./COPYRIGHT or http://www.gnu.org/licenses/gpl-3.0.html
20 #ifndef _DATASTRUCTURE_HPP_
21 #define _DATASTRUCTURE_HPP_
29 /* Abstract base class for the different data structures
31 * Defines the API they must implement. The API allows the Container and its
32 * Iterator to call common methods to perform store, retrieve, and search operations
36 class AbstractDataStructure
42 AbstractDataStructure() {};
43 virtual ~AbstractDataStructure() {};
45 /* API available to application
47 * Some methods are pure virtual making this an Abstract (base) class
48 * Sub-classes must implement these methods and over-ride others to
49 * provide the specific storage class functionality
52 virtual size_t get_capacity() // available storage (in units of storage type)
57 virtual size_t get_size() // used storage (in units of storage type)
62 /* the concepts of first, last, next and previous may not have a useful meaning in
63 * some storage classes e.g: 'previous' in a 1-way linked list (only has 'next').
65 virtual D* get_data() = 0;
66 virtual D& get_first() = 0;
67 virtual D& get_last() = 0;
68 virtual D& get_next() = 0;
69 virtual D& get_previous() = 0;
72 * @param data the item (of type S) to be stored
73 * @param index position to insert; 0 is 'beginning', -1 is 'end'
75 * Depending on the data structure, values of index other than 0 or -1 may have no meaning and be ignored
77 virtual bool insert_at(D& data, long index = -1) = 0;
79 virtual bool insert(D& data)
81 return insert_at(data, 0);
83 virtual bool append(D& data)
85 return insert_at(data, -1);
88 /* the remove operation can perform an optional erasure (delete) of the stored data.
90 * returns true if the operation succeeded
93 // 'data' element at 'index' must satisfy equality
94 virtual bool remove_at(D& data, long index = -1, bool erase = false) = 0; // element matching 'data' at 'index'
95 virtual bool remove_at(long index = -1, bool erase = false) = 0; // any element at 'index'
96 virtual bool remove_all(D& data, bool erase = false) = 0; // all elements matching 'data'
98 /* the replace operation can perform an optional erasure (delete) of the current data element
100 virtual bool replace_at(D& data_in, D& data_out, long index = -1, bool erase = false) = 0; // element matching 'data_out' at 'index'
101 virtual bool replace_at(D& data_in, long index = -1, bool erase = false) = 0; // any element at 'index'
102 virtual bool replace_all(D& data_in, D& data_out, bool erase = false) = 0; // all elements matching 'data_out'
105 /* Search for an element matching 'needle'. The stored type must have defined
106 * operator<() and operator==() to allow the container to compare 'needle' with each stored element.
108 * If find() is called with last_found == nullptr it searches from the start of the container. If
109 * last_found points to an element in the container the search continues from that element. The
110 * implementation must guard against last_found not pointing to a valid object, or that object not
111 * belonging to the container.
113 * returns a nullptr if there are no (more) matches
115 virtual D* find(D& needle, D* last_found = nullptr) = 0;
117 // mnemonic alternatives to plain 'index' values
127 /* Specialised data structure implementing a Dynamic Array
129 template <typename AD>
130 class DataStructure_ArrayDynamic : AbstractDataStructure<AD>
135 /* Specialised data structure implementing a 1-way Linked List
137 template <typename LLS>
138 class DataStructure_LinkedListSingle : AbstractDataStructure<LLS>
143 /* Specialised data structure implementing a 2-way Linked List
145 template <typename LLD>
146 class DataStructure_LinkedListDouble : DataStructure_LinkedListSingle<LLD>
151 /* Specialised data structure implementing a Binary Search Tree
153 template <typename BST>
154 class DataStructure_BinarySearchTree : AbstractDataStructure<BST>
159 }; // end of namespace