libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
sortedArray.hpp
1#pragma once
2#ifndef SORTEDARRAY_H_INCLUDED
3#define SORTEDARRAY_H_INCLUDED
4
5#include "array.hpp"
6
7namespace z {
8namespace core {
17template <typename T>
18class sortedArray : public array<T> {
19public:
22
33 template <typename... Args>
34 sortedArray(const T &arg1, const Args &...args) {
35 this->init(arg1, args...);
36 }
37
49 virtual int add(const T &object) override;
50
65 virtual int find(const T &object) const override;
66
79 virtual int findInsert(const T &object, bool allowDuplicates = true) const;
80
81 // For sorted arrays, shuffle does nothing.
82 virtual void shuffle() noexcept override {}
83
84 // For sorted arrays, reverse does nothing.
85 virtual void reverse() noexcept override {}
86};
87
88template <typename T>
89int sortedArray<T>::add(const T &object) {
90 int index = findInsert(object);
91
92 this->insert(object, index);
93
94 return index;
95}
96
97template <typename T>
98int sortedArray<T>::find(const T &object) const {
99 if (this->array_data.size() == 0) {
100 return -1;
101 }
102
103 int left = 0;
104 int right = this->array_data.size() - 1;
105
106 while (left < right) {
107 int center = (left + right) / 2;
108
109 if (this->lt(this->array_data.at(center), object)) {
110 left = center + 1;
111 } else if (this->gt(this->array_data.at(center), object)) {
112 right = center - 1;
113 } else {
114 return center;
115 }
116 }
117
118 if (this->eq(this->array_data.at(left), object)) {
119 return left;
120 } else {
121 return -1;
122 }
123}
124
125template <typename T>
126int sortedArray<T>::findInsert(const T &object, bool allowDuplicates) const {
127 if (this->array_data.size() == 0) {
128 return 0;
129 }
130
131 int left = 0;
132 int right = this->array_data.size() - 1;
133
134 while (left < right) {
135 int center = (left + right) / 2;
136
137 if (this->lt(this->array_data.at(center), object)) {
138 left = center + 1;
139 } else if (this->gt(this->array_data.at(center), object)) {
140 right = center - 1;
141 } else {
142 return allowDuplicates ? center : -1;
143 }
144 }
145
146 if (this->lt(this->array_data.at(left), object)) {
147 return left + 1;
148 } else if (allowDuplicates || !this->eq(this->array_data.at(left), object)) {
149 return left;
150 } else {
151 return -1;
152 }
153}
154
155} // namespace core
156} // namespace z
157
158#endif // SORTEDARRAY_H_INCLUDED
A wrapper for std::vector.
Definition array.hpp:72
virtual bool eq(const T &arg1, const T &arg2) const
Check if two objects are equal.
Definition array.hpp:108
void init(const T &arg1)
Helper function for single object initialization.
Definition array.hpp:81
std::vector< T > array_data
The data in the array.
Definition array.hpp:75
size_t size() const noexcept override
Get the size of the array.
Definition array.hpp:1000
array()
Default constructor.
Definition array.hpp:146
array & insert(const T &, int)
Insert an object into the array.
Definition array.hpp:899
virtual bool gt(const T &arg1, const T &arg2) const
Check if one object is greater than another.
Definition array.hpp:124
virtual bool lt(const T &arg1, const T &arg2) const
Check if one object is less than another.
Definition array.hpp:140
An extension of the core::array class which attempts to keep all data sorted.
Definition sortedArray.hpp:18
virtual void shuffle() noexcept override
Shuffle the elements of the array into a random order.
Definition sortedArray.hpp:82
sortedArray(const T &arg1, const Args &...args)
List-initialized constructor.
Definition sortedArray.hpp:34
virtual void reverse() noexcept override
Reverse the order of all elements in the array.
Definition sortedArray.hpp:85
virtual int find(const T &object) const override
Check if a given object is in the array.
Definition sortedArray.hpp:98
virtual int findInsert(const T &object, bool allowDuplicates=true) const
Find an index where the given object can be inserted while keeping the array sorted.
Definition sortedArray.hpp:126
sortedArray()
Default constructor.
Definition sortedArray.hpp:21
virtual int add(const T &object) override
Add an object to the array.
Definition sortedArray.hpp:89