src/misc/smallvec.h
changeset 9348 dc680f675138
parent 9335 4f1e59a9aed4
child 9427 af652de004a0
equal deleted inserted replaced
9347:923cdb28b336 9348:dc680f675138
    17 	{
    17 	{
    18 		free(this->data);
    18 		free(this->data);
    19 	}
    19 	}
    20 
    20 
    21 	/**
    21 	/**
       
    22 	 * Remove all items from the list.
       
    23 	 */
       
    24 	void Clear()
       
    25 	{
       
    26 		/* In fact we just reset the item counter avoiding the need to
       
    27 		 * probably reallocate the same amount of memory the list was
       
    28 		 * previously using. */
       
    29 		this->items = 0;
       
    30 	}
       
    31 
       
    32 	/**
       
    33 	 * Compact the list down to the smallest block size boundary.
       
    34 	 */
       
    35 	void Compact()
       
    36 	{
       
    37 		uint capacity = Align(this->items, S);
       
    38 		if (capacity >= this->capacity) return;
       
    39 
       
    40 		this->capacity = capacity;
       
    41 		this->data = ReallocT(this->data, this->capacity);
       
    42 	}
       
    43 
       
    44 	/**
    22 	 * Append an item and return it.
    45 	 * Append an item and return it.
    23 	 */
    46 	 */
    24 	T *Append()
    47 	T *Append()
    25 	{
    48 	{
    26 		if (this->items == this->capacity) {
    49 		if (this->items == this->capacity) {
    27 			this->capacity += S;
    50 			this->capacity += S;
    28 			this->data = ReallocT(this->data, this->capacity);
    51 			this->data = ReallocT(this->data, this->capacity);
    29 		}
    52 		}
    30 
    53 
    31 		return &this->data[this->items++];
    54 		return &this->data[this->items++];
       
    55 	}
       
    56 
       
    57 	/**
       
    58 	 * Get the number of items in the list.
       
    59 	 */
       
    60 	uint Length() const
       
    61 	{
       
    62 		return this->items;
    32 	}
    63 	}
    33 
    64 
    34 	const T *Begin() const
    65 	const T *Begin() const
    35 	{
    66 	{
    36 		return this->data;
    67 		return this->data;
    58 
    89 
    59 	T *Get(uint index)
    90 	T *Get(uint index)
    60 	{
    91 	{
    61 		return &this->data[index];
    92 		return &this->data[index];
    62 	}
    93 	}
       
    94 
       
    95 	const T &operator[](uint index) const
       
    96 	{
       
    97 		return this->data[index];
       
    98 	}
       
    99 
       
   100 	T &operator[](uint index)
       
   101 	{
       
   102 		return this->data[index];
       
   103 	}
    63 };
   104 };
    64 
   105 
    65 #endif /* SMALLVEC_H */
   106 #endif /* SMALLVEC_H */