13.01.2015 Views

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

✐<br />

✐<br />

✐<br />

“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 143 — #181<br />

✐<br />

4.1. Una librería pequeña al estilo C<br />

de datos que permite un compilador C, aunque <strong>en</strong> algunas máquinas puede ser de<br />

igual tamaño que la mayor. Aunque es dep<strong>en</strong>di<strong>en</strong>te de la implem<strong>en</strong>tación, por lo g<strong>en</strong>eral<br />

un unsigned char ti<strong>en</strong>e un tamaño de un byte. Dado que CStash está diseñado<br />

para almac<strong>en</strong>ar cualquier tipo de estructura, el lector se puede preguntar si no sería<br />

más apropiado un puntero void *. Sin embargo, el objetivo no es tratar este puntero<br />

de almac<strong>en</strong>ami<strong>en</strong>to como un bloque de datos de tipo desconocido, sino como un<br />

bloque de bytes contiguos.<br />

El archivo de código fu<strong>en</strong>te para la implem<strong>en</strong>tación (del que no se suele disponer<br />

si fuese una librería comercial —normalm<strong>en</strong>te sólo dispondrá de un .obj, .lib o<br />

.dll, etc) ti<strong>en</strong>e este aspecto:<br />

//: C04:CLib.cpp {O}<br />

// Implem<strong>en</strong>tation of example C-like library<br />

// Declare structure and functions:<br />

#include "CLib.h"<br />

#include <br />

#include <br />

using namespace std;<br />

// Quantity of elem<strong>en</strong>ts to add<br />

// wh<strong>en</strong> increasing storage:<br />

const int increm<strong>en</strong>t = 100;<br />

void initialize(CStash* s, int sz) {<br />

s->size = sz;<br />

s->quantity = 0;<br />

s->storage = 0;<br />

s->next = 0;<br />

}<br />

int add(CStash* s, const void* elem<strong>en</strong>t) {<br />

if(s->next >= s->quantity) //Enough space left<br />

inflate(s, increm<strong>en</strong>t);<br />

// Copy elem<strong>en</strong>t into storage,<br />

// starting at next empty space:<br />

int startBytes = s->next * s->size;<br />

unsigned char* e = (unsigned char*)elem<strong>en</strong>t;<br />

for(int i = 0; i < s->size; i++)<br />

s->storage[startBytes + i] = e[i];<br />

s->next++;<br />

return(s->next - 1); // Index number<br />

}<br />

void* fetch(CStash* s, int index) {<br />

// Check index boundaries:<br />

assert(0 = s->next)<br />

return 0; // To indicate the <strong>en</strong>d<br />

// Produce pointer to desired elem<strong>en</strong>t:<br />

return &(s->storage[index * s->size]);<br />

}<br />

int count(CStash* s) {<br />

return s->next; // Elem<strong>en</strong>ts in CStash<br />

}<br />

void inflate(CStash* s, int increase) {<br />

assert(increase > 0);<br />

143<br />

✐<br />

✐<br />

✐<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!