Changeset 20 for trunk

Show
Ignore:
Timestamp:
09/07/06 15:01:48 (2 years ago)
Author:
Freeagle
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Universal/UniCon/State/Vector.d

    r19 r20  
     1/** 
     2 * Module: Universal.UniCon.State.Vector 
     3 *  
     4 * Licence: 
     5 */ 
     6 
    17module Universal.UniCon.State.Vector; 
    28 
     
    915    /* public constructors */ 
    1016    public: 
    11     this(int size) 
     17     
     18    this(size_t size) 
    1219    { 
    1320        _data = new T[size]; 
     
    1724    public: 
    1825     
     26    IReader 
     27    read() 
     28    { 
     29        return new VectorHandler(_data); 
     30    } 
     31     
     32    IEditor 
     33    edit() 
     34    { 
     35        T[] temp = _data; 
     36        T[] arrayCopy = new T[temp.length]; 
     37        arrayCopy[] = temp[]; 
     38        return new VectorHandler(arrayCopy); 
     39    } 
     40     
    1941    void 
    20     read() 
    21     { 
    22     } 
    23      
    24     void edit() 
    25     { 
    26     } 
    27      
    28     void store() 
    29     { 
     42    store(IEditor array) 
     43    { 
     44        _data = (cast(VectorHandler)array)._data; 
     45        (cast(VectorHandler)array)._data = null; 
    3046    } 
    3147     
     
    3349    private: 
    3450     
    35     class VectorHandler(T) 
     51    static class VectorHandler : IReader, IEditor 
    3652    { 
    3753        /* fields */ 
     
    4864         
    4965        /* public methods */ 
     66        public: 
     67         
     68        /* reader-specific methods */ 
     69        IEditor 
     70        edit() 
     71        { 
     72            T[] arrayCopy = new T[_data.length]; 
     73            arrayCopy[] = _data[]; 
     74            return new Vector.VectorHandler(arrayCopy); 
     75        } 
     76         
     77        size_t 
     78        size() 
     79        { 
     80            return _data.length; 
     81        } 
     82         
     83        bool 
     84        empty() 
     85        { 
     86            return !(_data.length); 
     87        } 
     88         
     89        T[] 
     90        array() 
     91        { 
     92            return _data; 
     93        } 
     94         
     95        T 
     96        opIndex(size_t index) 
     97        { 
     98            return _data[index]; 
     99        } 
     100         
     101        T[] 
     102        opSlice() 
     103        { 
     104            return _data[]; 
     105        } 
     106         
     107        T[] 
     108        opSlice(size_t i1, size_t i2) 
     109        { 
     110            return _data[i1..i2]; 
     111        } 
     112         
     113        T 
     114        front() 
     115        { 
     116            return _data[0]; 
     117        } 
     118         
     119        T 
     120        back() 
     121        { 
     122            return _data[$]; 
     123        } 
     124         
     125        /* editor-specific methods */ 
     126        void 
     127        resize(size_t size) 
     128        { 
     129            _data.length = size; 
     130        } 
     131         
     132        void 
     133        clear() 
     134        { 
     135            _data = _data[0..0]; 
     136        } 
     137         
     138        T 
     139        opIndexAssign(T value, size_t index) 
     140        { 
     141            return _data[index] = value; 
     142        } 
     143         
     144        T[] 
     145        opSliceAssign(T value) 
     146        { 
     147            return _data[] = value; 
     148        } 
     149         
     150        T[] 
     151        opSliceAssign(T value, size_t i1, size_t i2) 
     152        { 
     153            return _data[i1..i2] = value; 
     154        } 
     155         
     156        T[] 
     157        opSliceAssign(T[] value) 
     158        { 
     159            return _data[] = value; 
     160        } 
     161         
     162        T[] 
     163        opSliceAssign(T[] value, size_t i1, size_t i2) 
     164        { 
     165            return _data[i1..i2] = value; 
     166        } 
     167         
     168        void 
     169        add(T value, size_t index) 
     170        { 
     171            T[] temp = new T[_data.length+1]; 
     172            temp[0..index] = _data[0..index]; 
     173            temp[index] = value; 
     174            temp[index+1..$] = _data[index..$]; 
     175            _data = temp; 
     176        } 
     177         
     178        T 
     179        remove(size_t index) 
     180        { 
     181            T ret = _data[index]; 
     182            T[] temp = new T[_data.length-1]; 
     183            temp[0..index] = _data[0..index]; 
     184            temp[index..$] = _data[index+1..$]; 
     185            _data = temp; 
     186             
     187            return ret; 
     188        } 
     189         
     190        void 
     191        pushFront(T value) 
     192        { 
     193            _data = value ~ _data[]; 
     194        } 
     195         
     196        T 
     197        popFront() 
     198        { 
     199            T ret = _data[0]; 
     200            _data = _data[1..$]; 
     201             
     202            return ret; 
     203        } 
     204         
     205        void 
     206        pushBack(T value) 
     207        { 
     208            _data ~= value; 
     209        } 
     210         
     211        T 
     212        popBack() 
     213        { 
     214            T ret = _data[$-1]; 
     215            _data = _data[0..$-1]; 
     216             
     217            return ret; 
     218        } 
     219    } 
     220     
     221    /* public interfaces */ 
     222    public: 
     223     
     224    interface IReader 
     225    { 
     226        IEditor         edit(); 
     227         
     228        size_t          size(); 
     229        bool            empty(); 
     230        T[]             array(); 
     231        T               opIndex(size_t index); 
     232        T[]             opSlice(); 
     233        T[]             opSlice(size_t i1, size_t i2); 
     234        T               front(); 
     235        T               back(); 
     236    } 
     237     
     238    interface IEditor 
     239    { 
     240        size_t          size(); 
     241        bool            empty(); 
     242        T[]             array(); 
     243        T               opIndex(size_t index); 
     244        T[]             opSlice(); 
     245        T[]             opSlice(size_t i1, size_t i2); 
     246        T               front(); 
     247        T               back(); 
     248         
     249        void            resize(size_t size); 
     250        void            clear(); 
     251        T               opIndexAssign(T value, size_t index); 
     252        T[]             opSliceAssign(T value); 
     253        T[]             opSliceAssign(T value, size_t i1, size_t i2); 
     254        T[]             opSliceAssign(T[] value); 
     255        T[]             opSliceAssign(T[] value, size_t i1, size_t i2); 
     256        void            add(T value, size_t index); 
     257        T               remove(size_t index); 
     258        void            pushFront(T value); 
     259        T               popFront(); 
     260        void            pushBack(T value); 
     261        T               popBack(); 
    50262    } 
    51263}