Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4863 was 4836, checked in by bensch, 19 years ago

orxonox/trunk: renamed all the \param → @param and so on in Doxygen tags.
Thanks a lot to the kDevelop team. this took since the last commit :)

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    inline bool isFinalized() const { return this->finalized; }
44    void debug() const ;
45
46  private:
47    //! One entry of the Array
48    struct Entry
49    {
50      T            value;          //!< The value of this Entry.
51      Entry*       next;           //!< Pointer to the Next entry.
52    };
53
54    T*            array;           //!< The array that will be produced when finalizing the Array.
55    unsigned int  entryCount;      //!< The count of Entries in this Array.
56    bool          finalized;       //!< If this variable is set to true, the Array can not be changed anymore. true if finalized, false else (initially).
57    Entry*        firstEntry;      //!< Pointer to the first Entry of this Array
58    Entry*        currentEntry;    //!< Pointer to the current Entry of this Array. The one Entry we are working with.
59};
60
61
62/**
63 *  creates a new Array
64*/
65template<class T>
66Array<T>::Array ()
67{
68  PRINTF(4)("crating new Array\n");
69  this->firstEntry = new Entry;
70  this->firstEntry->next =NULL;
71  this->currentEntry = this->firstEntry;
72  this->finalized = false;
73  this->entryCount = 0; //0 means one entry
74}
75
76/**
77 *  deletes an Array.
78   It does this by first deleting all the array-entries, and then delete the array[] itself
79*/
80template<class T>
81Array<T>::~Array()
82{
83  PRINTF(4)("deleting array\n");
84  Entry* walker = this->firstEntry;
85  Entry* previous;
86  while (walker)
87  {
88    previous = walker;
89    walker = walker->next;
90    delete previous;
91  }
92  if (finalized)
93    delete []this->array;
94}
95
96/**
97 *  finalizes an array.
98   This Function creates the array, and makes it ready to be sent to the application.
99*/
100template<class T>
101void Array<T>::finalizeArray ()
102{
103  PRINTF(4)("Finalizing array. Length: %i\n", entryCount);
104  //  if ((array = (GLfloat*)malloc( entryCount* sizeof(GLfloat))) == NULL)
105  if (!(this->array = new GLfloat [this->entryCount]))
106    PRINTF(0)("could not allocate %i data Blocks\n", this->entryCount);
107  Entry* walker = this->firstEntry;
108  for (int i=0; i<this->entryCount; i++)
109  {
110    this->array[i] = walker->value;
111    walker = walker->next;
112  }
113  walker = this->firstEntry;
114  Entry* previous;
115  while (walker)
116  {
117    previous = walker;
118    walker = walker->next;
119    delete previous;
120  }
121  this->firstEntry = NULL;
122  this->finalized = true;
123}
124
125/**
126 *  adds a new Entry to the Array
127 * @param entry Entry to add.
128*/
129template<class T>
130void Array<T>::addEntry (T entry)
131{
132  if (!this->finalized)
133  {
134    PRINTF(5)("adding new Entry to Array: %f\n", entry);
135
136    this->currentEntry->value = entry;
137    this->currentEntry->next = new Entry;
138    this->currentEntry = currentEntry->next;
139    this->currentEntry->next = NULL;
140    ++this->entryCount;
141  }
142  else
143    PRINTF(2)("adding failed, because array has already been finalized\n");
144}
145
146/**
147 *  Adds 3 entries at once (convenience)
148*/
149template<class T>
150void Array<T>::addEntry (T entry0, T entry1, T entry2)
151{
152  this->addEntry(entry0);
153  this->addEntry(entry1);
154  this->addEntry(entry2);
155}
156
157
158/**
159 *  gets back the index of the entry in the array. value check
160 * @param entry: the entry to look up
161 * @returns the index in the array, -1 if not found
162 */
163template<class T>
164int Array<T>::getIndex(T* entry) const
165{
166  if( unlikely(this->finalized == false))
167    return -1;
168
169  for(int i = 0; i < this->entryCount; ++i)
170  {
171    if( unlikely(*entry == this->array[i]))
172      return i;
173  }
174}
175
176
177/**
178 *  Simple debug info about the Array
179*/
180template<class T>
181void Array<T>::debug () const
182{
183  PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array);
184}
185
186#endif
Note: See TracBrowser for help on using the repository browser.