📄 rambuffer.cpp
字号:
/* Scalable K-means clustering softwareCopyright (C) 2000 Fredrik Farnstrom and James LewisThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.See the file README.TXT for more information.*//* rambuffer.cpp */#include <math.h>#include <stdio.h>#include "singleton.h"#include "subcluster.h"#include "rambuffer.h"RAMBuffer::RAMBuffer(long size, int dim) : totalUsed(0), dimensions(dim), compressedSetSize(0){ totalMem = size; discardSet = compressedSet = 0; retainedSet = 0;}RAMBuffer::~RAMBuffer(){ Subcluster *c, *nc; Singleton *p, *np; for(c = discardSet; c; c = nc) { nc = c->getNext(); delete c; } for(c = compressedSet; c; c = nc) { nc = c->getNext(); delete c; } for(p = retainedSet; p; p = np) { np = p->getNext(); delete p; } // Destroy everything in discardSet, retainedSet, compressedSet}Subcluster* RAMBuffer::newDiscardCluster(void){ long s; // Allocate new subcluster Subcluster *c = new Subcluster(dimensions); // See if it fits in buffer if((s = c->getMemUsed()) + totalUsed > totalMem) { // Return 0 if not delete c; return 0; } totalUsed += s; // Otherwise, put in discardSet list and return ptr to object. if(discardSet) discardSet->setPrevious(c); c->setPrevious(0); c->setNext(discardSet); discardSet = c; return c;}Singleton* RAMBuffer::newSingleton(void){ long s; // Allocate new singleton Singleton *c = new Singleton(dimensions); // See if it fits in buffer if((s = c->getMemUsed()) + totalUsed > totalMem) { // Return 0 if not delete c; return 0; } totalUsed += s; // Otherwise, put in retainedSet list and return ptr to object. if(retainedSet) retainedSet->setPrevious(c); c->setPrevious(0); c->setNext(retainedSet); retainedSet = c; return c;}Subcluster* RAMBuffer::newSecondaryCluster(void){ long s; // Allocate new subcluster Subcluster *c = new Subcluster(dimensions); // See if it fits in buffer if((s = c->getMemUsed()) + totalUsed > totalMem) { // Return 0 if not delete c; return 0; } totalUsed += s; // Otherwise, put in compressedSet list and return ptr to object. if(compressedSet) compressedSet->setPrevious(c); c->setPrevious(0); c->setNext(compressedSet); compressedSet = c; compressedSetSize++; return c;}void RAMBuffer::addSecondaryCluster(Subcluster *c){ long s; if((s = c->getMemUsed()) + totalUsed > totalMem) fprintf(stderr, "Error: Not enough room for secondary cluster.\n"); totalUsed += s; if(compressedSet) compressedSet->setPrevious(c); c->setPrevious(0); c->setNext(compressedSet); compressedSet = c; compressedSetSize++;// fprintf(stderr, "addSecondaryCluster()\n");}void RAMBuffer::removeFromRetainedSet(Singleton *p){ Singleton *prev = p->getPrevious(), *next = p->getNext(); if(prev) prev->setNext(next); else retainedSet = next; if(next) next->setPrevious(prev); p->setPrevious(0); p->setNext(0); totalUsed -= p->getMemUsed();};void RAMBuffer::removeFromCompressedSet(Subcluster *p){ Subcluster *prev = p->getPrevious(), *next = p->getNext(); if(prev) prev->setNext(next); else compressedSet = next; if(next) next->setPrevious(prev); p->setPrevious(0); p->setNext(0); totalUsed -= p->getMemUsed(); compressedSetSize--;};long RAMBuffer::getTotalPoints(void){ Singleton *p; Subcluster *c; long count = 0; for(p = retainedSet; p; p = p->getNext()) count++; for(c = compressedSet; c; c = c->getNext()) count += c->getNumPoints(); for(c = discardSet; c; c = c->getNext()) count += c->getNumPoints(); return count;}/* End of file rambuffer.cpp */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -