38 #include "NptConfig.h"    39 #if defined(NPT_CONFIG_HAVE_NEW_H)    43 #include "NptResults.h"    48 const int NPT_ARRAY_INITIAL_MAX_SIZE = 128; 
    62     NPT_Array<T>(): m_Capacity(0), m_ItemCount(0), m_Items(0) {}
    71     NPT_Cardinal GetItemCount()
 const { 
return m_ItemCount; }
    72     NPT_Result   Add(
const T& item);
    73     T& operator[](NPT_Ordinal 
pos)             { 
return m_Items[pos]; }
    74     const T& operator[](NPT_Ordinal pos)
 const { 
return m_Items[pos]; }
    75     NPT_Result   Erase(Iterator which);
    76     NPT_Result   Erase(NPT_Ordinal which) { 
return Erase(&m_Items[which]); }
    77     NPT_Result   Erase(Iterator first, Iterator last);
    78     NPT_Result   Erase(NPT_Ordinal first, NPT_Ordinal last) { 
return Erase(&m_Items[first], &m_Items[last]); }
    79     NPT_Result   Insert(Iterator where, 
const T& item, NPT_Cardinal count = 1);
    80     NPT_Result   Reserve(NPT_Cardinal count);
    81     NPT_Cardinal GetCapacity()
 const { 
return m_Capacity; }
    82     NPT_Result   Resize(NPT_Cardinal count);
    83     NPT_Result   Resize(NPT_Cardinal count, 
const T& fill);
    85     bool         Contains(
const T& data) 
const;
    86     Iterator     GetFirstItem()
 const { 
return m_ItemCount?&m_Items[0]:NULL; }
    87     Iterator     GetLastItem()
 const  { 
return m_ItemCount?&m_Items[m_ItemCount-1]:NULL; }
    88     Iterator     GetItem(NPT_Ordinal n) { 
return n<m_ItemCount?&m_Items[n]:NULL; }
    94     NPT_Result Apply(
const X& 
function)
 const    96         for (
unsigned int i=0; i<m_ItemCount; i++) 
function(m_Items[i]);
   100     template <
typename X, 
typename P>
   101     NPT_Result ApplyUntil(
const X& 
function, 
const P& predicate, 
bool* match = NULL)
 const   103         for (
unsigned int i=0; i<m_ItemCount; i++) {
   104             NPT_Result return_value;
   105             if (predicate(
function(m_Items[i]), return_value)) {
   106                 if (match) *match = 
true;
   110         if (match) *match = 
false;
   114     template <
typename X> 
   115     T* Find(
const X& predicate, NPT_Ordinal n=0, NPT_Ordinal* pos = NULL)
 const   119         for (
unsigned int i=0; i<m_ItemCount; i++) {
   120             if (predicate(m_Items[i])) {
   122                 if (n == 0) 
return &m_Items[i];
   131     T* Allocate(NPT_Cardinal count, NPT_Cardinal& allocated);
   134     NPT_Cardinal m_Capacity;
   135     NPT_Cardinal m_ItemCount;
   142 template <
typename T>
   155 template <
typename T>
   162     Reserve(copy.GetItemCount());
   163     for (NPT_Ordinal i=0; i<copy.m_ItemCount; i++) {
   164         new ((
void*)&m_Items[i]) T(copy.m_Items[i]);
   166     m_ItemCount = copy.m_ItemCount;
   172 template <
typename T>
   180     for (NPT_Ordinal i=0; i<count; i++) {
   181         new ((
void*)&m_Items[i]) T(item);
   188 template <
typename T>
   192     m_ItemCount(item_count),
   196     for (NPT_Ordinal i=0; i<item_count; i++) {
   197         new ((
void*)&m_Items[i]) T(items[i]);
   204 template <
typename T>
   212     ::operator 
delete((
void*)m_Items);
   218 template <
typename T>
   223     if (
this == ©) 
return *
this;
   229     Reserve(copy.GetItemCount());
   230     m_ItemCount = copy.m_ItemCount;
   231     for (NPT_Ordinal i=0; i<copy.m_ItemCount; i++) {
   232         new ((
void*)&m_Items[i]) T(copy.m_Items[i]);
   241 template <
typename T>
   246     for (NPT_Ordinal i=0; i<m_ItemCount; i++) {
   258 template <
typename T>
   263         allocated = 2*m_Capacity;
   267         allocated = NPT_ARRAY_INITIAL_MAX_SIZE/
sizeof(T);
   268         if (allocated == 0) allocated = 1;
   270     if (allocated < count) allocated = count;
   273     return (T*)::operator 
new(allocated*
sizeof(T));
   279 template <
typename T>
   283     if (count <= m_Capacity) 
return NPT_SUCCESS;
   286     NPT_Cardinal new_capacity;
   287     T* new_items = Allocate(count, new_capacity);
   288     if (new_items == NULL) {
   289         return NPT_ERROR_OUT_OF_MEMORY;
   291     if (m_ItemCount && m_Items) {
   292         for (
unsigned int i=0; i<m_ItemCount; i++) {
   294             new ((
void*)&new_items[i])T(m_Items[i]);
   300     ::operator 
delete((
void*)m_Items);
   302     m_Capacity = new_capacity;
   310 template <
typename T>
   316     NPT_Result result = Reserve(m_ItemCount+1);
   317     if (result != NPT_SUCCESS) 
return result;
   320     new ((
void*)&m_Items[m_ItemCount++]) T(item);
   328 template <
typename T>
   333     return Erase(which, which);
   339 template <
typename T>
   344     if (first == NULL || last == NULL) 
return NPT_ERROR_INVALID_PARAMETERS;
   347     NPT_Ordinal first_index = (NPT_Ordinal)(NPT_POINTER_TO_LONG(first-m_Items));
   348     NPT_Ordinal last_index  = (NPT_Ordinal)(NPT_POINTER_TO_LONG(last-m_Items));
   349     if (first_index >= m_ItemCount ||
   350         last_index  >= m_ItemCount ||
   351         first_index > last_index) {
   352         return NPT_ERROR_INVALID_PARAMETERS;
   356     NPT_Cardinal interval = last_index-first_index+1;
   357     NPT_Cardinal shifted = m_ItemCount-last_index-1;
   358     for (NPT_Ordinal i=first_index; i<first_index+shifted; i++) {
   359         m_Items[i] = m_Items[i+interval];
   363     for (NPT_Ordinal i=first_index+shifted; i<m_ItemCount; i++) {
   368     m_ItemCount -= interval;
   376 template <
typename T>
   381     NPT_Ordinal where_index = where?((NPT_Ordinal)NPT_POINTER_TO_LONG(where-m_Items)):m_ItemCount;
   382     if (where > &m_Items[m_ItemCount] || repeat == 0) 
return NPT_ERROR_INVALID_PARAMETERS;
   384     NPT_Cardinal needed = m_ItemCount+repeat;
   385     if (needed > m_Capacity) {
   387         NPT_Cardinal new_capacity;
   388         T* new_items = Allocate(needed, new_capacity);
   389         if (new_items == NULL) 
return NPT_ERROR_OUT_OF_MEMORY;
   390         m_Capacity = new_capacity;
   393         for (NPT_Ordinal i=0; i<where_index; i++) {
   394             new((
void*)&new_items[i])T(m_Items[i]);
   399         for (NPT_Ordinal i=where_index; i<m_ItemCount; i++) {
   400             new((
void*)&new_items[i+repeat])T(m_Items[i]);
   405         ::operator 
delete((
void*)m_Items);
   409         for (NPT_Ordinal i=m_ItemCount; i>where_index; i--) {
   410             new((
void*)&m_Items[i+repeat-1])T(m_Items[i-1]);
   416     for (NPT_Cardinal i=where_index; i<where_index+repeat; i++) {
   417         new((
void*)&m_Items[i])T(item);
   421     m_ItemCount += repeat;
   429 template <
typename T>
   433     if (size < m_ItemCount) {
   435         for (NPT_Ordinal i=size; i<m_ItemCount; i++) {
   439     } 
else if (size > m_ItemCount) {
   440         return Resize(size, T());
   449 template <
typename T>
   453     if (size < m_ItemCount) {
   455     } 
else if (size > m_ItemCount) {
   457         for (NPT_Ordinal i=m_ItemCount; i<size; i++) {
   458             new ((
void*)&m_Items[i]) T(fill);
   469 template <
typename T>
   473     for (NPT_Ordinal i=0; i<m_ItemCount; i++) {
   474         if (m_Items[i] == data) 
return true;
   483 template <
typename T>
   488     if (other.m_ItemCount != m_ItemCount) 
return false;
   491     for (NPT_Ordinal i=0; i<m_ItemCount; i++) {
   492         if (!(m_Items[i] == other.m_Items[i])) 
return false;
   501 template <
typename T>
   506     return !(*
this == other);
   509 #endif // _NPT_ARRAY_H_ Definition: NptArray.h:54
Definition: LibInputPointer.h:13