/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): */ #ifndef _SEQUENCE_H_ #define _SEQUENCE_H_ #include "Fundamentals.h" #include "Memory.h" template class Sequence { protected: N *elts; Uint32 cnt; public: // ---------------------------------------------------------------------------- // constructors. Sequence() : elts(NULL), cnt(0) {} Sequence(Uint32 count) : elts(NULL), cnt(0) { reserve(count); } ~Sequence() { if (elts) MEMOPS::free(elts); } // ---------------------------------------------------------------------------- // copy. Sequence(const Sequence& x) : elts(NULL), cnt(0) { Uint32 x_count = x.count(); const N *src = x.elements(); reserve(x_count); N *dst = elts; while (x_count--) *dst++ = *src++; } Sequence& operator=(const Sequence& x) { Uint32 x_count = x.count(); const N *src = x.elements(); N *dst; if ((&x == this) || (x_count == 0)) return *this; reserve(x_count); dst = elts; while (x_count--) *dst++ = *src++; // copy the elements. return *this; } // ---------------------------------------------------------------------------- // memory management. void reserve(Uint32 amount) { if (cnt < amount) { if (elts) MEMOPS::free(elts); elts = (N*)MEMOPS::alloc(amount * sizeof(N*)); cnt = amount; } } // ---------------------------------------------------------------------------- // pointers on the elements. N *elements() { return elts; } const N *elements() const { return elts; } // ---------------------------------------------------------------------------- // number of elements and the actual capacity. unsigned char empty() const { return cnt == 0; } Uint32 count() const { return cnt; } // ---------------------------------------------------------------------------- // add, remove, get element N& operator[](Uint32 index) { assert(index < count()); return elts[index]; } const N& operator[](Uint32 index) const { assert(index < count()); return elts[index]; } void fill(const N& element) { N* src = elts; Uint32 i = cnt; while (i--) *src++ = element; } }; #endif /* _SEQUENCE_H_ */