NAMD
FreeEnergyGroup.C
Go to the documentation of this file.
1 
7 // written by David Hurwitz, March to May 1998.
8 
9 #include <memory.h>
10 #include <iostream>
11 #include <iomanip>
12 using namespace std;
13 #include "charm++.h"
14 #include "FreeEnergyAssert.h"
15 #include "FreeEnergyGroup.h"
16 
17 
19 //------------------------------------------------------------------------
20 // make room for some ints.
21 //------------------------------------------------------------------------
22  m_NumInts = 0;
23  m_pInts = new int[kGroupNumToStart];
24  m_MaxNum = kGroupNumToStart;
25 }
26 
27 
29 //------------------------------------------------------------------------
30 // return borrowed memory to the free store.
31 //------------------------------------------------------------------------
32  delete []m_pInts;
33 }
34 
35 
36 void AGroup::Clear() {
37 //------------------------------------------------------------------------
38 // leave memory allocation alone.
39 //------------------------------------------------------------------------
40  m_NumInts = 0;
41 }
42 
43 
44 void AGroup::Add(int AnInt) {
45 //------------------------------------------------------------------------
46 // add an int to the list. if there's not enough room, make room.
47 //------------------------------------------------------------------------
48  int* pInts;
49 
50  // if there's no room for a new int
51  if (m_NumInts == m_MaxNum) {
52  // create an array with more space
53  m_MaxNum *= kGroupMultiplier;
54  pInts = new int[m_MaxNum];
55  // fast copy from the full array to the new one (memcpy(dest, src, bytes))
56  memcpy(pInts, m_pInts, sizeof(int)*m_NumInts);
57  // return the space used for the full array
58  delete []m_pInts;
59  // point to the bigger array
60  m_pInts = pInts;
61  }
62 
63  // add the int to the int array
64  m_pInts[m_NumInts] = AnInt;
65  m_NumInts++;
66 }
67 
68 
70 //------------------------------------------------------------------------
71 // return an int from this group of ints.
72 // note, by returning int, rather than int&, this function only allows
73 // retrieval of an int, not setting an int. to set, use add.
74 //------------------------------------------------------------------------
75  ASSERT((Index>=0) && (Index<m_NumInts));
76  return(m_pInts[Index]);
77 }
78 
79 
81 //------------------------------------------------------------------------
82 // make this object identical to the passed one.
83 //------------------------------------------------------------------------
84  // if there's not enough room here for Group's array
85  if (m_MaxNum < Group.m_MaxNum) {
86  // free this array and allocate space for the bigger one
87  delete []m_pInts;
88  m_MaxNum = Group.m_MaxNum;
89  m_pInts = new int[m_MaxNum];
90  }
91  // fast copy from the passed array to this one (memcpy(dest, src, bytes))
92  m_NumInts = Group.m_NumInts;
93  memcpy(m_pInts, Group.m_pInts, sizeof(int)*m_NumInts);
94  return(*this);
95 }
96 
97 void AGroup::List(int NumToList) {
98 //------------------------------------------------------------------------
99 // list NumToList integers in the group to standard out.
100 // if NumToList is negative, list them all.
101 //------------------------------------------------------------------------
102  int i;
103 
104  if (NumToList < 0) {
105  NumToList = m_NumInts;
106  }
107  for (i=0; i<NumToList; i++) {
108 // cout << setw(10) << i << " " << setw(10) << (*this)[i] << std::endl;
109  CkPrintf("%10d %10d\n", i, (*this)[i]);
110  }
111 }
const int kGroupMultiplier
int operator[](int Index)
int Index
Definition: structures.h:26
const int kGroupNumToStart
#define ASSERT(E)
void Clear()
AGroup & operator=(AGroup &Group)
void List(int NumToList=-1)
void Add(int AnInt)