Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
array_utils_example.cc
Go to the documentation of this file.
1
25#include <iostream>
26#include <array_utils.H>
27#include <tpl_dynArray.H>
28
29using namespace std;
30using namespace Aleph;
31
32int main()
33{
34 cout << "=== Array Utilities: Educational Examples ===\n\n";
35
36 // =========================================================================
37 // EXAMPLE 1: Reversing Arrays
38 // =========================================================================
39 // CONCEPT: Reverse array elements in-place with O(n) time, O(1) space
40 // APPLICATION: String reversal, palindrome checking, undo/redo stacks
41 {
42 cout << "--- Example 1: Reversing Arrays ---\n\n";
43
44 // Create a simple array
45 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
46 const size_t n = 8;
47
48 cout << "Original array: ";
49 for (size_t i = 0; i < n; ++i)
50 cout << arr[i] << " ";
51 cout << "\n";
52
53 // STEP 1: Reverse the entire array
54 // Algorithm: Swap elements from both ends moving towards center
55 // Time: O(n), Space: O(1)
56 Aleph::reverse(arr, n);
57
58 cout << "After reverse: ";
59 for (size_t i = 0; i < n; ++i)
60 cout << arr[i] << " ";
61 cout << "\n";
62
63 // LESSON: This is the most efficient way to reverse an array
64 // No extra memory needed, just n/2 swaps
65 cout << "\nLESSON: Reversal uses only n/2 swaps - very efficient!\n\n";
66 }
67
68 // =========================================================================
69 // EXAMPLE 2: Array Rotation
70 // =========================================================================
71 // CONCEPT: Circular shift of array elements
72 // REAL-WORLD: Circular buffers, sliding windows, scheduling algorithms
73 {
74 cout << "--- Example 2: Array Rotation ---\n\n";
75
76 char letters[] = {'A', 'B', 'C', 'D', 'E', 'F'};
77 const size_t n = 6;
78
79 cout << "Original: ";
80 for (size_t i = 0; i < n; ++i)
81 cout << letters[i] << " ";
82 cout << "\n";
83
84 // STEP 1: Rotate LEFT by 2 positions
85 // [A B C D E F] -> [C D E F A B]
86 // Complexity: O(n) time using reversal algorithm
88
89 cout << "Rotate left 2: ";
90 for (size_t i = 0; i < n; ++i)
91 cout << letters[i] << " ";
92 cout << "\n";
93
94 // STEP 2: Rotate RIGHT by 3 positions
95 // [C D E F A B] -> [F A B C D E]
97
98 cout << "Rotate right 3: ";
99 for (size_t i = 0; i < n; ++i)
100 cout << letters[i] << " ";
101 cout << "\n";
102
103 // KEY INSIGHT: Rotation uses clever reversal trick:
104 // To rotate left by k: reverse(0,k), reverse(k,n), reverse(0,n)
105 cout << "\nKEY ALGORITHM: Rotation = 3 reversals (Bentley's algorithm)\n";
106 cout << " 1. Reverse first k elements\n";
107 cout << " 2. Reverse remaining n-k elements\n";
108 cout << " 3. Reverse entire array\n\n";
109 }
110
111 // =========================================================================
112 // EXAMPLE 3: Gap Operations (Advanced)
113 // =========================================================================
114 // CONCEPT: Insert/remove space in arrays efficiently
115 // APPLICATION: Text editors, memory allocators, dynamic data structures
116 {
117 cout << "--- Example 3: Gap Operations (Insert/Delete Space) ---\n\n";
118
119 // Start with array with room for expansion
120 int buffer[10] = {10, 20, 30, 40, 50};
121 size_t used = 5; // Current number of elements
122
123 cout << "Initial buffer (5 elements): ";
124 for (size_t i = 0; i < used; ++i)
125 cout << buffer[i] << " ";
126 cout << "\n";
127
128 // STEP 1: Open a gap to insert new elements
129 // Want to insert 2 elements at position 2
130 // [10 20 30 40 50] -> [10 20 __ __ 30 40 50]
131 const size_t insert_pos = 2;
132 const size_t gap_size = 2;
133
134 cout << "\nOpening gap of size " << gap_size << " at position " << insert_pos << "...\n";
136
137 // STEP 2: Fill the gap with new values
138 buffer[insert_pos] = 25;
139 buffer[insert_pos + 1] = 27;
140 used += gap_size;
141
142 cout << "After inserting 25, 27: ";
143 for (size_t i = 0; i < used; ++i)
144 cout << buffer[i] << " ";
145 cout << "\n";
146
147 // STEP 3: Close a gap (remove elements)
148 // Remove 2 elements starting at position 3
149 cout << "\nClosing gap: removing 2 elements at position 3...\n";
150 Aleph::close_gap(buffer, used, 3, 2);
151 used -= 2;
152
153 cout << "After removal: ";
154 for (size_t i = 0; i < used; ++i)
155 cout << buffer[i] << " ";
156 cout << "\n";
157
158 // PRACTICAL USAGE: This is how text editors manage insertion/deletion
159 cout << "\nREAL-WORLD: Text editors use gap buffers for efficient editing\n";
160 cout << " - Gap moves with cursor\n";
161 cout << " - Insert/delete at gap position is O(1)\n";
162 cout << " - Moving gap is O(distance)\n\n";
163 }
164
165 // =========================================================================
166 // EXAMPLE 4: Using with DynArray
167 // =========================================================================
168 // CONCEPT: Integrate array utils with Aleph-w containers
169 {
170 cout << "--- Example 4: Integration with DynArray ---\n\n";
171
172 DynArray<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
173
174 cout << "DynArray: ";
175 arr.for_each([](int x) { cout << x << " "; });
176 cout << "\n";
177
178 // DynArray provides access method for internal array
179 size_t n = arr.size();
180
181 // Can iterate and reverse conceptually
182 cout << "Reversed (manual): ";
183 for (int i = n - 1; i >= 0; --i)
184 cout << arr[i] << " ";
185 cout << "\n\n";
186 }
187
188 cout << "=== SUMMARY: Key Concepts ===\n";
189 cout << "\n1. EFFICIENCY:\n";
190 cout << " All operations are O(n) time, O(1) space\n";
191 cout << " In-place algorithms minimize memory usage\n";
192 cout << "\n2. ROTATION ALGORITHM (Bentley):\n";
193 cout << " Three reversals achieve rotation\n";
194 cout << " More efficient than naive circular shifting\n";
195 cout << "\n3. GAP BUFFERS:\n";
196 cout << " Core technique for text editors\n";
197 cout << " Efficient insertion/deletion at cursor\n";
198 cout << "\n4. WHEN TO USE:\n";
199 cout << " - Implementing custom containers\n";
200 cout << " - Performance-critical array manipulation\n";
201 cout << " - Building higher-level data structures\n";
202 cout << "\n5. COMPLEXITY SUMMARY:\n";
203 cout << " reverse(): O(n) time, O(1) space\n";
204 cout << " rotate(): O(n) time, O(1) space\n";
205 cout << " open_gap(): O(n) time, O(1) space\n";
206 cout << " close_gap(): O(n) time, O(1) space\n";
207
208 return 0;
209}
Utility functions for array manipulation.
int main()
size_t size() const noexcept
Return the current dimension of array.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:685
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void reverse(Itor beg, Itor end)
Reverse elements in a range.
Definition ahAlgo.H:1094
void rotate_left(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements left by m positions.
void open_gap(Tarray &ptr, size_t n, size_t pos=0, size_t num_entries=1)
Open a gap in an array by shifting elements right.
Definition array_utils.H:96
void rotate_right(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements right by m positions.
void close_gap(T *ptr, size_t n, size_t pos, size_t num_entries=1)
Close a gap in an array by shifting elements left.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Lazy and scalable dynamic array implementation.