array.h

Go to the documentation of this file.
00001 #ifndef ARRAY_CLASS
00002 #define ARRAY_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : array                                                             *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1989-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "guards.h"
00019 
00021 
00043 template <class contents>
00044 class array
00045 {
00046 public:
00048   enum special_flags {
00049     NO_SPECIAL_MODES = 0x0,  
00050     SIMPLE_COPY = 0x1,  
00051     EXPONENTIAL_GROWTH = 0x2,  
00052     EXPONE = EXPONENTIAL_GROWTH,  
00053     FLUSH_INVISIBLE = 0x4  
00054   };
00055 
00056   array(int number = 0, const contents *init = NIL,
00057           u_short flags = EXPONENTIAL_GROWTH | FLUSH_INVISIBLE);
00059 
00074   array(const array<contents> &copy_from);
00076 
00077   ~array();  
00078 
00079   void reset(int number = 0, const contents *initial_contents = NIL);
00081     /*< If "initial_contents" is not NIL, then it must contain an array of
00082     "contents" with at least "number" objects in it.  If it is NIL, then
00083     the size of the array is changed but the contents are not.  note that
00084     any pre-existing elements that were previously out of range might still
00085     have their prior contents; the newly available elements are not necessarily
00086     "blank".  thus, before using them, ensure you store appropriate elements
00087     in those positions. */
00088 
00089   array &operator = (const array<contents> &copy_from);
00091 
00092   inline int length() const { return _active_length; }
00094 
00095   inline int last() const { return _active_length - 1; }
00097 
00098   inline u_short flags() const { return _flags; }
00100 
00101   inline bool exponential() const { return _flags & EXPONENTIAL_GROWTH; }
00103 
00104   inline bool simple() const { return _flags & SIMPLE_COPY; }
00106 
00107   const contents &get(int index) const;
00109 
00111   contents &use(int index);
00113 
00114   inline const contents &operator [] (int index) const { return get(index); }
00116   inline contents &operator [] (int index) { return use(index); }
00118 
00119   outcome put(int index, const contents &to_put);
00121 
00123   array concatenation(const array &to_concatenate) const;
00125   array concatenation(const contents &to_concatenate) const;
00127 
00128   array &concatenate(const array &to_concatenate);
00130   array &concatenate(const contents &to_concatenate);
00132   array &concatenate(const contents *to_concatenate, int length);
00134 
00136   inline array operator + (const array &to_cat) const
00137           { return concatenation(to_cat); }
00139   inline array operator + (const contents &to_concatenate) const
00140           { return concatenation(to_concatenate); }
00142   inline array &operator += (const array &to_concatenate)
00143           { return concatenate(to_concatenate); }
00145   inline array &operator += (const contents &to_concatenate)
00146           { return concatenate(to_concatenate); }
00148 
00149   inline const contents *observe() const { return _offset; }
00151 
00153   inline contents *access() { return _offset; }
00155 
00156   void swap_contents(array<contents> &other);
00158 
00161   void snarf(array &new_contents);
00163 
00166   array subarray(int start, int end) const;
00168 
00172   outcome insert(int index, int new_indices);
00174 
00175   outcome overwrite(int index, const array &write_with, int count = -1);
00177 
00183   outcome stuff(int length, contents *to_stuff) const;
00185 
00188   outcome resize(int new_size, common::how_to_copy way = common::NEW_AT_END);
00190 
00201   outcome zap(int start, int end);
00203 
00207   outcome shrink();
00209 
00210   outcome retrain(int new_size, const contents *to_copy);
00212 
00213   enum shift_directions { TO_LEFT, TO_RIGHT };
00215 
00216   void shift_data(shift_directions where);
00218 
00221   // These are gritty internal information methods and should not be used
00222   // except by appropriately careful code.
00223   inline int internal_real_length() const { return _real_length; }
00225   inline int internal_offset() const { return int(_offset - _mem_block); }
00227   inline const contents *internal_block_start() const { return _mem_block; }
00229   inline contents *internal_block_start() { return _mem_block; }
00231   inline contents * const *internal_offset_mem() const { return &_offset; }
00233 
00234 private:
00235   int _active_length; 
00236   int _real_length;  // the real number of objects that can be stored.
00237   contents *_mem_block;  
00238   contents *_offset;  
00239   u_short _flags;  
00240 
00241   outcome allocator_reset(int initial_elements, int blocking);
00243 
00246 };
00247 
00249 class int_array : public array<int>
00250 {
00251 public:
00252   int_array(int number = 0, const int *initial_contents = 0)
00253       : array<int>(number, initial_contents, SIMPLE_COPY | EXPONE) {}
00255 
00258 };
00259 
00261 class double_array : public array<double>
00262 {
00263 public:
00264   double_array(int len = 0, double *data = NIL)
00265       : array<double>(len, data, SIMPLE_COPY | EXPONE) {}
00266   double_array(const array<double> &to_copy) : array<double>(to_copy) {}
00267 };
00268 
00269 #endif
00270 

Generated on Wed Aug 27 04:32:42 2008 for HOOPLE Libraries by  doxygen 1.5.1