Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TRefArray.cxx
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Rene Brun 02/10/2001
3
4/*************************************************************************
5 * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class TRefArray
13\ingroup Containers
14An array of references to TObjects. The array expands automatically
15when objects are added (shrinking can be done by hand using Expand() )
16
17The TRefArray can be filled with:
18~~~ {.cpp}
19 array.Add(obj)
20 array.AddAt(obj,i)
21 but not array[i] = obj !!!
22~~~
23The array elements can be retrieved with:
24~~~ {.cpp}
25 TObject *obj = array.At(i);
26~~~
27By default the TRefArray 'points' to the current process and can only
28receive object that have been created in this process.
29To point the TRefArray to a different process do:
30~~~ {.cpp}
31 TRefArray array( processId );
32~~~
33For example, if 'obj' is an instance that was created in the different
34process and you do:
35~~~ {.cpp}
36 TRefArray array( TProcessID::GetProcessWithUID( obj ) );
37~~~
38Then
39~~~ {.cpp}
40 array.Add(obj);
41~~~
42is correct (obj comes from the process the array is pointed to
43while
44~~~ {.cpp}
45 TObject *nobj = new TObject;
46 array.Add(nobj);
47~~~
48is incorrect since 'nobj' was created in a different process than the
49one the array is pointed to. In this case you will see error message:
50~~~ {.cpp}
51 Error in <TRefArray::AddAtAndExpand>: The object at 0x... is not
52 registered in the process the TRefArray point to
53 (pid = ProcessID../....)
54~~~
55When a TRefArray is Streamed, only the pointer unique id is written,
56not the referenced object. TRefArray may be assigned to different
57branches of one Tree or several Trees.
58The branch containing the TRefArray can be read before or after the
59array (eg TClonesArray, STL vector,..) of the referenced objects.
60
61See an example in $ROOTSYS/test/Event.h
62
63### RESTRICTIONS when using TRefArray
64
65 - Elements in a TRefArray cannot point to a TFile or TDirectory.
66 - All elements of a TRefArray must be set in the same process,
67 In particular, one cannot modify some elements of the array in
68 a different process.
69
70Use an array of TRef when one of the above restrictions is met.
71
72The number of TRef handled by a single process id is limited to
7316777215 (see TRef for more detail). When the TProcessID is full
74(has seen 16777215 objects), we switch to new one TProcessID
75maximum 65535 including the TProcessIDs read from file).
76However TRefArray can not switch to new TProcessID if they already
77contain objects.
78
79When the TProcessID has been switched due to overflow and an new
80object is added to an existing, empty TRefArray, you will see:
81
82~~~ {.cpp}
83Warning in <TRefArray::AddAtAndExpand>: The ProcessID for the 0x5f83819e8 has been switched to ProcessID4/6c89f37e-8259-11e2-9717-166ee183beef:4
84~~~
85If the TRefArray was not empty, you will see:
86
87~~~ {.cpp}
88Error in <TRefArray::AddAtAndExpand>: The object at %p can not be registered in the process the TRefArray points to (pid = ProcessID4/6c89f37e-8259-11e2-9717-166ee183beef) because the ProcessID has too many objects and the TRefArray already contains other objects.
89~~~
90When running out of TProcessIds, you will see:
91
92~~~ {.cpp}
93Warning in <TProcessID::AddProcessID>: Maximum number of TProcessID (65535) is almost reached (one left). TRef will stop being functional when the limit is reached.
94Fatal in <TProcessID::AddProcessID>: Maximum number of TProcessID (65535) has been reached. TRef are not longer functional.
95~~~
96*/
97
98#include "TRefArray.h"
99#include "TRefTable.h"
100#include "TBuffer.h"
101#include "TError.h"
102#include "TBits.h"
103
104
105////////////////////////////////////////////////////////////////////////////////
106/// default constructor
107
109{
111 fUIDs = nullptr;
112 fSize = 0;
113 fLast = -1;
114 fLowerBound = 0;
115 Changed();
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// Create an object array. Using s one can set the array size
120/// and lowerBound can be used to set the array lowerbound
121/// index (default is 0).
122
124{
125 if (s < 0) {
126 Warning("TRefArray", "size (%d) < 0", s);
128 }
129
131 fUIDs = nullptr;
132 fSize = 0;
133 Init(s, 0);
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// Create an object array. Using s one can set the array size
138/// and lowerBound can be used to set the array lowerbound
139/// index (default is 0).
140
142{
143 if (s < 0) {
144 Warning("TRefArray", "size (%d) < 0", s);
146 }
147
149 fUIDs = nullptr;
150 fSize = 0;
151 Init(s, lowerBound);
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Create a copy of TRefArray a.
156
158{
159 fPID = a.fPID;
160 fUIDs = nullptr;
161 fSize = 0;
162 Init(a.fSize, a.fLowerBound);
163
164 for (Int_t i = 0; i < fSize; i++)
165 fUIDs[i] = a.fUIDs[i];
166
167 fLast = a.fLast;
168 fName = a.fName;
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Assignment operator.
173
175{
176 if (this != &a) {
177 // Copy this by hand because the assignment operator
178 // of TCollection is private
179 fName = a.fName;
180 fSorted = a.fSorted;
181 fPID = a.fPID;
182 Init(a.fSize, a.fLowerBound);
183
184 for (Int_t i = 0; i < fSize; i++)
185 fUIDs[i] = a.fUIDs[i];
186
187 fLast = a.fLast;
188 fName = a.fName;
189 }
190 return *this;
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Usual destructor (The object pointed to by the array are never deleted).
195
197{
198 if (fUIDs) delete [] fUIDs;
199 fPID = nullptr;
200 fUIDs = nullptr;
201 fSize = 0;
202}
203
204////////////////////////////////////////////////////////////////////////////////
205/// Private/static function, check for validity of pid.
206
207Bool_t TRefArray::GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
208{
209 // Check if the object can belong here.
210 Bool_t valid = kTRUE;
211 if (obj->TestBit(kHasUUID)) {
212 valid = kFALSE;
213 } else if (obj->TestBit(kIsReferenced)) {
214 valid = (fPID == TProcessID::GetProcessWithUID(obj));
215 if (valid) {
216 uid = obj->GetUniqueID();
217 } else {
218 if (GetAbsLast() < 0) {
219 // The container is empty, we can switch the ProcessID.
220 uid = obj->GetUniqueID();
222 valid = kTRUE;
223 if (gDebug > 3)
224 Info(TString::Format("TRefArray::%s",methodname),"The ProcessID for the %p has been switched to %s/%s:%d.",
225 this,fPID->GetName(),fPID->GetTitle(),fPID->GetUniqueID());
226 }
227 }
228 } else {
229 // If we could, we would just add the object to the
230 // TRefArray's ProcessID. For now, just check the
231 // ProcessID it would be added to, i.e the current one,
232 // is not full.
233
234 if (!(TProcessID::GetObjectCount() >= 16777215)) {
236 if (valid) {
237 uid = TProcessID::AssignID(obj);
238 }
239 } else {
240 // The AssignID will create a new TProcessID.
241 if (GetAbsLast() < 0) {
242 // If we are empty, we can handle it.
243 uid = TProcessID::AssignID(obj);
245 Warning(TString::Format("TRefArray::%s",methodname),"The ProcessID for the %p has been switched to %s/%s:%d. There are too many referenced objects.",
246 this,fPID->GetName(),fPID->GetTitle(),fPID->GetUniqueID());
247 return kTRUE;
248 } else {
249 Error(TString::Format("TRefArray::%s",methodname),"The object at %p can not be registered in the process the TRefArray points to (pid = %s/%s) because the ProcessID has too many objects and the TRefArray already contains other objects.",obj,fPID->GetName(),fPID->GetTitle());
250 return kFALSE;
251 }
252 }
253 }
254
255 if (!valid) {
256 ::Error(TString::Format("TRefArray::%s",methodname),
257 "The object at %p is not registered in the process the TRefArray points to (pid = %s/%s)",obj,fPID->GetName(),fPID->GetTitle());
258 }
259 return valid;
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Add object in the first slot of the array. This will overwrite the
264/// first element that might have been there. To have insertion semantics
265/// use either a TList or a TOrdCollection.
266
268{
269 if (!obj) return;
270
271 // Check if the object can belong here
272 Int_t uid;
273 if (GetObjectUID(uid, obj, "AddFirst")) {
274 fUIDs[0] = uid;
275 Changed();
276 }
277}
278
279////////////////////////////////////////////////////////////////////////////////
280/// Add object in the next empty slot in the array. Expand the array
281/// if necessary.
282
287
288////////////////////////////////////////////////////////////////////////////////
289/// Add object in the slot before object before. If before=0 add object
290/// in the first slot. Note that this will overwrite any object that
291/// might have already been in this slot. For insertion semantics use
292/// either a TList or a TOrdCollection.
293
294void TRefArray::AddBefore(const TObject *before, TObject *obj)
295{
296 if (!before)
297 AddFirst(obj);
298 else {
299 Int_t idx = IndexOf(before) - fLowerBound;
300 if (idx == -1) {
301 Error("AddBefore", "before not found, object not added");
302 return;
303 }
304 if (idx == 0) {
305 Error("AddBefore", "cannot add before lowerbound (%d)", fLowerBound);
306 return;
307 }
308 AddAt(obj, idx+fLowerBound-1);
309 }
310}
311
312////////////////////////////////////////////////////////////////////////////////
313/// Add object in the slot after object after. If after=0 add object in
314/// the last empty slot. Note that this will overwrite any object that
315/// might have already been in this slot. For insertion semantics use
316/// either a TList or a TOrdCollection.
317
318void TRefArray::AddAfter(const TObject *after, TObject *obj)
319{
320 if (!after)
321 AddLast(obj);
322 else {
323 Int_t idx = IndexOf(after) - fLowerBound;
324 if (idx == -1) {
325 Error("AddAfter", "after not found, object not added");
326 return;
327 }
328 AddAtAndExpand(obj, idx+fLowerBound+1);
329 }
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// Add object at position idx. If idx is larger than the current size
334/// of the array, expand the array (double its size).
335
337{
338 if (!obj) return;
339 if (idx < fLowerBound) {
340 Error("AddAt", "out of bounds at %d in %zx", idx, (size_t)this);
341 return;
342 }
343 if (idx-fLowerBound >= fSize)
344 Expand(std::max(idx-fLowerBound+1, GrowBy(fSize)));
345
346 // Check if the object can belong here
347 Int_t uid;
348 if (GetObjectUID(uid, obj, "AddAtAndExpand")) {
349 fUIDs[idx-fLowerBound] = uid; // NOLINT
350 fLast = std::max(idx-fLowerBound, GetAbsLast());
351 Changed();
352 }
353}
354
355////////////////////////////////////////////////////////////////////////////////
356/// Add object at position ids. Give an error when idx is out of bounds
357/// (i.e. the array is not expanded).
358
360{
361 if (!obj) return;
362 if (!BoundsOk("AddAt", idx)) return;
363
364 // Check if the object can belong here
365 Int_t uid;
366 if (GetObjectUID(uid, obj, "AddAt")) {
367 fUIDs[idx-fLowerBound] = uid;
368 fLast = std::max(idx-fLowerBound, GetAbsLast());
369 Changed();
370 }
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Return the position of the new object.
375/// Find the first empty cell or AddLast if there is no empty cell
376
378{
379 if (!obj) return 0;
380 if (Last()) { // <---------- This is to take in account "empty" TRefArray's
381 Int_t i;
382 for (i = 0; i < fSize; i++)
383 if (!fUIDs[i]) { // Add object at position i
384 // Check if the object can belong here
385 Int_t uid;
386 if (GetObjectUID(uid, obj, "AddAtFree")) {
387 fUIDs[i] = uid; // NOLINT
388 fLast = std::max(i, GetAbsLast());
389 Changed();
390 return i+fLowerBound;
391 }
392 }
393 }
394 AddLast(obj);
395 return GetLast();
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Return the object after obj. Returns 0 if obj is last object.
400
402{
403 if (!obj || !fPID) return nullptr;
404
405 Int_t idx = IndexOf(obj) - fLowerBound;
406 if (idx == -1 || idx == fSize-1) return nullptr;
407
408 TObject *after = GetFromTable(idx + 1);
409 if (!after)
410 after = fPID->GetObjectWithID(fUIDs[idx + 1]);
411 return after;
412}
413
414////////////////////////////////////////////////////////////////////////////////
415/// Return the object before obj. Returns 0 if obj is first object.
416
418{
419 if (!obj || !fPID) return nullptr;
420
421 Int_t idx = IndexOf(obj) - fLowerBound;
422 if (idx == -1 || idx == 0) return nullptr;
423
424 TObject *before = GetFromTable(idx - 1);
425 if (!before)
426 before = fPID->GetObjectWithID(fUIDs[idx - 1]);
427 return before;
428}
429
430////////////////////////////////////////////////////////////////////////////////
431/// Remove all objects from the array.
432
434{
435 fLast = - 1;
436
437 for (Int_t j=0 ; j < fSize; j++) fUIDs[j] = 0;
438
439 Changed();
440}
441
442////////////////////////////////////////////////////////////////////////////////
443/// Remove empty slots from array.
444
446{
447 Int_t j = 0;
448
449 for (Int_t i = 0; i < fSize; i++) {
450 if (fUIDs[i]) {
451 fUIDs[j] = fUIDs[i];
452 j++;
453 }
454 }
455
456 fLast = j - 1;
457
458 for ( ; j < fSize; j++) fUIDs[j] = 0;
459}
460
461////////////////////////////////////////////////////////////////////////////////
462/// Remove all objects from the array and free the internal memory.
463
465{
466 fLast = -1;
467
468 fSize = 0;
469 if (fUIDs) {
470 delete [] fUIDs;
471 fUIDs = nullptr;
472 }
473
474 Changed();
475}
476
477////////////////////////////////////////////////////////////////////////////////
478/// Expand or shrink the array to newSize elements.
479
481{
482 if (newSize < 0) {
483 Error ("Expand", "newSize must be positive (%d)", newSize);
484 return;
485 }
486 if (newSize == fSize) return;
487 UInt_t *temp = fUIDs;
488 if (newSize != 0) {
489 fUIDs = new UInt_t[newSize];
490 if (newSize < fSize) {
491 memcpy(fUIDs, temp, newSize*sizeof(UInt_t));
492 } else if (temp) {
493 memcpy(fUIDs, temp, fSize*sizeof(UInt_t));
494 memset(&fUIDs[fSize], 0, (newSize-fSize)*sizeof(UInt_t));
495 } else {
496 memset(fUIDs, 0, newSize*sizeof(UInt_t));
497 }
498 } else {
499 fUIDs = nullptr;
500 }
501 if (temp) delete [] temp;
502 fSize = newSize;
503}
504
505////////////////////////////////////////////////////////////////////////////////
506///the reference may be in the TRefTable
507
509{
511 if (table) {
512 table->SetUID(fUIDs[idx], fPID);
513 table->Notify();
514 return fPID->GetObjectWithID(fUIDs[idx]);
515 }
516 return nullptr;
517}
518
519////////////////////////////////////////////////////////////////////////////////
520/// Stream all objects in the array to or from the I/O buffer.
521
523{
524 UInt_t R__s, R__c;
525 Int_t nobjects;
526 UShort_t pidf;
527 if (R__b.IsReading()) {
528 R__b.ReadVersion(&R__s, &R__c);
529 TObject::Streamer(R__b);
530 fName.Streamer(R__b);
531 R__b >> nobjects;
532 R__b >> fLowerBound;
533 if (nobjects >= fSize) Expand(nobjects);
534 fLast = -1;
535 R__b >> pidf;
536 pidf += R__b.GetPidOffset();
537 fPID = R__b.ReadProcessID(pidf);
538 if (gDebug > 1) printf("Reading TRefArray, pidf=%d, fPID=%zx, nobjects=%d\n",pidf,(size_t)fPID,nobjects);
539 for (Int_t i = 0; i < nobjects; i++) {
540 R__b >> fUIDs[i];
541 if (fUIDs[i] != 0) fLast = i;
542 if (gDebug > 1) {
543 printf(" %d",fUIDs[i]);
544 if ((i > 0 && i%10 == 0) || (i == nobjects-1)) printf("\n");
545 }
546 }
547 memset(&fUIDs[fLast+1], 0, (fSize - fLast - 1)*sizeof(fUIDs[0]));
548 Changed();
549 R__b.CheckByteCount(R__s, R__c,TRefArray::IsA());
550 } else {
551 R__c = R__b.WriteVersion(TRefArray::IsA(), kTRUE);
552 TObject::Streamer(R__b);
553 fName.Streamer(R__b);
554 nobjects = GetAbsLast()+1;
555 R__b << nobjects;
556 R__b << fLowerBound;
557 pidf = R__b.WriteProcessID(fPID);
558 R__b << pidf;
559 if (gDebug > 1) printf("Writing TRefArray, pidf=%d, fPID=%zx, nobjects=%d\n",pidf,(size_t)fPID,nobjects);
560
561 for (Int_t i = 0; i < nobjects; i++) {
562 R__b << fUIDs[i];
563 if (gDebug > 1) {
564 printf(" %d",fUIDs[i]);
565 if ((i > 0 && i%10 == 0) || (i == nobjects-1)) printf("\n");
566 }
567 }
568 R__b.SetByteCount(R__c, kTRUE);
569 }
570}
571
572////////////////////////////////////////////////////////////////////////////////
573/// Return the object in the first slot.
574
576{
577 TObject *obj = GetFromTable(0);
578 if (!obj)
579 obj = fPID->GetObjectWithID(fUIDs[0]);
580 return obj;
581}
582
583////////////////////////////////////////////////////////////////////////////////
584/// Return the object in the last filled slot. Returns 0 if no entries.
585
587{
588 if (fLast == -1)
589 return nullptr;
590
592 if (!obj)
594 return obj;
595}
596
597////////////////////////////////////////////////////////////////////////////////
598/// Return the number of objects in array (i.e. number of non-empty slots).
599/// Attention: use this method ONLY if you want to know the number of
600/// non-empty slots. This function loops over the complete array and
601/// is therefore very slow when applied in a loop. Most of the time you
602/// better use GetLast()+1.
603
605{
606 Int_t cnt = 0;
607
608 for (Int_t i = 0; i < fSize; i++)
609 if (fUIDs[i]) cnt++;
610
611 return cnt;
612}
613
614////////////////////////////////////////////////////////////////////////////////
615/// Return absolute index to last object in array. Returns -1 in case
616/// array is empty.
617
619{
620 // For efficiency we need sometimes to update fLast so we have
621 // to cast const away. Ugly, but making GetAbsLast() not const breaks
622 // many other const functions.
623 if (fLast == -2) {
624 for (Int_t i = fSize-1; i >= 0; i--)
625 if (fUIDs[i]) {
626 ((TRefArray*)this)->fLast = i;
627 return fLast;
628 }
629 ((TRefArray*)this)->fLast = -1;
630 }
631 return fLast;
632}
633
634////////////////////////////////////////////////////////////////////////////////
635/// Return index of last object in array. Returns lowerBound-1 in case
636/// array is empty.
637
639{
640 return fLowerBound+GetAbsLast();
641}
642
643////////////////////////////////////////////////////////////////////////////////
644/// Return address of pointer obj.
645
647{
648 //Int_t index = IndexOf(obj);
649 //return &fCont[index];
650 return nullptr;
651}
652
653////////////////////////////////////////////////////////////////////////////////
654/// Return UID of element at.
655
657{
658 int j = at-fLowerBound;
659 if (j >= 0 && j < fSize) {
660 if (!fPID) return 0;
661 return fUIDs[j];
662 }
663 BoundsOk("At", at);
664 return 0;
665}
666
667////////////////////////////////////////////////////////////////////////////////
668/// - obj != 0 Return index of object in array.
669/// Returns lowerBound-1 in case array doesn't contain the obj.
670///
671/// - obj == 0 Return the index of the first empty slot.
672/// Returns lowerBound-1 in case array doesn't contain any empty slot.
673
675{
676 Int_t i;
677 if (obj) {
679 return fLowerBound-1;
680 }
681 for (i = 0; i < fSize; i++)
682 if (fUIDs[i] && fPID->GetObjectWithID(fUIDs[i]) == obj)
683 return i+fLowerBound;
684 } else { // Look for the first empty slot
685 for (i = 0; i < fSize; i++)
686 if (!fUIDs[i])
687 return i+fLowerBound;
688 }
689
690 return fLowerBound-1;
691}
692
693////////////////////////////////////////////////////////////////////////////////
694/// Initialize a TRefArray.
695
696void TRefArray::Init(Int_t s, Int_t lowerBound)
697{
698 if (s != fSize) {
699 if (fUIDs) {
700 delete [] fUIDs;
701 fUIDs = nullptr;
702 }
703 fSize = s;
704
705 if (fSize) {
706 fUIDs = new UInt_t[fSize];
707 for (Int_t i=0;i<s;i++)
708 fUIDs[i] = 0;
709 }
710 }
711
712 fLowerBound = lowerBound;
713 fLast = -1;
714 Changed();
715}
716
717////////////////////////////////////////////////////////////////////////////////
718/// Returns an array iterator.
719
721{
722 return new TRefArrayIter(this, dir);
723}
724
725////////////////////////////////////////////////////////////////////////////////
726/// Generate an out-of-bounds error. Always returns false.
727
728Bool_t TRefArray::OutOfBoundsError(const char *where, Int_t i) const
729{
730 Error(where, "index %d out of bounds (size: %d, this: 0x%zx)", i, fSize, (size_t)this);
731 return kFALSE;
732}
733
734////////////////////////////////////////////////////////////////////////////////
735/// Remove object at index idx.
736
738{
739 if (!BoundsOk("RemoveAt", idx)) return nullptr;
740
741 int i = idx-fLowerBound;
742
743 TObject *obj = nullptr;
744 if (fUIDs[i]) {
745 if (!TProcessID::IsValid(fPID)) return nullptr;
746 obj = fPID->GetObjectWithID(fUIDs[i]);
747 fUIDs[i] = 0;
748 // recalculate array size
749 if (i == fLast)
750 do {
751 fLast--;
752 } while (fLast >= 0 && fUIDs[fLast] == 0);
753 Changed();
754 }
755
756 return obj;
757}
758
759////////////////////////////////////////////////////////////////////////////////
760/// Remove object from array.
761
763{
764 if (!obj) return nullptr;
765
766 Int_t idx = IndexOf(obj) - fLowerBound;
767
768 if (idx == -1) return nullptr;
769
770 TObject *ob = fPID->GetObjectWithID(fUIDs[idx]);
771 fUIDs[idx] = 0;
772 // recalculate array size
773 if (idx == fLast)
774 do {
775 fLast--;
776 } while (fLast >= 0 && fUIDs[fLast] == 0);
777 Changed();
778 return ob;
779}
780
781////////////////////////////////////////////////////////////////////////////////
782/// Set index of last object in array, effectively truncating the
783/// array. Use carefully since whenever last position has to be
784/// recalculated, e.g. after a Remove() or Sort() it will be reset
785/// to the last non-empty slot. If last is -2 this will force the
786/// recalculation of the last used slot.
787
789{
790 if (last == -2)
791 fLast = -2;
792 else if (BoundsOk("SetLast", last))
793 fLast = last - fLowerBound;
794}
795
796////////////////////////////////////////////////////////////////////////////////
797/// If objects in array are sortable (i.e. IsSortable() returns true
798/// for all objects) then sort array.
799
801{
802 Error("Sort","Function not yet implemented");
803/*
804 if (GetAbsLast() == -1 || fSorted) return;
805 for (Int_t i = 0; i < fSize; i++)
806 if (fUIDs[i]) {
807 if (!fUIDs[i]->IsSortable()) {
808 Error("Sort", "objects in array are not sortable");
809 return;
810 }
811 }
812
813 QSort(fUIDs, 0, TMath::Min(fSize, upto-fLowerBound));
814
815 fLast = -2;
816 fSorted = kTRUE;
817*/
818}
819
820////////////////////////////////////////////////////////////////////////////////
821/// Find object using a binary search. Array must first have been sorted.
822/// Search can be limited by setting upto to desired index.
823
825{
826 Error("BinarySearch","Function not yet implemented");
827/*
828 Int_t base, position, last, result = 0;
829 TObject *op2;
830
831 if (!op) return -1;
832
833 if (!fSorted) {
834 Error("BinarySearch", "array must first be sorted");
835 return -1;
836 }
837
838 base = 0;
839 last = TMath::Min(fSize, upto-fLowerBound) - 1;
840
841 while (last >= base) {
842 //position = (base+last) / 2;
843 //op2 = fCont[position];
844 //if (op2 && (result = op->Compare(op2)) == 0)
845 // return position + fLowerBound;
846 //if (!op2 || result < 0)
847 // last = position-1;
848 //else
849 // base = position+1;
850 }
851*/
852 return -1;
853}
854
855/** \class TRefArrayIter
856Iterator of object array.
857*/
858
859
860////////////////////////////////////////////////////////////////////////////////
861/// Create array iterator. By default the iteration direction
862/// is kIterForward. To go backward use kIterBackward.
863
865{
866 fArray = arr;
867 fDirection = dir;
868 Reset();
869}
870
871////////////////////////////////////////////////////////////////////////////////
872/// Copy ctor.
873
875{
876 fArray = iter.fArray;
877 fDirection = iter.fDirection;
878 fCursor = iter.fCursor;
879 fCurCursor = iter.fCurCursor;
880}
881
882////////////////////////////////////////////////////////////////////////////////
883/// Overridden assignment operator.
884
886{
887 if (this != &rhs && rhs.IsA() == TRefArrayIter::Class()) {
888 const TRefArrayIter &rhs1 = (const TRefArrayIter &)rhs;
889 fArray = rhs1.fArray;
890 fDirection = rhs1.fDirection;
891 fCursor = rhs1.fCursor;
892 fCurCursor = rhs1.fCurCursor;
893 }
894 return *this;
895}
896
897////////////////////////////////////////////////////////////////////////////////
898/// Overloaded assignment operator.
899
901{
902 if (this != &rhs) {
903 fArray = rhs.fArray;
905 fCursor = rhs.fCursor;
907 }
908 return *this;
909}
910
911////////////////////////////////////////////////////////////////////////////////
912/// Return next object in array. Returns 0 when no more objects in array.
913
915{
916 if (fDirection == kIterForward) {
917 for ( ; fCursor < fArray->Capacity() && !fArray->At(fCursor+fArray->LowerBound());
918 fCursor++) { }
919
921 if (fCursor < fArray->Capacity()) {
922 fCursor++;
924 }
925 } else {
926 for ( ; fCursor >= 0 && !fArray->At(fCursor);
927 fCursor--) { }
928
930 if (fCursor >= 0) {
931 fCursor--;
933 }
934 }
935 return nullptr;
936}
937
938////////////////////////////////////////////////////////////////////////////////
939/// Reset array iterator.
940
942{
944 fCursor = 0;
945 else
946 fCursor = fArray->Capacity() - 1;
947
949}
950
951////////////////////////////////////////////////////////////////////////////////
952/// This operator compares two TIterator objects.
953
955{
956 if (aIter.IsA() == TRefArrayIter::Class()) {
957 const TRefArrayIter &iter(dynamic_cast<const TRefArrayIter &>(aIter));
958 return (fCurCursor != iter.fCurCursor);
959 }
960 return false; // for base class we don't implement a comparison
961}
962
963////////////////////////////////////////////////////////////////////////////////
964/// This operator compares two TRefArrayIter objects.
965
967{
968 return (fCurCursor != aIter.fCurCursor);
969}
970
971////////////////////////////////////////////////////////////////////////////////
972/// Return current object or nullptr.
973
975{
976 return (((fCurCursor >= 0) && (fCurCursor < fArray->Capacity())) ?
977 fArray->At(fCurCursor) : nullptr);
978}
#define a(i)
Definition RSha256.hxx:99
unsigned short UShort_t
Unsigned Short integer 2 bytes (unsigned short)
Definition RtypesCore.h:55
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
const Bool_t kIterForward
Definition TCollection.h:42
Int_t gDebug
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
Definition TROOT.cxx:792
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
virtual TProcessID * ReadProcessID(UShort_t pidf)=0
Return the current Process-ID.
Definition TBuffer.cxx:345
virtual UShort_t WriteProcessID(TProcessID *pid)=0
Always return 0 (current processID).
Definition TBuffer.cxx:354
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
virtual UShort_t GetPidOffset() const =0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
Int_t Capacity() const
virtual Int_t GrowBy(Int_t delta) const
Increase the collection's capacity by delta slots.
TString fName
Iterator abstract base class.
Definition TIterator.h:30
virtual TClass * IsA() const
Definition TIterator.h:48
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
Mother of all ROOT objects.
Definition TObject.h:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:479
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:995
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1082
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1096
@ kHasUUID
if object has a TUUID (its fUniqueID=UUIDNumber)
Definition TObject.h:75
@ kIsReferenced
if object is referenced by a TRef or TRefArray
Definition TObject.h:74
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1070
A TProcessID identifies a ROOT job in a unique way in time and space.
Definition TProcessID.h:74
static Bool_t IsValid(TProcessID *pid)
static function. return kTRUE if pid is a valid TProcessID
static UInt_t AssignID(TObject *obj)
static function returning the ID assigned to obj If the object is not yet referenced,...
static TProcessID * GetSessionProcessID()
static function returning the pointer to the session TProcessID
static TProcessID * GetProcessWithUID(const TObject *obj)
static function returning a pointer to TProcessID with its pid encoded in the highest byte of obj->Ge...
TObject * GetObjectWithID(UInt_t uid)
returns the TObject with unique identifier uid in the table of objects
static UInt_t GetObjectCount()
Return the current referenced object count fgNumber is incremented every time a new object is referen...
Iterator of object array.
Definition TRefArray.h:122
TObject * Next() override
Return next object in array. Returns 0 when no more objects in array.
Bool_t fDirection
Definition TRefArray.h:128
void Reset() override
Reset array iterator.
const TRefArray * fArray
Definition TRefArray.h:125
static TClass * Class()
TIterator & operator=(const TIterator &rhs) override
Overridden assignment operator.
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
Int_t fCurCursor
Definition TRefArray.h:126
TObject * operator*() const override
Return current object or nullptr.
An array of references to TObjects.
Definition TRefArray.h:33
void AddAt(TObject *obj, Int_t idx) override
Add object at position ids.
TObject * At(Int_t idx) const override
Definition TRefArray.h:188
void SetLast(Int_t last)
Set index of last object in array, effectively truncating the array.
Int_t fLowerBound
Definition TRefArray.h:40
TProcessID * fPID
Definition TRefArray.h:38
TRefArray(TProcessID *pid=nullptr)
default constructor
TObject * RemoveAt(Int_t idx) override
Remove object at index idx.
void Init(Int_t s, Int_t lowerBound)
Initialize a TRefArray.
virtual Int_t AddAtFree(TObject *obj)
Return the position of the new object.
TObject * GetFromTable(Int_t idx) const
the reference may be in the TRefTable
void AddFirst(TObject *obj) override
Add object in the first slot of the array.
UInt_t GetUID(Int_t at) const
Return UID of element at.
TClass * IsA() const override
Definition TRefArray.h:104
virtual ~TRefArray()
Usual destructor (The object pointed to by the array are never deleted).
virtual void Expand(Int_t newSize)
Expand or shrink the array to newSize elements.
void Delete(Option_t *option="") override
Remove all objects from the array and free the internal memory.
UInt_t * fUIDs
Definition TRefArray.h:39
Bool_t BoundsOk(const char *where, Int_t at) const
Definition TRefArray.h:163
Bool_t OutOfBoundsError(const char *where, Int_t i) const
Generate an out-of-bounds error. Always returns false.
Int_t GetLast() const override
Return index of last object in array.
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Returns an array iterator.
void AddLast(TObject *obj) override
Add object in the next empty slot in the array.
virtual Int_t BinarySearch(TObject *obj, Int_t upto=kMaxInt)
Find object using a binary search.
TObject * After(const TObject *obj) const override
Return the object after obj. Returns 0 if obj is last object.
void AddAfter(const TObject *after, TObject *obj) override
Add object in the slot after object after.
friend class TRefArrayIter
Definition TRefArray.h:35
void AddBefore(const TObject *before, TObject *obj) override
Add object in the slot before object before.
void Clear(Option_t *option="") override
Remove all objects from the array.
virtual void AddAtAndExpand(TObject *obj, Int_t idx)
Add object at position idx.
Int_t GetAbsLast() const
Return absolute index to last object in array.
TObject * Remove(TObject *obj) override
Remove object from array.
Int_t IndexOf(const TObject *obj) const override
Int_t fLast
Definition TRefArray.h:41
void Streamer(TBuffer &) override
Stream all objects in the array to or from the I/O buffer.
TRefArray & operator=(const TRefArray &a)
Assignment operator.
virtual void Sort(Int_t upto=kMaxInt)
If objects in array are sortable (i.e.
TObject * First() const override
Return the object in the first slot.
TObject ** GetObjectRef(const TObject *obj) const override
Return address of pointer obj.
Bool_t GetObjectUID(Int_t &uid, TObject *obj, const char *methodname)
Private/static function, check for validity of pid.
Int_t GetEntries() const override
Return the number of objects in array (i.e.
virtual void Compress()
Remove empty slots from array.
Int_t LowerBound() const
Definition TRefArray.h:97
TObject * Before(const TObject *obj) const override
Return the object before obj. Returns 0 if obj is first object.
TObject * Last() const override
Return the object in the last filled slot. Returns 0 if no entries.
A TRefTable maintains the association between a referenced object and the parent object supporting th...
Definition TRefTable.h:35
virtual void SetUID(UInt_t uid, TProcessID *context=nullptr)
Definition TRefTable.h:91
static TRefTable * GetRefTable()
Static function returning the current TRefTable.
Bool_t Notify() override
This function is called by TRef::Streamer or TStreamerInfo::ReadBuffer when reading a reference.
Sequenceable collection abstract base class.
virtual void Changed()
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1493
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460