00001 /* file "index.h" */ 00002 00003 00004 /* 00005 Copyright (c) 1996, 1997 Stanford University 00006 00007 All rights reserved. 00008 00009 This software is provided under the terms described in 00010 the "suif_copyright.h" include file. 00011 */ 00012 00013 #include <common/suif_copyright.h> 00014 00015 00016 #ifndef STY_INDEX_H 00017 #define STY_INDEX_H 00018 00019 //#ifndef SUPPRESS_PRAGMA_INTERFACE 00020 //#pragma interface 00021 //#endif 00022 00023 00024 /* 00025 This is the definition of the index template and related 00026 templates, which are templates for classes implementing mappings 00027 from arbitrary keys to arbitrary types, for sty, the first-level 00028 main library of the SUIF system. 00029 */ 00030 00031 #include "referenced_item.h" 00032 00033 template <class key_t, class elem_t> class Index; 00034 00035 template <class key_t, class elem_t> class index_handle : public ri_reference 00036 { 00037 friend class Index<key_t, elem_t>; 00038 00039 private: 00040 index_handle(referenced_item *init_data) : ri_reference(init_data) { } 00041 00042 public: 00043 index_handle(void) { } 00044 index_handle(const index_handle<key_t, elem_t> &other) : 00045 ri_reference(other._data) { } 00046 ~index_handle(void) { } 00047 00048 const index_handle<key_t, elem_t> &operator=(const index_handle<key_t, elem_t> &other) 00049 { set_raw_referenced_item(other._data); return(*this); } 00050 bool operator==(const index_handle<key_t, elem_t> &other) const 00051 { return (_data == other._data); } 00052 bool operator!=(const index_handle<key_t, elem_t> &other) const 00053 { return (_data != other._data); } 00054 }; 00055 00056 template <class key_t, class elem_t> class index_ref; 00057 00058 template <class key_t, class elem_t> class Index 00059 { 00060 friend class index_ref<key_t, elem_t>; 00061 00062 private: 00063 virtual void add_ref(void) { } 00064 virtual void remove_ref(void) { } 00065 virtual bool delete_me(void) { return false; } 00066 00067 protected: 00068 Index(void) { } 00069 public: 00070 virtual ~Index(void) { } 00071 protected: 00072 00073 index_handle<key_t, elem_t> build_handle(referenced_item *data) const 00074 { return index_handle<key_t, elem_t>(data); } 00075 referenced_item *from_handle(index_handle<key_t, elem_t> the_handle) const 00076 { return the_handle._data; } 00077 00078 public: 00079 virtual elem_t lookup(key_t) const = 0; 00080 virtual bool exists(key_t) const = 0; 00081 virtual index_handle<key_t, elem_t> enter(key_t, elem_t) = 0; 00082 virtual void remove(key_t) = 0; 00083 00084 virtual index_handle<key_t, elem_t> lookup_handle(key_t) const = 0; 00085 virtual elem_t elem(index_handle<key_t, elem_t>) const = 0; 00086 virtual void remove(index_handle<key_t, elem_t>) = 0; 00087 00088 virtual void clear(void) = 0; 00089 }; 00090 00091 template <class key_t, class elem_t> class index_ref : 00092 public Index<key_t, elem_t> 00093 { 00094 private: 00095 Index<key_t, elem_t> *_real_index; 00096 00097 public: 00098 index_ref(void) : _real_index(0) { } 00099 index_ref(const index_ref &other) : _real_index(other._real_index) 00100 { 00101 if (_real_index != 0) 00102 _real_index->add_ref(); 00103 } 00104 virtual ~index_ref(void) 00105 { 00106 if (_real_index != 0) 00107 { 00108 _real_index->remove_ref(); 00109 if (_real_index->delete_me()) 00110 delete _real_index; 00111 } 00112 } 00113 00114 elem_t lookup(key_t the_key) const 00115 { return _real_index->lookup(the_key); } 00116 bool exists(key_t the_key) const 00117 { return _real_index->exists(the_key); } 00118 index_handle<key_t, elem_t> enter(key_t the_key, elem_t the_elem) 00119 { return _real_index->enter(the_key, the_elem); } 00120 void remove(key_t the_key) { _real_index->remove(the_key); } 00121 00122 index_handle<key_t, elem_t> lookup_handle(key_t the_key) const 00123 { return _real_index->lookup_handle(the_key); } 00124 elem_t elem(index_handle<key_t, elem_t> handle) const 00125 { return _real_index->elem(handle); } 00126 void remove(index_handle<key_t, elem_t> handle) 00127 { _real_index->remove(handle); } 00128 00129 void clear(void) { _real_index->clear(); } 00130 00131 const index_ref<key_t, elem_t> &operator=(const index_ref<key_t, elem_t> &other) 00132 { 00133 if (&other == this) 00134 return(*this); 00135 if (_real_index != 0) 00136 { 00137 _real_index->remove_ref(); 00138 if (_real_index->delete_me()) 00139 delete _real_index; 00140 } 00141 _real_index = other._real_index; 00142 if (_real_index != 0) 00143 _real_index->add_ref(); 00144 return(*this); 00145 } 00146 }; 00147 00148 00149 #endif /* STY_INDEX_H */