Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/array.h @ 4793

Last change on this file since 4793 was 4793, checked in by patrick, 19 years ago

orxonox/trunk: some comments, started implementing the Model::buildTriangleList() function, Array::getIndex now makes a value not reference comparison

File size: 4.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16/*!
17  \file array.h
18  \brief Contains the Array Class that handles float arrays.
19  this class creates a Array of a semi-Dynamic length.
20  beware, that after finalizing the array may not be resized again.
21*/
22
23#ifndef _ARRAY_H
24#define _ARRAY_H
25#include "debug.h"
26
27//! Array Class that handles dynamic-float arrays.
28template<class T> class Array
29{
30  public:
31    Array ();
32    ~Array();
33
34    void finalizeArray ();
35    void addEntry (T entry);
36    void addEntry(T entry0, T entry1, T entry2);
37
38    /** \returns The array */
39    inline const T* getArray () const { return this->array; };
40    /**   \returns The Count of entries in the Array*/
41    inline unsigned int getCount()const { return this->entryCount; };
42    inline int getIndex(T* entry) const;
43    void debug() const ;
44
45  private:
46    //! One entry of the Array
47    struct Entry
48    {
49      T            value;          //!< The value of this Entry.
50      Entry*       next;           //!< Pointer to the Next entry.
51    };
52
53    T*            array;           //!< The array that will be produced when finalizing the Array.
54    unsigned int  entryCount;      //!< The count of Entries in this Array.
55    bool          finalized;       //!< If this variable is set to true, the Array can not be changed anymore. true if finalized, false else (initially).
56    Entry*        firstEntry;      //!< Pointer to the first Entry of this Array
57    Entry*        currentEntry;    //!< Pointer to the current Entry of this Array. The one Entry we are working with.
58};
59
60
61/**
62   \brief creates a new Array
63*/
64template<class T>
65Array<T>::Array ()
66{
67  PRINTF(4)("crating new Array\n");
68  this->firstEntry = new Entry;
69  this->firstEntry->next =NULL;
70  this->currentEntry = this->firstEntry;
71  this->finalized = false;
72  this->entryCount = 0; //0 means one entry
73}
74
75/**
76   \brief deletes an Array.
77   It does this by first deleting all the array-entries, and then delete the array[] itself
78*/
79template<class T>
80Array<T>::~Array()
81{
82  PRINTF(4)("deleting array\n");
83  Entry* walker = this->firstEntry;
84  Entry* previous;
85  while (walker)
86  {
87    previous = walker;
88    walker = walker->next;
89    delete previous;
90  }
91  if (finalized)
92    delete []this->array;
93}
94
95/**
96   \brief finalizes an array.
97   This Function creates the array, and makes it ready to be sent to the application.
98*/
99template<class T>
100void Array<T>::finalizeArray ()
101{
102  PRINTF(4)("Finalizing array. Length: %i\n", entryCount);
103  //  if ((array = (GLfloat*)malloc( entryCount* sizeof(GLfloat))) == NULL)
104  if (!(this->array = new GLfloat [this->entryCount]))
105    PRINTF(1)("could not allocate %i data Blocks\n", this->entryCount);
106  Entry* walker = this->firstEntry;
107  for (int i=0; i<this->entryCount; i++)
108  {
109    this->array[i] = walker->value;
110    walker = walker->next;
111  }
112  walker = this->firstEntry;
113  Entry* previous;
114  while (walker)
115  {
116    previous = walker;
117    walker = walker->next;
118    delete previous;
119  }
120  this->firstEntry = NULL;
121  this->finalized = true;
122}
123
124/**
125   \brief adds a new Entry to the Array
126   \param entry Entry to add.
127*/
128template<class T>
129void Array<T>::addEntry (T entry)
130{
131  if (!this->finalized)
132  {
133    PRINTF(5)("adding new Entry to Array: %f\n", entry);
134
135    this->currentEntry->value = entry;
136    this->currentEntry->next = new Entry;
137    this->currentEntry = currentEntry->next;
138    this->currentEntry->next = NULL;
139    ++this->entryCount;
140  }
141  else
142    PRINTF(2)("adding failed, because array has already been finalized\n");
143}
144
145/**
146   \brief Adds 3 entries at once (convenience)
147*/
148template<class T>
149void Array<T>::addEntry (T entry0, T entry1, T entry2)
150{
151  this->addEntry(entry0);
152  this->addEntry(entry1);
153  this->addEntry(entry2);
154}
155
156
157/**
158   \brief gets back the index of the entry in the array. value check
159   \param entry: the entry to look up
160   \returns the index in the array, -1 if not found
161 */
162template<class T>
163int Array<T>::getIndex(T* entry) const
164{
165  if( unlikely(this->finalized == false))
166    return -1;
167
168  for(int i = 0; i < this->entryCount; ++i)
169  {
170    if( unlikely(*entry == this->array[i]))
171      return i;
172  }
173}
174
175
176/**
177   \brief Simple debug info about the Array
178*/
179template<class T>
180void Array<T>::debug () const
181{
182  PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array);
183}
184
185#endif
Note: See TracBrowser for help on using the repository browser.