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)
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)
141 if (index_of(item, &i))
147 * Deleting all the items from the collection but not disposing them
151 cl_list::disconn_all(void)
158 * Deleting the indexed item from the collection and disposing it
162 cl_list::free_at(t_index index)
164 void *Item= at(index);
171 cl_list::free_all(void)
177 for (i= count-1; i; i--)
185 * Inserting a new item to the exact position
189 cl_list::add_at(t_index index, void *item)
194 set_limit(count + Delta);
196 memmove(&Items[index+1], &Items[index], (count-index)*sizeof(void *));
204 * Put a new item to the collection. This function replaces an existing
205 * item with a new one but it does not delete or dispose the old item!
209 cl_list::put_at(t_index index, void *item)
218 * Action taken when an error occure
222 cl_list::error(t_index code, t_index info)
225 "Collection index error. Code= %d, Info= %d.\n",
232 * Iterator method. This function calls 'Test' using every items as Test's
233 * argument until Test returns TRUE.
237 cl_list::first_that(match_func test, void *arg)
239 for (t_index i= 0; i < count; i++)
241 if (test(Items[i], arg)) return(Items[i]);
248 * Iterator method. This function calls 'Action' using every items as
253 cl_list::for_each(iterator_func action, void *arg)
255 for(t_index i= 0; i < count; i++)
256 action(Items[i], arg);
265 cl_list::free_item(void *item)
267 delete (class cl_base*)item;
272 * Get the number of collected items.
276 cl_list::get_count(void)
303 * Returning the index of an item.
307 cl_list::index_of(void *item)
309 for (t_index i= 0; i < count; i++)
310 if (item == Items[i])
313 return(0); /* Needed by Sun! */
317 cl_list::index_of(void *item, t_index *idx)
319 for (t_index i= 0; i < count; i++)
320 if (item == Items[i])
331 * Inserting a new item to the collection.
335 cl_list::add(void *item)
345 cl_list::push(void *item)
355 * Iterator method. This function calls 'Test' using every items
356 * (in reversed order) as Test's argument until Test returns TRUE.
360 cl_list::last_that(match_func test, void *arg)
362 for(t_index i= count; i > 0; i--)
363 if (test(Items[i-1], arg))
376 void **CurDst= Items;
377 void **CurSrc= Items;
378 void **Last = Items + count;
380 while (CurSrc < Last)
390 * Setting up the maximum number of items. This function may expand
391 * the size of the collection.
395 cl_list::set_limit(t_index alimit)
401 if (alimit > (int)max_list_size)
402 alimit= max_list_size;
409 //AItems = new void *[alimit];
410 int i= alimit*(sizeof(void *));
411 AItems= (void **)malloc(i);
413 memcpy(AItems, Items, count*sizeof(void *));
424 ==========================================================================*
426 ==========================================================================*
431 * Initilizing the sorted collection
434 cl_sorted_list::cl_sorted_list(t_index alimit, t_index adelta):
435 cl_list(alimit, adelta)
437 Duplicates= DD_FALSE;
441 cl_sorted_list::~cl_sorted_list(void) {}
445 * Get the address of the key field in an item.
449 cl_sorted_list::key_of(void *item)
456 * Get index of an item.
460 cl_sorted_list::index_of(void *item)
464 if (search(key_of(item), i) == 0)
481 * Inserting a new item to the collection
485 cl_sorted_list::add(void *item)
489 if (search(key_of(item), i) == 0 ||
490 Duplicates) // order dependency!
491 add_at(i, item); // must do Search
499 * Searching an item using binary search.
503 cl_sorted_list::search(void *key, t_index &index)
506 t_index h = count - 1;
511 t_index i= (l + h) >> 1;
512 t_index c= compare(key_of(Items[i]), key);
531 ==========================================================================*
533 ==========================================================================*
538 * Initilizing the string collection
541 cl_strings::cl_strings(t_index alimit, t_index adelta):
542 cl_sorted_list(alimit, adelta)
548 cl_strings::~cl_strings(void) {}
552 * Comapare two string from the collection
556 cl_strings::compare(void *key1, void *key2)
558 return(strcmp((char *)key1, (char *)key2));
563 * Deallocate string item of the collection
567 cl_strings::free_item(void* item)
569 delete (class cl_base*)item;
574 ==========================================================================*
576 ==========================================================================*
581 * Initilizing the unsorted string collection
584 cl_ustrings::cl_ustrings(t_index alimit, t_index adelta):
585 cl_strings(alimit, adelta)
589 cl_ustrings::~cl_ustrings(void) {}
593 * Comapare two string from the collection
597 cl_ustrings::compare(void *key1, void *key2)
604 * Searching an item using linear search.
608 cl_ustrings::search(void *key, t_index& index)
611 bool found= DD_FALSE;
616 while (!found && (i < count))
618 Actual= key_of(at(i));
619 found = (Actual != 0) &&
620 (compare(key, Actual) == 0);