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) {}
58 int cl_base::init(void) {return(0);}
62 ==========================================================================*
64 ==========================================================================*
69 * Initializing a collection
72 cl_list::cl_list(t_index alimit, t_index adelta):
84 * Disposing object's variables
87 cl_list::~cl_list(void)
94 * Get indexed item from the collection
98 cl_list::at(t_index index)
103 return(Items[index]);
107 cl_list::operator[](t_index index)
112 return(Items[index]);
117 * Deleting the indexed item from the collection
121 cl_list::disconn_at(t_index index)
127 memmove(&Items[index], &Items[index+1], (count-index)*sizeof(void *));
132 * Deleting an item from the collection but not disposing it
136 cl_list::disconn(void *item)
138 disconn_at(index_of(item));
143 * Deleting all the items from the collection but not disposing them
147 cl_list::disconn_all(void)
154 * Deleting the indexed item from the collection and disposing it
158 cl_list::free_at(t_index index)
160 void *Item= at(index);
168 * Inserting a new item to the exact position
172 cl_list::add_at(t_index index, void *item)
177 set_limit(count + Delta);
179 memmove(&Items[index+1], &Items[index], (count-index)*sizeof(void *));
187 * Put a new item to the collection. This function replaces an existing
188 * item with a new one but it does not delete or dispose the old item!
192 cl_list::put_at(t_index index, void *item)
201 * Action taken when an error occure
205 cl_list::error(t_index code, t_index info)
208 "Collection index error. Code= %d, Info= %d.\n",
215 * Iterator method. This function calls 'Test' using every items as Test's
216 * argument until Test returns TRUE.
220 cl_list::first_that(match_func test, void *arg)
222 for (t_index i= 0; i < count; i++)
224 if (test(Items[i], arg)) return(Items[i]);
231 * Iterator method. This function calls 'Action' using every items as
236 cl_list::for_each(iterator_func action, void *arg)
238 for(t_index i= 0; i < count; i++)
239 action(Items[i], arg);
248 cl_list::free_item(void *item)
250 delete (class cl_base*)item;
255 * Get the number of collected items.
259 cl_list::get_count(void)
286 * Returning the index of an item.
290 cl_list::index_of(void *item)
292 for (t_index i= 0; i < count; i++)
293 if (item == Items[i])
296 return(0); /* Needed by Sun! */
301 * Inserting a new item to the collection.
305 cl_list::add(void *item)
315 cl_list::push(void *item)
325 * Iterator method. This function calls 'Test' using every items
326 * (in reversed order) as Test's argument until Test returns TRUE.
330 cl_list::last_that(match_func test, void *arg)
332 for(t_index i= count; i > 0; i--)
333 if (test(Items[i-1], arg))
346 void **CurDst= Items;
347 void **CurSrc= Items;
348 void **Last = Items + count;
350 while (CurSrc < Last)
360 * Setting up the maximum number of items. This function may expand
361 * the size of the collection.
365 cl_list::set_limit(t_index alimit)
371 if (alimit > (int)max_list_size)
372 alimit= max_list_size;
379 AItems = new void *[alimit];
380 //i= ALimit*(sizeof(void *));
381 //AItems= (void **)malloc(i);
383 memcpy(AItems, Items, count*sizeof(void *));
393 ==========================================================================*
395 ==========================================================================*
400 * Initilizing the sorted collection
403 cl_sorted_list::cl_sorted_list(t_index alimit, t_index adelta):
404 cl_list(alimit, adelta)
406 Duplicates= DD_FALSE;
410 cl_sorted_list::~cl_sorted_list(void) {}
414 * Get the address of the key field in an item.
418 cl_sorted_list::key_of(void *item)
425 * Get index of an item.
429 cl_sorted_list::index_of(void *item)
433 if (search(key_of(item), i) == 0)
450 * Inserting a new item to the collection
454 cl_sorted_list::add(void *item)
458 if (search(key_of(item), i) == 0 ||
459 Duplicates) // order dependency!
460 add_at(i, item); // must do Search
468 * Searching an item using binary search.
472 cl_sorted_list::search(void *key, t_index &index)
475 t_index h = count - 1;
480 t_index i= (l + h) >> 1;
481 t_index c= compare(key_of(Items[i]), key);
500 ==========================================================================*
502 ==========================================================================*
507 * Initilizing the string collection
510 cl_strings::cl_strings(t_index alimit, t_index adelta):
511 cl_sorted_list(alimit, adelta)
517 cl_strings::~cl_strings(void) {}
521 * Comapare two string from the collection
525 cl_strings::compare(void *key1, void *key2)
527 return(strcmp((char *)key1, (char *)key2));
532 * Deallocate string item of the collection
536 cl_strings::free_item(void* item)
538 delete (class cl_base*)item;
543 ==========================================================================*
545 ==========================================================================*
550 * Initilizing the unsorted string collection
553 cl_ustrings::cl_ustrings(t_index alimit, t_index adelta):
554 cl_strings(alimit, adelta)
558 cl_ustrings::~cl_ustrings(void) {}
562 * Comapare two string from the collection
566 cl_ustrings::compare(void *key1, void *key2)
573 * Searching an item using linear search.
577 cl_ustrings::search(void *key, t_index& index)
580 bool found= DD_FALSE;
585 while (!found && (i < count))
587 Actual= key_of(at(i));
588 found = (Actual != 0) &&
589 (compare(key, Actual) == 0);