Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.3 KiB

  1. /*
  2. * s r t a r r a y . h
  3. *
  4. * Author: Greg Friedman
  5. *
  6. * Purpose: Sorted array that grows dynamically. Sorting is
  7. * deferred until an array element is accessed.
  8. *
  9. * Copyright (C) Microsoft Corp. 1998.
  10. */
  11. #ifndef __SRTARRAY_H
  12. #define __SRTARRAY_H
  13. typedef int (__cdecl *PFNSORTEDARRAYCOMPARE)(const void *first, const void *second);
  14. // Client-installed comparison callback that enables sorting.
  15. // NOTE: the const void* passed into the address of a pointer in the array.
  16. // In other words, if the array is a collection of Foo*, the items passed
  17. // into the comparison callback will be of type Foo**.
  18. // Return values should be as follows:
  19. // return a negative integer if first is less than second
  20. // return 0 if first == second
  21. // return a positive integer if first is greater than second
  22. typedef void (__cdecl *PFNSORTEDARRAYFREEITEM)(void *pItem);
  23. // client-installed free callback. If this optional callback is installed,
  24. // it will be called once for each item in the array when the array
  25. // is destroyed.
  26. class CSortedArray
  27. {
  28. public:
  29. // Factory function. Call this method to instantiate.
  30. static HRESULT Create(PFNSORTEDARRAYCOMPARE pfnCompare,
  31. PFNSORTEDARRAYFREEITEM pfnFreeItem,
  32. CSortedArray **ppArray);
  33. ~CSortedArray(void);
  34. private:
  35. // constructor is private. call "Create"
  36. CSortedArray(void);
  37. CSortedArray(PFNSORTEDARRAYCOMPARE pfnCompare, PFNSORTEDARRAYFREEITEM pfnFreeItem);
  38. // unimplemented copy constructor and assignment operator
  39. CSortedArray(const CSortedArray& other);
  40. CSortedArray& operator=(const CSortedArray& other);
  41. public:
  42. long GetLength(void) const;
  43. void* GetItemAt(long lIndex) const; // Retrive the item at ulIndex.
  44. // If ulIndex is out of bounds,
  45. // result is NULL.
  46. BOOL Find(void* pItem, long *plIndex) const; // Find item.
  47. // If found, result is TRUE
  48. // and pulIndex is the location of
  49. // the item. If item is not found,
  50. // result is FALSE, and pulIndex is
  51. // location where item would be inserted
  52. HRESULT Add(void *pItem); // Add pItem into the array.
  53. HRESULT Remove(long lIndex); // Remove the item at ulIndex
  54. // It is an error if ulIndex is
  55. // out of bounds.
  56. HRESULT Remove(void *pItem); // Remove pItem from the array.
  57. // It is an error if pItem does not exist
  58. private:
  59. void _Sort(void) const;
  60. HRESULT _Grow(void) const;
  61. private:
  62. long m_lLength;
  63. mutable long m_lCapacity;
  64. void **m_data;
  65. PFNSORTEDARRAYCOMPARE m_pfnCompare;
  66. PFNSORTEDARRAYFREEITEM m_pfnFreeItem;
  67. mutable BOOL m_fSorted;
  68. };
  69. #endif // __SRTARRAY_H