HackerReposA FIELD GUIDE TO OPEN SOURCE
ProSponsor the shelf♧ 0
← Back to top all-time
attilatorda
Shift-To-Middle_Array
C++2025–2025
attilatorda / Shift-To-Middle_Array

Shift-To-Middle_Array

An implementation strategy for lists, queues, deque(ue)s.

4Distinct people
4Mentions
1Explicit recommenders
120GitHub stars
REPOSITORY AT A GLANCE

What to know before exploring

What it is

An implementation strategy for lists, queues, deque(ue)s.

GitHub

Where it is used

No documented product use has been verified yet.

What people are saying

There’s a PDF with the results in the repo: https://github.com/attilatorda/Shift-To-Middle_Array/blob/ma... BTW, if OP is reading this, I recommend having the baseline in your…

nbonaparte used a recommendation phrase. One indexed comment matches. This is a phrase match, not a verdict.

No classified warning is attached to this page. Read the discussions before treating silence as approval.

Also in these discussions

No other repository is repeatedly linked in the same discussions.

This week on Hacker News

Nov 7–Nov 14 UTC · Direct repository links and separately labeled related project stories. Indexed through 11/14/2026, 1:49:46 PM UTC.

No direct repository links were found in the indexed HN discussions over the past seven days. Older discussions are available below.

Mentions over time

HN mentions by hour4 dated mentions

Observed mentions: 2025-03-23 23:20:27 UTC to 2025-03-24 10:02:31 UTC.

0122025-03-23 23:00:00 UTC to 2025-03-24 00:00:00 UTC (exclusive): 1 mentions; 1 submissions, 0 commentsMar 23, 23:002025-03-24 00:00:00 UTC to 2025-03-24 01:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 01:00:00 UTC to 2025-03-24 02:00:00 UTC (exclusive): 2 mentions; 0 submissions, 2 comments2025-03-24 02:00:00 UTC to 2025-03-24 03:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 03:00:00 UTC to 2025-03-24 04:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 04:00:00 UTC to 2025-03-24 05:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 commentsMar 24, 04:002025-03-24 05:00:00 UTC to 2025-03-24 06:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 06:00:00 UTC to 2025-03-24 07:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 07:00:00 UTC to 2025-03-24 08:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 08:00:00 UTC to 2025-03-24 09:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 09:00:00 UTC to 2025-03-24 10:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-03-24 10:00:00 UTC to 2025-03-24 11:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 commentsMar 24, 10:00

Counts come from the repository’s dated source evidence. Index cutoff: 2026-11-02 08:16:59 UTC.

View mention counts and date ranges
UTC ranges include their start and exclude their end. Select a period to filter the source evidence.
PeriodStart (UTC)End (exclusive, UTC)MentionsSubmissionsComments
Mar 23, 23:002025-03-23 23:00:00 UTC2025-03-24 00:00:00 UTC110
Mar 24, 00:002025-03-24 00:00:00 UTC2025-03-24 01:00:00 UTC000
Mar 24, 01:002025-03-24 01:00:00 UTC2025-03-24 02:00:00 UTC202
Mar 24, 02:002025-03-24 02:00:00 UTC2025-03-24 03:00:00 UTC000
Mar 24, 03:002025-03-24 03:00:00 UTC2025-03-24 04:00:00 UTC000
Mar 24, 04:002025-03-24 04:00:00 UTC2025-03-24 05:00:00 UTC000
Mar 24, 05:002025-03-24 05:00:00 UTC2025-03-24 06:00:00 UTC000
Mar 24, 06:002025-03-24 06:00:00 UTC2025-03-24 07:00:00 UTC000
Mar 24, 07:002025-03-24 07:00:00 UTC2025-03-24 08:00:00 UTC000
Mar 24, 08:002025-03-24 08:00:00 UTC2025-03-24 09:00:00 UTC000
Mar 24, 09:002025-03-24 09:00:00 UTC2025-03-24 10:00:00 UTC000
Mar 24, 10:002025-03-24 10:00:00 UTC2025-03-24 11:00:00 UTC101

What Hacker News says

4 sources
> I recently developed a new data structure Congratulations, you have discovered the array deque! https://en.wikipedia.org/wiki/Double-ended_queue#Implementat... I'd like to point out a performance optimization. On resize, you create a new array with double the size and copy all the old elements to the middle of the new array with more space at the beginning AND at the end: https://github.com/attilatorda/Shift-To-Middle_Array/blob/05... However, in practice, it is often the case that most operations append elements at either the front OR back, but rarely at both ends equally. For example, imagine a FIFO queue, where elements are popped from the front and pushed from the back. Therefore, it is more efficient to only reserve space at the back if the last operation was push_back or at the front if the last operation was push_front. One could
Read the rest
also carry along statistical information about the number of push_back and push_front operations and balance the space allocated at the front and back accordingly. In addition to ksherlock's points, there are also the following issues: - Vector implementations usually use size_t instead of int. Your implementation will fail for arrays larger than 2147483647, while size_t usually goes up to 18446744073709551615. - On memory allocation failure, you should throw std::bad_alloc instead of calling std::exit. - The front() function is missing a return statement. Turn on compiler warnings: -Wall -Wextra. For testing, -g -fsanitize=address,undefined is also helpful. - You are mixing delete[] with malloc'ed memory in shrink_to_fit. - You should implement copy constructor, move constructor, copy asignment and move assignment functions. See rule of five: https://en.cppreference.com/w/cpp/language/rule_of_three#Rul... - Switching features on or off is usually done with macros instead of comments. - 2 is not the best growth factor because it makes it harder for the memory allocator to reuse memory. More modern implementations use smaller growth factors: https://en.wikipedia.org/wiki/Dynamic_array#Growth_factor - A more reasonable initial capacity would be 0 instead of 16, as is common for all major std::vector implementations. An initial capacity of 16 wastes a lot of space when a large number of ShiftToMiddleArrays are allocated. - The compiler will most-likely ignore your inline instructions and decide on its own whether inlining is done or not, so might as well remove them. - Why are there two versions of the data structure? (ShiftToMiddleArray.cpp and ShiftToMiddleArray.h) - Some functions are just wrappers for other functions (pop_front = remove_head, pop_back = remove_tail). Why? LLMs can point out most of those issue, so you should use them to discover potential issues. Of course, make sure to double-check with more reliable sources once you know that a certain class of problems exists.
In Shift-to-Middle Array: A Faster Alternative to Std:Deque?
You don't want to always resize to same-size buffers in that situation; consider a size-1024 buffer containing 1023 elements and a free space on the right. Pushing another item on the left can be done in the very same buffer, but requires copying all 1023 items one space to the right. If you then pop another item off the right end you are back to the starting state, so by repeating the process you need 511.5 element copies (in general, O(N) copies) per push or pop. There are obvious ways to resolve problems like this, but there are tradeoffs among them, and I would like to know which way the author chose and what the resulting complexity is without having to analyze (and debug) 270 lines of C++. Hmm, there's a PDF at https://github.com/attilatorda/Shift-To-Middle_Array/blob/ma...... but it also doesn't explain things like this. It make
Read the rest
s assertions about big-O performance, but doesn't explain the algorithm in enough detail to know whether they are correct.
In Shift-to-Middle Array: A Faster Alternative to Std:Deque?

MAKE YOURSELF AT HOME

A few shortcuts

/
Search the collection
?
Open this guide
Esc
Close dialogs and navigation

Your saved repositories and interests stay in this browser.