matrix_sparse.hpp 226 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773
  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