home *** CD-ROM | disk | FTP | other *** search
- /*
- ** MacWT -- a 3d game engine for the Macintosh
- ** © 1995, Bill Hayden and Nikol Software
- ** Free for non-commercial use - address questions to the e-mail address below
- **
- ** Mail: afn28988@freenet.ufl.edu (Bill Hayden)
- ** MacWT FTP site: ftp.circa.ufl.edu/pub/software/ufmug/mirrors/LocalSW/Hayden/
- ** WWW Page: http://grove.ufl.edu:80/~nikolsw
- **
- ** All of the above addresses are due to changes sometime in 1996, so stay tuned
- **
- ** based on wt, by Chris Laurel
- **
- ** 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.
- */
-
-
- #include <string.h>
- #include "wt.h"
- #include "error.h"
- #include "wtmem.h"
- #include "table.h"
-
-
- #define STARTING_TABLE_SIZE 16
-
-
- /* For a dynamic table, pass 0 for the table_size; otherwise the table size
- ** will be fixed at table_size entries;
- */
- Table *new_table(size_t entry_size, short table_size)
- {
- Table *t;
-
- t = (Table *)wtmalloc(sizeof(Table));
- t->entry_size = entry_size;
- t->current_entries = 0;
- if (table_size != 0)
- {
- t->fixed_size = TRUE;
- t->max_entries = table_size;
- t->table = wtmalloc(entry_size * table_size);
- }
- else
- {
- t->fixed_size = FALSE;
- t->max_entries = 0;
- t->table = NULL;
- }
-
- return t;
- }
-
-
- /* Add an entry to a table and return its index. */
- short add_table_entry(Table *t, void *entry)
- {
- if (t->current_entries >= t->max_entries)
- {
- if (t->fixed_size)
- fatal_error("Fixed size table full.");
-
- if (t->max_entries == 0)
- {
- t->max_entries = STARTING_TABLE_SIZE;
- t->table = wtmalloc(t->max_entries * t->entry_size);
- }
- else
- {
- t->max_entries = (t->current_entries * 3) / 2;
- t->table = wtrealloc(t->table, t->max_entries * t->entry_size);
- }
- }
-
- memcpy((char *) t->table + t->entry_size * t->current_entries, entry, t->entry_size);
-
- return t->current_entries++;
- }
-
-