1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
//
// HtVector.h
//
// HtVector: A Vector class which holds objects of type Object.
// (A vector is an array that can expand as necessary)
// This class is very similar in interface to the List class
//
// Part of the ht://Dig package <https://htdig.sourceforge.net/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtVector.h,v 1.10 2004/05/28 13:15:21 lha Exp $
//
//
#ifndef _HtVector_h_
#define _HtVector_h_
#include "Object.h"
class HtVector : public Object
{
public:
//
// Constructor/Destructor
//
HtVector();
HtVector(int capacity);
~HtVector();
//
// Add() will append an Object to the end of the vector
//
void Add(Object *);
//
// Insert() will insert an object at the given position. If the
// position is larger than the number of objects in the vector, the
// object is appended; no new objects are created between the end
// of the vector and the given position.
//
void Insert(Object *, int position);
//
// Assign() will assign the object to the given position, replacing
// the object currently there. It is functionally equivalent to calling
// RemoveFrom() followed by Insert()
void Assign(Object *, int position);
//
// Find the given object in the vector and remove it from the vector.
// The object will NOT be deleted. If the object is not found,
// NOTOK will be returned, else OK.
//
int Remove(Object *);
//
// Remove the object at the given position
// (in some sense, the inverse of Insert)
//
int RemoveFrom(int position);
//
// Release() will remove all the objects from the vector.
// This will NOT delete them
void Release();
//
// Destroy() will delete all the objects in the vector. This is
// equivalent to calling the destructor
//
void Destroy();
//
// Vector traversel (a bit redundant since you can use [])
//
void Start_Get() {current_index = -1;}
Object *Get_Next();
Object *Get_First();
Object *Next(Object *current);
Object *Previous(Object *current);
Object *Last() {return element_count<=0?(Object *)NULL:data[element_count-1];}
//
// Direct access to vector items. To assign new objects, use
// Insert() or Add() or Assign()
//
Object *operator[] (int n) {return (n<0||n>=element_count)?(Object *)NULL:data[n];}
Object *Nth(int n) {return (n<0||n>=element_count)?(Object *)NULL:data[n];}
//
// Access to the number of elements
//
int Count() const {return element_count;}
int IsEmpty() {return element_count==0;}
//
// Get the index number of an object. If the object is not found,
// returns -1
//
int Index(Object *);
//
// Deep copy member function
//
Object *Copy() const;
//
// Vector Assignment
//
HtVector &operator= (HtVector *vector) {return *this = *vector;}
HtVector &operator= (HtVector &vector);
protected:
//
// The actual internal data array
Object **data;
//
// For traversal it is nice to know where we are...
//
int current_index;
//
// It's nice to keep track of how many things we contain...
// as well as how many slots we've declared
//
int element_count;
int allocated;
//
// Protected function to ensure capacity
//
void Allocate(int ensureCapacity);
};
#endif
|