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 _STORAGE_CLASS_HPP_
21 #define _STORAGE_CLASS_HPP_
29 /* Abstract base class for the different storage classes
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
51 virtual size_t get_capacity() // available storage (in units of storage type)
56 virtual size_t get_size() // used storage (in units of storage type)
61 /* the concepts of first, last, next and previous may not have a useful meaning in
62 * some storage classes e.g: 'previous' in a 1-way linked list (only has 'next').
64 virtual S& get_first() = 0;
65 virtual S& get_last() = 0;
66 virtual S& get_next() = 0;
67 virtual S& get_previous() = 0;
70 * @param data the item (of type S) to be stored
71 * @param index position to insert; 0 is 'beginning', -1 is 'end'
73 * Depending on the data structure, values of index other than 0 or -1 may have no meaning and be ignored
75 virtual bool insert_at(S& data, long index = -1) = 0;
77 virtual bool insert(S& data)
79 return insert_at(data, 0);
81 virtual bool append(S& data)
83 return insert_at(data, -1);
86 /* the remove operation can perform an optional erasure (delete) of the stored data.
88 * returns true if the operation succeeded
91 // 'data' element at 'index' must satisfy equality
92 virtual bool remove_at(S& data, long index = -1, bool erase = false) = 0; // element matching 'data' at 'index'
93 virtual bool remove_at(long index = -1, bool erase = false) = 0; // any element at 'index'
94 virtual bool remove_all(S& data, bool erase = false) = 0; // all elements matching 'data'
96 /* the replace operation can perform an optional erasure (delete) of the current data element
98 virtual bool replace_at(S& data_in, S& data_out, long index = -1, bool erase = false) = 0; // element matching 'data_out' at 'index'
99 virtual bool replace_at(S& data_in, long index = -1, bool erase = false) = 0; // any element at 'index'
100 virtual bool replace_all(S& data_in, S& data_out, bool erase = false) = 0; // all elements matching 'data_out'
103 /* Search for an element matching 'needle'. The stored type must have defined
104 * operator<() and operator==() to allow the container to compare 'needle' with each stored element.
106 * If find() is called with last_found == nullptr it searches from the start of the container. If
107 * last_found points to an element in the container the search continues from that element. The
108 * implementation must guard against last_found not pointing to a valid object, or that object not
109 * belonging to the container.
111 * returns a nullptr if there are no (more) matches
113 virtual S* find(S& needle, S* last_found = nullptr) = 0;
115 // mnemonic alternatives to plain 'index' values
123 /* Specialised data structure implementing a Dynamic Array
125 template <typename AD>
126 class DataStructure_ArrayDynamic : AbstractDataStructure<AD>
131 /* Specialised data structure implementing a 1-way Linked List
133 template <typename LLS>
134 class DataStructure_LinkedListSingle : AbstractDataStructure<LLS>
139 /* Specialised data structure implementing a 2-way Linked List
141 template <typename LLD>
142 class DataStructure_LinkedListDouble : DataStructure_LinkedListSingle<LLD>
147 /* Specialised data structure implementing a Binary Search Tree
149 template <typename BST>
150 class DataStructure_BinarySearchTree : DataStructure<BST>
155 }; // end of namespace