The Deque class

(No version information available, might only be in Git)

Introduction

A Deque (pronounced “deck”) is a sequence of values in a contiguous buffer that grows and shrinks automatically. The name is a common abbreviation of “double-ended queue” and is used internally by Ds\Queue.

Two pointers are used to keep track of a head and a tail. The pointers can “wrap around” the end of the buffer, which avoids the need to move other values around to make room. This makes shift and unshift very fast —  something a Ds\Vector can’t compete with.

Accessing a value by index requires a translation between the index and its corresponding position in the buffer: ((head + position) % capacity).

Strengths

  • Supports array syntax (square brackets).
  • Uses less overall memory than an array for the same number of values.
  • Automatically frees allocated memory when its size drops low enough.
  • get(), set(), push(), pop(), shift(), and unshift() are all O(1).

Weaknesses

  • Capacity must be a power of 2.
  • insert() and remove() are O(n).

Class synopsis

Ds\Deque implements nopsisinfo"> _abbr
"> _varstrong "languags-deque.php">Edit<#e.php">Edts.php'>Comin-y)pan> fvar>intrue.ass="type"><-scale=ps = 8intrue.intru;
nopsisinfo"> _abbr
dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocate.php"> "type"><"get" stronge.php"> pan> for thiv cl(n.ass="type"><"get" ers(ers$y) ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatepply "type"><"get" strongepplypan> for thiv cl(n.ass="type"><"get" ersreesight teesightpan> for thelass="code">(ers$y)llb. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocaty)<"get" strongy) for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatx"> "type"><"get" strongy"> . n> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatxoquai>Persi "type"><"get" strongyoquai>P. n> for thiv cl([n.ass="type"><"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$... . ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatxope "type"><"get" strongyope. n> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatfie" h "type"><"get" strongfie" h. n> for thiv cl([n.ass="type"><"get" ersreesight teesightpan> for thelass="code">(ers$y)llb. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocatfind "type"><"get" strongfindpan> for thiv cl(n.ass="type"><"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocatfirst "type"><"get" strongfirst. n> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocatget "type"><"get" stronggetpan> for thiv cl(n.ass="type"><"get" ers(ers$nd it. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocat)<"get" strong) for thiv cl(n.ass="type"><"get" ers(ers$nd it. <"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$... . ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatisempde "type"><"get" strongisEmpde. n> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type">re O(n.ass="type"><"get" strong "language.allocatjo>Insta "type"><"get" strongjo>I. n> for thiv cl([n.ass="type"><"get" ersre O(n.ass="code">(ers$gli>. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocatyper "type"><"get" strongyper. n> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatmap "type"><"get" strongmappan> for thiv cl(n.ass="type"><"get" ersreesight teesightpan> for thelass="code">(ers$y)llb. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatmerg "type"><"get" strongmerg pan> for thiv cl(n.ass="type"><"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$ . ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocatpop "type"><"get" strongpoppan> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocat/str "type"><"get" strongpuion>n> for thiv cl([n.ass="type"><"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$... . ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocatreduc "type"><"get" strongreduc pan> for thiv cl(n.ass="type"><"get" ersreesight teesightpan> for thelass="code">(ers$y)llb.<"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$-scale=. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocatre/str "type"><"get" strongre/strpan> for thiv cl(n.ass="type"><"get" ers(ers$nd it. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatre infr "type"><"get" strongre infrpan> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatre infrd "type"><"get" strongre infrdpan> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatrot"> "type"><"get" strongrot"> pan> for thiv cl(n.ass="type"><"get" ers(ers$rot">> ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatset "type"><"get" strongsetpan> for thiv cl(n.ass="type"><"get" ers(ers$nd it. <"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$. ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> pa e O(n.ass="type"><"get" strong "language.allocathp"> "type"><"get" strongsp"> pan> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatslic "type"><"get" strongslic pan> for thiv cl(n.ass="type"><"get" ers(ers$nd it. <"get" ers(ers$l/h2> . ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocatsotr "type"><"get" strongsotrpan> for thiv cl([n.ass="type"><"get" ersreesight teesightpan> for thelass="code">(ers$yomers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatsop'>Getti "type"><"get" strongsotrrdpan> for thiv cl([n.ass="type"><"get" ersreesight teesightpan> for thelass="code">(ers$yomers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"> e O(n.ass="type"><"get" strong "language.allocatsumetti "type"><"get" strongsumpan> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocattohp" class="type ar"get" strongtoA> for thiv cl(n.ass="type"><"get" ers ss="edit-bu"get" sinfo"> dc-tion+xml" hh2 clasass="interfamod/opt pdedic are O(n.ass="type"><"get" strong "language.allocat.php"> "type"><"get" strong.php"> n>n> for thiv cl([n.ass="type"><"get" ers ="type array">mp"> =>mp"> pan> for thelass="code">(ers$ . iv c}
Class sned Interfats

Keydiv clintrue.dt-deque.synopsits.php'>Comin-y)Focintrue.ddocintruess="para"> /.intrue.iddocintruiv clal>

Td Typofl Exte

— Updquos.). around by epplyalue by)llb. e::allocaty"> . n> — Re/strs.). around fromed tollocat href='book.due.allocatxs.phs — Crequos.) new i.php'cat href='book.due.allocatxs.uai>Persi e::allocatyoquai>P. n> — Da>P given. href='book.due.allocatxspe e::allocatyope. n> — Returneea she &rw yope buffer,llocat href='book.due.allocatxsuna e::allocatxsuna. n> — Returneed toof values. buffer:con — Attempd rofinde by ind'sand itt href='book.due.allocatfirst e::allocatfirst. n> — Returneed tofirstby indebuffer:llocat href='book.due.allocatget e::allocatgetpan> — Returneed toy indeagiven.nd itt href='book.due.allocat) — Igiven.nd itt href='book.due.allocat)sempde e::allocat)sEmpde. n> — Returneewhesic Ed tolloca > empde. href='book.due.allocatjo>Insta e::allocatjo>I. n> — Joi>P ). around togesic Eaeea str — Returneed tolast aroun href='book.due.allocatmap e::allocatmappan> — Returneed to">
    ead applyalue by)llb. — Returneed to">
      ead adsitio). given. er:llocat href='book.due.allocatpop e::allocatpoppan> — Re/strs.)ndereturneed tolast aroun href='book.due.allocat/str e::allocatpuion>n> — Adds. er:the buffer,llocat href='book.due.allocatreduc e::allocatreduc pan> — Reduc sEd tolloca /dda sitiltoy indeusalue by)llb. — Re infrsEd tolloca in-lder=t href='book.due.allocatre infrd e::allocatre infrdpan> — Returneea ss infrd yopet href='book.due.allocatrot"> e::allocatrot"> pan> — Rot"> sEd tolloca by e given.of values.rot">> e::allocatsp"> n>n> — Re/strs.)ndereturneed tofirstby indt href='book.due.allocathlic e::allocatslic pan> — Returneea suballocaead angiven.rp" me href='book.due.allocathotr e::allocatsotrpan> — Sray sd tolloca in-lder=t href='book.due.allocatsop'>Getti e::allocatsotrrdpan> — Returneea sotrrd yopet href='book.due.allocatsumetti e::allocatsumpan> — Returneed tosumead a. around buffer:llocat href='book.due.allocattohp" class= e::allocattoA> — CoqveotsEd tolloca /ddan>arrayt href='book.due.allocat.php"> e::allocat.php"> n>n> — Adds. er:front buffer,llocat h

      < n id="layout-u/stn>'book.du/change.ads-n>Repornpageard rid=dfr.php.net/manual/en/class.ds-deque.php"> <'ads.) n>ads.) n>n>n> for thiv Class sbmitt Exteiburrd N><3> > ass../nav> IE] content"> 'contentemiv clintrue lass="simple'page) iv clintrue la In> duction

afintrue la ct as aalu/ ExfigurConst="PHP: Hyped Interfatsafintrue la Exats Cd>afintrue la ion intrue la Hashightd> intrue la on" id="ds intrue la /strong> intrue la intrue la intrue la ontpan>intrue la intrue la Qtrong>n>intrue la PriorrenQtrong>n>intrue la an cccccccccccccintrue la g>nions
intruass="classsynoquai>er fo>
clasref='book.du/noquaca clasref='book.du/si'>MagicMi> clasref='book.du/mihp'>Error= Mihp'> si'>Mi> clasref='book.du/privacclass= Privacc policyi> c
an
iiv cldif]E] onsretersty librar'>Co base on+xml> on+xml> on+xml> on+xml> on+xml> Sejv> on+xml> on+xml> ='bck"> for thc="/i48" he"40"="24" al"40"=p"><"To To "mages/logos/pto-top@2x.png">n>n> =/bodyp> htm