2 * Simulator of microcontrollers (pobj.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 /*#include "pobjt.h"*/
39 ==========================================================================*
41 ==========================================================================*
46 * Initializing the object
49 cl_base::cl_base(void) {}
53 * Destructing the object: calling hte virtual Done method
56 cl_base::~cl_base(void) {}
59 int cl_base::init(void) {return(0);}
63 ==========================================================================*
65 ==========================================================================*
70 * Initializing a collection
73 cl_list::cl_list(t_index alimit, t_index adelta):
85 * Disposing object's variables
88 cl_list::~cl_list(void)
95 * Get indexed item from the collection
99 cl_list::at(t_index index)
104 return(Items[index]);
108 cl_list::operator[](t_index index)
113 return(Items[index]);
118 * Deleting the indexed item from the collection
122 cl_list::disconn_at(t_index index)
128 memmove(&Items[index], &Items[index+1], (count-index)*sizeof(void *));
133 * Deleting an item from the collection but not disposing it
137 cl_list::disconn(void *item)
139 disconn_at(index_of(item));
144 * Deleting all the items from the collection but not disposing them
148 cl_list::disconn_all(void)
155 * Deleting the indexed item from the collection and disposing it
159 cl_list::free_at(t_index index)
161 void *Item= at(index);
169 * Inserting a new item to the exact position
173 cl_list::add_at(t_index index, void *item)
178 set_limit(count + Delta);
180 memmove(&Items[index+1], &Items[index], (count-index)*sizeof(void *));
188 * Put a new item to the collection. This function replaces an existing
189 * item with a new one but it does not delete or dispose the old item!
193 cl_list::put_at(t_index index, void *item)
202 * Action taken when an error occure
206 cl_list::error(t_index code, t_index info)
209 "Collection index error. Code= %d, Info= %d.\n",
216 * Iterator method. This function calls 'Test' using every items as Test's
217 * argument until Test returns TRUE.
221 cl_list::first_that(match_func test, void *arg)
223 for (t_index i= 0; i < count; i++)
225 if (test(Items[i], arg)) return(Items[i]);
232 * Iterator method. This function calls 'Action' using every items as
237 cl_list::for_each(iterator_func action, void *arg)
239 for(t_index i= 0; i < count; i++)
240 action(Items[i], arg);
249 cl_list::free_item(void *item)
256 * Get the number of collected items.
260 cl_list::get_count(void)
287 * Returning the index of an item.
291 cl_list::index_of(void *item)
293 for (t_index i= 0; i < count; i++)
294 if (item == Items[i])
297 return(0); /* Needed by Sun! */
302 * Inserting a new item to the collection.
306 cl_list::add(void *item)
316 cl_list::push(void *item)
326 * Iterator method. This function calls 'Test' using every items
327 * (in reversed order) as Test's argument until Test returns TRUE.
331 cl_list::last_that(match_func test, void *arg)
333 for(t_index i= count; i > 0; i--)
334 if (test(Items[i-1], arg))
347 void **CurDst= Items;
348 void **CurSrc= Items;
349 void **Last = Items + count;
351 while (CurSrc < Last)
361 * Setting up the maximum number of items. This function may expand
362 * the size of the collection.
366 cl_list::set_limit(t_index alimit)
372 if (alimit > (int)max_list_size)
373 alimit= max_list_size;
380 AItems = new void *[alimit];
381 //i= ALimit*(sizeof(void *));
382 //AItems= (void **)malloc(i);
384 memcpy(AItems, Items, count*sizeof(void *));
394 ==========================================================================*
396 ==========================================================================*
401 * Initilizing the sorted collection
404 cl_sorted_list::cl_sorted_list(t_index alimit, t_index adelta):
405 cl_list(alimit, adelta)
411 cl_sorted_list::~cl_sorted_list(void) {}
415 * Get the address of the key field in an item.
419 cl_sorted_list::key_of(void *item)
426 * Get index of an item.
430 cl_sorted_list::index_of(void *item)
434 if (search(key_of(item), i) == 0)
451 * Inserting a new item to the collection
455 cl_sorted_list::add(void *item)
459 if (search(key_of(item), i) == 0 ||
460 Duplicates) // order dependency!
461 add_at(i, item); // must do Search
469 * Searching an item using binary search.
473 cl_sorted_list::search(void *key, t_index &index)
476 t_index h = count - 1;
481 t_index i= (l + h) >> 1;
482 t_index c= compare(key_of(Items[i]), key);
501 ==========================================================================*
503 ==========================================================================*
508 * Initilizing the string collection
511 cl_strings::cl_strings(t_index alimit, t_index adelta):
512 cl_sorted_list(alimit, adelta)
518 cl_strings::~cl_strings(void) {}
522 * Comapare two string from the collection
526 cl_strings::compare(void *key1, void *key2)
528 return(strcmp((char *)key1, (char *)key2));
533 * Deallocate string item of the collection
537 cl_strings::free_item(void* item)
544 ==========================================================================*
546 ==========================================================================*
551 * Initilizing the unsorted string collection
554 cl_ustrings::cl_ustrings(t_index alimit, t_index adelta):
555 cl_strings(alimit, adelta)
559 cl_ustrings::~cl_ustrings(void) {}
563 * Comapare two string from the collection
567 cl_ustrings::compare(void *key1, void *key2)
574 * Searching an item using linear search.
578 cl_ustrings::search(void *key, t_index& index)
586 while (!found && (i < count))
588 Actual= key_of(at(i));
589 found = (Actual != 0) &&
590 (compare(key, Actual) == 0);