📄 stringdictc.c
字号:
/* Loop through the list until the entry is found *//* Compare actual objects */ register int i; for (i=0; (i<_count && (_list[i] != obj)); i++); if (i == _count) { return NULL_INDEX; } return i;}/*-------------------------------------------------------------*/StringDictCEntry*StringDictCEntryList::insert(const StringDictCEntry& obj, unsigned i){/* If this list is sorted, ignore the given index and compute the real one */ if ( sorted ) i = orderOf(obj);/* Otherwise, see if index is valid */ if (i > _count) return (StringDictCEntry*)NULL;/* Don't allow same address in list twice */ if ( !allow_duplicates && includes(obj) ) return (StringDictCEntry*)NULL;/* See if there is enough space for another entry */ if ( _count >= _space ) grow();/* Insert entry in position i *//* Make a space for the new entry by moving all others back */ register int j; for (j=_count; j>i; j--) { _list[j] = _list[j-1]; /* Copying objects here */ } _count++; StringDictCEntry *tp = (StringDictCEntry *)&obj;/* Copy the object to the list object */ _list[i] = *tp; return &_list[i];}/*-------------------------------------------------------------*/voidStringDictCEntryList::remove(unsigned index){ if (index < _count) { /* Move entries forward in list and overwrite this one */ register int i; for (i=index+1; i<_count; i++) { _list[i-1] = _list[i]; /* Copying objects here */ } _count--; if ( autoShrink ) shrink(); }}/*-------------------------------------------------------------*/voidStringDictCEntryList::remove(const StringDictCEntry& obj){/* Look up this entry */ register unsigned i = indexOf(obj); if ( i != NULL_INDEX ) remove(i);}/*-------------------------------------------------------------*//* Return a pointer to the list entry just before this one */StringDictCEntry*StringDictCEntryList::before(const StringDictCEntry& obj) const{/* Look up this entry */ register int i = indexOf(obj); if ( i == NULL_INDEX || i == 0 ) return (StringDictCEntry*)NULL; return &_list[i-1];}/*-------------------------------------------------------------*/unsignedStringDictCEntryList::orderOf(const StringDictCEntry& 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 StringDictCEntryList::orderOf *//*-------------------------------------------------------------*/intStringDictCEntryList::compare(const void *a, const void *b){ StringDictCEntry *ta = (StringDictCEntry *)a; StringDictCEntry *tb = (StringDictCEntry *)b; /* return ta->compare(*tb); */ return (*ta < *tb) ? -1 : ((*ta > *tb) ? 1 : 0);}/*-------------------------------------------------------------*/voidStringDictCEntryList::sort(){ qsort((void *)_list, (size_t)_count, (size_t)sizeof(StringDictCEntry), (int (*)(const void*, const void*))compare);}/*-------------------------------------------------------------*/voidStringDictCEntryList::sort(int (*compar)(const void*, const void*)){ if ( !compar ) return; qsort((void *)_list, (size_t)_count, (size_t)sizeof(StringDictCEntry), 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. *//* * ODictC.meth -- method definition file for class ODictC * * This type of list store the members themselves * PDictC stores only pointers to the members *//* Copy one dictionary to another */StringDictC&StringDictC::operator=(const StringDictC& d){ if ( this != &d ) { _count = 0; register int count = d.size();//// Add each entry in source to destination// register int i; for (i=0; i<count; i++) { StringDictCEntry *e = d[i]; add(e->key, e->val); } } return *this;}/* Lookup up entries */intStringDictC::indexOf(const StringDictCEntry& entry) const{ return StringDictCEntryList::indexOf(entry);}intStringDictC::includes(const StringC& key) const{ return (indexOf(key) != NULL_INDEX);}intStringDictC::includes(const StringDictCEntry& entry) const{ return StringDictCEntryList::includes(entry);}/* Deleting entries */voidStringDictC::remove(unsigned index){ StringDictCEntryList::remove(index);}voidStringDictC::remove(StringDictCEntry& entry){ StringDictCEntryList::remove(entry);}/*-------------------------------------------------------------*/intStringDictC::indexOf(const StringC& 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;}/*-------------------------------------------------------------*/StringC*StringDictC::definitionOf(const StringC& key) const{ int i = indexOf(key); /* Look up this entry */ return ((i==NULL_INDEX) ? (StringC*)NULL : &_list[i].val);}/*-------------------------------------------------------------*/StringC&StringDictC::keyOf(const int index) const{ return _list[index].key;}/*-------------------------------------------------------------*/StringC&StringDictC::valOf(const int index) const{ return _list[index].val;}/*-------------------------------------------------------------*/StringDictCEntry*StringDictC::entryOf(const StringC& key) const{ int i = indexOf(key); /* Get index of key */ return ((i==NULL_INDEX) ? (StringDictCEntry*)NULL : &_list[i]);}/*-------------------------------------------------------------*/StringDictCEntry*StringDictC::add(const StringC& key, const StringC& val){/* Check if this entry is already present */ if ( includes(key) ) { if ( *definitionOf(key) != val ) { return (StringDictCEntry*)NULL; /* Same key with different value */ } else { return entryOf(key); } } else { /* Add it */ StringDictCEntry ent(key, val); return append(ent); }}/*-------------------------------------------------------------*//* Change the definition for this key */StringDictCEntry*StringDictC::modify(const StringC& key, const StringC& val){ int i = indexOf(key); /* Look up this entry */ if ( i == NULL_INDEX ) { return (StringDictCEntry*)NULL; } else { _list[i].val = val; /* Change it */ return &_list[i]; }}/*-------------------------------------------------------------*/StringDictCEntry*StringDictC::modify(unsigned index, const StringC& val){ if (index < _count) { _list[index].val = val; /* Change it */ return &_list[index]; } else { return (StringDictCEntry*)NULL; }}/*-------------------------------------------------------------*/voidStringDictC::remove( StringC& key){ int i = indexOf(key); /* Look up this entry */ if ( i != NULL_INDEX ) { StringDictCEntryList::remove(i); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -