📄 itemdatadictc.c
字号:
}}/*-------------------------------------------------------------*/voidItemDataDictCEntryList::remove(const ItemDataDictCEntry& obj){/* Look up this entry */ register int i = indexOf(obj); if ( i != NULL_INDEX ) remove(i);}/*-------------------------------------------------------------*/voidItemDataDictCEntryList::remove(const ItemDataDictCEntry* op){/* Look up this entry */ register int i = indexOf(op); if ( i != NULL_INDEX ) remove(i);}/*-------------------------------------------------------------*/voidItemDataDictCEntryList::removeNulls(){/* Collapse list */ register int j = 0; register int i; for (i=0; i<_count; i++) { if ( _list[i] != (ItemDataDictCEntry*)NULL ) { if ( j != i ) _list[j] = _list[i]; /* Just copying pointers here */ j++; } } if ( j != _count ) { _count = j; if ( autoShrink ) shrink(); }}/*-------------------------------------------------------------*//* Return a pointer to the list entry just before this one */ItemDataDictCEntry*ItemDataDictCEntryList::before(const ItemDataDictCEntry* op) const{/* Look up this entry */ register int i = indexOf(op); if ( i == NULL_INDEX || i == 0 ) return (ItemDataDictCEntry*)NULL; return _list[i-1];}/*-------------------------------------------------------------*/unsignedItemDataDictCEntryList::orderOf(const ItemDataDictCEntry& obj) const{/* * This algorithm is a simple binary search * If the object is in the list, the index of the object is returned. * If the object is not in the list, the index where the object would fit * in the sorting order is returned. *//* Check special cases first */ if ( _count == 0 || obj < *_list[0] ) return 0; if ( obj > *_list[_count-1] ) return _count; int low = 0; int high = _count - 1; int mid = 0; while ( low <= high ) { mid = (low + high) / 2; /* int comp = obj.compare(*_list[mid]); */ int comp = (obj < *_list[mid]) ? -1 : ((obj > *_list[mid]) ? 1 : 0); if ( comp > 0 ) low = mid + 1; else if (comp == 0) return mid; else high = mid - 1; }/* * If the last comparison said the value was greater than the mid, * then we want to return an index 1 greater than the mid because * the entry should get inserted after the mid. */ if (low > mid) mid++; return mid;} /* End ItemDataDictCEntryList::orderOf *//*-------------------------------------------------------------*/intItemDataDictCEntryList::compare(const void *a, const void *b){ ItemDataDictCEntry *ta = *(ItemDataDictCEntry **)a; ItemDataDictCEntry *tb = *(ItemDataDictCEntry **)b; /* return ta->compare(*tb); */ return (*ta < *tb) ? -1 : ((*ta > *tb) ? 1 : 0);}/*-------------------------------------------------------------*/voidItemDataDictCEntryList::sort(){ qsort((void *)_list, (size_t)_count, (size_t)sizeof(ItemDataDictCEntry*), (int (*)(const void*, const void*))compare);}/*-------------------------------------------------------------*/voidItemDataDictCEntryList::sort(int (*compar)(const void*, const void*)){ if ( !compar ) return; qsort((void *)_list, (size_t)_count, (size_t)sizeof(ItemDataDictCEntry*), compar);}/* * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. * 1315 Dell Avenue * Campbell, CA 95008 * * Author: Greg Hilton * Contributors: Tom Lang, Frank Bieser, and others * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * http://www.gnu.org/copyleft/gpl.html * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* * PDictC.meth -- method definition file for class PDictC * * This type of dictionary stores only pointers to the keys and values * ODictC.h stores the actual keys and values *//* Copy one dictionary to another */ItemDataDictC&ItemDataDictC::operator=(const ItemDataDictC& d){ int count = d.size(); /* Add each entry in source to destination */ for (int i=0; i<count; i++) { ItemDataDictCEntry *e = d[i]; add(e->key, e->val); } return *this;}intItemDataDictC::indexOf(const ItemDataDictCEntry& entry) const{ return ItemDataDictCEntryList::indexOf(entry);}intItemDataDictC::indexOf(const ItemDataDictCEntry* entry) const{ return ItemDataDictCEntryList::indexOf(entry);}intItemDataDictC::includes(const VItemC& key) const{ return (indexOf(key) != NULL_INDEX);}intItemDataDictC::includes(const VItemC* kp) const{ return (indexOf(kp) != NULL_INDEX);}intItemDataDictC::includes(const ItemDataDictCEntry& entry) const{ return ItemDataDictCEntryList::includes(entry);}intItemDataDictC::includes(const ItemDataDictCEntry* entry) const{ return ItemDataDictCEntryList::includes(entry);}voidItemDataDictC::remove(unsigned index){ if ( index < _count ) { ItemDataDictCEntry *entry = _list[index]; if (entry) { ItemDataDictCEntryList::remove(index); delete entry; } }}voidItemDataDictC::remove(const ItemDataDictCEntry& entry){ ItemDataDictCEntryList::remove(entry); delete (ItemDataDictCEntry *)&entry;}voidItemDataDictC::remove(const ItemDataDictCEntry* entry){ if (entry) { ItemDataDictCEntryList::remove(entry); delete (ItemDataDictCEntry *)entry; }}/* Printing */voidItemDataDictC::printOn(ostream& strm) const{ ItemDataDictCEntryList::printOn(strm);}/*-------------------------------------------------------------*/intItemDataDictC::indexOf(const VItemC& key) const{/* Loop through list, checking keys for a match */ register int i; for (i=0; i<_count; i++) {/* If this one matches, return the index */ if ( *(_list[i]->key) == key ) { return i; } } return NULL_INDEX;}/*-------------------------------------------------------------*/intItemDataDictC::indexOf(const VItemC* kp) const{ if ( !kp ) return NULL_INDEX;/* Loop through list, checking key pointers for a match */ register int i; for (i=0; i<_count; i++) {/* If this one matches, return the index */ if ( _list[i]->key == (VItemC *)kp ) { return i; } } return NULL_INDEX;}/*-------------------------------------------------------------*/ItemDataC*ItemDataDictC::definitionOf(const VItemC& key) const{ int i = indexOf(key); /* Look up this entry */ return ((i==NULL_INDEX) ? (ItemDataC*)NULL : _list[i]->val);}/*-------------------------------------------------------------*/ItemDataC*ItemDataDictC::definitionOf(const VItemC* kp) const{ int i = indexOf(kp); /* Look up this entry */ return ((i==NULL_INDEX) ? (ItemDataC*)NULL : _list[i]->val);}/*-------------------------------------------------------------*/ItemDataC*ItemDataDictC::valOf(const int index) const{ return _list[index]->val;}/*-------------------------------------------------------------*/VItemC*ItemDataDictC::keyOf(const int index) const{ return _list[index]->key;}/*-------------------------------------------------------------*/ItemDataDictCEntry*ItemDataDictC::entryOf(const VItemC& key) const{ int i = indexOf(key); /* Get index of key */ return ((i==NULL_INDEX) ? (ItemDataDictCEntry*)NULL : _list[i]);}/*-------------------------------------------------------------*/ItemDataDictCEntry*ItemDataDictC::entryOf(const VItemC* kp) const{ int i = indexOf(kp); /* Get index of key */ return ((i==NULL_INDEX) ? (ItemDataDictCEntry*)NULL : _list[i]);}/*-------------------------------------------------------------*/ItemDataDictCEntry*ItemDataDictC::add(const VItemC* kp, const ItemDataC* vp){/* Check if this entry is already present */ if ( includes(kp) ) { if ( definitionOf(kp) != vp ) { return (ItemDataDictCEntry*)NULL; /* Same key with different value */ } else { return entryOf(kp); } } else { /* Add it */ /* Allocate a new entry */ ItemDataDictCEntry *ent = new ItemDataDictCEntry(kp, vp); append(ent); return ent; }}/*-------------------------------------------------------------*//* Change the definition for this key */ItemDataDictCEntry*ItemDataDictC::modify(const VItemC& key, const ItemDataC* vp){ int i = indexOf(key); /* Look up this entry */ if ( i == NULL_INDEX ) { return (ItemDataDictCEntry*)NULL; } else { _list[i]->val = (ItemDataC *)vp; /* Change it */ return _list[i]; }}/*-------------------------------------------------------------*/ItemDataDictCEntry*ItemDataDictC::modify(const VItemC* kp, const ItemDataC* vp){ int i = indexOf(kp); /* Look up this entry */ if ( i == NULL_INDEX ) { return (ItemDataDictCEntry*)NULL; } else { _list[i]->val = (ItemDataC *)vp; /* Change it */ return _list[i]; }}/*-------------------------------------------------------------*/ItemDataDictCEntry*ItemDataDictC::modify(unsigned index, const ItemDataC* vp){ if (index < _count) { _list[index]->val = (ItemDataC *)vp; /* Change it */ return _list[index]; } else { return (ItemDataDictCEntry*)NULL; }}/*-------------------------------------------------------------*/voidItemDataDictC::remove(const VItemC& key){ int i = indexOf(key); /* Look up this entry */ if ( i != NULL_INDEX ) { ItemDataDictCEntry *entry = _list[i]; if (entry) { ItemDataDictCEntryList::remove(i); delete entry; } }}/*-------------------------------------------------------------*/voidItemDataDictC::remove(const VItemC* kp){ int i = indexOf(kp); /* Look up this entry */ if ( i != NULL_INDEX ) { ItemDataDictCEntry *entry = _list[i]; if (entry) { ItemDataDictCEntryList::remove(i); delete entry; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -