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)
56 * Destructing the object: calling hte virtual Done method
59 cl_base::~cl_base(void)
65 int cl_base::init(void) {return(0);}
68 cl_base::get_name(char *def)
76 cl_base::set_name(char *new_name)
83 name= strdup(new_name);
91 ==========================================================================*
93 ==========================================================================*
98 * Initializing a collection
101 cl_list::cl_list(t_index alimit, t_index adelta):
113 * Disposing object's variables
116 cl_list::~cl_list(void)
124 * Get indexed item from the collection
128 cl_list::at(t_index index)
133 return(Items[index]);
137 cl_list::operator[](t_index index)
142 return(Items[index]);
147 * Deleting the indexed item from the collection
151 cl_list::disconn_at(t_index index)
157 memmove(&Items[index], &Items[index+1], (count-index)*sizeof(void *));
162 * Deleting an item from the collection but not disposing it
166 cl_list::disconn(void *item)
170 if (index_of(item, &i))
176 * Deleting all the items from the collection but not disposing them
180 cl_list::disconn_all(void)
187 * Deleting the indexed item from the collection and disposing it
191 cl_list::free_at(t_index index)
193 void *Item= at(index);
200 cl_list::free_all(void)
206 for (i= count-1; i; i--)
214 * Inserting a new item to the exact position
218 cl_list::add_at(t_index index, void *item)
223 set_limit(count + Delta);
225 memmove(&Items[index+1], &Items[index], (count-index)*sizeof(void *));
233 * Put a new item to the collection. This function replaces an existing
234 * item with a new one but it does not delete or dispose the old item!
238 cl_list::put_at(t_index index, void *item)
247 * Action taken when an error occure
251 cl_list::error(t_index code, t_index info)
254 "Collection index error. Code= %d, Info= %d.\n",
261 * Iterator method. This function calls 'Test' using every items as Test's
262 * argument until Test returns TRUE.
266 cl_list::first_that(match_func test, void *arg)
268 for (t_index i= 0; i < count; i++)
270 if (test(Items[i], arg)) return(Items[i]);
277 * Iterator method. This function calls 'Action' using every items as
282 cl_list::for_each(iterator_func action, void *arg)
284 for(t_index i= 0; i < count; i++)
285 action(Items[i], arg);
294 cl_list::free_item(void *item)
296 delete (class cl_base*)item;
301 * Get the number of collected items.
305 cl_list::get_count(void)
332 * Returning the index of an item.
336 cl_list::index_of(void *item)
338 for (t_index i= 0; i < count; i++)
339 if (item == Items[i])
342 return(0); /* Needed by Sun! */
346 cl_list::index_of(void *item, t_index *idx)
348 for (t_index i= 0; i < count; i++)
349 if (item == Items[i])
360 * Inserting a new item to the collection.
364 cl_list::add(void *item)
374 cl_list::push(void *item)
384 * Iterator method. This function calls 'Test' using every items
385 * (in reversed order) as Test's argument until Test returns TRUE.
389 cl_list::last_that(match_func test, void *arg)
391 for(t_index i= count; i > 0; i--)
392 if (test(Items[i-1], arg))
405 void **CurDst= Items;
406 void **CurSrc= Items;
407 void **Last = Items + count;
409 while (CurSrc < Last)
419 * Setting up the maximum number of items. This function may expand
420 * the size of the collection.
424 cl_list::set_limit(t_index alimit)
430 if (alimit > (int)max_list_size)
431 alimit= max_list_size;
438 //AItems = new void *[alimit];
439 int i= alimit*(sizeof(void *));
440 AItems= (void **)malloc(i);
442 memcpy(AItems, Items, count*sizeof(void *));
453 ==========================================================================*
455 ==========================================================================*
460 * Initilizing the sorted collection
463 cl_sorted_list::cl_sorted_list(t_index alimit, t_index adelta):
464 cl_list(alimit, adelta)
466 Duplicates= DD_FALSE;
470 cl_sorted_list::~cl_sorted_list(void) {}
474 * Get the address of the key field in an item.
478 cl_sorted_list::key_of(void *item)
485 * Get index of an item.
489 cl_sorted_list::index_of(void *item)
493 if (search(key_of(item), i) == 0)
510 * Inserting a new item to the collection
514 cl_sorted_list::add(void *item)
518 if (search(key_of(item), i) == 0 ||
519 Duplicates) // order dependency!
520 add_at(i, item); // must do Search
528 * Searching an item using binary search.
532 cl_sorted_list::search(void *key, t_index &index)
535 t_index h = count - 1;
540 t_index i= (l + h) >> 1;
541 t_index c= compare(key_of(Items[i]), key);
560 ==========================================================================*
562 ==========================================================================*
567 * Initilizing the string collection
570 cl_strings::cl_strings(t_index alimit, t_index adelta):
571 cl_sorted_list(alimit, adelta)
577 cl_strings::~cl_strings(void) {}
581 * Comapare two string from the collection
585 cl_strings::compare(void *key1, void *key2)
587 return(strcmp((char *)key1, (char *)key2));
592 * Deallocate string item of the collection
596 cl_strings::free_item(void* item)
598 delete (class cl_base*)item;
603 ==========================================================================*
605 ==========================================================================*
610 * Initilizing the unsorted string collection
613 cl_ustrings::cl_ustrings(t_index alimit, t_index adelta):
614 cl_strings(alimit, adelta)
618 cl_ustrings::~cl_ustrings(void) {}
622 * Comapare two string from the collection
626 cl_ustrings::compare(void *key1, void *key2)
633 * Searching an item using linear search.
637 cl_ustrings::search(void *key, t_index& index)
640 bool found= DD_FALSE;
645 while (!found && (i < count))
647 Actual= key_of(at(i));
648 found = (Actual != 0) &&
649 (compare(key, Actual) == 0);