matrix_sparse.hpp 226 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774
  1. //
  2. // Copyright (c) 2000-2007
  3. // Joerg Walter, Mathias Koch, Gunter Winkler
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // The authors gratefully acknowledge the support of
  10. // GeNeSys mbH & Co. KG in producing this work.
  11. //
  12. #ifndef _BOOST_UBLAS_MATRIX_SPARSE_
  13. #define _BOOST_UBLAS_MATRIX_SPARSE_
  14. #include <boost/numeric/ublas/vector_sparse.hpp>
  15. #include <boost/numeric/ublas/matrix_expression.hpp>
  16. #include <boost/numeric/ublas/detail/matrix_assign.hpp>
  17. #if BOOST_UBLAS_TYPE_CHECK
  18. #include <boost/numeric/ublas/matrix.hpp>
  19. #endif
  20. // Iterators based on ideas of Jeremy Siek
  21. namespace boost { namespace numeric { namespace ublas {
  22. #ifdef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  23. template<class M>
  24. class sparse_matrix_element:
  25. public container_reference<M> {
  26. public:
  27. typedef M matrix_type;
  28. typedef typename M::size_type size_type;
  29. typedef typename M::value_type value_type;
  30. typedef const value_type &const_reference;
  31. typedef value_type *pointer;
  32. typedef const value_type *const_pointer;
  33. private:
  34. // Proxied element operations
  35. void get_d () const {
  36. const_pointer p = (*this) ().find_element (i_, j_);
  37. if (p)
  38. d_ = *p;
  39. else
  40. d_ = value_type/*zero*/();
  41. }
  42. void set (const value_type &s) const {
  43. pointer p = (*this) ().find_element (i_, j_);
  44. if (!p)
  45. (*this) ().insert_element (i_, j_, s);
  46. else
  47. *p = s;
  48. }
  49. public:
  50. // Construction and destruction
  51. BOOST_UBLAS_INLINE
  52. sparse_matrix_element (matrix_type &m, size_type i, size_type j):
  53. container_reference<matrix_type> (m), i_ (i), j_ (j) {
  54. }
  55. BOOST_UBLAS_INLINE
  56. sparse_matrix_element (const sparse_matrix_element &p):
  57. container_reference<matrix_type> (p), i_ (p.i_), j_ (p.j_) {}
  58. BOOST_UBLAS_INLINE
  59. ~sparse_matrix_element () {
  60. }
  61. // Assignment
  62. BOOST_UBLAS_INLINE
  63. sparse_matrix_element &operator = (const sparse_matrix_element &p) {
  64. // Overide the implict copy assignment
  65. p.get_d ();
  66. set (p.d_);
  67. return *this;
  68. }
  69. template<class D>
  70. BOOST_UBLAS_INLINE
  71. sparse_matrix_element &operator = (const D &d) {
  72. set (d);
  73. return *this;
  74. }
  75. template<class D>
  76. BOOST_UBLAS_INLINE
  77. sparse_matrix_element &operator += (const D &d) {
  78. get_d ();
  79. d_ += d;
  80. set (d_);
  81. return *this;
  82. }
  83. template<class D>
  84. BOOST_UBLAS_INLINE
  85. sparse_matrix_element &operator -= (const D &d) {
  86. get_d ();
  87. d_ -= d;
  88. set (d_);
  89. return *this;
  90. }
  91. template<class D>
  92. BOOST_UBLAS_INLINE
  93. sparse_matrix_element &operator *= (const D &d) {
  94. get_d ();
  95. d_ *= d;
  96. set (d_);
  97. return *this;
  98. }
  99. template<class D>
  100. BOOST_UBLAS_INLINE
  101. sparse_matrix_element &operator /= (const D &d) {
  102. get_d ();
  103. d_ /= d;
  104. set (d_);
  105. return *this;
  106. }
  107. // Comparison
  108. template<class D>
  109. BOOST_UBLAS_INLINE
  110. bool operator == (const D &d) const {
  111. get_d ();
  112. return d_ == d;
  113. }
  114. template<class D>
  115. BOOST_UBLAS_INLINE
  116. bool operator != (const D &d) const {
  117. get_d ();
  118. return d_ != d;
  119. }
  120. // Conversion - weak link in proxy as d_ is not a perfect alias for the element
  121. BOOST_UBLAS_INLINE
  122. operator const_reference () const {
  123. get_d ();
  124. return d_;
  125. }
  126. // Conversion to reference - may be invalidated
  127. BOOST_UBLAS_INLINE
  128. value_type& ref () const {
  129. const pointer p = (*this) ().find_element (i_, j_);
  130. if (!p)
  131. return (*this) ().insert_element (i_, j_, value_type/*zero*/());
  132. else
  133. return *p;
  134. }
  135. private:
  136. size_type i_;
  137. size_type j_;
  138. mutable value_type d_;
  139. };
  140. /*
  141. * Generalise explicit reference access
  142. */
  143. namespace detail {
  144. template <class V>
  145. struct element_reference<sparse_matrix_element<V> > {
  146. typedef typename V::value_type& reference;
  147. static reference get_reference (const sparse_matrix_element<V>& sve)
  148. {
  149. return sve.ref ();
  150. }
  151. };
  152. }
  153. template<class M>
  154. struct type_traits<sparse_matrix_element<M> > {
  155. typedef typename M::value_type element_type;
  156. typedef type_traits<sparse_matrix_element<M> > self_type;
  157. typedef typename type_traits<element_type>::value_type value_type;
  158. typedef typename type_traits<element_type>::const_reference const_reference;
  159. typedef sparse_matrix_element<M> reference;
  160. typedef typename type_traits<element_type>::real_type real_type;
  161. typedef typename type_traits<element_type>::precision_type precision_type;
  162. static const unsigned plus_complexity = type_traits<element_type>::plus_complexity;
  163. static const unsigned multiplies_complexity = type_traits<element_type>::multiplies_complexity;
  164. static
  165. BOOST_UBLAS_INLINE
  166. real_type real (const_reference t) {
  167. return type_traits<element_type>::real (t);
  168. }
  169. static
  170. BOOST_UBLAS_INLINE
  171. real_type imag (const_reference t) {
  172. return type_traits<element_type>::imag (t);
  173. }
  174. static
  175. BOOST_UBLAS_INLINE
  176. value_type conj (const_reference t) {
  177. return type_traits<element_type>::conj (t);
  178. }
  179. static
  180. BOOST_UBLAS_INLINE
  181. real_type type_abs (const_reference t) {
  182. return type_traits<element_type>::type_abs (t);
  183. }
  184. static
  185. BOOST_UBLAS_INLINE
  186. value_type type_sqrt (const_reference t) {
  187. return type_traits<element_type>::type_sqrt (t);
  188. }
  189. static
  190. BOOST_UBLAS_INLINE
  191. real_type norm_1 (const_reference t) {
  192. return type_traits<element_type>::norm_1 (t);
  193. }
  194. static
  195. BOOST_UBLAS_INLINE
  196. real_type norm_2 (const_reference t) {
  197. return type_traits<element_type>::norm_2 (t);
  198. }
  199. static
  200. BOOST_UBLAS_INLINE
  201. real_type norm_inf (const_reference t) {
  202. return type_traits<element_type>::norm_inf (t);
  203. }
  204. static
  205. BOOST_UBLAS_INLINE
  206. bool equals (const_reference t1, const_reference t2) {
  207. return type_traits<element_type>::equals (t1, t2);
  208. }
  209. };
  210. template<class M1, class T2>
  211. struct promote_traits<sparse_matrix_element<M1>, T2> {
  212. typedef typename promote_traits<typename sparse_matrix_element<M1>::value_type, T2>::promote_type promote_type;
  213. };
  214. template<class T1, class M2>
  215. struct promote_traits<T1, sparse_matrix_element<M2> > {
  216. typedef typename promote_traits<T1, typename sparse_matrix_element<M2>::value_type>::promote_type promote_type;
  217. };
  218. template<class M1, class M2>
  219. struct promote_traits<sparse_matrix_element<M1>, sparse_matrix_element<M2> > {
  220. typedef typename promote_traits<typename sparse_matrix_element<M1>::value_type,
  221. typename sparse_matrix_element<M2>::value_type>::promote_type promote_type;
  222. };
  223. #endif
  224. /** \brief Index map based sparse matrix of values of type \c T
  225. *
  226. * This class represents a matrix by using a \c key to value mapping. The default type is
  227. * \code template<class T, class L = row_major, class A = map_std<std::size_t, T> > class mapped_matrix; \endcode
  228. * So, by default a STL map container is used to associate keys and values. The key is computed depending on
  229. * the layout type \c L as \code key = layout_type::element(i, size1_, j, size2_); \endcode
  230. * which means \code key = (i*size2+j) \endcode for a row major matrix.
  231. * Limitations: The matrix size must not exceed \f$(size1*size2) < \f$ \code std::limits<std::size_t> \endcode.
  232. * The \ref find1() and \ref find2() operations have a complexity of at least \f$\mathcal{O}(log(nnz))\f$, depending
  233. * on the efficiency of \c std::lower_bound on the key set of the map.
  234. * Orientation and storage can also be specified, otherwise a row major orientation is used.
  235. * It is \b not required by the storage to initialize elements of the matrix. By default, the orientation is \c row_major.
  236. *
  237. * \sa fwd.hpp, storage_sparse.hpp
  238. *
  239. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  240. * \tparam L the storage organization. It can be either \c row_major or \c column_major. By default it is \c row_major
  241. */
  242. template<class T, class L, class A>
  243. class mapped_matrix:
  244. public matrix_container<mapped_matrix<T, L, A> > {
  245. typedef T &true_reference;
  246. typedef T *pointer;
  247. typedef const T * const_pointer;
  248. typedef L layout_type;
  249. typedef mapped_matrix<T, L, A> self_type;
  250. public:
  251. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  252. using matrix_container<self_type>::operator ();
  253. #endif
  254. typedef typename A::size_type size_type;
  255. typedef typename A::difference_type difference_type;
  256. typedef T value_type;
  257. typedef A array_type;
  258. typedef const T &const_reference;
  259. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  260. typedef typename detail::map_traits<A, T>::reference reference;
  261. #else
  262. typedef sparse_matrix_element<self_type> reference;
  263. #endif
  264. typedef const matrix_reference<const self_type> const_closure_type;
  265. typedef matrix_reference<self_type> closure_type;
  266. typedef mapped_vector<T, A> vector_temporary_type;
  267. typedef self_type matrix_temporary_type;
  268. typedef sparse_tag storage_category;
  269. typedef typename L::orientation_category orientation_category;
  270. // Construction and destruction
  271. BOOST_UBLAS_INLINE
  272. mapped_matrix ():
  273. matrix_container<self_type> (),
  274. size1_ (0), size2_ (0), data_ () {}
  275. BOOST_UBLAS_INLINE
  276. mapped_matrix (size_type size1, size_type size2, size_type non_zeros = 0):
  277. matrix_container<self_type> (),
  278. size1_ (size1), size2_ (size2), data_ () {
  279. detail::map_reserve (data (), restrict_capacity (non_zeros));
  280. }
  281. BOOST_UBLAS_INLINE
  282. mapped_matrix (const mapped_matrix &m):
  283. matrix_container<self_type> (),
  284. size1_ (m.size1_), size2_ (m.size2_), data_ (m.data_) {}
  285. template<class AE>
  286. BOOST_UBLAS_INLINE
  287. mapped_matrix (const matrix_expression<AE> &ae, size_type non_zeros = 0):
  288. matrix_container<self_type> (),
  289. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), data_ () {
  290. detail::map_reserve (data (), restrict_capacity (non_zeros));
  291. matrix_assign<scalar_assign> (*this, ae);
  292. }
  293. // Accessors
  294. BOOST_UBLAS_INLINE
  295. size_type size1 () const {
  296. return size1_;
  297. }
  298. BOOST_UBLAS_INLINE
  299. size_type size2 () const {
  300. return size2_;
  301. }
  302. BOOST_UBLAS_INLINE
  303. size_type nnz_capacity () const {
  304. return detail::map_capacity (data ());
  305. }
  306. BOOST_UBLAS_INLINE
  307. size_type nnz () const {
  308. return data (). size ();
  309. }
  310. // Storage accessors
  311. BOOST_UBLAS_INLINE
  312. const array_type &data () const {
  313. return data_;
  314. }
  315. BOOST_UBLAS_INLINE
  316. array_type &data () {
  317. return data_;
  318. }
  319. // Resizing
  320. private:
  321. BOOST_UBLAS_INLINE
  322. size_type restrict_capacity (size_type non_zeros) const {
  323. // Guarding against overflow - thanks to Alexei Novakov for the hint.
  324. // non_zeros = (std::min) (non_zeros, size1_ * size2_);
  325. if (size1_ > 0 && non_zeros / size1_ >= size2_)
  326. non_zeros = size1_ * size2_;
  327. return non_zeros;
  328. }
  329. public:
  330. BOOST_UBLAS_INLINE
  331. void resize (size_type size1, size_type size2, bool preserve = true) {
  332. // FIXME preserve unimplemented
  333. BOOST_UBLAS_CHECK (!preserve, internal_logic ());
  334. size1_ = size1;
  335. size2_ = size2;
  336. data ().clear ();
  337. }
  338. // Reserving
  339. BOOST_UBLAS_INLINE
  340. void reserve (size_type non_zeros, bool /*preserve*/ = true) {
  341. detail::map_reserve (data (), restrict_capacity (non_zeros));
  342. }
  343. // Element support
  344. BOOST_UBLAS_INLINE
  345. pointer find_element (size_type i, size_type j) {
  346. return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i, j));
  347. }
  348. BOOST_UBLAS_INLINE
  349. const_pointer find_element (size_type i, size_type j) const {
  350. const size_type element = layout_type::element (i, size1_, j, size2_);
  351. const_subiterator_type it (data ().find (element));
  352. if (it == data ().end ())
  353. return 0;
  354. BOOST_UBLAS_CHECK ((*it).first == element, internal_logic ()); // broken map
  355. return &(*it).second;
  356. }
  357. // Element access
  358. BOOST_UBLAS_INLINE
  359. const_reference operator () (size_type i, size_type j) const {
  360. const size_type element = layout_type::element (i, size1_, j, size2_);
  361. const_subiterator_type it (data ().find (element));
  362. if (it == data ().end ())
  363. return zero_;
  364. BOOST_UBLAS_CHECK ((*it).first == element, internal_logic ()); // broken map
  365. return (*it).second;
  366. }
  367. BOOST_UBLAS_INLINE
  368. reference operator () (size_type i, size_type j) {
  369. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  370. const size_type element = layout_type::element (i, size1_, j, size2_);
  371. std::pair<subiterator_type, bool> ii (data ().insert (typename array_type::value_type (element, value_type/*zero*/())));
  372. BOOST_UBLAS_CHECK ((ii.first)->first == element, internal_logic ()); // broken map
  373. return (ii.first)->second;
  374. #else
  375. return reference (*this, i, j);
  376. #endif
  377. }
  378. // Element assingment
  379. BOOST_UBLAS_INLINE
  380. true_reference insert_element (size_type i, size_type j, const_reference t) {
  381. BOOST_UBLAS_CHECK (!find_element (i, j), bad_index ()); // duplicate element
  382. const size_type element = layout_type::element (i, size1_, j, size2_);
  383. std::pair<subiterator_type, bool> ii (data ().insert (typename array_type::value_type (element, t)));
  384. BOOST_UBLAS_CHECK ((ii.first)->first == element, internal_logic ()); // broken map
  385. if (!ii.second) // existing element
  386. (ii.first)->second = t;
  387. return (ii.first)->second;
  388. }
  389. BOOST_UBLAS_INLINE
  390. void erase_element (size_type i, size_type j) {
  391. subiterator_type it = data ().find (layout_type::element (i, size1_, j, size2_));
  392. if (it == data ().end ())
  393. return;
  394. data ().erase (it);
  395. }
  396. // Zeroing
  397. BOOST_UBLAS_INLINE
  398. void clear () {
  399. data ().clear ();
  400. }
  401. // Assignment
  402. BOOST_UBLAS_INLINE
  403. mapped_matrix &operator = (const mapped_matrix &m) {
  404. if (this != &m) {
  405. size1_ = m.size1_;
  406. size2_ = m.size2_;
  407. data () = m.data ();
  408. }
  409. return *this;
  410. }
  411. template<class C> // Container assignment without temporary
  412. BOOST_UBLAS_INLINE
  413. mapped_matrix &operator = (const matrix_container<C> &m) {
  414. resize (m ().size1 (), m ().size2 (), false);
  415. assign (m);
  416. return *this;
  417. }
  418. BOOST_UBLAS_INLINE
  419. mapped_matrix &assign_temporary (mapped_matrix &m) {
  420. swap (m);
  421. return *this;
  422. }
  423. template<class AE>
  424. BOOST_UBLAS_INLINE
  425. mapped_matrix &operator = (const matrix_expression<AE> &ae) {
  426. self_type temporary (ae, detail::map_capacity (data ()));
  427. return assign_temporary (temporary);
  428. }
  429. template<class AE>
  430. BOOST_UBLAS_INLINE
  431. mapped_matrix &assign (const matrix_expression<AE> &ae) {
  432. matrix_assign<scalar_assign> (*this, ae);
  433. return *this;
  434. }
  435. template<class AE>
  436. BOOST_UBLAS_INLINE
  437. mapped_matrix& operator += (const matrix_expression<AE> &ae) {
  438. self_type temporary (*this + ae, detail::map_capacity (data ()));
  439. return assign_temporary (temporary);
  440. }
  441. template<class C> // Container assignment without temporary
  442. BOOST_UBLAS_INLINE
  443. mapped_matrix &operator += (const matrix_container<C> &m) {
  444. plus_assign (m);
  445. return *this;
  446. }
  447. template<class AE>
  448. BOOST_UBLAS_INLINE
  449. mapped_matrix &plus_assign (const matrix_expression<AE> &ae) {
  450. matrix_assign<scalar_plus_assign> (*this, ae);
  451. return *this;
  452. }
  453. template<class AE>
  454. BOOST_UBLAS_INLINE
  455. mapped_matrix& operator -= (const matrix_expression<AE> &ae) {
  456. self_type temporary (*this - ae, detail::map_capacity (data ()));
  457. return assign_temporary (temporary);
  458. }
  459. template<class C> // Container assignment without temporary
  460. BOOST_UBLAS_INLINE
  461. mapped_matrix &operator -= (const matrix_container<C> &m) {
  462. minus_assign (m);
  463. return *this;
  464. }
  465. template<class AE>
  466. BOOST_UBLAS_INLINE
  467. mapped_matrix &minus_assign (const matrix_expression<AE> &ae) {
  468. matrix_assign<scalar_minus_assign> (*this, ae);
  469. return *this;
  470. }
  471. template<class AT>
  472. BOOST_UBLAS_INLINE
  473. mapped_matrix& operator *= (const AT &at) {
  474. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  475. return *this;
  476. }
  477. template<class AT>
  478. BOOST_UBLAS_INLINE
  479. mapped_matrix& operator /= (const AT &at) {
  480. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  481. return *this;
  482. }
  483. // Swapping
  484. BOOST_UBLAS_INLINE
  485. void swap (mapped_matrix &m) {
  486. if (this != &m) {
  487. std::swap (size1_, m.size1_);
  488. std::swap (size2_, m.size2_);
  489. data ().swap (m.data ());
  490. }
  491. }
  492. BOOST_UBLAS_INLINE
  493. friend void swap (mapped_matrix &m1, mapped_matrix &m2) {
  494. m1.swap (m2);
  495. }
  496. // Iterator types
  497. private:
  498. // Use storage iterator
  499. typedef typename A::const_iterator const_subiterator_type;
  500. typedef typename A::iterator subiterator_type;
  501. BOOST_UBLAS_INLINE
  502. true_reference at_element (size_type i, size_type j) {
  503. const size_type element = layout_type::element (i, size1_, j, size2_);
  504. subiterator_type it (data ().find (element));
  505. BOOST_UBLAS_CHECK (it != data ().end(), bad_index ());
  506. BOOST_UBLAS_CHECK ((*it).first == element, internal_logic ()); // broken map
  507. return it->second;
  508. }
  509. public:
  510. class const_iterator1;
  511. class iterator1;
  512. class const_iterator2;
  513. class iterator2;
  514. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  515. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  516. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  517. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  518. // Element lookup
  519. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  520. const_iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) const {
  521. const_subiterator_type it (data ().lower_bound (layout_type::address (i, size1_, j, size2_)));
  522. const_subiterator_type it_end (data ().end ());
  523. size_type index1 = size_type (-1);
  524. size_type index2 = size_type (-1);
  525. while (rank == 1 && it != it_end) {
  526. index1 = layout_type::index_i ((*it).first, size1_, size2_);
  527. index2 = layout_type::index_j ((*it).first, size1_, size2_);
  528. if (direction > 0) {
  529. if ((index1 >= i && index2 == j) || (i >= size1_))
  530. break;
  531. ++ i;
  532. } else /* if (direction < 0) */ {
  533. if ((index1 <= i && index2 == j) || (i == 0))
  534. break;
  535. -- i;
  536. }
  537. it = data ().lower_bound (layout_type::address (i, size1_, j, size2_));
  538. }
  539. if (rank == 1 && index2 != j) {
  540. if (direction > 0)
  541. i = size1_;
  542. else /* if (direction < 0) */
  543. i = 0;
  544. rank = 0;
  545. }
  546. return const_iterator1 (*this, rank, i, j, it);
  547. }
  548. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  549. iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) {
  550. subiterator_type it (data ().lower_bound (layout_type::address (i, size1_, j, size2_)));
  551. subiterator_type it_end (data ().end ());
  552. size_type index1 = size_type (-1);
  553. size_type index2 = size_type (-1);
  554. while (rank == 1 && it != it_end) {
  555. index1 = layout_type::index_i ((*it).first, size1_, size2_);
  556. index2 = layout_type::index_j ((*it).first, size1_, size2_);
  557. if (direction > 0) {
  558. if ((index1 >= i && index2 == j) || (i >= size1_))
  559. break;
  560. ++ i;
  561. } else /* if (direction < 0) */ {
  562. if ((index1 <= i && index2 == j) || (i == 0))
  563. break;
  564. -- i;
  565. }
  566. it = data ().lower_bound (layout_type::address (i, size1_, j, size2_));
  567. }
  568. if (rank == 1 && index2 != j) {
  569. if (direction > 0)
  570. i = size1_;
  571. else /* if (direction < 0) */
  572. i = 0;
  573. rank = 0;
  574. }
  575. return iterator1 (*this, rank, i, j, it);
  576. }
  577. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  578. const_iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) const {
  579. const_subiterator_type it (data ().lower_bound (layout_type::address (i, size1_, j, size2_)));
  580. const_subiterator_type it_end (data ().end ());
  581. size_type index1 = size_type (-1);
  582. size_type index2 = size_type (-1);
  583. while (rank == 1 && it != it_end) {
  584. index1 = layout_type::index_i ((*it).first, size1_, size2_);
  585. index2 = layout_type::index_j ((*it).first, size1_, size2_);
  586. if (direction > 0) {
  587. if ((index2 >= j && index1 == i) || (j >= size2_))
  588. break;
  589. ++ j;
  590. } else /* if (direction < 0) */ {
  591. if ((index2 <= j && index1 == i) || (j == 0))
  592. break;
  593. -- j;
  594. }
  595. it = data ().lower_bound (layout_type::address (i, size1_, j, size2_));
  596. }
  597. if (rank == 1 && index1 != i) {
  598. if (direction > 0)
  599. j = size2_;
  600. else /* if (direction < 0) */
  601. j = 0;
  602. rank = 0;
  603. }
  604. return const_iterator2 (*this, rank, i, j, it);
  605. }
  606. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  607. iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) {
  608. subiterator_type it (data ().lower_bound (layout_type::address (i, size1_, j, size2_)));
  609. subiterator_type it_end (data ().end ());
  610. size_type index1 = size_type (-1);
  611. size_type index2 = size_type (-1);
  612. while (rank == 1 && it != it_end) {
  613. index1 = layout_type::index_i ((*it).first, size1_, size2_);
  614. index2 = layout_type::index_j ((*it).first, size1_, size2_);
  615. if (direction > 0) {
  616. if ((index2 >= j && index1 == i) || (j >= size2_))
  617. break;
  618. ++ j;
  619. } else /* if (direction < 0) */ {
  620. if ((index2 <= j && index1 == i) || (j == 0))
  621. break;
  622. -- j;
  623. }
  624. it = data ().lower_bound (layout_type::address (i, size1_, j, size2_));
  625. }
  626. if (rank == 1 && index1 != i) {
  627. if (direction > 0)
  628. j = size2_;
  629. else /* if (direction < 0) */
  630. j = 0;
  631. rank = 0;
  632. }
  633. return iterator2 (*this, rank, i, j, it);
  634. }
  635. class const_iterator1:
  636. public container_const_reference<mapped_matrix>,
  637. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  638. const_iterator1, value_type> {
  639. public:
  640. typedef typename mapped_matrix::value_type value_type;
  641. typedef typename mapped_matrix::difference_type difference_type;
  642. typedef typename mapped_matrix::const_reference reference;
  643. typedef const typename mapped_matrix::pointer pointer;
  644. typedef const_iterator2 dual_iterator_type;
  645. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  646. // Construction and destruction
  647. BOOST_UBLAS_INLINE
  648. const_iterator1 ():
  649. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), it_ () {}
  650. BOOST_UBLAS_INLINE
  651. const_iterator1 (const self_type &m, int rank, size_type i, size_type j, const const_subiterator_type &it):
  652. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), it_ (it) {}
  653. BOOST_UBLAS_INLINE
  654. const_iterator1 (const iterator1 &it):
  655. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), it_ (it.it_) {}
  656. // Arithmetic
  657. BOOST_UBLAS_INLINE
  658. const_iterator1 &operator ++ () {
  659. if (rank_ == 1 && layout_type::fast_i ())
  660. ++ it_;
  661. else
  662. *this = (*this) ().find1 (rank_, index1 () + 1, j_, 1);
  663. return *this;
  664. }
  665. BOOST_UBLAS_INLINE
  666. const_iterator1 &operator -- () {
  667. if (rank_ == 1 && layout_type::fast_i ())
  668. -- it_;
  669. else
  670. *this = (*this) ().find1 (rank_, index1 () - 1, j_, -1);
  671. return *this;
  672. }
  673. // Dereference
  674. BOOST_UBLAS_INLINE
  675. const_reference operator * () const {
  676. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  677. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  678. if (rank_ == 1) {
  679. return (*it_).second;
  680. } else {
  681. return (*this) () (i_, j_);
  682. }
  683. }
  684. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  685. BOOST_UBLAS_INLINE
  686. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  687. typename self_type::
  688. #endif
  689. const_iterator2 begin () const {
  690. const self_type &m = (*this) ();
  691. return m.find2 (1, index1 (), 0);
  692. }
  693. BOOST_UBLAS_INLINE
  694. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  695. typename self_type::
  696. #endif
  697. const_iterator2 cbegin () const {
  698. return begin ();
  699. }
  700. BOOST_UBLAS_INLINE
  701. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  702. typename self_type::
  703. #endif
  704. const_iterator2 end () const {
  705. const self_type &m = (*this) ();
  706. return m.find2 (1, index1 (), m.size2 ());
  707. }
  708. BOOST_UBLAS_INLINE
  709. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  710. typename self_type::
  711. #endif
  712. const_iterator2 cend () const {
  713. return end ();
  714. }
  715. BOOST_UBLAS_INLINE
  716. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  717. typename self_type::
  718. #endif
  719. const_reverse_iterator2 rbegin () const {
  720. return const_reverse_iterator2 (end ());
  721. }
  722. BOOST_UBLAS_INLINE
  723. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  724. typename self_type::
  725. #endif
  726. const_reverse_iterator2 crbegin () const {
  727. return rbegin ();
  728. }
  729. BOOST_UBLAS_INLINE
  730. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  731. typename self_type::
  732. #endif
  733. const_reverse_iterator2 rend () const {
  734. return const_reverse_iterator2 (begin ());
  735. }
  736. BOOST_UBLAS_INLINE
  737. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  738. typename self_type::
  739. #endif
  740. const_reverse_iterator2 crend () const {
  741. return rend ();
  742. }
  743. #endif
  744. // Indices
  745. BOOST_UBLAS_INLINE
  746. size_type index1 () const {
  747. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  748. if (rank_ == 1) {
  749. const self_type &m = (*this) ();
  750. BOOST_UBLAS_CHECK (layout_type::index_i ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size1 (), bad_index ());
  751. return layout_type::index_i ((*it_).first, m.size1 (), m.size2 ());
  752. } else {
  753. return i_;
  754. }
  755. }
  756. BOOST_UBLAS_INLINE
  757. size_type index2 () const {
  758. if (rank_ == 1) {
  759. const self_type &m = (*this) ();
  760. BOOST_UBLAS_CHECK (layout_type::index_j ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size2 (), bad_index ());
  761. return layout_type::index_j ((*it_).first, m.size1 (), m.size2 ());
  762. } else {
  763. return j_;
  764. }
  765. }
  766. // Assignment
  767. BOOST_UBLAS_INLINE
  768. const_iterator1 &operator = (const const_iterator1 &it) {
  769. container_const_reference<self_type>::assign (&it ());
  770. rank_ = it.rank_;
  771. i_ = it.i_;
  772. j_ = it.j_;
  773. it_ = it.it_;
  774. return *this;
  775. }
  776. // Comparison
  777. BOOST_UBLAS_INLINE
  778. bool operator == (const const_iterator1 &it) const {
  779. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  780. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  781. if (rank_ == 1 || it.rank_ == 1) {
  782. return it_ == it.it_;
  783. } else {
  784. return i_ == it.i_ && j_ == it.j_;
  785. }
  786. }
  787. private:
  788. int rank_;
  789. size_type i_;
  790. size_type j_;
  791. const_subiterator_type it_;
  792. };
  793. BOOST_UBLAS_INLINE
  794. const_iterator1 begin1 () const {
  795. return find1 (0, 0, 0);
  796. }
  797. BOOST_UBLAS_INLINE
  798. const_iterator1 cbegin1 () const {
  799. return begin1 ();
  800. }
  801. BOOST_UBLAS_INLINE
  802. const_iterator1 end1 () const {
  803. return find1 (0, size1_, 0);
  804. }
  805. BOOST_UBLAS_INLINE
  806. const_iterator1 cend1 () const {
  807. return end1 ();
  808. }
  809. class iterator1:
  810. public container_reference<mapped_matrix>,
  811. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  812. iterator1, value_type> {
  813. public:
  814. typedef typename mapped_matrix::value_type value_type;
  815. typedef typename mapped_matrix::difference_type difference_type;
  816. typedef typename mapped_matrix::true_reference reference;
  817. typedef typename mapped_matrix::pointer pointer;
  818. typedef iterator2 dual_iterator_type;
  819. typedef reverse_iterator2 dual_reverse_iterator_type;
  820. // Construction and destruction
  821. BOOST_UBLAS_INLINE
  822. iterator1 ():
  823. container_reference<self_type> (), rank_ (), i_ (), j_ (), it_ () {}
  824. BOOST_UBLAS_INLINE
  825. iterator1 (self_type &m, int rank, size_type i, size_type j, const subiterator_type &it):
  826. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), it_ (it) {}
  827. // Arithmetic
  828. BOOST_UBLAS_INLINE
  829. iterator1 &operator ++ () {
  830. if (rank_ == 1 && layout_type::fast_i ())
  831. ++ it_;
  832. else
  833. *this = (*this) ().find1 (rank_, index1 () + 1, j_, 1);
  834. return *this;
  835. }
  836. BOOST_UBLAS_INLINE
  837. iterator1 &operator -- () {
  838. if (rank_ == 1 && layout_type::fast_i ())
  839. -- it_;
  840. else
  841. *this = (*this) ().find1 (rank_, index1 () - 1, j_, -1);
  842. return *this;
  843. }
  844. // Dereference
  845. BOOST_UBLAS_INLINE
  846. reference operator * () const {
  847. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  848. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  849. if (rank_ == 1) {
  850. return (*it_).second;
  851. } else {
  852. return (*this) ().at_element (i_, j_);
  853. }
  854. }
  855. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  856. BOOST_UBLAS_INLINE
  857. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  858. typename self_type::
  859. #endif
  860. iterator2 begin () const {
  861. self_type &m = (*this) ();
  862. return m.find2 (1, index1 (), 0);
  863. }
  864. BOOST_UBLAS_INLINE
  865. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  866. typename self_type::
  867. #endif
  868. iterator2 end () const {
  869. self_type &m = (*this) ();
  870. return m.find2 (1, index1 (), m.size2 ());
  871. }
  872. BOOST_UBLAS_INLINE
  873. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  874. typename self_type::
  875. #endif
  876. reverse_iterator2 rbegin () const {
  877. return reverse_iterator2 (end ());
  878. }
  879. BOOST_UBLAS_INLINE
  880. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  881. typename self_type::
  882. #endif
  883. reverse_iterator2 rend () const {
  884. return reverse_iterator2 (begin ());
  885. }
  886. #endif
  887. // Indices
  888. BOOST_UBLAS_INLINE
  889. size_type index1 () const {
  890. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  891. if (rank_ == 1) {
  892. const self_type &m = (*this) ();
  893. BOOST_UBLAS_CHECK (layout_type::index_i ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size1 (), bad_index ());
  894. return layout_type::index_i ((*it_).first, m.size1 (), m.size2 ());
  895. } else {
  896. return i_;
  897. }
  898. }
  899. BOOST_UBLAS_INLINE
  900. size_type index2 () const {
  901. if (rank_ == 1) {
  902. const self_type &m = (*this) ();
  903. BOOST_UBLAS_CHECK (layout_type::index_j ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size2 (), bad_index ());
  904. return layout_type::index_j ((*it_).first, m.size1 (), m.size2 ());
  905. } else {
  906. return j_;
  907. }
  908. }
  909. // Assignment
  910. BOOST_UBLAS_INLINE
  911. iterator1 &operator = (const iterator1 &it) {
  912. container_reference<self_type>::assign (&it ());
  913. rank_ = it.rank_;
  914. i_ = it.i_;
  915. j_ = it.j_;
  916. it_ = it.it_;
  917. return *this;
  918. }
  919. // Comparison
  920. BOOST_UBLAS_INLINE
  921. bool operator == (const iterator1 &it) const {
  922. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  923. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  924. if (rank_ == 1 || it.rank_ == 1) {
  925. return it_ == it.it_;
  926. } else {
  927. return i_ == it.i_ && j_ == it.j_;
  928. }
  929. }
  930. private:
  931. int rank_;
  932. size_type i_;
  933. size_type j_;
  934. subiterator_type it_;
  935. friend class const_iterator1;
  936. };
  937. BOOST_UBLAS_INLINE
  938. iterator1 begin1 () {
  939. return find1 (0, 0, 0);
  940. }
  941. BOOST_UBLAS_INLINE
  942. iterator1 end1 () {
  943. return find1 (0, size1_, 0);
  944. }
  945. class const_iterator2:
  946. public container_const_reference<mapped_matrix>,
  947. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  948. const_iterator2, value_type> {
  949. public:
  950. typedef typename mapped_matrix::value_type value_type;
  951. typedef typename mapped_matrix::difference_type difference_type;
  952. typedef typename mapped_matrix::const_reference reference;
  953. typedef const typename mapped_matrix::pointer pointer;
  954. typedef const_iterator1 dual_iterator_type;
  955. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  956. // Construction and destruction
  957. BOOST_UBLAS_INLINE
  958. const_iterator2 ():
  959. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), it_ () {}
  960. BOOST_UBLAS_INLINE
  961. const_iterator2 (const self_type &m, int rank, size_type i, size_type j, const const_subiterator_type &it):
  962. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), it_ (it) {}
  963. BOOST_UBLAS_INLINE
  964. const_iterator2 (const iterator2 &it):
  965. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), it_ (it.it_) {}
  966. // Arithmetic
  967. BOOST_UBLAS_INLINE
  968. const_iterator2 &operator ++ () {
  969. if (rank_ == 1 && layout_type::fast_j ())
  970. ++ it_;
  971. else
  972. *this = (*this) ().find2 (rank_, i_, index2 () + 1, 1);
  973. return *this;
  974. }
  975. BOOST_UBLAS_INLINE
  976. const_iterator2 &operator -- () {
  977. if (rank_ == 1 && layout_type::fast_j ())
  978. -- it_;
  979. else
  980. *this = (*this) ().find2 (rank_, i_, index2 () - 1, -1);
  981. return *this;
  982. }
  983. // Dereference
  984. BOOST_UBLAS_INLINE
  985. const_reference operator * () const {
  986. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  987. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  988. if (rank_ == 1) {
  989. return (*it_).second;
  990. } else {
  991. return (*this) () (i_, j_);
  992. }
  993. }
  994. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  995. BOOST_UBLAS_INLINE
  996. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  997. typename self_type::
  998. #endif
  999. const_iterator1 begin () const {
  1000. const self_type &m = (*this) ();
  1001. return m.find1 (1, 0, index2 ());
  1002. }
  1003. BOOST_UBLAS_INLINE
  1004. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1005. typename self_type::
  1006. #endif
  1007. const_iterator1 cbegin () const {
  1008. return begin ();
  1009. }
  1010. BOOST_UBLAS_INLINE
  1011. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1012. typename self_type::
  1013. #endif
  1014. const_iterator1 end () const {
  1015. const self_type &m = (*this) ();
  1016. return m.find1 (1, m.size1 (), index2 ());
  1017. }
  1018. BOOST_UBLAS_INLINE
  1019. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1020. typename self_type::
  1021. #endif
  1022. const_iterator1 cend () const {
  1023. return end ();
  1024. }
  1025. BOOST_UBLAS_INLINE
  1026. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1027. typename self_type::
  1028. #endif
  1029. const_reverse_iterator1 rbegin () const {
  1030. return const_reverse_iterator1 (end ());
  1031. }
  1032. BOOST_UBLAS_INLINE
  1033. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1034. typename self_type::
  1035. #endif
  1036. const_reverse_iterator1 crbegin () const {
  1037. return rbegin ();
  1038. }
  1039. BOOST_UBLAS_INLINE
  1040. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1041. typename self_type::
  1042. #endif
  1043. const_reverse_iterator1 rend () const {
  1044. return const_reverse_iterator1 (begin ());
  1045. }
  1046. BOOST_UBLAS_INLINE
  1047. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1048. typename self_type::
  1049. #endif
  1050. const_reverse_iterator1 crend () const {
  1051. return rend ();
  1052. }
  1053. #endif
  1054. // Indices
  1055. BOOST_UBLAS_INLINE
  1056. size_type index1 () const {
  1057. if (rank_ == 1) {
  1058. const self_type &m = (*this) ();
  1059. BOOST_UBLAS_CHECK (layout_type::index_i ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size1 (), bad_index ());
  1060. return layout_type::index_i ((*it_).first, m.size1 (), m.size2 ());
  1061. } else {
  1062. return i_;
  1063. }
  1064. }
  1065. BOOST_UBLAS_INLINE
  1066. size_type index2 () const {
  1067. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  1068. if (rank_ == 1) {
  1069. const self_type &m = (*this) ();
  1070. BOOST_UBLAS_CHECK (layout_type::index_j ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size2 (), bad_index ());
  1071. return layout_type::index_j ((*it_).first, m.size1 (), m.size2 ());
  1072. } else {
  1073. return j_;
  1074. }
  1075. }
  1076. // Assignment
  1077. BOOST_UBLAS_INLINE
  1078. const_iterator2 &operator = (const const_iterator2 &it) {
  1079. container_const_reference<self_type>::assign (&it ());
  1080. rank_ = it.rank_;
  1081. i_ = it.i_;
  1082. j_ = it.j_;
  1083. it_ = it.it_;
  1084. return *this;
  1085. }
  1086. // Comparison
  1087. BOOST_UBLAS_INLINE
  1088. bool operator == (const const_iterator2 &it) const {
  1089. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1090. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  1091. if (rank_ == 1 || it.rank_ == 1) {
  1092. return it_ == it.it_;
  1093. } else {
  1094. return i_ == it.i_ && j_ == it.j_;
  1095. }
  1096. }
  1097. private:
  1098. int rank_;
  1099. size_type i_;
  1100. size_type j_;
  1101. const_subiterator_type it_;
  1102. };
  1103. BOOST_UBLAS_INLINE
  1104. const_iterator2 begin2 () const {
  1105. return find2 (0, 0, 0);
  1106. }
  1107. BOOST_UBLAS_INLINE
  1108. const_iterator2 cbegin2 () const {
  1109. return begin2 ();
  1110. }
  1111. BOOST_UBLAS_INLINE
  1112. const_iterator2 end2 () const {
  1113. return find2 (0, 0, size2_);
  1114. }
  1115. BOOST_UBLAS_INLINE
  1116. const_iterator2 cend2 () const {
  1117. return end2 ();
  1118. }
  1119. class iterator2:
  1120. public container_reference<mapped_matrix>,
  1121. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  1122. iterator2, value_type> {
  1123. public:
  1124. typedef typename mapped_matrix::value_type value_type;
  1125. typedef typename mapped_matrix::difference_type difference_type;
  1126. typedef typename mapped_matrix::true_reference reference;
  1127. typedef typename mapped_matrix::pointer pointer;
  1128. typedef iterator1 dual_iterator_type;
  1129. typedef reverse_iterator1 dual_reverse_iterator_type;
  1130. // Construction and destruction
  1131. BOOST_UBLAS_INLINE
  1132. iterator2 ():
  1133. container_reference<self_type> (), rank_ (), i_ (), j_ (), it_ () {}
  1134. BOOST_UBLAS_INLINE
  1135. iterator2 (self_type &m, int rank, size_type i, size_type j, const subiterator_type &it):
  1136. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), it_ (it) {}
  1137. // Arithmetic
  1138. BOOST_UBLAS_INLINE
  1139. iterator2 &operator ++ () {
  1140. if (rank_ == 1 && layout_type::fast_j ())
  1141. ++ it_;
  1142. else
  1143. *this = (*this) ().find2 (rank_, i_, index2 () + 1, 1);
  1144. return *this;
  1145. }
  1146. BOOST_UBLAS_INLINE
  1147. iterator2 &operator -- () {
  1148. if (rank_ == 1 && layout_type::fast_j ())
  1149. -- it_;
  1150. else
  1151. *this = (*this) ().find2 (rank_, i_, index2 () - 1, -1);
  1152. return *this;
  1153. }
  1154. // Dereference
  1155. BOOST_UBLAS_INLINE
  1156. reference operator * () const {
  1157. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1158. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1159. if (rank_ == 1) {
  1160. return (*it_).second;
  1161. } else {
  1162. return (*this) ().at_element (i_, j_);
  1163. }
  1164. }
  1165. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1166. BOOST_UBLAS_INLINE
  1167. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1168. typename self_type::
  1169. #endif
  1170. iterator1 begin () const {
  1171. self_type &m = (*this) ();
  1172. return m.find1 (1, 0, index2 ());
  1173. }
  1174. BOOST_UBLAS_INLINE
  1175. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1176. typename self_type::
  1177. #endif
  1178. iterator1 end () const {
  1179. self_type &m = (*this) ();
  1180. return m.find1 (1, m.size1 (), index2 ());
  1181. }
  1182. BOOST_UBLAS_INLINE
  1183. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1184. typename self_type::
  1185. #endif
  1186. reverse_iterator1 rbegin () const {
  1187. return reverse_iterator1 (end ());
  1188. }
  1189. BOOST_UBLAS_INLINE
  1190. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1191. typename self_type::
  1192. #endif
  1193. reverse_iterator1 rend () const {
  1194. return reverse_iterator1 (begin ());
  1195. }
  1196. #endif
  1197. // Indices
  1198. BOOST_UBLAS_INLINE
  1199. size_type index1 () const {
  1200. if (rank_ == 1) {
  1201. const self_type &m = (*this) ();
  1202. BOOST_UBLAS_CHECK (layout_type::index_i ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size1 (), bad_index ());
  1203. return layout_type::index_i ((*it_).first, m.size1 (), m.size2 ());
  1204. } else {
  1205. return i_;
  1206. }
  1207. }
  1208. BOOST_UBLAS_INLINE
  1209. size_type index2 () const {
  1210. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  1211. if (rank_ == 1) {
  1212. const self_type &m = (*this) ();
  1213. BOOST_UBLAS_CHECK (layout_type::index_j ((*it_).first, m.size1 (), m.size2 ()) < (*this) ().size2 (), bad_index ());
  1214. return layout_type::index_j ((*it_).first, m.size1 (), m.size2 ());
  1215. } else {
  1216. return j_;
  1217. }
  1218. }
  1219. // Assignment
  1220. BOOST_UBLAS_INLINE
  1221. iterator2 &operator = (const iterator2 &it) {
  1222. container_reference<self_type>::assign (&it ());
  1223. rank_ = it.rank_;
  1224. i_ = it.i_;
  1225. j_ = it.j_;
  1226. it_ = it.it_;
  1227. return *this;
  1228. }
  1229. // Comparison
  1230. BOOST_UBLAS_INLINE
  1231. bool operator == (const iterator2 &it) const {
  1232. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1233. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  1234. if (rank_ == 1 || it.rank_ == 1) {
  1235. return it_ == it.it_;
  1236. } else {
  1237. return i_ == it.i_ && j_ == it.j_;
  1238. }
  1239. }
  1240. private:
  1241. int rank_;
  1242. size_type i_;
  1243. size_type j_;
  1244. subiterator_type it_;
  1245. friend class const_iterator2;
  1246. };
  1247. BOOST_UBLAS_INLINE
  1248. iterator2 begin2 () {
  1249. return find2 (0, 0, 0);
  1250. }
  1251. BOOST_UBLAS_INLINE
  1252. iterator2 end2 () {
  1253. return find2 (0, 0, size2_);
  1254. }
  1255. // Reverse iterators
  1256. BOOST_UBLAS_INLINE
  1257. const_reverse_iterator1 rbegin1 () const {
  1258. return const_reverse_iterator1 (end1 ());
  1259. }
  1260. BOOST_UBLAS_INLINE
  1261. const_reverse_iterator1 crbegin1 () const {
  1262. return rbegin1 ();
  1263. }
  1264. BOOST_UBLAS_INLINE
  1265. const_reverse_iterator1 rend1 () const {
  1266. return const_reverse_iterator1 (begin1 ());
  1267. }
  1268. BOOST_UBLAS_INLINE
  1269. const_reverse_iterator1 crend1 () const {
  1270. return rend1 ();
  1271. }
  1272. BOOST_UBLAS_INLINE
  1273. reverse_iterator1 rbegin1 () {
  1274. return reverse_iterator1 (end1 ());
  1275. }
  1276. BOOST_UBLAS_INLINE
  1277. reverse_iterator1 rend1 () {
  1278. return reverse_iterator1 (begin1 ());
  1279. }
  1280. BOOST_UBLAS_INLINE
  1281. const_reverse_iterator2 rbegin2 () const {
  1282. return const_reverse_iterator2 (end2 ());
  1283. }
  1284. BOOST_UBLAS_INLINE
  1285. const_reverse_iterator2 crbegin2 () const {
  1286. return rbegin2 ();
  1287. }
  1288. BOOST_UBLAS_INLINE
  1289. const_reverse_iterator2 rend2 () const {
  1290. return const_reverse_iterator2 (begin2 ());
  1291. }
  1292. BOOST_UBLAS_INLINE
  1293. const_reverse_iterator2 crend2 () const {
  1294. return rend2 ();
  1295. }
  1296. BOOST_UBLAS_INLINE
  1297. reverse_iterator2 rbegin2 () {
  1298. return reverse_iterator2 (end2 ());
  1299. }
  1300. BOOST_UBLAS_INLINE
  1301. reverse_iterator2 rend2 () {
  1302. return reverse_iterator2 (begin2 ());
  1303. }
  1304. // Serialization
  1305. template<class Archive>
  1306. void serialize(Archive & ar, const unsigned int /* file_version */){
  1307. serialization::collection_size_type s1 (size1_);
  1308. serialization::collection_size_type s2 (size2_);
  1309. ar & serialization::make_nvp("size1",s1);
  1310. ar & serialization::make_nvp("size2",s2);
  1311. if (Archive::is_loading::value) {
  1312. size1_ = s1;
  1313. size2_ = s2;
  1314. }
  1315. ar & serialization::make_nvp("data", data_);
  1316. }
  1317. private:
  1318. size_type size1_;
  1319. size_type size2_;
  1320. array_type data_;
  1321. static const value_type zero_;
  1322. };
  1323. template<class T, class L, class A>
  1324. const typename mapped_matrix<T, L, A>::value_type mapped_matrix<T, L, A>::zero_ = value_type/*zero*/();
  1325. // Vector index map based sparse matrix class
  1326. template<class T, class L, class A>
  1327. class mapped_vector_of_mapped_vector:
  1328. public matrix_container<mapped_vector_of_mapped_vector<T, L, A> > {
  1329. typedef T &true_reference;
  1330. typedef T *pointer;
  1331. typedef const T *const_pointer;
  1332. typedef A array_type;
  1333. typedef const A const_array_type;
  1334. typedef L layout_type;
  1335. typedef mapped_vector_of_mapped_vector<T, L, A> self_type;
  1336. public:
  1337. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  1338. using matrix_container<self_type>::operator ();
  1339. #endif
  1340. typedef typename A::size_type size_type;
  1341. typedef typename A::difference_type difference_type;
  1342. typedef T value_type;
  1343. typedef const T &const_reference;
  1344. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  1345. typedef typename detail::map_traits<typename A::data_value_type, T>::reference reference;
  1346. #else
  1347. typedef sparse_matrix_element<self_type> reference;
  1348. #endif
  1349. typedef const matrix_reference<const self_type> const_closure_type;
  1350. typedef matrix_reference<self_type> closure_type;
  1351. typedef mapped_vector<T> vector_temporary_type;
  1352. typedef self_type matrix_temporary_type;
  1353. typedef typename A::value_type::second_type vector_data_value_type;
  1354. typedef sparse_tag storage_category;
  1355. typedef typename L::orientation_category orientation_category;
  1356. // Construction and destruction
  1357. BOOST_UBLAS_INLINE
  1358. mapped_vector_of_mapped_vector ():
  1359. matrix_container<self_type> (),
  1360. size1_ (0), size2_ (0), data_ () {
  1361. data_ [layout_type::size_M (size1_, size2_)] = vector_data_value_type ();
  1362. }
  1363. BOOST_UBLAS_INLINE
  1364. mapped_vector_of_mapped_vector (size_type size1, size_type size2, size_type /*non_zeros*/ = 0):
  1365. matrix_container<self_type> (),
  1366. size1_ (size1), size2_ (size2), data_ () {
  1367. data_ [layout_type::size_M (size1_, size2_)] = vector_data_value_type ();
  1368. }
  1369. BOOST_UBLAS_INLINE
  1370. mapped_vector_of_mapped_vector (const mapped_vector_of_mapped_vector &m):
  1371. matrix_container<self_type> (),
  1372. size1_ (m.size1_), size2_ (m.size2_), data_ (m.data_) {}
  1373. template<class AE>
  1374. BOOST_UBLAS_INLINE
  1375. mapped_vector_of_mapped_vector (const matrix_expression<AE> &ae, size_type /*non_zeros*/ = 0):
  1376. matrix_container<self_type> (),
  1377. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), data_ () {
  1378. data_ [layout_type::size_M (size1_, size2_)] = vector_data_value_type ();
  1379. matrix_assign<scalar_assign> (*this, ae);
  1380. }
  1381. // Accessors
  1382. BOOST_UBLAS_INLINE
  1383. size_type size1 () const {
  1384. return size1_;
  1385. }
  1386. BOOST_UBLAS_INLINE
  1387. size_type size2 () const {
  1388. return size2_;
  1389. }
  1390. BOOST_UBLAS_INLINE
  1391. size_type nnz_capacity () const {
  1392. size_type non_zeros = 0;
  1393. for (vector_const_subiterator_type itv = data_ ().begin (); itv != data_ ().end (); ++ itv)
  1394. non_zeros += detail::map_capacity (*itv);
  1395. return non_zeros;
  1396. }
  1397. BOOST_UBLAS_INLINE
  1398. size_type nnz () const {
  1399. size_type filled = 0;
  1400. for (vector_const_subiterator_type itv = data_ ().begin (); itv != data_ ().end (); ++ itv)
  1401. filled += (*itv).size ();
  1402. return filled;
  1403. }
  1404. // Storage accessors
  1405. BOOST_UBLAS_INLINE
  1406. const_array_type &data () const {
  1407. return data_;
  1408. }
  1409. BOOST_UBLAS_INLINE
  1410. array_type &data () {
  1411. return data_;
  1412. }
  1413. // Resizing
  1414. BOOST_UBLAS_INLINE
  1415. void resize (size_type size1, size_type size2, bool preserve = true) {
  1416. // FIXME preserve unimplemented
  1417. BOOST_UBLAS_CHECK (!preserve, internal_logic ());
  1418. size1_ = size1;
  1419. size2_ = size2;
  1420. data ().clear ();
  1421. data () [layout_type::size_M (size1_, size2_)] = vector_data_value_type ();
  1422. }
  1423. // Element support
  1424. BOOST_UBLAS_INLINE
  1425. pointer find_element (size_type i, size_type j) {
  1426. return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i, j));
  1427. }
  1428. BOOST_UBLAS_INLINE
  1429. const_pointer find_element (size_type i, size_type j) const {
  1430. const size_type element1 = layout_type::index_M (i, j);
  1431. const size_type element2 = layout_type::index_m (i, j);
  1432. vector_const_subiterator_type itv (data ().find (element1));
  1433. if (itv == data ().end ())
  1434. return 0;
  1435. BOOST_UBLAS_CHECK ((*itv).first == element1, internal_logic ()); // broken map
  1436. const_subiterator_type it ((*itv).second.find (element2));
  1437. if (it == (*itv).second.end ())
  1438. return 0;
  1439. BOOST_UBLAS_CHECK ((*it).first == element2, internal_logic ()); // broken map
  1440. return &(*it).second;
  1441. }
  1442. // Element access
  1443. BOOST_UBLAS_INLINE
  1444. const_reference operator () (size_type i, size_type j) const {
  1445. const size_type element1 = layout_type::index_M (i, j);
  1446. const size_type element2 = layout_type::index_m (i, j);
  1447. vector_const_subiterator_type itv (data ().find (element1));
  1448. if (itv == data ().end ())
  1449. return zero_;
  1450. BOOST_UBLAS_CHECK ((*itv).first == element1, internal_logic ()); // broken map
  1451. const_subiterator_type it ((*itv).second.find (element2));
  1452. if (it == (*itv).second.end ())
  1453. return zero_;
  1454. BOOST_UBLAS_CHECK ((*itv).first == element1, internal_logic ()); // broken map
  1455. return (*it).second;
  1456. }
  1457. BOOST_UBLAS_INLINE
  1458. reference operator () (size_type i, size_type j) {
  1459. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  1460. const size_type element1 = layout_type::index_M (i, j);
  1461. const size_type element2 = layout_type::index_m (i, j);
  1462. vector_data_value_type& vd (data () [element1]);
  1463. std::pair<subiterator_type, bool> ii (vd.insert (typename array_type::value_type::second_type::value_type (element2, value_type/*zero*/())));
  1464. BOOST_UBLAS_CHECK ((ii.first)->first == element2, internal_logic ()); // broken map
  1465. return (ii.first)->second;
  1466. #else
  1467. return reference (*this, i, j);
  1468. #endif
  1469. }
  1470. // Element assignment
  1471. BOOST_UBLAS_INLINE
  1472. true_reference insert_element (size_type i, size_type j, const_reference t) {
  1473. BOOST_UBLAS_CHECK (!find_element (i, j), bad_index ()); // duplicate element
  1474. const size_type element1 = layout_type::index_M (i, j);
  1475. const size_type element2 = layout_type::index_m (i, j);
  1476. vector_data_value_type& vd (data () [element1]);
  1477. std::pair<subiterator_type, bool> ii (vd.insert (typename vector_data_value_type::value_type (element2, t)));
  1478. BOOST_UBLAS_CHECK ((ii.first)->first == element2, internal_logic ()); // broken map
  1479. if (!ii.second) // existing element
  1480. (ii.first)->second = t;
  1481. return (ii.first)->second;
  1482. }
  1483. BOOST_UBLAS_INLINE
  1484. void erase_element (size_type i, size_type j) {
  1485. vector_subiterator_type itv (data ().find (layout_type::index_M (i, j)));
  1486. if (itv == data ().end ())
  1487. return;
  1488. subiterator_type it ((*itv).second.find (layout_type::index_m (i, j)));
  1489. if (it == (*itv).second.end ())
  1490. return;
  1491. (*itv).second.erase (it);
  1492. }
  1493. // Zeroing
  1494. BOOST_UBLAS_INLINE
  1495. void clear () {
  1496. data ().clear ();
  1497. data_ [layout_type::size_M (size1_, size2_)] = vector_data_value_type ();
  1498. }
  1499. // Assignment
  1500. BOOST_UBLAS_INLINE
  1501. mapped_vector_of_mapped_vector &operator = (const mapped_vector_of_mapped_vector &m) {
  1502. if (this != &m) {
  1503. size1_ = m.size1_;
  1504. size2_ = m.size2_;
  1505. data () = m.data ();
  1506. }
  1507. return *this;
  1508. }
  1509. template<class C> // Container assignment without temporary
  1510. BOOST_UBLAS_INLINE
  1511. mapped_vector_of_mapped_vector &operator = (const matrix_container<C> &m) {
  1512. resize (m ().size1 (), m ().size2 (), false);
  1513. assign (m);
  1514. return *this;
  1515. }
  1516. BOOST_UBLAS_INLINE
  1517. mapped_vector_of_mapped_vector &assign_temporary (mapped_vector_of_mapped_vector &m) {
  1518. swap (m);
  1519. return *this;
  1520. }
  1521. template<class AE>
  1522. BOOST_UBLAS_INLINE
  1523. mapped_vector_of_mapped_vector &operator = (const matrix_expression<AE> &ae) {
  1524. self_type temporary (ae);
  1525. return assign_temporary (temporary);
  1526. }
  1527. template<class AE>
  1528. BOOST_UBLAS_INLINE
  1529. mapped_vector_of_mapped_vector &assign (const matrix_expression<AE> &ae) {
  1530. matrix_assign<scalar_assign> (*this, ae);
  1531. return *this;
  1532. }
  1533. template<class AE>
  1534. BOOST_UBLAS_INLINE
  1535. mapped_vector_of_mapped_vector& operator += (const matrix_expression<AE> &ae) {
  1536. self_type temporary (*this + ae);
  1537. return assign_temporary (temporary);
  1538. }
  1539. template<class C> // Container assignment without temporary
  1540. BOOST_UBLAS_INLINE
  1541. mapped_vector_of_mapped_vector &operator += (const matrix_container<C> &m) {
  1542. plus_assign (m);
  1543. return *this;
  1544. }
  1545. template<class AE>
  1546. BOOST_UBLAS_INLINE
  1547. mapped_vector_of_mapped_vector &plus_assign (const matrix_expression<AE> &ae) {
  1548. matrix_assign<scalar_plus_assign> (*this, ae);
  1549. return *this;
  1550. }
  1551. template<class AE>
  1552. BOOST_UBLAS_INLINE
  1553. mapped_vector_of_mapped_vector& operator -= (const matrix_expression<AE> &ae) {
  1554. self_type temporary (*this - ae);
  1555. return assign_temporary (temporary);
  1556. }
  1557. template<class C> // Container assignment without temporary
  1558. BOOST_UBLAS_INLINE
  1559. mapped_vector_of_mapped_vector &operator -= (const matrix_container<C> &m) {
  1560. minus_assign (m);
  1561. return *this;
  1562. }
  1563. template<class AE>
  1564. BOOST_UBLAS_INLINE
  1565. mapped_vector_of_mapped_vector &minus_assign (const matrix_expression<AE> &ae) {
  1566. matrix_assign<scalar_minus_assign> (*this, ae);
  1567. return *this;
  1568. }
  1569. template<class AT>
  1570. BOOST_UBLAS_INLINE
  1571. mapped_vector_of_mapped_vector& operator *= (const AT &at) {
  1572. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  1573. return *this;
  1574. }
  1575. template<class AT>
  1576. BOOST_UBLAS_INLINE
  1577. mapped_vector_of_mapped_vector& operator /= (const AT &at) {
  1578. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  1579. return *this;
  1580. }
  1581. // Swapping
  1582. BOOST_UBLAS_INLINE
  1583. void swap (mapped_vector_of_mapped_vector &m) {
  1584. if (this != &m) {
  1585. std::swap (size1_, m.size1_);
  1586. std::swap (size2_, m.size2_);
  1587. data ().swap (m.data ());
  1588. }
  1589. }
  1590. BOOST_UBLAS_INLINE
  1591. friend void swap (mapped_vector_of_mapped_vector &m1, mapped_vector_of_mapped_vector &m2) {
  1592. m1.swap (m2);
  1593. }
  1594. // Iterator types
  1595. private:
  1596. // Use storage iterators
  1597. typedef typename A::const_iterator vector_const_subiterator_type;
  1598. typedef typename A::iterator vector_subiterator_type;
  1599. typedef typename A::value_type::second_type::const_iterator const_subiterator_type;
  1600. typedef typename A::value_type::second_type::iterator subiterator_type;
  1601. BOOST_UBLAS_INLINE
  1602. true_reference at_element (size_type i, size_type j) {
  1603. const size_type element1 = layout_type::index_M (i, j);
  1604. const size_type element2 = layout_type::index_m (i, j);
  1605. vector_subiterator_type itv (data ().find (element1));
  1606. BOOST_UBLAS_CHECK (itv != data ().end(), bad_index ());
  1607. BOOST_UBLAS_CHECK ((*itv).first == element1, internal_logic ()); // broken map
  1608. subiterator_type it ((*itv).second.find (element2));
  1609. BOOST_UBLAS_CHECK (it != (*itv).second.end (), bad_index ());
  1610. BOOST_UBLAS_CHECK ((*it).first == element2, internal_logic ()); // broken map
  1611. return it->second;
  1612. }
  1613. public:
  1614. class const_iterator1;
  1615. class iterator1;
  1616. class const_iterator2;
  1617. class iterator2;
  1618. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  1619. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  1620. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  1621. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  1622. // Element lookup
  1623. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1624. const_iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) const {
  1625. BOOST_UBLAS_CHECK (data ().begin () != data ().end (), internal_logic ());
  1626. for (;;) {
  1627. vector_const_subiterator_type itv (data ().lower_bound (layout_type::index_M (i, j)));
  1628. vector_const_subiterator_type itv_end (data ().end ());
  1629. if (itv == itv_end)
  1630. return const_iterator1 (*this, rank, i, j, itv_end, (*(-- itv)).second.end ());
  1631. const_subiterator_type it ((*itv).second.lower_bound (layout_type::index_m (i, j)));
  1632. const_subiterator_type it_end ((*itv).second.end ());
  1633. if (rank == 0) {
  1634. // advance to the first available major index
  1635. size_type M = itv->first;
  1636. size_type m;
  1637. if (it != it_end) {
  1638. m = it->first;
  1639. } else {
  1640. m = layout_type::size_m(size1_, size2_);
  1641. }
  1642. size_type first_i = layout_type::index_M(M,m);
  1643. return const_iterator1 (*this, rank, first_i, j, itv, it);
  1644. }
  1645. if (it != it_end && (*it).first == layout_type::index_m (i, j))
  1646. return const_iterator1 (*this, rank, i, j, itv, it);
  1647. if (direction > 0) {
  1648. if (layout_type::fast_i ()) {
  1649. if (it == it_end)
  1650. return const_iterator1 (*this, rank, i, j, itv, it);
  1651. i = (*it).first;
  1652. } else {
  1653. if (i >= size1_)
  1654. return const_iterator1 (*this, rank, i, j, itv, it);
  1655. ++ i;
  1656. }
  1657. } else /* if (direction < 0) */ {
  1658. if (layout_type::fast_i ()) {
  1659. if (it == (*itv).second.begin ())
  1660. return const_iterator1 (*this, rank, i, j, itv, it);
  1661. -- it;
  1662. i = (*it).first;
  1663. } else {
  1664. if (i == 0)
  1665. return const_iterator1 (*this, rank, i, j, itv, it);
  1666. -- i;
  1667. }
  1668. }
  1669. }
  1670. }
  1671. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1672. iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) {
  1673. BOOST_UBLAS_CHECK (data ().begin () != data ().end (), internal_logic ());
  1674. for (;;) {
  1675. vector_subiterator_type itv (data ().lower_bound (layout_type::index_M (i, j)));
  1676. vector_subiterator_type itv_end (data ().end ());
  1677. if (itv == itv_end)
  1678. return iterator1 (*this, rank, i, j, itv_end, (*(-- itv)).second.end ());
  1679. subiterator_type it ((*itv).second.lower_bound (layout_type::index_m (i, j)));
  1680. subiterator_type it_end ((*itv).second.end ());
  1681. if (rank == 0) {
  1682. // advance to the first available major index
  1683. size_type M = itv->first;
  1684. size_type m;
  1685. if (it != it_end) {
  1686. m = it->first;
  1687. } else {
  1688. m = layout_type::size_m(size1_, size2_);
  1689. }
  1690. size_type first_i = layout_type::index_M(M,m);
  1691. return iterator1 (*this, rank, first_i, j, itv, it);
  1692. }
  1693. if (it != it_end && (*it).first == layout_type::index_m (i, j))
  1694. return iterator1 (*this, rank, i, j, itv, it);
  1695. if (direction > 0) {
  1696. if (layout_type::fast_i ()) {
  1697. if (it == it_end)
  1698. return iterator1 (*this, rank, i, j, itv, it);
  1699. i = (*it).first;
  1700. } else {
  1701. if (i >= size1_)
  1702. return iterator1 (*this, rank, i, j, itv, it);
  1703. ++ i;
  1704. }
  1705. } else /* if (direction < 0) */ {
  1706. if (layout_type::fast_i ()) {
  1707. if (it == (*itv).second.begin ())
  1708. return iterator1 (*this, rank, i, j, itv, it);
  1709. -- it;
  1710. i = (*it).first;
  1711. } else {
  1712. if (i == 0)
  1713. return iterator1 (*this, rank, i, j, itv, it);
  1714. -- i;
  1715. }
  1716. }
  1717. }
  1718. }
  1719. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1720. const_iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) const {
  1721. BOOST_UBLAS_CHECK (data ().begin () != data ().end (), internal_logic ());
  1722. for (;;) {
  1723. vector_const_subiterator_type itv (data ().lower_bound (layout_type::index_M (i, j)));
  1724. vector_const_subiterator_type itv_end (data ().end ());
  1725. if (itv == itv_end)
  1726. return const_iterator2 (*this, rank, i, j, itv_end, (*(-- itv)).second.end ());
  1727. const_subiterator_type it ((*itv).second.lower_bound (layout_type::index_m (i, j)));
  1728. const_subiterator_type it_end ((*itv).second.end ());
  1729. if (rank == 0) {
  1730. // advance to the first available major index
  1731. size_type M = itv->first;
  1732. size_type m;
  1733. if (it != it_end) {
  1734. m = it->first;
  1735. } else {
  1736. m = layout_type::size_m(size1_, size2_);
  1737. }
  1738. size_type first_j = layout_type::index_m(M,m);
  1739. return const_iterator2 (*this, rank, i, first_j, itv, it);
  1740. }
  1741. if (it != it_end && (*it).first == layout_type::index_m (i, j))
  1742. return const_iterator2 (*this, rank, i, j, itv, it);
  1743. if (direction > 0) {
  1744. if (layout_type::fast_j ()) {
  1745. if (it == it_end)
  1746. return const_iterator2 (*this, rank, i, j, itv, it);
  1747. j = (*it).first;
  1748. } else {
  1749. if (j >= size2_)
  1750. return const_iterator2 (*this, rank, i, j, itv, it);
  1751. ++ j;
  1752. }
  1753. } else /* if (direction < 0) */ {
  1754. if (layout_type::fast_j ()) {
  1755. if (it == (*itv).second.begin ())
  1756. return const_iterator2 (*this, rank, i, j, itv, it);
  1757. -- it;
  1758. j = (*it).first;
  1759. } else {
  1760. if (j == 0)
  1761. return const_iterator2 (*this, rank, i, j, itv, it);
  1762. -- j;
  1763. }
  1764. }
  1765. }
  1766. }
  1767. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  1768. iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) {
  1769. BOOST_UBLAS_CHECK (data ().begin () != data ().end (), internal_logic ());
  1770. for (;;) {
  1771. vector_subiterator_type itv (data ().lower_bound (layout_type::index_M (i, j)));
  1772. vector_subiterator_type itv_end (data ().end ());
  1773. if (itv == itv_end)
  1774. return iterator2 (*this, rank, i, j, itv_end, (*(-- itv)).second.end ());
  1775. subiterator_type it ((*itv).second.lower_bound (layout_type::index_m (i, j)));
  1776. subiterator_type it_end ((*itv).second.end ());
  1777. if (rank == 0) {
  1778. // advance to the first available major index
  1779. size_type M = itv->first;
  1780. size_type m;
  1781. if (it != it_end) {
  1782. m = it->first;
  1783. } else {
  1784. m = layout_type::size_m(size1_, size2_);
  1785. }
  1786. size_type first_j = layout_type::index_m(M,m);
  1787. return iterator2 (*this, rank, i, first_j, itv, it);
  1788. }
  1789. if (it != it_end && (*it).first == layout_type::index_m (i, j))
  1790. return iterator2 (*this, rank, i, j, itv, it);
  1791. if (direction > 0) {
  1792. if (layout_type::fast_j ()) {
  1793. if (it == it_end)
  1794. return iterator2 (*this, rank, i, j, itv, it);
  1795. j = (*it).first;
  1796. } else {
  1797. if (j >= size2_)
  1798. return iterator2 (*this, rank, i, j, itv, it);
  1799. ++ j;
  1800. }
  1801. } else /* if (direction < 0) */ {
  1802. if (layout_type::fast_j ()) {
  1803. if (it == (*itv).second.begin ())
  1804. return iterator2 (*this, rank, i, j, itv, it);
  1805. -- it;
  1806. j = (*it).first;
  1807. } else {
  1808. if (j == 0)
  1809. return iterator2 (*this, rank, i, j, itv, it);
  1810. -- j;
  1811. }
  1812. }
  1813. }
  1814. }
  1815. class const_iterator1:
  1816. public container_const_reference<mapped_vector_of_mapped_vector>,
  1817. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  1818. const_iterator1, value_type> {
  1819. public:
  1820. typedef typename mapped_vector_of_mapped_vector::value_type value_type;
  1821. typedef typename mapped_vector_of_mapped_vector::difference_type difference_type;
  1822. typedef typename mapped_vector_of_mapped_vector::const_reference reference;
  1823. typedef const typename mapped_vector_of_mapped_vector::pointer pointer;
  1824. typedef const_iterator2 dual_iterator_type;
  1825. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  1826. // Construction and destruction
  1827. BOOST_UBLAS_INLINE
  1828. const_iterator1 ():
  1829. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  1830. BOOST_UBLAS_INLINE
  1831. const_iterator1 (const self_type &m, int rank, size_type i, size_type j, const vector_const_subiterator_type &itv, const const_subiterator_type &it):
  1832. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  1833. BOOST_UBLAS_INLINE
  1834. const_iterator1 (const iterator1 &it):
  1835. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), itv_ (it.itv_), it_ (it.it_) {}
  1836. // Arithmetic
  1837. BOOST_UBLAS_INLINE
  1838. const_iterator1 &operator ++ () {
  1839. if (rank_ == 1 && layout_type::fast_i ())
  1840. ++ it_;
  1841. else {
  1842. const self_type &m = (*this) ();
  1843. if (rank_ == 0) {
  1844. ++ itv_;
  1845. i_ = itv_->first;
  1846. } else {
  1847. i_ = index1 () + 1;
  1848. }
  1849. if (rank_ == 1 && ++ itv_ == m.end1 ().itv_)
  1850. *this = m.find1 (rank_, i_, j_, 1);
  1851. else if (rank_ == 1) {
  1852. it_ = (*itv_).second.begin ();
  1853. if (it_ == (*itv_).second.end () || index2 () != j_)
  1854. *this = m.find1 (rank_, i_, j_, 1);
  1855. }
  1856. }
  1857. return *this;
  1858. }
  1859. BOOST_UBLAS_INLINE
  1860. const_iterator1 &operator -- () {
  1861. if (rank_ == 1 && layout_type::fast_i ())
  1862. -- it_;
  1863. else {
  1864. const self_type &m = (*this) ();
  1865. if (rank_ == 0) {
  1866. -- itv_;
  1867. i_ = itv_->first;
  1868. } else {
  1869. i_ = index1 () - 1;
  1870. }
  1871. // FIXME: this expression should never become true!
  1872. if (rank_ == 1 && -- itv_ == m.end1 ().itv_)
  1873. *this = m.find1 (rank_, i_, j_, -1);
  1874. else if (rank_ == 1) {
  1875. it_ = (*itv_).second.begin ();
  1876. if (it_ == (*itv_).second.end () || index2 () != j_)
  1877. *this = m.find1 (rank_, i_, j_, -1);
  1878. }
  1879. }
  1880. return *this;
  1881. }
  1882. // Dereference
  1883. BOOST_UBLAS_INLINE
  1884. const_reference operator * () const {
  1885. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1886. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1887. if (rank_ == 1) {
  1888. return (*it_).second;
  1889. } else {
  1890. return (*this) () (i_, j_);
  1891. }
  1892. }
  1893. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1894. BOOST_UBLAS_INLINE
  1895. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1896. typename self_type::
  1897. #endif
  1898. const_iterator2 begin () const {
  1899. const self_type &m = (*this) ();
  1900. return m.find2 (1, index1 (), 0);
  1901. }
  1902. BOOST_UBLAS_INLINE
  1903. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1904. typename self_type::
  1905. #endif
  1906. const_iterator2 cbegin () const {
  1907. return begin ();
  1908. }
  1909. BOOST_UBLAS_INLINE
  1910. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1911. typename self_type::
  1912. #endif
  1913. const_iterator2 end () const {
  1914. const self_type &m = (*this) ();
  1915. return m.find2 (1, index1 (), m.size2 ());
  1916. }
  1917. BOOST_UBLAS_INLINE
  1918. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1919. typename self_type::
  1920. #endif
  1921. const_iterator2 cend () const {
  1922. return end ();
  1923. }
  1924. BOOST_UBLAS_INLINE
  1925. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1926. typename self_type::
  1927. #endif
  1928. const_reverse_iterator2 rbegin () const {
  1929. return const_reverse_iterator2 (end ());
  1930. }
  1931. BOOST_UBLAS_INLINE
  1932. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1933. typename self_type::
  1934. #endif
  1935. const_reverse_iterator2 crbegin () const {
  1936. return rbegin ();
  1937. }
  1938. BOOST_UBLAS_INLINE
  1939. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1940. typename self_type::
  1941. #endif
  1942. const_reverse_iterator2 rend () const {
  1943. return const_reverse_iterator2 (begin ());
  1944. }
  1945. BOOST_UBLAS_INLINE
  1946. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1947. typename self_type::
  1948. #endif
  1949. const_reverse_iterator2 crend () const {
  1950. return rend ();
  1951. }
  1952. #endif
  1953. // Indices
  1954. BOOST_UBLAS_INLINE
  1955. size_type index1 () const {
  1956. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  1957. if (rank_ == 1) {
  1958. BOOST_UBLAS_CHECK (layout_type::index_M ((*itv_).first, (*it_).first) < (*this) ().size1 (), bad_index ());
  1959. return layout_type::index_M ((*itv_).first, (*it_).first);
  1960. } else {
  1961. return i_;
  1962. }
  1963. }
  1964. BOOST_UBLAS_INLINE
  1965. size_type index2 () const {
  1966. if (rank_ == 1) {
  1967. BOOST_UBLAS_CHECK (layout_type::index_m ((*itv_).first, (*it_).first) < (*this) ().size2 (), bad_index ());
  1968. return layout_type::index_m ((*itv_).first, (*it_).first);
  1969. } else {
  1970. return j_;
  1971. }
  1972. }
  1973. // Assignment
  1974. BOOST_UBLAS_INLINE
  1975. const_iterator1 &operator = (const const_iterator1 &it) {
  1976. container_const_reference<self_type>::assign (&it ());
  1977. rank_ = it.rank_;
  1978. i_ = it.i_;
  1979. j_ = it.j_;
  1980. itv_ = it.itv_;
  1981. it_ = it.it_;
  1982. return *this;
  1983. }
  1984. // Comparison
  1985. BOOST_UBLAS_INLINE
  1986. bool operator == (const const_iterator1 &it) const {
  1987. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1988. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  1989. if (rank_ == 1 || it.rank_ == 1) {
  1990. return it_ == it.it_;
  1991. } else {
  1992. return i_ == it.i_ && j_ == it.j_;
  1993. }
  1994. }
  1995. private:
  1996. int rank_;
  1997. size_type i_;
  1998. size_type j_;
  1999. vector_const_subiterator_type itv_;
  2000. const_subiterator_type it_;
  2001. };
  2002. BOOST_UBLAS_INLINE
  2003. const_iterator1 begin1 () const {
  2004. return find1 (0, 0, 0);
  2005. }
  2006. BOOST_UBLAS_INLINE
  2007. const_iterator1 cbegin1 () const {
  2008. return begin1 ();
  2009. }
  2010. BOOST_UBLAS_INLINE
  2011. const_iterator1 end1 () const {
  2012. return find1 (0, size1_, 0);
  2013. }
  2014. BOOST_UBLAS_INLINE
  2015. const_iterator1 cend1 () const {
  2016. return end1 ();
  2017. }
  2018. class iterator1:
  2019. public container_reference<mapped_vector_of_mapped_vector>,
  2020. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  2021. iterator1, value_type> {
  2022. public:
  2023. typedef typename mapped_vector_of_mapped_vector::value_type value_type;
  2024. typedef typename mapped_vector_of_mapped_vector::difference_type difference_type;
  2025. typedef typename mapped_vector_of_mapped_vector::true_reference reference;
  2026. typedef typename mapped_vector_of_mapped_vector::pointer pointer;
  2027. typedef iterator2 dual_iterator_type;
  2028. typedef reverse_iterator2 dual_reverse_iterator_type;
  2029. // Construction and destruction
  2030. BOOST_UBLAS_INLINE
  2031. iterator1 ():
  2032. container_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  2033. BOOST_UBLAS_INLINE
  2034. iterator1 (self_type &m, int rank, size_type i, size_type j, const vector_subiterator_type &itv, const subiterator_type &it):
  2035. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  2036. // Arithmetic
  2037. BOOST_UBLAS_INLINE
  2038. iterator1 &operator ++ () {
  2039. if (rank_ == 1 && layout_type::fast_i ())
  2040. ++ it_;
  2041. else {
  2042. self_type &m = (*this) ();
  2043. if (rank_ == 0) {
  2044. ++ itv_;
  2045. i_ = itv_->first;
  2046. } else {
  2047. i_ = index1 () + 1;
  2048. }
  2049. if (rank_ == 1 && ++ itv_ == m.end1 ().itv_)
  2050. *this = m.find1 (rank_, i_, j_, 1);
  2051. else if (rank_ == 1) {
  2052. it_ = (*itv_).second.begin ();
  2053. if (it_ == (*itv_).second.end () || index2 () != j_)
  2054. *this = m.find1 (rank_, i_, j_, 1);
  2055. }
  2056. }
  2057. return *this;
  2058. }
  2059. BOOST_UBLAS_INLINE
  2060. iterator1 &operator -- () {
  2061. if (rank_ == 1 && layout_type::fast_i ())
  2062. -- it_;
  2063. else {
  2064. self_type &m = (*this) ();
  2065. if (rank_ == 0) {
  2066. -- itv_;
  2067. i_ = itv_->first;
  2068. } else {
  2069. i_ = index1 () - 1;
  2070. }
  2071. // FIXME: this expression should never become true!
  2072. if (rank_ == 1 && -- itv_ == m.end1 ().itv_)
  2073. *this = m.find1 (rank_, i_, j_, -1);
  2074. else if (rank_ == 1) {
  2075. it_ = (*itv_).second.begin ();
  2076. if (it_ == (*itv_).second.end () || index2 () != j_)
  2077. *this = m.find1 (rank_, i_, j_, -1);
  2078. }
  2079. }
  2080. return *this;
  2081. }
  2082. // Dereference
  2083. BOOST_UBLAS_INLINE
  2084. reference operator * () const {
  2085. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  2086. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  2087. if (rank_ == 1) {
  2088. return (*it_).second;
  2089. } else {
  2090. return (*this) ().at_element (i_, j_);
  2091. }
  2092. }
  2093. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2094. BOOST_UBLAS_INLINE
  2095. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2096. typename self_type::
  2097. #endif
  2098. iterator2 begin () const {
  2099. self_type &m = (*this) ();
  2100. return m.find2 (1, index1 (), 0);
  2101. }
  2102. BOOST_UBLAS_INLINE
  2103. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2104. typename self_type::
  2105. #endif
  2106. iterator2 end () const {
  2107. self_type &m = (*this) ();
  2108. return m.find2 (1, index1 (), m.size2 ());
  2109. }
  2110. BOOST_UBLAS_INLINE
  2111. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2112. typename self_type::
  2113. #endif
  2114. reverse_iterator2 rbegin () const {
  2115. return reverse_iterator2 (end ());
  2116. }
  2117. BOOST_UBLAS_INLINE
  2118. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2119. typename self_type::
  2120. #endif
  2121. reverse_iterator2 rend () const {
  2122. return reverse_iterator2 (begin ());
  2123. }
  2124. #endif
  2125. // Indices
  2126. BOOST_UBLAS_INLINE
  2127. size_type index1 () const {
  2128. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  2129. if (rank_ == 1) {
  2130. BOOST_UBLAS_CHECK (layout_type::index_M ((*itv_).first, (*it_).first) < (*this) ().size1 (), bad_index ());
  2131. return layout_type::index_M ((*itv_).first, (*it_).first);
  2132. } else {
  2133. return i_;
  2134. }
  2135. }
  2136. BOOST_UBLAS_INLINE
  2137. size_type index2 () const {
  2138. if (rank_ == 1) {
  2139. BOOST_UBLAS_CHECK (layout_type::index_m ((*itv_).first, (*it_).first) < (*this) ().size2 (), bad_index ());
  2140. return layout_type::index_m ((*itv_).first, (*it_).first);
  2141. } else {
  2142. return j_;
  2143. }
  2144. }
  2145. // Assignment
  2146. BOOST_UBLAS_INLINE
  2147. iterator1 &operator = (const iterator1 &it) {
  2148. container_reference<self_type>::assign (&it ());
  2149. rank_ = it.rank_;
  2150. i_ = it.i_;
  2151. j_ = it.j_;
  2152. itv_ = it.itv_;
  2153. it_ = it.it_;
  2154. return *this;
  2155. }
  2156. // Comparison
  2157. BOOST_UBLAS_INLINE
  2158. bool operator == (const iterator1 &it) const {
  2159. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2160. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  2161. if (rank_ == 1 || it.rank_ == 1) {
  2162. return it_ == it.it_;
  2163. } else {
  2164. return i_ == it.i_ && j_ == it.j_;
  2165. }
  2166. }
  2167. private:
  2168. int rank_;
  2169. size_type i_;
  2170. size_type j_;
  2171. vector_subiterator_type itv_;
  2172. subiterator_type it_;
  2173. friend class const_iterator1;
  2174. };
  2175. BOOST_UBLAS_INLINE
  2176. iterator1 begin1 () {
  2177. return find1 (0, 0, 0);
  2178. }
  2179. BOOST_UBLAS_INLINE
  2180. iterator1 end1 () {
  2181. return find1 (0, size1_, 0);
  2182. }
  2183. class const_iterator2:
  2184. public container_const_reference<mapped_vector_of_mapped_vector>,
  2185. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  2186. const_iterator2, value_type> {
  2187. public:
  2188. typedef typename mapped_vector_of_mapped_vector::value_type value_type;
  2189. typedef typename mapped_vector_of_mapped_vector::difference_type difference_type;
  2190. typedef typename mapped_vector_of_mapped_vector::const_reference reference;
  2191. typedef const typename mapped_vector_of_mapped_vector::pointer pointer;
  2192. typedef const_iterator1 dual_iterator_type;
  2193. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  2194. // Construction and destruction
  2195. BOOST_UBLAS_INLINE
  2196. const_iterator2 ():
  2197. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  2198. BOOST_UBLAS_INLINE
  2199. const_iterator2 (const self_type &m, int rank, size_type i, size_type j, const vector_const_subiterator_type &itv, const const_subiterator_type &it):
  2200. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  2201. BOOST_UBLAS_INLINE
  2202. const_iterator2 (const iterator2 &it):
  2203. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), itv_ (it.itv_), it_ (it.it_) {}
  2204. // Arithmetic
  2205. BOOST_UBLAS_INLINE
  2206. const_iterator2 &operator ++ () {
  2207. if (rank_ == 1 && layout_type::fast_j ())
  2208. ++ it_;
  2209. else {
  2210. const self_type &m = (*this) ();
  2211. if (rank_ == 0) {
  2212. ++ itv_;
  2213. j_ = itv_->first;
  2214. } else {
  2215. j_ = index2 () + 1;
  2216. }
  2217. if (rank_ == 1 && ++ itv_ == m.end2 ().itv_)
  2218. *this = m.find2 (rank_, i_, j_, 1);
  2219. else if (rank_ == 1) {
  2220. it_ = (*itv_).second.begin ();
  2221. if (it_ == (*itv_).second.end () || index1 () != i_)
  2222. *this = m.find2 (rank_, i_, j_, 1);
  2223. }
  2224. }
  2225. return *this;
  2226. }
  2227. BOOST_UBLAS_INLINE
  2228. const_iterator2 &operator -- () {
  2229. if (rank_ == 1 && layout_type::fast_j ())
  2230. -- it_;
  2231. else {
  2232. const self_type &m = (*this) ();
  2233. if (rank_ == 0) {
  2234. -- itv_;
  2235. j_ = itv_->first;
  2236. } else {
  2237. j_ = index2 () - 1;
  2238. }
  2239. // FIXME: this expression should never become true!
  2240. if (rank_ == 1 && -- itv_ == m.end2 ().itv_)
  2241. *this = m.find2 (rank_, i_, j_, -1);
  2242. else if (rank_ == 1) {
  2243. it_ = (*itv_).second.begin ();
  2244. if (it_ == (*itv_).second.end () || index1 () != i_)
  2245. *this = m.find2 (rank_, i_, j_, -1);
  2246. }
  2247. }
  2248. return *this;
  2249. }
  2250. // Dereference
  2251. BOOST_UBLAS_INLINE
  2252. const_reference operator * () const {
  2253. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  2254. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  2255. if (rank_ == 1) {
  2256. return (*it_).second;
  2257. } else {
  2258. return (*this) () (i_, j_);
  2259. }
  2260. }
  2261. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2262. BOOST_UBLAS_INLINE
  2263. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2264. typename self_type::
  2265. #endif
  2266. const_iterator1 begin () const {
  2267. const self_type &m = (*this) ();
  2268. return m.find1 (1, 0, index2 ());
  2269. }
  2270. BOOST_UBLAS_INLINE
  2271. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2272. typename self_type::
  2273. #endif
  2274. const_iterator1 cbegin () const {
  2275. return begin ();
  2276. }
  2277. BOOST_UBLAS_INLINE
  2278. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2279. typename self_type::
  2280. #endif
  2281. const_iterator1 end () const {
  2282. const self_type &m = (*this) ();
  2283. return m.find1 (1, m.size1 (), index2 ());
  2284. }
  2285. BOOST_UBLAS_INLINE
  2286. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2287. typename self_type::
  2288. #endif
  2289. const_iterator1 cend () const {
  2290. return end ();
  2291. }
  2292. BOOST_UBLAS_INLINE
  2293. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2294. typename self_type::
  2295. #endif
  2296. const_reverse_iterator1 rbegin () const {
  2297. return const_reverse_iterator1 (end ());
  2298. }
  2299. BOOST_UBLAS_INLINE
  2300. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2301. typename self_type::
  2302. #endif
  2303. const_reverse_iterator1 crbegin () const {
  2304. return rbegin ();
  2305. }
  2306. BOOST_UBLAS_INLINE
  2307. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2308. typename self_type::
  2309. #endif
  2310. const_reverse_iterator1 rend () const {
  2311. return const_reverse_iterator1 (begin ());
  2312. }
  2313. BOOST_UBLAS_INLINE
  2314. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2315. typename self_type::
  2316. #endif
  2317. const_reverse_iterator1 crend () const {
  2318. return rend ();
  2319. }
  2320. #endif
  2321. // Indices
  2322. BOOST_UBLAS_INLINE
  2323. size_type index1 () const {
  2324. if (rank_ == 1) {
  2325. BOOST_UBLAS_CHECK (layout_type::index_M ((*itv_).first, (*it_).first) < (*this) ().size1 (), bad_index ());
  2326. return layout_type::index_M ((*itv_).first, (*it_).first);
  2327. } else {
  2328. return i_;
  2329. }
  2330. }
  2331. BOOST_UBLAS_INLINE
  2332. size_type index2 () const {
  2333. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  2334. if (rank_ == 1) {
  2335. BOOST_UBLAS_CHECK (layout_type::index_m ((*itv_).first, (*it_).first) < (*this) ().size2 (), bad_index ());
  2336. return layout_type::index_m ((*itv_).first, (*it_).first);
  2337. } else {
  2338. return j_;
  2339. }
  2340. }
  2341. // Assignment
  2342. BOOST_UBLAS_INLINE
  2343. const_iterator2 &operator = (const const_iterator2 &it) {
  2344. container_const_reference<self_type>::assign (&it ());
  2345. rank_ = it.rank_;
  2346. i_ = it.i_;
  2347. j_ = it.j_;
  2348. itv_ = it.itv_;
  2349. it_ = it.it_;
  2350. return *this;
  2351. }
  2352. // Comparison
  2353. BOOST_UBLAS_INLINE
  2354. bool operator == (const const_iterator2 &it) const {
  2355. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2356. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  2357. if (rank_ == 1 || it.rank_ == 1) {
  2358. return it_ == it.it_;
  2359. } else {
  2360. return i_ == it.i_ && j_ == it.j_;
  2361. }
  2362. }
  2363. private:
  2364. int rank_;
  2365. size_type i_;
  2366. size_type j_;
  2367. vector_const_subiterator_type itv_;
  2368. const_subiterator_type it_;
  2369. };
  2370. BOOST_UBLAS_INLINE
  2371. const_iterator2 begin2 () const {
  2372. return find2 (0, 0, 0);
  2373. }
  2374. BOOST_UBLAS_INLINE
  2375. const_iterator2 cbegin2 () const {
  2376. return begin2 ();
  2377. }
  2378. BOOST_UBLAS_INLINE
  2379. const_iterator2 end2 () const {
  2380. return find2 (0, 0, size2_);
  2381. }
  2382. BOOST_UBLAS_INLINE
  2383. const_iterator2 cend2 () const {
  2384. return end2 ();
  2385. }
  2386. class iterator2:
  2387. public container_reference<mapped_vector_of_mapped_vector>,
  2388. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  2389. iterator2, value_type> {
  2390. public:
  2391. typedef typename mapped_vector_of_mapped_vector::value_type value_type;
  2392. typedef typename mapped_vector_of_mapped_vector::difference_type difference_type;
  2393. typedef typename mapped_vector_of_mapped_vector::true_reference reference;
  2394. typedef typename mapped_vector_of_mapped_vector::pointer pointer;
  2395. typedef iterator1 dual_iterator_type;
  2396. typedef reverse_iterator1 dual_reverse_iterator_type;
  2397. // Construction and destruction
  2398. BOOST_UBLAS_INLINE
  2399. iterator2 ():
  2400. container_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  2401. BOOST_UBLAS_INLINE
  2402. iterator2 (self_type &m, int rank, size_type i, size_type j, const vector_subiterator_type &itv, const subiterator_type &it):
  2403. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  2404. // Arithmetic
  2405. BOOST_UBLAS_INLINE
  2406. iterator2 &operator ++ () {
  2407. if (rank_ == 1 && layout_type::fast_j ())
  2408. ++ it_;
  2409. else {
  2410. self_type &m = (*this) ();
  2411. if (rank_ == 0) {
  2412. ++ itv_;
  2413. j_ = itv_->first;
  2414. } else {
  2415. j_ = index2 () + 1;
  2416. }
  2417. if (rank_ == 1 && ++ itv_ == m.end2 ().itv_)
  2418. *this = m.find2 (rank_, i_, j_, 1);
  2419. else if (rank_ == 1) {
  2420. it_ = (*itv_).second.begin ();
  2421. if (it_ == (*itv_).second.end () || index1 () != i_)
  2422. *this = m.find2 (rank_, i_, j_, 1);
  2423. }
  2424. }
  2425. return *this;
  2426. }
  2427. BOOST_UBLAS_INLINE
  2428. iterator2 &operator -- () {
  2429. if (rank_ == 1 && layout_type::fast_j ())
  2430. -- it_;
  2431. else {
  2432. self_type &m = (*this) ();
  2433. if (rank_ == 0) {
  2434. -- itv_;
  2435. j_ = itv_->first;
  2436. } else {
  2437. j_ = index2 () - 1;
  2438. }
  2439. // FIXME: this expression should never become true!
  2440. if (rank_ == 1 && -- itv_ == m.end2 ().itv_)
  2441. *this = m.find2 (rank_, i_, j_, -1);
  2442. else if (rank_ == 1) {
  2443. it_ = (*itv_).second.begin ();
  2444. if (it_ == (*itv_).second.end () || index1 () != i_)
  2445. *this = m.find2 (rank_, i_, j_, -1);
  2446. }
  2447. }
  2448. return *this;
  2449. }
  2450. // Dereference
  2451. BOOST_UBLAS_INLINE
  2452. reference operator * () const {
  2453. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  2454. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  2455. if (rank_ == 1) {
  2456. return (*it_).second;
  2457. } else {
  2458. return (*this) ().at_element (i_, j_);
  2459. }
  2460. }
  2461. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2462. BOOST_UBLAS_INLINE
  2463. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2464. typename self_type::
  2465. #endif
  2466. iterator1 begin () const {
  2467. self_type &m = (*this) ();
  2468. return m.find1 (1, 0, index2 ());
  2469. }
  2470. BOOST_UBLAS_INLINE
  2471. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2472. typename self_type::
  2473. #endif
  2474. iterator1 end () const {
  2475. self_type &m = (*this) ();
  2476. return m.find1 (1, m.size1 (), index2 ());
  2477. }
  2478. BOOST_UBLAS_INLINE
  2479. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2480. typename self_type::
  2481. #endif
  2482. reverse_iterator1 rbegin () const {
  2483. return reverse_iterator1 (end ());
  2484. }
  2485. BOOST_UBLAS_INLINE
  2486. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2487. typename self_type::
  2488. #endif
  2489. reverse_iterator1 rend () const {
  2490. return reverse_iterator1 (begin ());
  2491. }
  2492. #endif
  2493. // Indices
  2494. BOOST_UBLAS_INLINE
  2495. size_type index1 () const {
  2496. if (rank_ == 1) {
  2497. BOOST_UBLAS_CHECK (layout_type::index_M ((*itv_).first, (*it_).first) < (*this) ().size1 (), bad_index ());
  2498. return layout_type::index_M ((*itv_).first, (*it_).first);
  2499. } else {
  2500. return i_;
  2501. }
  2502. }
  2503. BOOST_UBLAS_INLINE
  2504. size_type index2 () const {
  2505. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  2506. if (rank_ == 1) {
  2507. BOOST_UBLAS_CHECK (layout_type::index_m ((*itv_).first, (*it_).first) < (*this) ().size2 (), bad_index ());
  2508. return layout_type::index_m ((*itv_).first, (*it_).first);
  2509. } else {
  2510. return j_;
  2511. }
  2512. }
  2513. // Assignment
  2514. BOOST_UBLAS_INLINE
  2515. iterator2 &operator = (const iterator2 &it) {
  2516. container_reference<self_type>::assign (&it ());
  2517. rank_ = it.rank_;
  2518. i_ = it.i_;
  2519. j_ = it.j_;
  2520. itv_ = it.itv_;
  2521. it_ = it.it_;
  2522. return *this;
  2523. }
  2524. // Comparison
  2525. BOOST_UBLAS_INLINE
  2526. bool operator == (const iterator2 &it) const {
  2527. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2528. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  2529. if (rank_ == 1 || it.rank_ == 1) {
  2530. return it_ == it.it_;
  2531. } else {
  2532. return i_ == it.i_ && j_ == it.j_;
  2533. }
  2534. }
  2535. private:
  2536. int rank_;
  2537. size_type i_;
  2538. size_type j_;
  2539. vector_subiterator_type itv_;
  2540. subiterator_type it_;
  2541. friend class const_iterator2;
  2542. };
  2543. BOOST_UBLAS_INLINE
  2544. iterator2 begin2 () {
  2545. return find2 (0, 0, 0);
  2546. }
  2547. BOOST_UBLAS_INLINE
  2548. iterator2 end2 () {
  2549. return find2 (0, 0, size2_);
  2550. }
  2551. // Reverse iterators
  2552. BOOST_UBLAS_INLINE
  2553. const_reverse_iterator1 rbegin1 () const {
  2554. return const_reverse_iterator1 (end1 ());
  2555. }
  2556. BOOST_UBLAS_INLINE
  2557. const_reverse_iterator1 crbegin1 () const {
  2558. return rbegin1 ();
  2559. }
  2560. BOOST_UBLAS_INLINE
  2561. const_reverse_iterator1 rend1 () const {
  2562. return const_reverse_iterator1 (begin1 ());
  2563. }
  2564. BOOST_UBLAS_INLINE
  2565. const_reverse_iterator1 crend1 () const {
  2566. return rend1 ();
  2567. }
  2568. BOOST_UBLAS_INLINE
  2569. reverse_iterator1 rbegin1 () {
  2570. return reverse_iterator1 (end1 ());
  2571. }
  2572. BOOST_UBLAS_INLINE
  2573. reverse_iterator1 rend1 () {
  2574. return reverse_iterator1 (begin1 ());
  2575. }
  2576. BOOST_UBLAS_INLINE
  2577. const_reverse_iterator2 rbegin2 () const {
  2578. return const_reverse_iterator2 (end2 ());
  2579. }
  2580. BOOST_UBLAS_INLINE
  2581. const_reverse_iterator2 crbegin2 () const {
  2582. return rbegin2 ();
  2583. }
  2584. BOOST_UBLAS_INLINE
  2585. const_reverse_iterator2 rend2 () const {
  2586. return const_reverse_iterator2 (begin2 ());
  2587. }
  2588. BOOST_UBLAS_INLINE
  2589. const_reverse_iterator2 crend2 () const {
  2590. return rend2 ();
  2591. }
  2592. BOOST_UBLAS_INLINE
  2593. reverse_iterator2 rbegin2 () {
  2594. return reverse_iterator2 (end2 ());
  2595. }
  2596. BOOST_UBLAS_INLINE
  2597. reverse_iterator2 rend2 () {
  2598. return reverse_iterator2 (begin2 ());
  2599. }
  2600. // Serialization
  2601. template<class Archive>
  2602. void serialize(Archive & ar, const unsigned int /* file_version */){
  2603. serialization::collection_size_type s1 (size1_);
  2604. serialization::collection_size_type s2 (size2_);
  2605. ar & serialization::make_nvp("size1",s1);
  2606. ar & serialization::make_nvp("size2",s2);
  2607. if (Archive::is_loading::value) {
  2608. size1_ = s1;
  2609. size2_ = s2;
  2610. }
  2611. ar & serialization::make_nvp("data", data_);
  2612. }
  2613. private:
  2614. size_type size1_;
  2615. size_type size2_;
  2616. array_type data_;
  2617. static const value_type zero_;
  2618. };
  2619. template<class T, class L, class A>
  2620. const typename mapped_vector_of_mapped_vector<T, L, A>::value_type mapped_vector_of_mapped_vector<T, L, A>::zero_ = value_type/*zero*/();
  2621. // Comperssed array based sparse matrix class
  2622. // Thanks to Kresimir Fresl for extending this to cover different index bases.
  2623. template<class T, class L, std::size_t IB, class IA, class TA>
  2624. class compressed_matrix:
  2625. public matrix_container<compressed_matrix<T, L, IB, IA, TA> > {
  2626. typedef T &true_reference;
  2627. typedef T *pointer;
  2628. typedef const T *const_pointer;
  2629. typedef L layout_type;
  2630. typedef compressed_matrix<T, L, IB, IA, TA> self_type;
  2631. public:
  2632. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  2633. using matrix_container<self_type>::operator ();
  2634. #endif
  2635. // ISSUE require type consistency check
  2636. // is_convertable (IA::size_type, TA::size_type)
  2637. typedef typename IA::value_type size_type;
  2638. // size_type for the data arrays.
  2639. typedef typename IA::size_type array_size_type;
  2640. // FIXME difference type for sparse storage iterators should it be in the container?
  2641. typedef typename IA::difference_type difference_type;
  2642. typedef T value_type;
  2643. typedef const T &const_reference;
  2644. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  2645. typedef T &reference;
  2646. #else
  2647. typedef sparse_matrix_element<self_type> reference;
  2648. #endif
  2649. typedef IA index_array_type;
  2650. typedef TA value_array_type;
  2651. typedef const matrix_reference<const self_type> const_closure_type;
  2652. typedef matrix_reference<self_type> closure_type;
  2653. typedef compressed_vector<T, IB, IA, TA> vector_temporary_type;
  2654. typedef self_type matrix_temporary_type;
  2655. typedef sparse_tag storage_category;
  2656. typedef typename L::orientation_category orientation_category;
  2657. // Construction and destruction
  2658. BOOST_UBLAS_INLINE
  2659. compressed_matrix ():
  2660. matrix_container<self_type> (),
  2661. size1_ (0), size2_ (0), capacity_ (restrict_capacity (0)),
  2662. filled1_ (1), filled2_ (0),
  2663. index1_data_ (layout_type::size_M (size1_, size2_) + 1), index2_data_ (capacity_), value_data_ (capacity_) {
  2664. index1_data_ [filled1_ - 1] = k_based (filled2_);
  2665. storage_invariants ();
  2666. }
  2667. BOOST_UBLAS_INLINE
  2668. compressed_matrix (size_type size1, size_type size2, size_type non_zeros = 0):
  2669. matrix_container<self_type> (),
  2670. size1_ (size1), size2_ (size2), capacity_ (restrict_capacity (non_zeros)),
  2671. filled1_ (1), filled2_ (0),
  2672. index1_data_ (layout_type::size_M (size1_, size2_) + 1), index2_data_ (capacity_), value_data_ (capacity_) {
  2673. index1_data_ [filled1_ - 1] = k_based (filled2_);
  2674. storage_invariants ();
  2675. }
  2676. BOOST_UBLAS_INLINE
  2677. compressed_matrix (const compressed_matrix &m):
  2678. matrix_container<self_type> (),
  2679. size1_ (m.size1_), size2_ (m.size2_), capacity_ (m.capacity_),
  2680. filled1_ (m.filled1_), filled2_ (m.filled2_),
  2681. index1_data_ (m.index1_data_), index2_data_ (m.index2_data_), value_data_ (m.value_data_) {
  2682. storage_invariants ();
  2683. }
  2684. BOOST_UBLAS_INLINE
  2685. compressed_matrix (const coordinate_matrix<T, L, IB, IA, TA> &m):
  2686. matrix_container<self_type> (),
  2687. size1_ (m.size1()), size2_ (m.size2()),
  2688. index1_data_ (layout_type::size_M (size1_, size2_) + 1)
  2689. {
  2690. m.sort();
  2691. reserve(m.nnz(), false);
  2692. filled2_ = m.nnz();
  2693. const_subiterator_type i_start = m.index1_data().begin();
  2694. const_subiterator_type i_end = (i_start + filled2_);
  2695. const_subiterator_type i = i_start;
  2696. size_type r = 1;
  2697. for (; (r < layout_type::size_M (size1_, size2_)) && (i != i_end); ++r) {
  2698. i = std::lower_bound(i, i_end, r);
  2699. index1_data_[r] = k_based( i - i_start );
  2700. }
  2701. filled1_ = r + 1;
  2702. std::copy( m.index2_data().begin(), m.index2_data().begin() + filled2_, index2_data_.begin());
  2703. std::copy( m.value_data().begin(), m.value_data().begin() + filled2_, value_data_.begin());
  2704. index1_data_ [filled1_ - 1] = k_based(filled2_);
  2705. storage_invariants ();
  2706. }
  2707. template<class AE>
  2708. BOOST_UBLAS_INLINE
  2709. compressed_matrix (const matrix_expression<AE> &ae, size_type non_zeros = 0):
  2710. matrix_container<self_type> (),
  2711. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), capacity_ (restrict_capacity (non_zeros)),
  2712. filled1_ (1), filled2_ (0),
  2713. index1_data_ (layout_type::size_M (ae ().size1 (), ae ().size2 ()) + 1),
  2714. index2_data_ (capacity_), value_data_ (capacity_) {
  2715. index1_data_ [filled1_ - 1] = k_based (filled2_);
  2716. storage_invariants ();
  2717. matrix_assign<scalar_assign> (*this, ae);
  2718. }
  2719. // Accessors
  2720. BOOST_UBLAS_INLINE
  2721. size_type size1 () const {
  2722. return size1_;
  2723. }
  2724. BOOST_UBLAS_INLINE
  2725. size_type size2 () const {
  2726. return size2_;
  2727. }
  2728. BOOST_UBLAS_INLINE
  2729. size_type nnz_capacity () const {
  2730. return capacity_;
  2731. }
  2732. BOOST_UBLAS_INLINE
  2733. size_type nnz () const {
  2734. return filled2_;
  2735. }
  2736. // Storage accessors
  2737. BOOST_UBLAS_INLINE
  2738. static size_type index_base () {
  2739. return IB;
  2740. }
  2741. BOOST_UBLAS_INLINE
  2742. array_size_type filled1 () const {
  2743. return filled1_;
  2744. }
  2745. BOOST_UBLAS_INLINE
  2746. array_size_type filled2 () const {
  2747. return filled2_;
  2748. }
  2749. BOOST_UBLAS_INLINE
  2750. const index_array_type &index1_data () const {
  2751. return index1_data_;
  2752. }
  2753. BOOST_UBLAS_INLINE
  2754. const index_array_type &index2_data () const {
  2755. return index2_data_;
  2756. }
  2757. BOOST_UBLAS_INLINE
  2758. const value_array_type &value_data () const {
  2759. return value_data_;
  2760. }
  2761. BOOST_UBLAS_INLINE
  2762. void set_filled (const array_size_type& filled1, const array_size_type& filled2) {
  2763. filled1_ = filled1;
  2764. filled2_ = filled2;
  2765. storage_invariants ();
  2766. }
  2767. BOOST_UBLAS_INLINE
  2768. index_array_type &index1_data () {
  2769. return index1_data_;
  2770. }
  2771. BOOST_UBLAS_INLINE
  2772. index_array_type &index2_data () {
  2773. return index2_data_;
  2774. }
  2775. BOOST_UBLAS_INLINE
  2776. value_array_type &value_data () {
  2777. return value_data_;
  2778. }
  2779. BOOST_UBLAS_INLINE
  2780. void complete_index1_data () {
  2781. while (filled1_ <= layout_type::size_M (size1_, size2_)) {
  2782. this->index1_data_ [filled1_] = k_based (filled2_);
  2783. ++ this->filled1_;
  2784. }
  2785. }
  2786. // Resizing
  2787. private:
  2788. BOOST_UBLAS_INLINE
  2789. size_type restrict_capacity (size_type non_zeros) const {
  2790. non_zeros = (std::max) (non_zeros, (std::min) (size1_, size2_));
  2791. // Guarding against overflow - Thanks to Alexei Novakov for the hint.
  2792. // non_zeros = (std::min) (non_zeros, size1_ * size2_);
  2793. if (size1_ > 0 && non_zeros / size1_ >= size2_)
  2794. non_zeros = size1_ * size2_;
  2795. return non_zeros;
  2796. }
  2797. public:
  2798. BOOST_UBLAS_INLINE
  2799. void resize (size_type size1, size_type size2, bool preserve = true) {
  2800. // FIXME preserve unimplemented
  2801. BOOST_UBLAS_CHECK (!preserve, internal_logic ());
  2802. size1_ = size1;
  2803. size2_ = size2;
  2804. capacity_ = restrict_capacity (capacity_);
  2805. filled1_ = 1;
  2806. filled2_ = 0;
  2807. index1_data_.resize (layout_type::size_M (size1_, size2_) + 1);
  2808. index2_data_.resize (capacity_);
  2809. value_data_.resize (capacity_);
  2810. index1_data_ [filled1_ - 1] = k_based (filled2_);
  2811. storage_invariants ();
  2812. }
  2813. // Reserving
  2814. BOOST_UBLAS_INLINE
  2815. void reserve (size_type non_zeros, bool preserve = true) {
  2816. capacity_ = restrict_capacity (non_zeros);
  2817. if (preserve) {
  2818. index2_data_.resize (capacity_, size_type ());
  2819. value_data_.resize (capacity_, value_type ());
  2820. filled2_ = (std::min) (capacity_, filled2_);
  2821. }
  2822. else {
  2823. index2_data_.resize (capacity_);
  2824. value_data_.resize (capacity_);
  2825. filled1_ = 1;
  2826. filled2_ = 0;
  2827. index1_data_ [filled1_ - 1] = k_based (filled2_);
  2828. }
  2829. storage_invariants ();
  2830. }
  2831. // Element support
  2832. BOOST_UBLAS_INLINE
  2833. pointer find_element (size_type i, size_type j) {
  2834. return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i, j));
  2835. }
  2836. BOOST_UBLAS_INLINE
  2837. const_pointer find_element (size_type i, size_type j) const {
  2838. size_type element1 (layout_type::index_M (i, j));
  2839. size_type element2 (layout_type::index_m (i, j));
  2840. if (filled1_ <= element1 + 1)
  2841. return 0;
  2842. vector_const_subiterator_type itv (index1_data_.begin () + element1);
  2843. const_subiterator_type it_begin (index2_data_.begin () + zero_based (*itv));
  2844. const_subiterator_type it_end (index2_data_.begin () + zero_based (*(itv + 1)));
  2845. const_subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (element2), std::less<size_type> ()));
  2846. if (it == it_end || *it != k_based (element2))
  2847. return 0;
  2848. return &value_data_ [it - index2_data_.begin ()];
  2849. }
  2850. // Element access
  2851. BOOST_UBLAS_INLINE
  2852. const_reference operator () (size_type i, size_type j) const {
  2853. const_pointer p = find_element (i, j);
  2854. if (p)
  2855. return *p;
  2856. else
  2857. return zero_;
  2858. }
  2859. BOOST_UBLAS_INLINE
  2860. reference operator () (size_type i, size_type j) {
  2861. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  2862. size_type element1 (layout_type::index_M (i, j));
  2863. size_type element2 (layout_type::index_m (i, j));
  2864. if (filled1_ <= element1 + 1)
  2865. return insert_element (i, j, value_type/*zero*/());
  2866. pointer p = find_element (i, j);
  2867. if (p)
  2868. return *p;
  2869. else
  2870. return insert_element (i, j, value_type/*zero*/());
  2871. #else
  2872. return reference (*this, i, j);
  2873. #endif
  2874. }
  2875. // Element assignment
  2876. BOOST_UBLAS_INLINE
  2877. true_reference insert_element (size_type i, size_type j, const_reference t) {
  2878. BOOST_UBLAS_CHECK (!find_element (i, j), bad_index ()); // duplicate element
  2879. if (filled2_ >= capacity_)
  2880. reserve (2 * filled2_, true);
  2881. BOOST_UBLAS_CHECK (filled2_ < capacity_, internal_logic ());
  2882. size_type element1 = layout_type::index_M (i, j);
  2883. size_type element2 = layout_type::index_m (i, j);
  2884. while (filled1_ <= element1 + 1) {
  2885. index1_data_ [filled1_] = k_based (filled2_);
  2886. ++ filled1_;
  2887. }
  2888. vector_subiterator_type itv (index1_data_.begin () + element1);
  2889. subiterator_type it_begin (index2_data_.begin () + zero_based (*itv));
  2890. subiterator_type it_end (index2_data_.begin () + zero_based (*(itv + 1)));
  2891. subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (element2), std::less<size_type> ()));
  2892. typename std::iterator_traits<subiterator_type>::difference_type n = it - index2_data_.begin ();
  2893. BOOST_UBLAS_CHECK (it == it_end || *it != k_based (element2), internal_logic ()); // duplicate bound by lower_bound
  2894. ++ filled2_;
  2895. it = index2_data_.begin () + n;
  2896. std::copy_backward (it, index2_data_.begin () + filled2_ - 1, index2_data_.begin () + filled2_);
  2897. *it = k_based (element2);
  2898. typename value_array_type::iterator itt (value_data_.begin () + n);
  2899. std::copy_backward (itt, value_data_.begin () + filled2_ - 1, value_data_.begin () + filled2_);
  2900. *itt = t;
  2901. while (element1 + 1 < filled1_) {
  2902. ++ index1_data_ [element1 + 1];
  2903. ++ element1;
  2904. }
  2905. storage_invariants ();
  2906. return *itt;
  2907. }
  2908. BOOST_UBLAS_INLINE
  2909. void erase_element (size_type i, size_type j) {
  2910. size_type element1 = layout_type::index_M (i, j);
  2911. size_type element2 = layout_type::index_m (i, j);
  2912. if (element1 + 1 >= filled1_)
  2913. return;
  2914. vector_subiterator_type itv (index1_data_.begin () + element1);
  2915. subiterator_type it_begin (index2_data_.begin () + zero_based (*itv));
  2916. subiterator_type it_end (index2_data_.begin () + zero_based (*(itv + 1)));
  2917. subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (element2), std::less<size_type> ()));
  2918. if (it != it_end && *it == k_based (element2)) {
  2919. typename std::iterator_traits<subiterator_type>::difference_type n = it - index2_data_.begin ();
  2920. std::copy (it + 1, index2_data_.begin () + filled2_, it);
  2921. typename value_array_type::iterator itt (value_data_.begin () + n);
  2922. std::copy (itt + 1, value_data_.begin () + filled2_, itt);
  2923. -- filled2_;
  2924. while (index1_data_ [filled1_ - 2] > k_based (filled2_)) {
  2925. index1_data_ [filled1_ - 1] = 0;
  2926. -- filled1_;
  2927. }
  2928. while (element1 + 1 < filled1_) {
  2929. -- index1_data_ [element1 + 1];
  2930. ++ element1;
  2931. }
  2932. }
  2933. storage_invariants ();
  2934. }
  2935. // Zeroing
  2936. BOOST_UBLAS_INLINE
  2937. void clear () {
  2938. filled1_ = 1;
  2939. filled2_ = 0;
  2940. index1_data_ [filled1_ - 1] = k_based (filled2_);
  2941. storage_invariants ();
  2942. }
  2943. // Assignment
  2944. BOOST_UBLAS_INLINE
  2945. compressed_matrix &operator = (const compressed_matrix &m) {
  2946. if (this != &m) {
  2947. size1_ = m.size1_;
  2948. size2_ = m.size2_;
  2949. capacity_ = m.capacity_;
  2950. filled1_ = m.filled1_;
  2951. filled2_ = m.filled2_;
  2952. index1_data_ = m.index1_data_;
  2953. index2_data_ = m.index2_data_;
  2954. value_data_ = m.value_data_;
  2955. }
  2956. storage_invariants ();
  2957. return *this;
  2958. }
  2959. template<class C> // Container assignment without temporary
  2960. BOOST_UBLAS_INLINE
  2961. compressed_matrix &operator = (const matrix_container<C> &m) {
  2962. resize (m ().size1 (), m ().size2 (), false);
  2963. assign (m);
  2964. return *this;
  2965. }
  2966. BOOST_UBLAS_INLINE
  2967. compressed_matrix &assign_temporary (compressed_matrix &m) {
  2968. swap (m);
  2969. return *this;
  2970. }
  2971. template<class AE>
  2972. BOOST_UBLAS_INLINE
  2973. compressed_matrix &operator = (const matrix_expression<AE> &ae) {
  2974. self_type temporary (ae, capacity_);
  2975. return assign_temporary (temporary);
  2976. }
  2977. template<class AE>
  2978. BOOST_UBLAS_INLINE
  2979. compressed_matrix &assign (const matrix_expression<AE> &ae) {
  2980. matrix_assign<scalar_assign> (*this, ae);
  2981. return *this;
  2982. }
  2983. template<class AE>
  2984. BOOST_UBLAS_INLINE
  2985. compressed_matrix& operator += (const matrix_expression<AE> &ae) {
  2986. self_type temporary (*this + ae, capacity_);
  2987. return assign_temporary (temporary);
  2988. }
  2989. template<class C> // Container assignment without temporary
  2990. BOOST_UBLAS_INLINE
  2991. compressed_matrix &operator += (const matrix_container<C> &m) {
  2992. plus_assign (m);
  2993. return *this;
  2994. }
  2995. template<class AE>
  2996. BOOST_UBLAS_INLINE
  2997. compressed_matrix &plus_assign (const matrix_expression<AE> &ae) {
  2998. matrix_assign<scalar_plus_assign> (*this, ae);
  2999. return *this;
  3000. }
  3001. template<class AE>
  3002. BOOST_UBLAS_INLINE
  3003. compressed_matrix& operator -= (const matrix_expression<AE> &ae) {
  3004. self_type temporary (*this - ae, capacity_);
  3005. return assign_temporary (temporary);
  3006. }
  3007. template<class C> // Container assignment without temporary
  3008. BOOST_UBLAS_INLINE
  3009. compressed_matrix &operator -= (const matrix_container<C> &m) {
  3010. minus_assign (m);
  3011. return *this;
  3012. }
  3013. template<class AE>
  3014. BOOST_UBLAS_INLINE
  3015. compressed_matrix &minus_assign (const matrix_expression<AE> &ae) {
  3016. matrix_assign<scalar_minus_assign> (*this, ae);
  3017. return *this;
  3018. }
  3019. template<class AT>
  3020. BOOST_UBLAS_INLINE
  3021. compressed_matrix& operator *= (const AT &at) {
  3022. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  3023. return *this;
  3024. }
  3025. template<class AT>
  3026. BOOST_UBLAS_INLINE
  3027. compressed_matrix& operator /= (const AT &at) {
  3028. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  3029. return *this;
  3030. }
  3031. // Swapping
  3032. BOOST_UBLAS_INLINE
  3033. void swap (compressed_matrix &m) {
  3034. if (this != &m) {
  3035. std::swap (size1_, m.size1_);
  3036. std::swap (size2_, m.size2_);
  3037. std::swap (capacity_, m.capacity_);
  3038. std::swap (filled1_, m.filled1_);
  3039. std::swap (filled2_, m.filled2_);
  3040. index1_data_.swap (m.index1_data_);
  3041. index2_data_.swap (m.index2_data_);
  3042. value_data_.swap (m.value_data_);
  3043. }
  3044. storage_invariants ();
  3045. }
  3046. BOOST_UBLAS_INLINE
  3047. friend void swap (compressed_matrix &m1, compressed_matrix &m2) {
  3048. m1.swap (m2);
  3049. }
  3050. // Back element insertion and erasure
  3051. BOOST_UBLAS_INLINE
  3052. void push_back (size_type i, size_type j, const_reference t) {
  3053. if (filled2_ >= capacity_)
  3054. reserve (2 * filled2_, true);
  3055. BOOST_UBLAS_CHECK (filled2_ < capacity_, internal_logic ());
  3056. size_type element1 = layout_type::index_M (i, j);
  3057. size_type element2 = layout_type::index_m (i, j);
  3058. while (filled1_ < element1 + 2) {
  3059. index1_data_ [filled1_] = k_based (filled2_);
  3060. ++ filled1_;
  3061. }
  3062. // must maintain sort order
  3063. BOOST_UBLAS_CHECK ((filled1_ == element1 + 2 &&
  3064. (filled2_ == zero_based (index1_data_ [filled1_ - 2]) ||
  3065. index2_data_ [filled2_ - 1] < k_based (element2))), external_logic ());
  3066. ++ filled2_;
  3067. index1_data_ [filled1_ - 1] = k_based (filled2_);
  3068. index2_data_ [filled2_ - 1] = k_based (element2);
  3069. value_data_ [filled2_ - 1] = t;
  3070. storage_invariants ();
  3071. }
  3072. BOOST_UBLAS_INLINE
  3073. void pop_back () {
  3074. BOOST_UBLAS_CHECK (filled1_ > 0 && filled2_ > 0, external_logic ());
  3075. -- filled2_;
  3076. while (index1_data_ [filled1_ - 2] > k_based (filled2_)) {
  3077. index1_data_ [filled1_ - 1] = 0;
  3078. -- filled1_;
  3079. }
  3080. -- index1_data_ [filled1_ - 1];
  3081. storage_invariants ();
  3082. }
  3083. // Iterator types
  3084. private:
  3085. // Use index array iterator
  3086. typedef typename IA::const_iterator vector_const_subiterator_type;
  3087. typedef typename IA::iterator vector_subiterator_type;
  3088. typedef typename IA::const_iterator const_subiterator_type;
  3089. typedef typename IA::iterator subiterator_type;
  3090. BOOST_UBLAS_INLINE
  3091. true_reference at_element (size_type i, size_type j) {
  3092. pointer p = find_element (i, j);
  3093. BOOST_UBLAS_CHECK (p, bad_index ());
  3094. return *p;
  3095. }
  3096. public:
  3097. class const_iterator1;
  3098. class iterator1;
  3099. class const_iterator2;
  3100. class iterator2;
  3101. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  3102. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  3103. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  3104. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  3105. // Element lookup
  3106. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  3107. const_iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) const {
  3108. for (;;) {
  3109. array_size_type address1 (layout_type::index_M (i, j));
  3110. array_size_type address2 (layout_type::index_m (i, j));
  3111. vector_const_subiterator_type itv (index1_data_.begin () + (std::min) (filled1_ - 1, address1));
  3112. if (filled1_ <= address1 + 1)
  3113. return const_iterator1 (*this, rank, i, j, itv, index2_data_.begin () + filled2_);
  3114. const_subiterator_type it_begin (index2_data_.begin () + zero_based (*itv));
  3115. const_subiterator_type it_end (index2_data_.begin () + zero_based (*(itv + 1)));
  3116. const_subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  3117. if (rank == 0)
  3118. return const_iterator1 (*this, rank, i, j, itv, it);
  3119. if (it != it_end && zero_based (*it) == address2)
  3120. return const_iterator1 (*this, rank, i, j, itv, it);
  3121. if (direction > 0) {
  3122. if (layout_type::fast_i ()) {
  3123. if (it == it_end)
  3124. return const_iterator1 (*this, rank, i, j, itv, it);
  3125. i = zero_based (*it);
  3126. } else {
  3127. if (i >= size1_)
  3128. return const_iterator1 (*this, rank, i, j, itv, it);
  3129. ++ i;
  3130. }
  3131. } else /* if (direction < 0) */ {
  3132. if (layout_type::fast_i ()) {
  3133. if (it == index2_data_.begin () + zero_based (*itv))
  3134. return const_iterator1 (*this, rank, i, j, itv, it);
  3135. i = zero_based (*(it - 1));
  3136. } else {
  3137. if (i == 0)
  3138. return const_iterator1 (*this, rank, i, j, itv, it);
  3139. -- i;
  3140. }
  3141. }
  3142. }
  3143. }
  3144. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  3145. iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) {
  3146. for (;;) {
  3147. array_size_type address1 (layout_type::index_M (i, j));
  3148. array_size_type address2 (layout_type::index_m (i, j));
  3149. vector_subiterator_type itv (index1_data_.begin () + (std::min) (filled1_ - 1, address1));
  3150. if (filled1_ <= address1 + 1)
  3151. return iterator1 (*this, rank, i, j, itv, index2_data_.begin () + filled2_);
  3152. subiterator_type it_begin (index2_data_.begin () + zero_based (*itv));
  3153. subiterator_type it_end (index2_data_.begin () + zero_based (*(itv + 1)));
  3154. subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  3155. if (rank == 0)
  3156. return iterator1 (*this, rank, i, j, itv, it);
  3157. if (it != it_end && zero_based (*it) == address2)
  3158. return iterator1 (*this, rank, i, j, itv, it);
  3159. if (direction > 0) {
  3160. if (layout_type::fast_i ()) {
  3161. if (it == it_end)
  3162. return iterator1 (*this, rank, i, j, itv, it);
  3163. i = zero_based (*it);
  3164. } else {
  3165. if (i >= size1_)
  3166. return iterator1 (*this, rank, i, j, itv, it);
  3167. ++ i;
  3168. }
  3169. } else /* if (direction < 0) */ {
  3170. if (layout_type::fast_i ()) {
  3171. if (it == index2_data_.begin () + zero_based (*itv))
  3172. return iterator1 (*this, rank, i, j, itv, it);
  3173. i = zero_based (*(it - 1));
  3174. } else {
  3175. if (i == 0)
  3176. return iterator1 (*this, rank, i, j, itv, it);
  3177. -- i;
  3178. }
  3179. }
  3180. }
  3181. }
  3182. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  3183. const_iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) const {
  3184. for (;;) {
  3185. array_size_type address1 (layout_type::index_M (i, j));
  3186. array_size_type address2 (layout_type::index_m (i, j));
  3187. vector_const_subiterator_type itv (index1_data_.begin () + (std::min) (filled1_ - 1, address1));
  3188. if (filled1_ <= address1 + 1)
  3189. return const_iterator2 (*this, rank, i, j, itv, index2_data_.begin () + filled2_);
  3190. const_subiterator_type it_begin (index2_data_.begin () + zero_based (*itv));
  3191. const_subiterator_type it_end (index2_data_.begin () + zero_based (*(itv + 1)));
  3192. const_subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  3193. if (rank == 0)
  3194. return const_iterator2 (*this, rank, i, j, itv, it);
  3195. if (it != it_end && zero_based (*it) == address2)
  3196. return const_iterator2 (*this, rank, i, j, itv, it);
  3197. if (direction > 0) {
  3198. if (layout_type::fast_j ()) {
  3199. if (it == it_end)
  3200. return const_iterator2 (*this, rank, i, j, itv, it);
  3201. j = zero_based (*it);
  3202. } else {
  3203. if (j >= size2_)
  3204. return const_iterator2 (*this, rank, i, j, itv, it);
  3205. ++ j;
  3206. }
  3207. } else /* if (direction < 0) */ {
  3208. if (layout_type::fast_j ()) {
  3209. if (it == index2_data_.begin () + zero_based (*itv))
  3210. return const_iterator2 (*this, rank, i, j, itv, it);
  3211. j = zero_based (*(it - 1));
  3212. } else {
  3213. if (j == 0)
  3214. return const_iterator2 (*this, rank, i, j, itv, it);
  3215. -- j;
  3216. }
  3217. }
  3218. }
  3219. }
  3220. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  3221. iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) {
  3222. for (;;) {
  3223. array_size_type address1 (layout_type::index_M (i, j));
  3224. array_size_type address2 (layout_type::index_m (i, j));
  3225. vector_subiterator_type itv (index1_data_.begin () + (std::min) (filled1_ - 1, address1));
  3226. if (filled1_ <= address1 + 1)
  3227. return iterator2 (*this, rank, i, j, itv, index2_data_.begin () + filled2_);
  3228. subiterator_type it_begin (index2_data_.begin () + zero_based (*itv));
  3229. subiterator_type it_end (index2_data_.begin () + zero_based (*(itv + 1)));
  3230. subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  3231. if (rank == 0)
  3232. return iterator2 (*this, rank, i, j, itv, it);
  3233. if (it != it_end && zero_based (*it) == address2)
  3234. return iterator2 (*this, rank, i, j, itv, it);
  3235. if (direction > 0) {
  3236. if (layout_type::fast_j ()) {
  3237. if (it == it_end)
  3238. return iterator2 (*this, rank, i, j, itv, it);
  3239. j = zero_based (*it);
  3240. } else {
  3241. if (j >= size2_)
  3242. return iterator2 (*this, rank, i, j, itv, it);
  3243. ++ j;
  3244. }
  3245. } else /* if (direction < 0) */ {
  3246. if (layout_type::fast_j ()) {
  3247. if (it == index2_data_.begin () + zero_based (*itv))
  3248. return iterator2 (*this, rank, i, j, itv, it);
  3249. j = zero_based (*(it - 1));
  3250. } else {
  3251. if (j == 0)
  3252. return iterator2 (*this, rank, i, j, itv, it);
  3253. -- j;
  3254. }
  3255. }
  3256. }
  3257. }
  3258. class const_iterator1:
  3259. public container_const_reference<compressed_matrix>,
  3260. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3261. const_iterator1, value_type> {
  3262. public:
  3263. typedef typename compressed_matrix::value_type value_type;
  3264. typedef typename compressed_matrix::difference_type difference_type;
  3265. typedef typename compressed_matrix::const_reference reference;
  3266. typedef const typename compressed_matrix::pointer pointer;
  3267. typedef const_iterator2 dual_iterator_type;
  3268. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  3269. // Construction and destruction
  3270. BOOST_UBLAS_INLINE
  3271. const_iterator1 ():
  3272. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  3273. BOOST_UBLAS_INLINE
  3274. const_iterator1 (const self_type &m, int rank, size_type i, size_type j, const vector_const_subiterator_type &itv, const const_subiterator_type &it):
  3275. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  3276. BOOST_UBLAS_INLINE
  3277. const_iterator1 (const iterator1 &it):
  3278. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), itv_ (it.itv_), it_ (it.it_) {}
  3279. // Arithmetic
  3280. BOOST_UBLAS_INLINE
  3281. const_iterator1 &operator ++ () {
  3282. if (rank_ == 1 && layout_type::fast_i ())
  3283. ++ it_;
  3284. else {
  3285. i_ = index1 () + 1;
  3286. if (rank_ == 1)
  3287. *this = (*this) ().find1 (rank_, i_, j_, 1);
  3288. }
  3289. return *this;
  3290. }
  3291. BOOST_UBLAS_INLINE
  3292. const_iterator1 &operator -- () {
  3293. if (rank_ == 1 && layout_type::fast_i ())
  3294. -- it_;
  3295. else {
  3296. --i_;
  3297. if (rank_ == 1)
  3298. *this = (*this) ().find1 (rank_, i_, j_, -1);
  3299. }
  3300. return *this;
  3301. }
  3302. // Dereference
  3303. BOOST_UBLAS_INLINE
  3304. const_reference operator * () const {
  3305. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3306. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3307. if (rank_ == 1) {
  3308. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  3309. } else {
  3310. return (*this) () (i_, j_);
  3311. }
  3312. }
  3313. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3314. BOOST_UBLAS_INLINE
  3315. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3316. typename self_type::
  3317. #endif
  3318. const_iterator2 begin () const {
  3319. const self_type &m = (*this) ();
  3320. return m.find2 (1, index1 (), 0);
  3321. }
  3322. BOOST_UBLAS_INLINE
  3323. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3324. typename self_type::
  3325. #endif
  3326. const_iterator2 cbegin () const {
  3327. return begin ();
  3328. }
  3329. BOOST_UBLAS_INLINE
  3330. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3331. typename self_type::
  3332. #endif
  3333. const_iterator2 end () const {
  3334. const self_type &m = (*this) ();
  3335. return m.find2 (1, index1 (), m.size2 ());
  3336. }
  3337. BOOST_UBLAS_INLINE
  3338. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3339. typename self_type::
  3340. #endif
  3341. const_iterator2 cend () const {
  3342. return end ();
  3343. }
  3344. BOOST_UBLAS_INLINE
  3345. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3346. typename self_type::
  3347. #endif
  3348. const_reverse_iterator2 rbegin () const {
  3349. return const_reverse_iterator2 (end ());
  3350. }
  3351. BOOST_UBLAS_INLINE
  3352. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3353. typename self_type::
  3354. #endif
  3355. const_reverse_iterator2 crbegin () const {
  3356. return rbegin ();
  3357. }
  3358. BOOST_UBLAS_INLINE
  3359. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3360. typename self_type::
  3361. #endif
  3362. const_reverse_iterator2 rend () const {
  3363. return const_reverse_iterator2 (begin ());
  3364. }
  3365. BOOST_UBLAS_INLINE
  3366. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3367. typename self_type::
  3368. #endif
  3369. const_reverse_iterator2 crend () const {
  3370. return rend ();
  3371. }
  3372. #endif
  3373. // Indices
  3374. BOOST_UBLAS_INLINE
  3375. size_type index1 () const {
  3376. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  3377. if (rank_ == 1) {
  3378. BOOST_UBLAS_CHECK (layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  3379. return layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3380. } else {
  3381. return i_;
  3382. }
  3383. }
  3384. BOOST_UBLAS_INLINE
  3385. size_type index2 () const {
  3386. if (rank_ == 1) {
  3387. BOOST_UBLAS_CHECK (layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  3388. return layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3389. } else {
  3390. return j_;
  3391. }
  3392. }
  3393. // Assignment
  3394. BOOST_UBLAS_INLINE
  3395. const_iterator1 &operator = (const const_iterator1 &it) {
  3396. container_const_reference<self_type>::assign (&it ());
  3397. rank_ = it.rank_;
  3398. i_ = it.i_;
  3399. j_ = it.j_;
  3400. itv_ = it.itv_;
  3401. it_ = it.it_;
  3402. return *this;
  3403. }
  3404. // Comparison
  3405. BOOST_UBLAS_INLINE
  3406. bool operator == (const const_iterator1 &it) const {
  3407. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3408. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  3409. if (rank_ == 1 || it.rank_ == 1) {
  3410. return it_ == it.it_;
  3411. } else {
  3412. return i_ == it.i_ && j_ == it.j_;
  3413. }
  3414. }
  3415. private:
  3416. int rank_;
  3417. size_type i_;
  3418. size_type j_;
  3419. vector_const_subiterator_type itv_;
  3420. const_subiterator_type it_;
  3421. };
  3422. BOOST_UBLAS_INLINE
  3423. const_iterator1 begin1 () const {
  3424. return find1 (0, 0, 0);
  3425. }
  3426. BOOST_UBLAS_INLINE
  3427. const_iterator1 cbegin1 () const {
  3428. return begin1 ();
  3429. }
  3430. BOOST_UBLAS_INLINE
  3431. const_iterator1 end1 () const {
  3432. return find1 (0, size1_, 0);
  3433. }
  3434. BOOST_UBLAS_INLINE
  3435. const_iterator1 cend1 () const {
  3436. return end1 ();
  3437. }
  3438. class iterator1:
  3439. public container_reference<compressed_matrix>,
  3440. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3441. iterator1, value_type> {
  3442. public:
  3443. typedef typename compressed_matrix::value_type value_type;
  3444. typedef typename compressed_matrix::difference_type difference_type;
  3445. typedef typename compressed_matrix::true_reference reference;
  3446. typedef typename compressed_matrix::pointer pointer;
  3447. typedef iterator2 dual_iterator_type;
  3448. typedef reverse_iterator2 dual_reverse_iterator_type;
  3449. // Construction and destruction
  3450. BOOST_UBLAS_INLINE
  3451. iterator1 ():
  3452. container_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  3453. BOOST_UBLAS_INLINE
  3454. iterator1 (self_type &m, int rank, size_type i, size_type j, const vector_subiterator_type &itv, const subiterator_type &it):
  3455. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  3456. // Arithmetic
  3457. BOOST_UBLAS_INLINE
  3458. iterator1 &operator ++ () {
  3459. if (rank_ == 1 && layout_type::fast_i ())
  3460. ++ it_;
  3461. else {
  3462. i_ = index1 () + 1;
  3463. if (rank_ == 1)
  3464. *this = (*this) ().find1 (rank_, i_, j_, 1);
  3465. }
  3466. return *this;
  3467. }
  3468. BOOST_UBLAS_INLINE
  3469. iterator1 &operator -- () {
  3470. if (rank_ == 1 && layout_type::fast_i ())
  3471. -- it_;
  3472. else {
  3473. --i_;
  3474. if (rank_ == 1)
  3475. *this = (*this) ().find1 (rank_, i_, j_, -1);
  3476. }
  3477. return *this;
  3478. }
  3479. // Dereference
  3480. BOOST_UBLAS_INLINE
  3481. reference operator * () const {
  3482. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3483. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3484. if (rank_ == 1) {
  3485. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  3486. } else {
  3487. return (*this) ().at_element (i_, j_);
  3488. }
  3489. }
  3490. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3491. BOOST_UBLAS_INLINE
  3492. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3493. typename self_type::
  3494. #endif
  3495. iterator2 begin () const {
  3496. self_type &m = (*this) ();
  3497. return m.find2 (1, index1 (), 0);
  3498. }
  3499. BOOST_UBLAS_INLINE
  3500. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3501. typename self_type::
  3502. #endif
  3503. iterator2 end () const {
  3504. self_type &m = (*this) ();
  3505. return m.find2 (1, index1 (), m.size2 ());
  3506. }
  3507. BOOST_UBLAS_INLINE
  3508. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3509. typename self_type::
  3510. #endif
  3511. reverse_iterator2 rbegin () const {
  3512. return reverse_iterator2 (end ());
  3513. }
  3514. BOOST_UBLAS_INLINE
  3515. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3516. typename self_type::
  3517. #endif
  3518. reverse_iterator2 rend () const {
  3519. return reverse_iterator2 (begin ());
  3520. }
  3521. #endif
  3522. // Indices
  3523. BOOST_UBLAS_INLINE
  3524. size_type index1 () const {
  3525. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  3526. if (rank_ == 1) {
  3527. BOOST_UBLAS_CHECK (layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  3528. return layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3529. } else {
  3530. return i_;
  3531. }
  3532. }
  3533. BOOST_UBLAS_INLINE
  3534. size_type index2 () const {
  3535. if (rank_ == 1) {
  3536. BOOST_UBLAS_CHECK (layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  3537. return layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3538. } else {
  3539. return j_;
  3540. }
  3541. }
  3542. // Assignment
  3543. BOOST_UBLAS_INLINE
  3544. iterator1 &operator = (const iterator1 &it) {
  3545. container_reference<self_type>::assign (&it ());
  3546. rank_ = it.rank_;
  3547. i_ = it.i_;
  3548. j_ = it.j_;
  3549. itv_ = it.itv_;
  3550. it_ = it.it_;
  3551. return *this;
  3552. }
  3553. // Comparison
  3554. BOOST_UBLAS_INLINE
  3555. bool operator == (const iterator1 &it) const {
  3556. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3557. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  3558. if (rank_ == 1 || it.rank_ == 1) {
  3559. return it_ == it.it_;
  3560. } else {
  3561. return i_ == it.i_ && j_ == it.j_;
  3562. }
  3563. }
  3564. private:
  3565. int rank_;
  3566. size_type i_;
  3567. size_type j_;
  3568. vector_subiterator_type itv_;
  3569. subiterator_type it_;
  3570. friend class const_iterator1;
  3571. };
  3572. BOOST_UBLAS_INLINE
  3573. iterator1 begin1 () {
  3574. return find1 (0, 0, 0);
  3575. }
  3576. BOOST_UBLAS_INLINE
  3577. iterator1 end1 () {
  3578. return find1 (0, size1_, 0);
  3579. }
  3580. class const_iterator2:
  3581. public container_const_reference<compressed_matrix>,
  3582. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3583. const_iterator2, value_type> {
  3584. public:
  3585. typedef typename compressed_matrix::value_type value_type;
  3586. typedef typename compressed_matrix::difference_type difference_type;
  3587. typedef typename compressed_matrix::const_reference reference;
  3588. typedef const typename compressed_matrix::pointer pointer;
  3589. typedef const_iterator1 dual_iterator_type;
  3590. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  3591. // Construction and destruction
  3592. BOOST_UBLAS_INLINE
  3593. const_iterator2 ():
  3594. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  3595. BOOST_UBLAS_INLINE
  3596. const_iterator2 (const self_type &m, int rank, size_type i, size_type j, const vector_const_subiterator_type itv, const const_subiterator_type &it):
  3597. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  3598. BOOST_UBLAS_INLINE
  3599. const_iterator2 (const iterator2 &it):
  3600. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), itv_ (it.itv_), it_ (it.it_) {}
  3601. // Arithmetic
  3602. BOOST_UBLAS_INLINE
  3603. const_iterator2 &operator ++ () {
  3604. if (rank_ == 1 && layout_type::fast_j ())
  3605. ++ it_;
  3606. else {
  3607. j_ = index2 () + 1;
  3608. if (rank_ == 1)
  3609. *this = (*this) ().find2 (rank_, i_, j_, 1);
  3610. }
  3611. return *this;
  3612. }
  3613. BOOST_UBLAS_INLINE
  3614. const_iterator2 &operator -- () {
  3615. if (rank_ == 1 && layout_type::fast_j ())
  3616. -- it_;
  3617. else {
  3618. --j_;
  3619. if (rank_ == 1)
  3620. *this = (*this) ().find2 (rank_, i_, j_, -1);
  3621. }
  3622. return *this;
  3623. }
  3624. // Dereference
  3625. BOOST_UBLAS_INLINE
  3626. const_reference operator * () const {
  3627. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3628. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3629. if (rank_ == 1) {
  3630. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  3631. } else {
  3632. return (*this) () (i_, j_);
  3633. }
  3634. }
  3635. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3636. BOOST_UBLAS_INLINE
  3637. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3638. typename self_type::
  3639. #endif
  3640. const_iterator1 begin () const {
  3641. const self_type &m = (*this) ();
  3642. return m.find1 (1, 0, index2 ());
  3643. }
  3644. BOOST_UBLAS_INLINE
  3645. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3646. typename self_type::
  3647. #endif
  3648. const_iterator1 cbegin () const {
  3649. return begin ();
  3650. }
  3651. BOOST_UBLAS_INLINE
  3652. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3653. typename self_type::
  3654. #endif
  3655. const_iterator1 end () const {
  3656. const self_type &m = (*this) ();
  3657. return m.find1 (1, m.size1 (), index2 ());
  3658. }
  3659. BOOST_UBLAS_INLINE
  3660. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3661. typename self_type::
  3662. #endif
  3663. const_iterator1 cend () const {
  3664. return end ();
  3665. }
  3666. BOOST_UBLAS_INLINE
  3667. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3668. typename self_type::
  3669. #endif
  3670. const_reverse_iterator1 rbegin () const {
  3671. return const_reverse_iterator1 (end ());
  3672. }
  3673. BOOST_UBLAS_INLINE
  3674. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3675. typename self_type::
  3676. #endif
  3677. const_reverse_iterator1 crbegin () const {
  3678. return rbegin ();
  3679. }
  3680. BOOST_UBLAS_INLINE
  3681. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3682. typename self_type::
  3683. #endif
  3684. const_reverse_iterator1 rend () const {
  3685. return const_reverse_iterator1 (begin ());
  3686. }
  3687. BOOST_UBLAS_INLINE
  3688. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3689. typename self_type::
  3690. #endif
  3691. const_reverse_iterator1 crend () const {
  3692. return rend ();
  3693. }
  3694. #endif
  3695. // Indices
  3696. BOOST_UBLAS_INLINE
  3697. size_type index1 () const {
  3698. if (rank_ == 1) {
  3699. BOOST_UBLAS_CHECK (layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  3700. return layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3701. } else {
  3702. return i_;
  3703. }
  3704. }
  3705. BOOST_UBLAS_INLINE
  3706. size_type index2 () const {
  3707. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  3708. if (rank_ == 1) {
  3709. BOOST_UBLAS_CHECK (layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  3710. return layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3711. } else {
  3712. return j_;
  3713. }
  3714. }
  3715. // Assignment
  3716. BOOST_UBLAS_INLINE
  3717. const_iterator2 &operator = (const const_iterator2 &it) {
  3718. container_const_reference<self_type>::assign (&it ());
  3719. rank_ = it.rank_;
  3720. i_ = it.i_;
  3721. j_ = it.j_;
  3722. itv_ = it.itv_;
  3723. it_ = it.it_;
  3724. return *this;
  3725. }
  3726. // Comparison
  3727. BOOST_UBLAS_INLINE
  3728. bool operator == (const const_iterator2 &it) const {
  3729. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3730. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  3731. if (rank_ == 1 || it.rank_ == 1) {
  3732. return it_ == it.it_;
  3733. } else {
  3734. return i_ == it.i_ && j_ == it.j_;
  3735. }
  3736. }
  3737. private:
  3738. int rank_;
  3739. size_type i_;
  3740. size_type j_;
  3741. vector_const_subiterator_type itv_;
  3742. const_subiterator_type it_;
  3743. };
  3744. BOOST_UBLAS_INLINE
  3745. const_iterator2 begin2 () const {
  3746. return find2 (0, 0, 0);
  3747. }
  3748. BOOST_UBLAS_INLINE
  3749. const_iterator2 cbegin2 () const {
  3750. return begin2 ();
  3751. }
  3752. BOOST_UBLAS_INLINE
  3753. const_iterator2 end2 () const {
  3754. return find2 (0, 0, size2_);
  3755. }
  3756. BOOST_UBLAS_INLINE
  3757. const_iterator2 cend2 () const {
  3758. return end2 ();
  3759. }
  3760. class iterator2:
  3761. public container_reference<compressed_matrix>,
  3762. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3763. iterator2, value_type> {
  3764. public:
  3765. typedef typename compressed_matrix::value_type value_type;
  3766. typedef typename compressed_matrix::difference_type difference_type;
  3767. typedef typename compressed_matrix::true_reference reference;
  3768. typedef typename compressed_matrix::pointer pointer;
  3769. typedef iterator1 dual_iterator_type;
  3770. typedef reverse_iterator1 dual_reverse_iterator_type;
  3771. // Construction and destruction
  3772. BOOST_UBLAS_INLINE
  3773. iterator2 ():
  3774. container_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  3775. BOOST_UBLAS_INLINE
  3776. iterator2 (self_type &m, int rank, size_type i, size_type j, const vector_subiterator_type &itv, const subiterator_type &it):
  3777. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  3778. // Arithmetic
  3779. BOOST_UBLAS_INLINE
  3780. iterator2 &operator ++ () {
  3781. if (rank_ == 1 && layout_type::fast_j ())
  3782. ++ it_;
  3783. else {
  3784. j_ = index2 () + 1;
  3785. if (rank_ == 1)
  3786. *this = (*this) ().find2 (rank_, i_, j_, 1);
  3787. }
  3788. return *this;
  3789. }
  3790. BOOST_UBLAS_INLINE
  3791. iterator2 &operator -- () {
  3792. if (rank_ == 1 && layout_type::fast_j ())
  3793. -- it_;
  3794. else {
  3795. --j_;
  3796. if (rank_ == 1)
  3797. *this = (*this) ().find2 (rank_, i_, j_, -1);
  3798. }
  3799. return *this;
  3800. }
  3801. // Dereference
  3802. BOOST_UBLAS_INLINE
  3803. reference operator * () const {
  3804. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3805. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3806. if (rank_ == 1) {
  3807. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  3808. } else {
  3809. return (*this) ().at_element (i_, j_);
  3810. }
  3811. }
  3812. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3813. BOOST_UBLAS_INLINE
  3814. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3815. typename self_type::
  3816. #endif
  3817. iterator1 begin () const {
  3818. self_type &m = (*this) ();
  3819. return m.find1 (1, 0, index2 ());
  3820. }
  3821. BOOST_UBLAS_INLINE
  3822. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3823. typename self_type::
  3824. #endif
  3825. iterator1 end () const {
  3826. self_type &m = (*this) ();
  3827. return m.find1 (1, m.size1 (), index2 ());
  3828. }
  3829. BOOST_UBLAS_INLINE
  3830. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3831. typename self_type::
  3832. #endif
  3833. reverse_iterator1 rbegin () const {
  3834. return reverse_iterator1 (end ());
  3835. }
  3836. BOOST_UBLAS_INLINE
  3837. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3838. typename self_type::
  3839. #endif
  3840. reverse_iterator1 rend () const {
  3841. return reverse_iterator1 (begin ());
  3842. }
  3843. #endif
  3844. // Indices
  3845. BOOST_UBLAS_INLINE
  3846. size_type index1 () const {
  3847. if (rank_ == 1) {
  3848. BOOST_UBLAS_CHECK (layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  3849. return layout_type::index_M (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3850. } else {
  3851. return i_;
  3852. }
  3853. }
  3854. BOOST_UBLAS_INLINE
  3855. size_type index2 () const {
  3856. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  3857. if (rank_ == 1) {
  3858. BOOST_UBLAS_CHECK (layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  3859. return layout_type::index_m (itv_ - (*this) ().index1_data_.begin (), (*this) ().zero_based (*it_));
  3860. } else {
  3861. return j_;
  3862. }
  3863. }
  3864. // Assignment
  3865. BOOST_UBLAS_INLINE
  3866. iterator2 &operator = (const iterator2 &it) {
  3867. container_reference<self_type>::assign (&it ());
  3868. rank_ = it.rank_;
  3869. i_ = it.i_;
  3870. j_ = it.j_;
  3871. itv_ = it.itv_;
  3872. it_ = it.it_;
  3873. return *this;
  3874. }
  3875. // Comparison
  3876. BOOST_UBLAS_INLINE
  3877. bool operator == (const iterator2 &it) const {
  3878. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3879. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  3880. if (rank_ == 1 || it.rank_ == 1) {
  3881. return it_ == it.it_;
  3882. } else {
  3883. return i_ == it.i_ && j_ == it.j_;
  3884. }
  3885. }
  3886. private:
  3887. int rank_;
  3888. size_type i_;
  3889. size_type j_;
  3890. vector_subiterator_type itv_;
  3891. subiterator_type it_;
  3892. friend class const_iterator2;
  3893. };
  3894. BOOST_UBLAS_INLINE
  3895. iterator2 begin2 () {
  3896. return find2 (0, 0, 0);
  3897. }
  3898. BOOST_UBLAS_INLINE
  3899. iterator2 end2 () {
  3900. return find2 (0, 0, size2_);
  3901. }
  3902. // Reverse iterators
  3903. BOOST_UBLAS_INLINE
  3904. const_reverse_iterator1 rbegin1 () const {
  3905. return const_reverse_iterator1 (end1 ());
  3906. }
  3907. BOOST_UBLAS_INLINE
  3908. const_reverse_iterator1 crbegin1 () const {
  3909. return rbegin1 ();
  3910. }
  3911. BOOST_UBLAS_INLINE
  3912. const_reverse_iterator1 rend1 () const {
  3913. return const_reverse_iterator1 (begin1 ());
  3914. }
  3915. BOOST_UBLAS_INLINE
  3916. const_reverse_iterator1 crend1 () const {
  3917. return rend1 ();
  3918. }
  3919. BOOST_UBLAS_INLINE
  3920. reverse_iterator1 rbegin1 () {
  3921. return reverse_iterator1 (end1 ());
  3922. }
  3923. BOOST_UBLAS_INLINE
  3924. reverse_iterator1 rend1 () {
  3925. return reverse_iterator1 (begin1 ());
  3926. }
  3927. BOOST_UBLAS_INLINE
  3928. const_reverse_iterator2 rbegin2 () const {
  3929. return const_reverse_iterator2 (end2 ());
  3930. }
  3931. BOOST_UBLAS_INLINE
  3932. const_reverse_iterator2 crbegin2 () const {
  3933. return rbegin2 ();
  3934. }
  3935. BOOST_UBLAS_INLINE
  3936. const_reverse_iterator2 rend2 () const {
  3937. return const_reverse_iterator2 (begin2 ());
  3938. }
  3939. BOOST_UBLAS_INLINE
  3940. const_reverse_iterator2 crend2 () const {
  3941. return rend2 ();
  3942. }
  3943. BOOST_UBLAS_INLINE
  3944. reverse_iterator2 rbegin2 () {
  3945. return reverse_iterator2 (end2 ());
  3946. }
  3947. BOOST_UBLAS_INLINE
  3948. reverse_iterator2 rend2 () {
  3949. return reverse_iterator2 (begin2 ());
  3950. }
  3951. // Serialization
  3952. template<class Archive>
  3953. void serialize(Archive & ar, const unsigned int /* file_version */){
  3954. serialization::collection_size_type s1 (size1_);
  3955. serialization::collection_size_type s2 (size2_);
  3956. ar & serialization::make_nvp("size1",s1);
  3957. ar & serialization::make_nvp("size2",s2);
  3958. if (Archive::is_loading::value) {
  3959. size1_ = s1;
  3960. size2_ = s2;
  3961. }
  3962. ar & serialization::make_nvp("capacity", capacity_);
  3963. ar & serialization::make_nvp("filled1", filled1_);
  3964. ar & serialization::make_nvp("filled2", filled2_);
  3965. ar & serialization::make_nvp("index1_data", index1_data_);
  3966. ar & serialization::make_nvp("index2_data", index2_data_);
  3967. ar & serialization::make_nvp("value_data", value_data_);
  3968. storage_invariants();
  3969. }
  3970. private:
  3971. void storage_invariants () const {
  3972. BOOST_UBLAS_CHECK (layout_type::size_M (size1_, size2_) + 1 == index1_data_.size (), internal_logic ());
  3973. BOOST_UBLAS_CHECK (capacity_ == index2_data_.size (), internal_logic ());
  3974. BOOST_UBLAS_CHECK (capacity_ == value_data_.size (), internal_logic ());
  3975. BOOST_UBLAS_CHECK (filled1_ > 0 && filled1_ <= layout_type::size_M (size1_, size2_) + 1, internal_logic ());
  3976. BOOST_UBLAS_CHECK (filled2_ <= capacity_, internal_logic ());
  3977. BOOST_UBLAS_CHECK (index1_data_ [filled1_ - 1] == k_based (filled2_), internal_logic ());
  3978. }
  3979. size_type size1_;
  3980. size_type size2_;
  3981. array_size_type capacity_;
  3982. array_size_type filled1_;
  3983. array_size_type filled2_;
  3984. index_array_type index1_data_;
  3985. index_array_type index2_data_;
  3986. value_array_type value_data_;
  3987. static const value_type zero_;
  3988. BOOST_UBLAS_INLINE
  3989. static size_type zero_based (size_type k_based_index) {
  3990. return k_based_index - IB;
  3991. }
  3992. BOOST_UBLAS_INLINE
  3993. static size_type k_based (size_type zero_based_index) {
  3994. return zero_based_index + IB;
  3995. }
  3996. friend class iterator1;
  3997. friend class iterator2;
  3998. friend class const_iterator1;
  3999. friend class const_iterator2;
  4000. };
  4001. template<class T, class L, std::size_t IB, class IA, class TA>
  4002. const typename compressed_matrix<T, L, IB, IA, TA>::value_type compressed_matrix<T, L, IB, IA, TA>::zero_ = value_type/*zero*/();
  4003. // Coordinate array based sparse matrix class
  4004. // Thanks to Kresimir Fresl for extending this to cover different index bases.
  4005. template<class T, class L, std::size_t IB, class IA, class TA>
  4006. class coordinate_matrix:
  4007. public matrix_container<coordinate_matrix<T, L, IB, IA, TA> > {
  4008. typedef T &true_reference;
  4009. typedef T *pointer;
  4010. typedef const T *const_pointer;
  4011. typedef L layout_type;
  4012. typedef coordinate_matrix<T, L, IB, IA, TA> self_type;
  4013. public:
  4014. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  4015. using matrix_container<self_type>::operator ();
  4016. #endif
  4017. // ISSUE require type consistency check, is_convertable (IA::size_type, TA::size_type)
  4018. typedef typename IA::value_type size_type;
  4019. // ISSUE difference_type cannot be deduced for sparse indices, we only know the value_type
  4020. typedef std::ptrdiff_t difference_type;
  4021. // size_type for the data arrays.
  4022. typedef typename IA::size_type array_size_type;
  4023. typedef T value_type;
  4024. typedef const T &const_reference;
  4025. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  4026. typedef T &reference;
  4027. #else
  4028. typedef sparse_matrix_element<self_type> reference;
  4029. #endif
  4030. typedef IA index_array_type;
  4031. typedef TA value_array_type;
  4032. typedef const matrix_reference<const self_type> const_closure_type;
  4033. typedef matrix_reference<self_type> closure_type;
  4034. typedef coordinate_vector<T, IB, IA, TA> vector_temporary_type;
  4035. typedef self_type matrix_temporary_type;
  4036. typedef sparse_tag storage_category;
  4037. typedef typename L::orientation_category orientation_category;
  4038. // Construction and destruction
  4039. BOOST_UBLAS_INLINE
  4040. coordinate_matrix ():
  4041. matrix_container<self_type> (),
  4042. size1_ (0), size2_ (0), capacity_ (restrict_capacity (0)),
  4043. filled_ (0), sorted_filled_ (filled_), sorted_ (true),
  4044. index1_data_ (capacity_), index2_data_ (capacity_), value_data_ (capacity_) {
  4045. storage_invariants ();
  4046. }
  4047. BOOST_UBLAS_INLINE
  4048. coordinate_matrix (size_type size1, size_type size2, array_size_type non_zeros = 0):
  4049. matrix_container<self_type> (),
  4050. size1_ (size1), size2_ (size2), capacity_ (restrict_capacity (non_zeros)),
  4051. filled_ (0), sorted_filled_ (filled_), sorted_ (true),
  4052. index1_data_ (capacity_), index2_data_ (capacity_), value_data_ (capacity_) {
  4053. storage_invariants ();
  4054. }
  4055. BOOST_UBLAS_INLINE
  4056. coordinate_matrix (const coordinate_matrix &m):
  4057. matrix_container<self_type> (),
  4058. size1_ (m.size1_), size2_ (m.size2_), capacity_ (m.capacity_),
  4059. filled_ (m.filled_), sorted_filled_ (m.sorted_filled_), sorted_ (m.sorted_),
  4060. index1_data_ (m.index1_data_), index2_data_ (m.index2_data_), value_data_ (m.value_data_) {
  4061. storage_invariants ();
  4062. }
  4063. template<class AE>
  4064. BOOST_UBLAS_INLINE
  4065. coordinate_matrix (const matrix_expression<AE> &ae, array_size_type non_zeros = 0):
  4066. matrix_container<self_type> (),
  4067. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), capacity_ (restrict_capacity (non_zeros)),
  4068. filled_ (0), sorted_filled_ (filled_), sorted_ (true),
  4069. index1_data_ (capacity_), index2_data_ (capacity_), value_data_ (capacity_) {
  4070. storage_invariants ();
  4071. matrix_assign<scalar_assign> (*this, ae);
  4072. }
  4073. // Accessors
  4074. BOOST_UBLAS_INLINE
  4075. size_type size1 () const {
  4076. return size1_;
  4077. }
  4078. BOOST_UBLAS_INLINE
  4079. size_type size2 () const {
  4080. return size2_;
  4081. }
  4082. BOOST_UBLAS_INLINE
  4083. size_type nnz_capacity () const {
  4084. return capacity_;
  4085. }
  4086. BOOST_UBLAS_INLINE
  4087. size_type nnz () const {
  4088. return filled_;
  4089. }
  4090. // Storage accessors
  4091. BOOST_UBLAS_INLINE
  4092. static size_type index_base () {
  4093. return IB;
  4094. }
  4095. BOOST_UBLAS_INLINE
  4096. array_size_type filled () const {
  4097. return filled_;
  4098. }
  4099. BOOST_UBLAS_INLINE
  4100. const index_array_type &index1_data () const {
  4101. return index1_data_;
  4102. }
  4103. BOOST_UBLAS_INLINE
  4104. const index_array_type &index2_data () const {
  4105. return index2_data_;
  4106. }
  4107. BOOST_UBLAS_INLINE
  4108. const value_array_type &value_data () const {
  4109. return value_data_;
  4110. }
  4111. BOOST_UBLAS_INLINE
  4112. void set_filled (const array_size_type &filled) {
  4113. // Make sure that storage_invariants() succeeds
  4114. if (sorted_ && filled < filled_)
  4115. sorted_filled_ = filled;
  4116. else
  4117. sorted_ = (sorted_filled_ == filled);
  4118. filled_ = filled;
  4119. storage_invariants ();
  4120. }
  4121. BOOST_UBLAS_INLINE
  4122. index_array_type &index1_data () {
  4123. return index1_data_;
  4124. }
  4125. BOOST_UBLAS_INLINE
  4126. index_array_type &index2_data () {
  4127. return index2_data_;
  4128. }
  4129. BOOST_UBLAS_INLINE
  4130. value_array_type &value_data () {
  4131. return value_data_;
  4132. }
  4133. // Resizing
  4134. private:
  4135. BOOST_UBLAS_INLINE
  4136. array_size_type restrict_capacity (array_size_type non_zeros) const {
  4137. // minimum non_zeros
  4138. non_zeros = (std::max) (non_zeros, array_size_type((std::min) (size1_, size2_)));
  4139. // ISSUE no maximum as coordinate may contain inserted duplicates
  4140. return non_zeros;
  4141. }
  4142. public:
  4143. BOOST_UBLAS_INLINE
  4144. void resize (size_type size1, size_type size2, bool preserve = true) {
  4145. // FIXME preserve unimplemented
  4146. BOOST_UBLAS_CHECK (!preserve, internal_logic ());
  4147. size1_ = size1;
  4148. size2_ = size2;
  4149. capacity_ = restrict_capacity (capacity_);
  4150. index1_data_.resize (capacity_);
  4151. index2_data_.resize (capacity_);
  4152. value_data_.resize (capacity_);
  4153. filled_ = 0;
  4154. sorted_filled_ = filled_;
  4155. sorted_ = true;
  4156. storage_invariants ();
  4157. }
  4158. // Reserving
  4159. BOOST_UBLAS_INLINE
  4160. void reserve (array_size_type non_zeros, bool preserve = true) {
  4161. sort (); // remove duplicate elements
  4162. capacity_ = restrict_capacity (non_zeros);
  4163. if (preserve) {
  4164. index1_data_.resize (capacity_, size_type ());
  4165. index2_data_.resize (capacity_, size_type ());
  4166. value_data_.resize (capacity_, value_type ());
  4167. filled_ = (std::min) (capacity_, filled_);
  4168. }
  4169. else {
  4170. index1_data_.resize (capacity_);
  4171. index2_data_.resize (capacity_);
  4172. value_data_.resize (capacity_);
  4173. filled_ = 0;
  4174. }
  4175. sorted_filled_ = filled_;
  4176. storage_invariants ();
  4177. }
  4178. // Element support
  4179. BOOST_UBLAS_INLINE
  4180. pointer find_element (size_type i, size_type j) {
  4181. return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i, j));
  4182. }
  4183. BOOST_UBLAS_INLINE
  4184. const_pointer find_element (size_type i, size_type j) const {
  4185. sort ();
  4186. size_type element1 (layout_type::index_M (i, j));
  4187. size_type element2 (layout_type::index_m (i, j));
  4188. vector_const_subiterator_type itv_begin (detail::lower_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (element1), std::less<size_type> ()));
  4189. vector_const_subiterator_type itv_end (detail::upper_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (element1), std::less<size_type> ()));
  4190. if (itv_begin == itv_end)
  4191. return 0;
  4192. const_subiterator_type it_begin (index2_data_.begin () + (itv_begin - index1_data_.begin ()));
  4193. const_subiterator_type it_end (index2_data_.begin () + (itv_end - index1_data_.begin ()));
  4194. const_subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (element2), std::less<size_type> ()));
  4195. if (it == it_end || *it != k_based (element2))
  4196. return 0;
  4197. return &value_data_ [it - index2_data_.begin ()];
  4198. }
  4199. // Element access
  4200. BOOST_UBLAS_INLINE
  4201. const_reference operator () (size_type i, size_type j) const {
  4202. const_pointer p = find_element (i, j);
  4203. if (p)
  4204. return *p;
  4205. else
  4206. return zero_;
  4207. }
  4208. BOOST_UBLAS_INLINE
  4209. reference operator () (size_type i, size_type j) {
  4210. #ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
  4211. pointer p = find_element (i, j);
  4212. if (p)
  4213. return *p;
  4214. else
  4215. return insert_element (i, j, value_type/*zero*/());
  4216. #else
  4217. return reference (*this, i, j);
  4218. #endif
  4219. }
  4220. // Element assignment
  4221. BOOST_UBLAS_INLINE
  4222. void append_element (size_type i, size_type j, const_reference t) {
  4223. if (filled_ >= capacity_)
  4224. reserve (2 * filled_, true);
  4225. BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
  4226. size_type element1 = layout_type::index_M (i, j);
  4227. size_type element2 = layout_type::index_m (i, j);
  4228. index1_data_ [filled_] = k_based (element1);
  4229. index2_data_ [filled_] = k_based (element2);
  4230. value_data_ [filled_] = t;
  4231. ++ filled_;
  4232. sorted_ = false;
  4233. storage_invariants ();
  4234. }
  4235. BOOST_UBLAS_INLINE
  4236. true_reference insert_element (size_type i, size_type j, const_reference t) {
  4237. BOOST_UBLAS_CHECK (!find_element (i, j), bad_index ()); // duplicate element
  4238. append_element (i, j, t);
  4239. return value_data_ [filled_ - 1];
  4240. }
  4241. BOOST_UBLAS_INLINE
  4242. void erase_element (size_type i, size_type j) {
  4243. size_type element1 = layout_type::index_M (i, j);
  4244. size_type element2 = layout_type::index_m (i, j);
  4245. sort ();
  4246. vector_subiterator_type itv_begin (detail::lower_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (element1), std::less<size_type> ()));
  4247. vector_subiterator_type itv_end (detail::upper_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (element1), std::less<size_type> ()));
  4248. subiterator_type it_begin (index2_data_.begin () + (itv_begin - index1_data_.begin ()));
  4249. subiterator_type it_end (index2_data_.begin () + (itv_end - index1_data_.begin ()));
  4250. subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (element2), std::less<size_type> ()));
  4251. if (it != it_end && *it == k_based (element2)) {
  4252. typename std::iterator_traits<subiterator_type>::difference_type n = it - index2_data_.begin ();
  4253. vector_subiterator_type itv (index1_data_.begin () + n);
  4254. std::copy (itv + 1, index1_data_.begin () + filled_, itv);
  4255. std::copy (it + 1, index2_data_.begin () + filled_, it);
  4256. typename value_array_type::iterator itt (value_data_.begin () + n);
  4257. std::copy (itt + 1, value_data_.begin () + filled_, itt);
  4258. -- filled_;
  4259. sorted_filled_ = filled_;
  4260. }
  4261. storage_invariants ();
  4262. }
  4263. // Zeroing
  4264. BOOST_UBLAS_INLINE
  4265. void clear () {
  4266. filled_ = 0;
  4267. sorted_filled_ = filled_;
  4268. sorted_ = true;
  4269. storage_invariants ();
  4270. }
  4271. // Assignment
  4272. BOOST_UBLAS_INLINE
  4273. coordinate_matrix &operator = (const coordinate_matrix &m) {
  4274. if (this != &m) {
  4275. size1_ = m.size1_;
  4276. size2_ = m.size2_;
  4277. capacity_ = m.capacity_;
  4278. filled_ = m.filled_;
  4279. sorted_filled_ = m.sorted_filled_;
  4280. sorted_ = m.sorted_;
  4281. index1_data_ = m.index1_data_;
  4282. index2_data_ = m.index2_data_;
  4283. value_data_ = m.value_data_;
  4284. BOOST_UBLAS_CHECK (capacity_ == index1_data_.size (), internal_logic ());
  4285. BOOST_UBLAS_CHECK (capacity_ == index2_data_.size (), internal_logic ());
  4286. BOOST_UBLAS_CHECK (capacity_ == value_data_.size (), internal_logic ());
  4287. }
  4288. storage_invariants ();
  4289. return *this;
  4290. }
  4291. template<class C> // Container assignment without temporary
  4292. BOOST_UBLAS_INLINE
  4293. coordinate_matrix &operator = (const matrix_container<C> &m) {
  4294. resize (m ().size1 (), m ().size2 (), false);
  4295. assign (m);
  4296. return *this;
  4297. }
  4298. BOOST_UBLAS_INLINE
  4299. coordinate_matrix &assign_temporary (coordinate_matrix &m) {
  4300. swap (m);
  4301. return *this;
  4302. }
  4303. template<class AE>
  4304. BOOST_UBLAS_INLINE
  4305. coordinate_matrix &operator = (const matrix_expression<AE> &ae) {
  4306. self_type temporary (ae, capacity_);
  4307. return assign_temporary (temporary);
  4308. }
  4309. template<class AE>
  4310. BOOST_UBLAS_INLINE
  4311. coordinate_matrix &assign (const matrix_expression<AE> &ae) {
  4312. matrix_assign<scalar_assign> (*this, ae);
  4313. return *this;
  4314. }
  4315. template<class AE>
  4316. BOOST_UBLAS_INLINE
  4317. coordinate_matrix& operator += (const matrix_expression<AE> &ae) {
  4318. self_type temporary (*this + ae, capacity_);
  4319. return assign_temporary (temporary);
  4320. }
  4321. template<class C> // Container assignment without temporary
  4322. BOOST_UBLAS_INLINE
  4323. coordinate_matrix &operator += (const matrix_container<C> &m) {
  4324. plus_assign (m);
  4325. return *this;
  4326. }
  4327. template<class AE>
  4328. BOOST_UBLAS_INLINE
  4329. coordinate_matrix &plus_assign (const matrix_expression<AE> &ae) {
  4330. matrix_assign<scalar_plus_assign> (*this, ae);
  4331. return *this;
  4332. }
  4333. template<class AE>
  4334. BOOST_UBLAS_INLINE
  4335. coordinate_matrix& operator -= (const matrix_expression<AE> &ae) {
  4336. self_type temporary (*this - ae, capacity_);
  4337. return assign_temporary (temporary);
  4338. }
  4339. template<class C> // Container assignment without temporary
  4340. BOOST_UBLAS_INLINE
  4341. coordinate_matrix &operator -= (const matrix_container<C> &m) {
  4342. minus_assign (m);
  4343. return *this;
  4344. }
  4345. template<class AE>
  4346. BOOST_UBLAS_INLINE
  4347. coordinate_matrix &minus_assign (const matrix_expression<AE> &ae) {
  4348. matrix_assign<scalar_minus_assign> (*this, ae);
  4349. return *this;
  4350. }
  4351. template<class AT>
  4352. BOOST_UBLAS_INLINE
  4353. coordinate_matrix& operator *= (const AT &at) {
  4354. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  4355. return *this;
  4356. }
  4357. template<class AT>
  4358. BOOST_UBLAS_INLINE
  4359. coordinate_matrix& operator /= (const AT &at) {
  4360. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  4361. return *this;
  4362. }
  4363. // Swapping
  4364. BOOST_UBLAS_INLINE
  4365. void swap (coordinate_matrix &m) {
  4366. if (this != &m) {
  4367. std::swap (size1_, m.size1_);
  4368. std::swap (size2_, m.size2_);
  4369. std::swap (capacity_, m.capacity_);
  4370. std::swap (filled_, m.filled_);
  4371. std::swap (sorted_filled_, m.sorted_filled_);
  4372. std::swap (sorted_, m.sorted_);
  4373. index1_data_.swap (m.index1_data_);
  4374. index2_data_.swap (m.index2_data_);
  4375. value_data_.swap (m.value_data_);
  4376. }
  4377. storage_invariants ();
  4378. }
  4379. BOOST_UBLAS_INLINE
  4380. friend void swap (coordinate_matrix &m1, coordinate_matrix &m2) {
  4381. m1.swap (m2);
  4382. }
  4383. // replacement if STL lower bound algorithm for use of inplace_merge
  4384. array_size_type lower_bound (array_size_type beg, array_size_type end, array_size_type target) const {
  4385. while (end > beg) {
  4386. array_size_type mid = (beg + end) / 2;
  4387. if (((index1_data_[mid] < index1_data_[target]) ||
  4388. ((index1_data_[mid] == index1_data_[target]) &&
  4389. (index2_data_[mid] < index2_data_[target])))) {
  4390. beg = mid + 1;
  4391. } else {
  4392. end = mid;
  4393. }
  4394. }
  4395. return beg;
  4396. }
  4397. // specialized replacement of STL inplace_merge to avoid compilation
  4398. // problems with respect to the array_triple iterator
  4399. void inplace_merge (array_size_type beg, array_size_type mid, array_size_type end) const {
  4400. array_size_type len_lef = mid - beg;
  4401. array_size_type len_rig = end - mid;
  4402. if (len_lef == 1 && len_rig == 1) {
  4403. if ((index1_data_[mid] < index1_data_[beg]) ||
  4404. ((index1_data_[mid] == index1_data_[beg]) && (index2_data_[mid] < index2_data_[beg])))
  4405. {
  4406. std::swap(index1_data_[beg], index1_data_[mid]);
  4407. std::swap(index2_data_[beg], index2_data_[mid]);
  4408. std::swap(value_data_[beg], value_data_[mid]);
  4409. }
  4410. } else if (len_lef > 0 && len_rig > 0) {
  4411. array_size_type lef_mid, rig_mid;
  4412. if (len_lef >= len_rig) {
  4413. lef_mid = (beg + mid) / 2;
  4414. rig_mid = lower_bound(mid, end, lef_mid);
  4415. } else {
  4416. rig_mid = (mid + end) / 2;
  4417. lef_mid = lower_bound(beg, mid, rig_mid);
  4418. }
  4419. std::rotate(&index1_data_[0] + lef_mid, &index1_data_[0] + mid, &index1_data_[0] + rig_mid);
  4420. std::rotate(&index2_data_[0] + lef_mid, &index2_data_[0] + mid, &index2_data_[0] + rig_mid);
  4421. std::rotate(&value_data_[0] + lef_mid, &value_data_[0] + mid, &value_data_[0] + rig_mid);
  4422. array_size_type new_mid = lef_mid + rig_mid - mid;
  4423. inplace_merge(beg, lef_mid, new_mid);
  4424. inplace_merge(new_mid, rig_mid, end);
  4425. }
  4426. }
  4427. // Sorting and summation of duplicates
  4428. BOOST_UBLAS_INLINE
  4429. void sort () const {
  4430. if (! sorted_ && filled_ > 0) {
  4431. typedef index_triple_array<index_array_type, index_array_type, value_array_type> array_triple;
  4432. array_triple ita (filled_, index1_data_, index2_data_, value_data_);
  4433. #ifndef BOOST_UBLAS_COO_ALWAYS_DO_FULL_SORT
  4434. const typename array_triple::iterator iunsorted = ita.begin () + sorted_filled_;
  4435. // sort new elements and merge
  4436. std::sort (iunsorted, ita.end ());
  4437. inplace_merge(0, sorted_filled_, filled_);
  4438. #else
  4439. const typename array_triple::iterator iunsorted = ita.begin ();
  4440. std::sort (iunsorted, ita.end ());
  4441. #endif
  4442. // sum duplicates with += and remove
  4443. array_size_type filled = 0;
  4444. for (array_size_type i = 1; i < filled_; ++ i) {
  4445. if (index1_data_ [filled] != index1_data_ [i] ||
  4446. index2_data_ [filled] != index2_data_ [i]) {
  4447. ++ filled;
  4448. if (filled != i) {
  4449. index1_data_ [filled] = index1_data_ [i];
  4450. index2_data_ [filled] = index2_data_ [i];
  4451. value_data_ [filled] = value_data_ [i];
  4452. }
  4453. } else {
  4454. value_data_ [filled] += value_data_ [i];
  4455. }
  4456. }
  4457. filled_ = filled + 1;
  4458. sorted_filled_ = filled_;
  4459. sorted_ = true;
  4460. storage_invariants ();
  4461. }
  4462. }
  4463. // Back element insertion and erasure
  4464. BOOST_UBLAS_INLINE
  4465. void push_back (size_type i, size_type j, const_reference t) {
  4466. size_type element1 = layout_type::index_M (i, j);
  4467. size_type element2 = layout_type::index_m (i, j);
  4468. // must maintain sort order
  4469. BOOST_UBLAS_CHECK (sorted_ &&
  4470. (filled_ == 0 ||
  4471. index1_data_ [filled_ - 1] < k_based (element1) ||
  4472. (index1_data_ [filled_ - 1] == k_based (element1) && index2_data_ [filled_ - 1] < k_based (element2)))
  4473. , external_logic ());
  4474. if (filled_ >= capacity_)
  4475. reserve (2 * filled_, true);
  4476. BOOST_UBLAS_CHECK (filled_ < capacity_, internal_logic ());
  4477. index1_data_ [filled_] = k_based (element1);
  4478. index2_data_ [filled_] = k_based (element2);
  4479. value_data_ [filled_] = t;
  4480. ++ filled_;
  4481. sorted_filled_ = filled_;
  4482. storage_invariants ();
  4483. }
  4484. BOOST_UBLAS_INLINE
  4485. void pop_back () {
  4486. // ISSUE invariants could be simpilfied if sorted required as precondition
  4487. BOOST_UBLAS_CHECK (filled_ > 0, external_logic ());
  4488. -- filled_;
  4489. sorted_filled_ = (std::min) (sorted_filled_, filled_);
  4490. sorted_ = sorted_filled_ = filled_;
  4491. storage_invariants ();
  4492. }
  4493. // Iterator types
  4494. private:
  4495. // Use index array iterator
  4496. typedef typename IA::const_iterator vector_const_subiterator_type;
  4497. typedef typename IA::iterator vector_subiterator_type;
  4498. typedef typename IA::const_iterator const_subiterator_type;
  4499. typedef typename IA::iterator subiterator_type;
  4500. BOOST_UBLAS_INLINE
  4501. true_reference at_element (size_type i, size_type j) {
  4502. pointer p = find_element (i, j);
  4503. BOOST_UBLAS_CHECK (p, bad_index ());
  4504. return *p;
  4505. }
  4506. public:
  4507. class const_iterator1;
  4508. class iterator1;
  4509. class const_iterator2;
  4510. class iterator2;
  4511. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  4512. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  4513. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  4514. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  4515. // Element lookup
  4516. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  4517. const_iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) const {
  4518. sort ();
  4519. for (;;) {
  4520. size_type address1 (layout_type::index_M (i, j));
  4521. size_type address2 (layout_type::index_m (i, j));
  4522. vector_const_subiterator_type itv_begin (detail::lower_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4523. vector_const_subiterator_type itv_end (detail::upper_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4524. const_subiterator_type it_begin (index2_data_.begin () + (itv_begin - index1_data_.begin ()));
  4525. const_subiterator_type it_end (index2_data_.begin () + (itv_end - index1_data_.begin ()));
  4526. const_subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  4527. vector_const_subiterator_type itv (index1_data_.begin () + (it - index2_data_.begin ()));
  4528. if (rank == 0)
  4529. return const_iterator1 (*this, rank, i, j, itv, it);
  4530. if (it != it_end && zero_based (*it) == address2)
  4531. return const_iterator1 (*this, rank, i, j, itv, it);
  4532. if (direction > 0) {
  4533. if (layout_type::fast_i ()) {
  4534. if (it == it_end)
  4535. return const_iterator1 (*this, rank, i, j, itv, it);
  4536. i = zero_based (*it);
  4537. } else {
  4538. if (i >= size1_)
  4539. return const_iterator1 (*this, rank, i, j, itv, it);
  4540. ++ i;
  4541. }
  4542. } else /* if (direction < 0) */ {
  4543. if (layout_type::fast_i ()) {
  4544. if (it == index2_data_.begin () + array_size_type (zero_based (*itv)))
  4545. return const_iterator1 (*this, rank, i, j, itv, it);
  4546. i = zero_based (*(it - 1));
  4547. } else {
  4548. if (i == 0)
  4549. return const_iterator1 (*this, rank, i, j, itv, it);
  4550. -- i;
  4551. }
  4552. }
  4553. }
  4554. }
  4555. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  4556. iterator1 find1 (int rank, size_type i, size_type j, int direction = 1) {
  4557. sort ();
  4558. for (;;) {
  4559. size_type address1 (layout_type::index_M (i, j));
  4560. size_type address2 (layout_type::index_m (i, j));
  4561. vector_subiterator_type itv_begin (detail::lower_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4562. vector_subiterator_type itv_end (detail::upper_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4563. subiterator_type it_begin (index2_data_.begin () + (itv_begin - index1_data_.begin ()));
  4564. subiterator_type it_end (index2_data_.begin () + (itv_end - index1_data_.begin ()));
  4565. subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  4566. vector_subiterator_type itv (index1_data_.begin () + (it - index2_data_.begin ()));
  4567. if (rank == 0)
  4568. return iterator1 (*this, rank, i, j, itv, it);
  4569. if (it != it_end && zero_based (*it) == address2)
  4570. return iterator1 (*this, rank, i, j, itv, it);
  4571. if (direction > 0) {
  4572. if (layout_type::fast_i ()) {
  4573. if (it == it_end)
  4574. return iterator1 (*this, rank, i, j, itv, it);
  4575. i = zero_based (*it);
  4576. } else {
  4577. if (i >= size1_)
  4578. return iterator1 (*this, rank, i, j, itv, it);
  4579. ++ i;
  4580. }
  4581. } else /* if (direction < 0) */ {
  4582. if (layout_type::fast_i ()) {
  4583. if (it == index2_data_.begin () + array_size_type (zero_based (*itv)))
  4584. return iterator1 (*this, rank, i, j, itv, it);
  4585. i = zero_based (*(it - 1));
  4586. } else {
  4587. if (i == 0)
  4588. return iterator1 (*this, rank, i, j, itv, it);
  4589. -- i;
  4590. }
  4591. }
  4592. }
  4593. }
  4594. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  4595. const_iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) const {
  4596. sort ();
  4597. for (;;) {
  4598. size_type address1 (layout_type::index_M (i, j));
  4599. size_type address2 (layout_type::index_m (i, j));
  4600. vector_const_subiterator_type itv_begin (detail::lower_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4601. vector_const_subiterator_type itv_end (detail::upper_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4602. const_subiterator_type it_begin (index2_data_.begin () + (itv_begin - index1_data_.begin ()));
  4603. const_subiterator_type it_end (index2_data_.begin () + (itv_end - index1_data_.begin ()));
  4604. const_subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  4605. vector_const_subiterator_type itv (index1_data_.begin () + (it - index2_data_.begin ()));
  4606. if (rank == 0)
  4607. return const_iterator2 (*this, rank, i, j, itv, it);
  4608. if (it != it_end && zero_based (*it) == address2)
  4609. return const_iterator2 (*this, rank, i, j, itv, it);
  4610. if (direction > 0) {
  4611. if (layout_type::fast_j ()) {
  4612. if (it == it_end)
  4613. return const_iterator2 (*this, rank, i, j, itv, it);
  4614. j = zero_based (*it);
  4615. } else {
  4616. if (j >= size2_)
  4617. return const_iterator2 (*this, rank, i, j, itv, it);
  4618. ++ j;
  4619. }
  4620. } else /* if (direction < 0) */ {
  4621. if (layout_type::fast_j ()) {
  4622. if (it == index2_data_.begin () + array_size_type (zero_based (*itv)))
  4623. return const_iterator2 (*this, rank, i, j, itv, it);
  4624. j = zero_based (*(it - 1));
  4625. } else {
  4626. if (j == 0)
  4627. return const_iterator2 (*this, rank, i, j, itv, it);
  4628. -- j;
  4629. }
  4630. }
  4631. }
  4632. }
  4633. // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
  4634. iterator2 find2 (int rank, size_type i, size_type j, int direction = 1) {
  4635. sort ();
  4636. for (;;) {
  4637. size_type address1 (layout_type::index_M (i, j));
  4638. size_type address2 (layout_type::index_m (i, j));
  4639. vector_subiterator_type itv_begin (detail::lower_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4640. vector_subiterator_type itv_end (detail::upper_bound (index1_data_.begin (), index1_data_.begin () + filled_, k_based (address1), std::less<size_type> ()));
  4641. subiterator_type it_begin (index2_data_.begin () + (itv_begin - index1_data_.begin ()));
  4642. subiterator_type it_end (index2_data_.begin () + (itv_end - index1_data_.begin ()));
  4643. subiterator_type it (detail::lower_bound (it_begin, it_end, k_based (address2), std::less<size_type> ()));
  4644. vector_subiterator_type itv (index1_data_.begin () + (it - index2_data_.begin ()));
  4645. if (rank == 0)
  4646. return iterator2 (*this, rank, i, j, itv, it);
  4647. if (it != it_end && zero_based (*it) == address2)
  4648. return iterator2 (*this, rank, i, j, itv, it);
  4649. if (direction > 0) {
  4650. if (layout_type::fast_j ()) {
  4651. if (it == it_end)
  4652. return iterator2 (*this, rank, i, j, itv, it);
  4653. j = zero_based (*it);
  4654. } else {
  4655. if (j >= size2_)
  4656. return iterator2 (*this, rank, i, j, itv, it);
  4657. ++ j;
  4658. }
  4659. } else /* if (direction < 0) */ {
  4660. if (layout_type::fast_j ()) {
  4661. if (it == index2_data_.begin () + array_size_type (zero_based (*itv)))
  4662. return iterator2 (*this, rank, i, j, itv, it);
  4663. j = zero_based (*(it - 1));
  4664. } else {
  4665. if (j == 0)
  4666. return iterator2 (*this, rank, i, j, itv, it);
  4667. -- j;
  4668. }
  4669. }
  4670. }
  4671. }
  4672. class const_iterator1:
  4673. public container_const_reference<coordinate_matrix>,
  4674. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  4675. const_iterator1, value_type> {
  4676. public:
  4677. typedef typename coordinate_matrix::value_type value_type;
  4678. typedef typename coordinate_matrix::difference_type difference_type;
  4679. typedef typename coordinate_matrix::const_reference reference;
  4680. typedef const typename coordinate_matrix::pointer pointer;
  4681. typedef const_iterator2 dual_iterator_type;
  4682. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  4683. // Construction and destruction
  4684. BOOST_UBLAS_INLINE
  4685. const_iterator1 ():
  4686. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  4687. BOOST_UBLAS_INLINE
  4688. const_iterator1 (const self_type &m, int rank, size_type i, size_type j, const vector_const_subiterator_type &itv, const const_subiterator_type &it):
  4689. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  4690. BOOST_UBLAS_INLINE
  4691. const_iterator1 (const iterator1 &it):
  4692. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), itv_ (it.itv_), it_ (it.it_) {}
  4693. // Arithmetic
  4694. BOOST_UBLAS_INLINE
  4695. const_iterator1 &operator ++ () {
  4696. if (rank_ == 1 && layout_type::fast_i ())
  4697. ++ it_;
  4698. else {
  4699. i_ = index1 () + 1;
  4700. if (rank_ == 1)
  4701. *this = (*this) ().find1 (rank_, i_, j_, 1);
  4702. }
  4703. return *this;
  4704. }
  4705. BOOST_UBLAS_INLINE
  4706. const_iterator1 &operator -- () {
  4707. if (rank_ == 1 && layout_type::fast_i ())
  4708. -- it_;
  4709. else {
  4710. i_ = index1 () - 1;
  4711. if (rank_ == 1)
  4712. *this = (*this) ().find1 (rank_, i_, j_, -1);
  4713. }
  4714. return *this;
  4715. }
  4716. // Dereference
  4717. BOOST_UBLAS_INLINE
  4718. const_reference operator * () const {
  4719. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  4720. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  4721. if (rank_ == 1) {
  4722. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  4723. } else {
  4724. return (*this) () (i_, j_);
  4725. }
  4726. }
  4727. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  4728. BOOST_UBLAS_INLINE
  4729. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4730. typename self_type::
  4731. #endif
  4732. const_iterator2 begin () const {
  4733. const self_type &m = (*this) ();
  4734. return m.find2 (1, index1 (), 0);
  4735. }
  4736. BOOST_UBLAS_INLINE
  4737. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4738. typename self_type::
  4739. #endif
  4740. const_iterator2 cbegin () const {
  4741. return begin ();
  4742. }
  4743. BOOST_UBLAS_INLINE
  4744. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4745. typename self_type::
  4746. #endif
  4747. const_iterator2 end () const {
  4748. const self_type &m = (*this) ();
  4749. return m.find2 (1, index1 (), m.size2 ());
  4750. }
  4751. BOOST_UBLAS_INLINE
  4752. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4753. typename self_type::
  4754. #endif
  4755. const_iterator2 cend () const {
  4756. return end ();
  4757. }
  4758. BOOST_UBLAS_INLINE
  4759. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4760. typename self_type::
  4761. #endif
  4762. const_reverse_iterator2 rbegin () const {
  4763. return const_reverse_iterator2 (end ());
  4764. }
  4765. BOOST_UBLAS_INLINE
  4766. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4767. typename self_type::
  4768. #endif
  4769. const_reverse_iterator2 crbegin () const {
  4770. return rbegin ();
  4771. }
  4772. BOOST_UBLAS_INLINE
  4773. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4774. typename self_type::
  4775. #endif
  4776. const_reverse_iterator2 rend () const {
  4777. return const_reverse_iterator2 (begin ());
  4778. }
  4779. BOOST_UBLAS_INLINE
  4780. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4781. typename self_type::
  4782. #endif
  4783. const_reverse_iterator2 crend () const {
  4784. return rend ();
  4785. }
  4786. #endif
  4787. // Indices
  4788. BOOST_UBLAS_INLINE
  4789. size_type index1 () const {
  4790. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  4791. if (rank_ == 1) {
  4792. BOOST_UBLAS_CHECK (layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  4793. return layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  4794. } else {
  4795. return i_;
  4796. }
  4797. }
  4798. BOOST_UBLAS_INLINE
  4799. size_type index2 () const {
  4800. if (rank_ == 1) {
  4801. BOOST_UBLAS_CHECK (layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  4802. return layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  4803. } else {
  4804. return j_;
  4805. }
  4806. }
  4807. // Assignment
  4808. BOOST_UBLAS_INLINE
  4809. const_iterator1 &operator = (const const_iterator1 &it) {
  4810. container_const_reference<self_type>::assign (&it ());
  4811. rank_ = it.rank_;
  4812. i_ = it.i_;
  4813. j_ = it.j_;
  4814. itv_ = it.itv_;
  4815. it_ = it.it_;
  4816. return *this;
  4817. }
  4818. // Comparison
  4819. BOOST_UBLAS_INLINE
  4820. bool operator == (const const_iterator1 &it) const {
  4821. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4822. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  4823. if (rank_ == 1 || it.rank_ == 1) {
  4824. return it_ == it.it_;
  4825. } else {
  4826. return i_ == it.i_ && j_ == it.j_;
  4827. }
  4828. }
  4829. private:
  4830. int rank_;
  4831. size_type i_;
  4832. size_type j_;
  4833. vector_const_subiterator_type itv_;
  4834. const_subiterator_type it_;
  4835. };
  4836. BOOST_UBLAS_INLINE
  4837. const_iterator1 begin1 () const {
  4838. return find1 (0, 0, 0);
  4839. }
  4840. BOOST_UBLAS_INLINE
  4841. const_iterator1 cbegin1 () const {
  4842. return begin1 ();
  4843. }
  4844. BOOST_UBLAS_INLINE
  4845. const_iterator1 end1 () const {
  4846. return find1 (0, size1_, 0);
  4847. }
  4848. BOOST_UBLAS_INLINE
  4849. const_iterator1 cend1 () const {
  4850. return end1 ();
  4851. }
  4852. class iterator1:
  4853. public container_reference<coordinate_matrix>,
  4854. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  4855. iterator1, value_type> {
  4856. public:
  4857. typedef typename coordinate_matrix::value_type value_type;
  4858. typedef typename coordinate_matrix::difference_type difference_type;
  4859. typedef typename coordinate_matrix::true_reference reference;
  4860. typedef typename coordinate_matrix::pointer pointer;
  4861. typedef iterator2 dual_iterator_type;
  4862. typedef reverse_iterator2 dual_reverse_iterator_type;
  4863. // Construction and destruction
  4864. BOOST_UBLAS_INLINE
  4865. iterator1 ():
  4866. container_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  4867. BOOST_UBLAS_INLINE
  4868. iterator1 (self_type &m, int rank, size_type i, size_type j, const vector_subiterator_type &itv, const subiterator_type &it):
  4869. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  4870. // Arithmetic
  4871. BOOST_UBLAS_INLINE
  4872. iterator1 &operator ++ () {
  4873. if (rank_ == 1 && layout_type::fast_i ())
  4874. ++ it_;
  4875. else {
  4876. i_ = index1 () + 1;
  4877. if (rank_ == 1)
  4878. *this = (*this) ().find1 (rank_, i_, j_, 1);
  4879. }
  4880. return *this;
  4881. }
  4882. BOOST_UBLAS_INLINE
  4883. iterator1 &operator -- () {
  4884. if (rank_ == 1 && layout_type::fast_i ())
  4885. -- it_;
  4886. else {
  4887. i_ = index1 () - 1;
  4888. if (rank_ == 1)
  4889. *this = (*this) ().find1 (rank_, i_, j_, -1);
  4890. }
  4891. return *this;
  4892. }
  4893. // Dereference
  4894. BOOST_UBLAS_INLINE
  4895. reference operator * () const {
  4896. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  4897. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  4898. if (rank_ == 1) {
  4899. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  4900. } else {
  4901. return (*this) ().at_element (i_, j_);
  4902. }
  4903. }
  4904. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  4905. BOOST_UBLAS_INLINE
  4906. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4907. typename self_type::
  4908. #endif
  4909. iterator2 begin () const {
  4910. self_type &m = (*this) ();
  4911. return m.find2 (1, index1 (), 0);
  4912. }
  4913. BOOST_UBLAS_INLINE
  4914. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4915. typename self_type::
  4916. #endif
  4917. iterator2 end () const {
  4918. self_type &m = (*this) ();
  4919. return m.find2 (1, index1 (), m.size2 ());
  4920. }
  4921. BOOST_UBLAS_INLINE
  4922. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4923. typename self_type::
  4924. #endif
  4925. reverse_iterator2 rbegin () const {
  4926. return reverse_iterator2 (end ());
  4927. }
  4928. BOOST_UBLAS_INLINE
  4929. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4930. typename self_type::
  4931. #endif
  4932. reverse_iterator2 rend () const {
  4933. return reverse_iterator2 (begin ());
  4934. }
  4935. #endif
  4936. // Indices
  4937. BOOST_UBLAS_INLINE
  4938. size_type index1 () const {
  4939. BOOST_UBLAS_CHECK (*this != (*this) ().find1 (0, (*this) ().size1 (), j_), bad_index ());
  4940. if (rank_ == 1) {
  4941. BOOST_UBLAS_CHECK (layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  4942. return layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  4943. } else {
  4944. return i_;
  4945. }
  4946. }
  4947. BOOST_UBLAS_INLINE
  4948. size_type index2 () const {
  4949. if (rank_ == 1) {
  4950. BOOST_UBLAS_CHECK (layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  4951. return layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  4952. } else {
  4953. return j_;
  4954. }
  4955. }
  4956. // Assignment
  4957. BOOST_UBLAS_INLINE
  4958. iterator1 &operator = (const iterator1 &it) {
  4959. container_reference<self_type>::assign (&it ());
  4960. rank_ = it.rank_;
  4961. i_ = it.i_;
  4962. j_ = it.j_;
  4963. itv_ = it.itv_;
  4964. it_ = it.it_;
  4965. return *this;
  4966. }
  4967. // Comparison
  4968. BOOST_UBLAS_INLINE
  4969. bool operator == (const iterator1 &it) const {
  4970. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4971. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  4972. if (rank_ == 1 || it.rank_ == 1) {
  4973. return it_ == it.it_;
  4974. } else {
  4975. return i_ == it.i_ && j_ == it.j_;
  4976. }
  4977. }
  4978. private:
  4979. int rank_;
  4980. size_type i_;
  4981. size_type j_;
  4982. vector_subiterator_type itv_;
  4983. subiterator_type it_;
  4984. friend class const_iterator1;
  4985. };
  4986. BOOST_UBLAS_INLINE
  4987. iterator1 begin1 () {
  4988. return find1 (0, 0, 0);
  4989. }
  4990. BOOST_UBLAS_INLINE
  4991. iterator1 end1 () {
  4992. return find1 (0, size1_, 0);
  4993. }
  4994. class const_iterator2:
  4995. public container_const_reference<coordinate_matrix>,
  4996. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  4997. const_iterator2, value_type> {
  4998. public:
  4999. typedef typename coordinate_matrix::value_type value_type;
  5000. typedef typename coordinate_matrix::difference_type difference_type;
  5001. typedef typename coordinate_matrix::const_reference reference;
  5002. typedef const typename coordinate_matrix::pointer pointer;
  5003. typedef const_iterator1 dual_iterator_type;
  5004. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  5005. // Construction and destruction
  5006. BOOST_UBLAS_INLINE
  5007. const_iterator2 ():
  5008. container_const_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  5009. BOOST_UBLAS_INLINE
  5010. const_iterator2 (const self_type &m, int rank, size_type i, size_type j, const vector_const_subiterator_type itv, const const_subiterator_type &it):
  5011. container_const_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  5012. BOOST_UBLAS_INLINE
  5013. const_iterator2 (const iterator2 &it):
  5014. container_const_reference<self_type> (it ()), rank_ (it.rank_), i_ (it.i_), j_ (it.j_), itv_ (it.itv_), it_ (it.it_) {}
  5015. // Arithmetic
  5016. BOOST_UBLAS_INLINE
  5017. const_iterator2 &operator ++ () {
  5018. if (rank_ == 1 && layout_type::fast_j ())
  5019. ++ it_;
  5020. else {
  5021. j_ = index2 () + 1;
  5022. if (rank_ == 1)
  5023. *this = (*this) ().find2 (rank_, i_, j_, 1);
  5024. }
  5025. return *this;
  5026. }
  5027. BOOST_UBLAS_INLINE
  5028. const_iterator2 &operator -- () {
  5029. if (rank_ == 1 && layout_type::fast_j ())
  5030. -- it_;
  5031. else {
  5032. j_ = index2 () - 1;
  5033. if (rank_ == 1)
  5034. *this = (*this) ().find2 (rank_, i_, j_, -1);
  5035. }
  5036. return *this;
  5037. }
  5038. // Dereference
  5039. BOOST_UBLAS_INLINE
  5040. const_reference operator * () const {
  5041. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  5042. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  5043. if (rank_ == 1) {
  5044. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  5045. } else {
  5046. return (*this) () (i_, j_);
  5047. }
  5048. }
  5049. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  5050. BOOST_UBLAS_INLINE
  5051. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5052. typename self_type::
  5053. #endif
  5054. const_iterator1 begin () const {
  5055. const self_type &m = (*this) ();
  5056. return m.find1 (1, 0, index2 ());
  5057. }
  5058. BOOST_UBLAS_INLINE
  5059. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5060. typename self_type::
  5061. #endif
  5062. const_iterator1 cbegin () const {
  5063. return begin ();
  5064. }
  5065. BOOST_UBLAS_INLINE
  5066. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5067. typename self_type::
  5068. #endif
  5069. const_iterator1 end () const {
  5070. const self_type &m = (*this) ();
  5071. return m.find1 (1, m.size1 (), index2 ());
  5072. }
  5073. BOOST_UBLAS_INLINE
  5074. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5075. typename self_type::
  5076. #endif
  5077. const_iterator1 cend () const {
  5078. return end ();
  5079. }
  5080. BOOST_UBLAS_INLINE
  5081. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5082. typename self_type::
  5083. #endif
  5084. const_reverse_iterator1 rbegin () const {
  5085. return const_reverse_iterator1 (end ());
  5086. }
  5087. BOOST_UBLAS_INLINE
  5088. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5089. typename self_type::
  5090. #endif
  5091. const_reverse_iterator1 crbegin () const {
  5092. return rbegin ();
  5093. }
  5094. BOOST_UBLAS_INLINE
  5095. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5096. typename self_type::
  5097. #endif
  5098. const_reverse_iterator1 rend () const {
  5099. return const_reverse_iterator1 (begin ());
  5100. }
  5101. BOOST_UBLAS_INLINE
  5102. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5103. typename self_type::
  5104. #endif
  5105. const_reverse_iterator1 crend () const {
  5106. return rend ();
  5107. }
  5108. #endif
  5109. // Indices
  5110. BOOST_UBLAS_INLINE
  5111. size_type index1 () const {
  5112. if (rank_ == 1) {
  5113. BOOST_UBLAS_CHECK (layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  5114. return layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  5115. } else {
  5116. return i_;
  5117. }
  5118. }
  5119. BOOST_UBLAS_INLINE
  5120. size_type index2 () const {
  5121. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  5122. if (rank_ == 1) {
  5123. BOOST_UBLAS_CHECK (layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  5124. return layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  5125. } else {
  5126. return j_;
  5127. }
  5128. }
  5129. // Assignment
  5130. BOOST_UBLAS_INLINE
  5131. const_iterator2 &operator = (const const_iterator2 &it) {
  5132. container_const_reference<self_type>::assign (&it ());
  5133. rank_ = it.rank_;
  5134. i_ = it.i_;
  5135. j_ = it.j_;
  5136. itv_ = it.itv_;
  5137. it_ = it.it_;
  5138. return *this;
  5139. }
  5140. // Comparison
  5141. BOOST_UBLAS_INLINE
  5142. bool operator == (const const_iterator2 &it) const {
  5143. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5144. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  5145. if (rank_ == 1 || it.rank_ == 1) {
  5146. return it_ == it.it_;
  5147. } else {
  5148. return i_ == it.i_ && j_ == it.j_;
  5149. }
  5150. }
  5151. private:
  5152. int rank_;
  5153. size_type i_;
  5154. size_type j_;
  5155. vector_const_subiterator_type itv_;
  5156. const_subiterator_type it_;
  5157. };
  5158. BOOST_UBLAS_INLINE
  5159. const_iterator2 begin2 () const {
  5160. return find2 (0, 0, 0);
  5161. }
  5162. BOOST_UBLAS_INLINE
  5163. const_iterator2 cbegin2 () const {
  5164. return begin2 ();
  5165. }
  5166. BOOST_UBLAS_INLINE
  5167. const_iterator2 end2 () const {
  5168. return find2 (0, 0, size2_);
  5169. }
  5170. BOOST_UBLAS_INLINE
  5171. const_iterator2 cend2 () const {
  5172. return end2 ();
  5173. }
  5174. class iterator2:
  5175. public container_reference<coordinate_matrix>,
  5176. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  5177. iterator2, value_type> {
  5178. public:
  5179. typedef typename coordinate_matrix::value_type value_type;
  5180. typedef typename coordinate_matrix::difference_type difference_type;
  5181. typedef typename coordinate_matrix::true_reference reference;
  5182. typedef typename coordinate_matrix::pointer pointer;
  5183. typedef iterator1 dual_iterator_type;
  5184. typedef reverse_iterator1 dual_reverse_iterator_type;
  5185. // Construction and destruction
  5186. BOOST_UBLAS_INLINE
  5187. iterator2 ():
  5188. container_reference<self_type> (), rank_ (), i_ (), j_ (), itv_ (), it_ () {}
  5189. BOOST_UBLAS_INLINE
  5190. iterator2 (self_type &m, int rank, size_type i, size_type j, const vector_subiterator_type &itv, const subiterator_type &it):
  5191. container_reference<self_type> (m), rank_ (rank), i_ (i), j_ (j), itv_ (itv), it_ (it) {}
  5192. // Arithmetic
  5193. BOOST_UBLAS_INLINE
  5194. iterator2 &operator ++ () {
  5195. if (rank_ == 1 && layout_type::fast_j ())
  5196. ++ it_;
  5197. else {
  5198. j_ = index2 () + 1;
  5199. if (rank_ == 1)
  5200. *this = (*this) ().find2 (rank_, i_, j_, 1);
  5201. }
  5202. return *this;
  5203. }
  5204. BOOST_UBLAS_INLINE
  5205. iterator2 &operator -- () {
  5206. if (rank_ == 1 && layout_type::fast_j ())
  5207. -- it_;
  5208. else {
  5209. j_ = index2 ();
  5210. if (rank_ == 1)
  5211. *this = (*this) ().find2 (rank_, i_, j_, -1);
  5212. }
  5213. return *this;
  5214. }
  5215. // Dereference
  5216. BOOST_UBLAS_INLINE
  5217. reference operator * () const {
  5218. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  5219. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  5220. if (rank_ == 1) {
  5221. return (*this) ().value_data_ [it_ - (*this) ().index2_data_.begin ()];
  5222. } else {
  5223. return (*this) ().at_element (i_, j_);
  5224. }
  5225. }
  5226. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  5227. BOOST_UBLAS_INLINE
  5228. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5229. typename self_type::
  5230. #endif
  5231. iterator1 begin () const {
  5232. self_type &m = (*this) ();
  5233. return m.find1 (1, 0, index2 ());
  5234. }
  5235. BOOST_UBLAS_INLINE
  5236. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5237. typename self_type::
  5238. #endif
  5239. iterator1 end () const {
  5240. self_type &m = (*this) ();
  5241. return m.find1 (1, m.size1 (), index2 ());
  5242. }
  5243. BOOST_UBLAS_INLINE
  5244. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5245. typename self_type::
  5246. #endif
  5247. reverse_iterator1 rbegin () const {
  5248. return reverse_iterator1 (end ());
  5249. }
  5250. BOOST_UBLAS_INLINE
  5251. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5252. typename self_type::
  5253. #endif
  5254. reverse_iterator1 rend () const {
  5255. return reverse_iterator1 (begin ());
  5256. }
  5257. #endif
  5258. // Indices
  5259. BOOST_UBLAS_INLINE
  5260. size_type index1 () const {
  5261. if (rank_ == 1) {
  5262. BOOST_UBLAS_CHECK (layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size1 (), bad_index ());
  5263. return layout_type::index_M ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  5264. } else {
  5265. return i_;
  5266. }
  5267. }
  5268. BOOST_UBLAS_INLINE
  5269. size_type index2 () const {
  5270. BOOST_UBLAS_CHECK (*this != (*this) ().find2 (0, i_, (*this) ().size2 ()), bad_index ());
  5271. if (rank_ == 1) {
  5272. BOOST_UBLAS_CHECK (layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_)) < (*this) ().size2 (), bad_index ());
  5273. return layout_type::index_m ((*this) ().zero_based (*itv_), (*this) ().zero_based (*it_));
  5274. } else {
  5275. return j_;
  5276. }
  5277. }
  5278. // Assignment
  5279. BOOST_UBLAS_INLINE
  5280. iterator2 &operator = (const iterator2 &it) {
  5281. container_reference<self_type>::assign (&it ());
  5282. rank_ = it.rank_;
  5283. i_ = it.i_;
  5284. j_ = it.j_;
  5285. itv_ = it.itv_;
  5286. it_ = it.it_;
  5287. return *this;
  5288. }
  5289. // Comparison
  5290. BOOST_UBLAS_INLINE
  5291. bool operator == (const iterator2 &it) const {
  5292. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5293. // BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
  5294. if (rank_ == 1 || it.rank_ == 1) {
  5295. return it_ == it.it_;
  5296. } else {
  5297. return i_ == it.i_ && j_ == it.j_;
  5298. }
  5299. }
  5300. private:
  5301. int rank_;
  5302. size_type i_;
  5303. size_type j_;
  5304. vector_subiterator_type itv_;
  5305. subiterator_type it_;
  5306. friend class const_iterator2;
  5307. };
  5308. BOOST_UBLAS_INLINE
  5309. iterator2 begin2 () {
  5310. return find2 (0, 0, 0);
  5311. }
  5312. BOOST_UBLAS_INLINE
  5313. iterator2 end2 () {
  5314. return find2 (0, 0, size2_);
  5315. }
  5316. // Reverse iterators
  5317. BOOST_UBLAS_INLINE
  5318. const_reverse_iterator1 rbegin1 () const {
  5319. return const_reverse_iterator1 (end1 ());
  5320. }
  5321. BOOST_UBLAS_INLINE
  5322. const_reverse_iterator1 crbegin1 () const {
  5323. return rbegin1 ();
  5324. }
  5325. BOOST_UBLAS_INLINE
  5326. const_reverse_iterator1 rend1 () const {
  5327. return const_reverse_iterator1 (begin1 ());
  5328. }
  5329. BOOST_UBLAS_INLINE
  5330. const_reverse_iterator1 crend1 () const {
  5331. return rend1 ();
  5332. }
  5333. BOOST_UBLAS_INLINE
  5334. reverse_iterator1 rbegin1 () {
  5335. return reverse_iterator1 (end1 ());
  5336. }
  5337. BOOST_UBLAS_INLINE
  5338. reverse_iterator1 rend1 () {
  5339. return reverse_iterator1 (begin1 ());
  5340. }
  5341. BOOST_UBLAS_INLINE
  5342. const_reverse_iterator2 rbegin2 () const {
  5343. return const_reverse_iterator2 (end2 ());
  5344. }
  5345. BOOST_UBLAS_INLINE
  5346. const_reverse_iterator2 crbegin2 () const {
  5347. return rbegin2 ();
  5348. }
  5349. BOOST_UBLAS_INLINE
  5350. const_reverse_iterator2 rend2 () const {
  5351. return const_reverse_iterator2 (begin2 ());
  5352. }
  5353. BOOST_UBLAS_INLINE
  5354. const_reverse_iterator2 crend2 () const {
  5355. return rend2 ();
  5356. }
  5357. BOOST_UBLAS_INLINE
  5358. reverse_iterator2 rbegin2 () {
  5359. return reverse_iterator2 (end2 ());
  5360. }
  5361. BOOST_UBLAS_INLINE
  5362. reverse_iterator2 rend2 () {
  5363. return reverse_iterator2 (begin2 ());
  5364. }
  5365. // Serialization
  5366. template<class Archive>
  5367. void serialize(Archive & ar, const unsigned int /* file_version */){
  5368. serialization::collection_size_type s1 (size1_);
  5369. serialization::collection_size_type s2 (size2_);
  5370. ar & serialization::make_nvp("size1",s1);
  5371. ar & serialization::make_nvp("size2",s2);
  5372. if (Archive::is_loading::value) {
  5373. size1_ = s1;
  5374. size2_ = s2;
  5375. }
  5376. ar & serialization::make_nvp("capacity", capacity_);
  5377. ar & serialization::make_nvp("filled", filled_);
  5378. ar & serialization::make_nvp("sorted_filled", sorted_filled_);
  5379. ar & serialization::make_nvp("sorted", sorted_);
  5380. ar & serialization::make_nvp("index1_data", index1_data_);
  5381. ar & serialization::make_nvp("index2_data", index2_data_);
  5382. ar & serialization::make_nvp("value_data", value_data_);
  5383. storage_invariants();
  5384. }
  5385. private:
  5386. void storage_invariants () const
  5387. {
  5388. BOOST_UBLAS_CHECK (capacity_ == index1_data_.size (), internal_logic ());
  5389. BOOST_UBLAS_CHECK (capacity_ == index2_data_.size (), internal_logic ());
  5390. BOOST_UBLAS_CHECK (capacity_ == value_data_.size (), internal_logic ());
  5391. BOOST_UBLAS_CHECK (filled_ <= capacity_, internal_logic ());
  5392. BOOST_UBLAS_CHECK (sorted_filled_ <= filled_, internal_logic ());
  5393. BOOST_UBLAS_CHECK (sorted_ == (sorted_filled_ == filled_), internal_logic ());
  5394. }
  5395. size_type size1_;
  5396. size_type size2_;
  5397. array_size_type capacity_;
  5398. mutable array_size_type filled_;
  5399. mutable array_size_type sorted_filled_;
  5400. mutable bool sorted_;
  5401. mutable index_array_type index1_data_;
  5402. mutable index_array_type index2_data_;
  5403. mutable value_array_type value_data_;
  5404. static const value_type zero_;
  5405. BOOST_UBLAS_INLINE
  5406. static size_type zero_based (size_type k_based_index) {
  5407. return k_based_index - IB;
  5408. }
  5409. BOOST_UBLAS_INLINE
  5410. static size_type k_based (size_type zero_based_index) {
  5411. return zero_based_index + IB;
  5412. }
  5413. friend class iterator1;
  5414. friend class iterator2;
  5415. friend class const_iterator1;
  5416. friend class const_iterator2;
  5417. };
  5418. template<class T, class L, std::size_t IB, class IA, class TA>
  5419. const typename coordinate_matrix<T, L, IB, IA, TA>::value_type coordinate_matrix<T, L, IB, IA, TA>::zero_ = value_type/*zero*/();
  5420. }}}
  5421. #endif