00001 #ifndef __UT_Pointers__
00002 #define __UT_Pointers__
00003
00004 #include "UT_API.h"
00005
00006
00007
00008
00009
00010
00011
00012 #include "UT_Assert.h"
00013 #include "UT_Iterator.h"
00014 #include "UT_Notifier.h"
00015
00016 typedef void *UT_ThingP;
00017
00018 class UT_API UT_Pointers {
00019
00020 public:
00021 UT_Pointers() { list = (UT_ThingP *)0; nentries = size = 0; }
00022 virtual ~UT_Pointers();
00023
00024 int index(UT_ThingP thing) const;
00025 int addMember(UT_ThingP thing);
00026 int removeMember(UT_ThingP thing);
00027 UT_ThingP removeMember(int idx);
00028 int removeMembers(int from, int to);
00029 void insertMember(UT_ThingP thing, int idx);
00030
00031 typedef UT_Iterator<UT_ThingP, UT_Pointers> Iterator;
00032
00033 Iterator iterator(int idx = 0)
00034 {
00035 return UT_Iterator<UT_ThingP, UT_Pointers>(*this, &myNotifier, idx);
00036 }
00037
00038 Iterator iteratorReverse(int idx = 0)
00039 {
00040 return UT_Iterator<UT_ThingP, UT_Pointers>(*this, &myNotifier, idx,
00041 true);
00042 }
00043
00044 int entries() const { return nentries; }
00045
00046
00047 int count() const { return entries(); }
00048 int getSize() const { return size; }
00049 void grow(int newsize);
00050
00051
00052 void empty();
00053 void clear();
00054
00055 UT_ThingP &operator[](int idx);
00056 UT_ThingP operator()(int idx) const
00057 {
00058 UT_ASSERT_P(idx >= 0 && (unsigned)idx < nentries);
00059 if( ((unsigned)idx) >= nentries || idx<0 ) return 0;
00060
00061 return *(list + idx);
00062 }
00063
00064
00065 void sortPointers(int (*compare)(const void *, const void *));
00066 int searchPointers(int (*compare)(const void *, const void *),
00067 const void *key) const;
00068
00069 void reverse();
00070
00071 const UT_ThingP &null() const { return theNullPointer; }
00072
00073 private:
00074
00075 UT_NotifierImpl<UT_IteratorEvent &> myNotifier;
00076
00077
00078 void notifyAdd(int lo, int hi)
00079 {
00080 UT_IteratorEvent event(UT_IteratorEvent::ADD, lo, hi);
00081 myNotifier.notifyObservers(event);
00082 }
00083 void notifyAdd(int idx) { notifyAdd(idx, idx); }
00084 void notifyRemove(int lo, int hi)
00085 {
00086 UT_IteratorEvent event(UT_IteratorEvent::REMOVE, lo, hi);
00087 myNotifier.notifyObservers(event);
00088 }
00089 void notifyRemove(int idx) { notifyRemove(idx, idx); }
00090 void notifyInvalid() {
00091 UT_IteratorEvent event(UT_IteratorEvent::INVALID, 0, 0);
00092 myNotifier.notifyObservers(event);
00093 }
00094
00095 UT_ThingP *list;
00096 unsigned int size;
00097 unsigned int nentries;
00098
00099 static UT_ThingP theNullPointer;
00100 };
00101
00102 #endif