[Assorted-commits] SF.net SVN: assorted:[1359] cpp-commons/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-04-30 20:39:21
|
Revision: 1359 http://assorted.svn.sourceforge.net/assorted/?rev=1359&view=rev Author: yangzhang Date: 2009-04-30 20:39:12 +0000 (Thu, 30 Apr 2009) Log Message: ----------- renamed getbit, setbit to get_bit, set_bit; added whole_units Modified Paths: -------------- cpp-commons/trunk/src/commons/memory.h cpp-commons/trunk/src/commons/versioned_heap.h Added Paths: ----------- cpp-commons/trunk/src/test/memory.cc Modified: cpp-commons/trunk/src/commons/memory.h =================================================================== --- cpp-commons/trunk/src/commons/memory.h 2009-04-30 15:56:53 UTC (rev 1358) +++ cpp-commons/trunk/src/commons/memory.h 2009-04-30 20:39:12 UTC (rev 1359) @@ -2,6 +2,7 @@ #define COMMONS_MEMORY_H #include <cstring> // for size_t +#include <commons/utility.h> // for UNUSED namespace commons { @@ -61,22 +62,32 @@ void skip(size_t n) { p_ += n; } }; - size_t nbits2nints(size_t nbits) { + /** Round `value` up to the nearest multiple of `unit` and return this + * factor. */ + template<typename T> + T whole_units(T value, T unit) { + return (value + unit - 1) / unit; + } + + /** Number of 32-bit ints to hold the given number of bits. */ + UNUSED static size_t nbits2nints(size_t nbits) { return (nbits + 31) / 32; } template<typename T> - bool getbit(T x, size_t bit) { + bool get_bit(T x, size_t bit) { return (x >> bit) & 1; } + /** Note: there exists a setbit macro in /usr/include/sys/param.h */ template<typename T> - T setbit(T x, size_t bit) { + T set_bit(T x, size_t bit) { return x | (1U << bit); } + /** Note: there exists a setbit macro in /usr/include/sys/param.h */ template<typename T> - T setbit(T x, size_t bit, T val) { + T set_bit(T x, size_t bit, T val) { return x | (val << bit); } Modified: cpp-commons/trunk/src/commons/versioned_heap.h =================================================================== --- cpp-commons/trunk/src/commons/versioned_heap.h 2009-04-30 15:56:53 UTC (rev 1358) +++ cpp-commons/trunk/src/commons/versioned_heap.h 2009-04-30 20:39:12 UTC (rev 1359) @@ -1,5 +1,5 @@ -#ifndef VERSIONED_HEAP_H -#define VERSIONED_HEAP_H +#ifndef COMMONS_VERSIONED_HEAP_H +#define COMMONS_VERSIONED_HEAP_H #define _POSIX_C_SOURCE 200112L #include <cstdlib> @@ -136,7 +136,7 @@ for (size_t i = 0; i < freesize; ++i) { uint32_t val = 0; for (size_t j = 0; j < 32 && 32 * i + j < freemap_.size(); ++j) { - val = setbit(val, j, uint32_t(freemap_[32 * i + j])); + val = set_bit(val, j, uint32_t(freemap_[32 * i + j])); } w.write(val); } @@ -165,7 +165,7 @@ uint32_t val; r.read(val); for (size_t j = 0; j < nbits && 32 * i + j < nbits; ++j) { - freemap_.push_back(getbit(val, j)); + freemap_.push_back(get_bit(val, j)); } } Added: cpp-commons/trunk/src/test/memory.cc =================================================================== --- cpp-commons/trunk/src/test/memory.cc (rev 0) +++ cpp-commons/trunk/src/test/memory.cc 2009-04-30 20:39:12 UTC (rev 1359) @@ -0,0 +1,13 @@ +#include <commons/memory.h> +#include "test.h" + +TEST(memory, whole_units) { + EXPECT_EQ(0, whole_units(0, 100)); + EXPECT_EQ(1, whole_units(1, 100)); + EXPECT_EQ(1, whole_units(2, 100)); + EXPECT_EQ(1, whole_units(98, 100)); + EXPECT_EQ(1, whole_units(99, 100)); + EXPECT_EQ(1, whole_units(100, 100)); + EXPECT_EQ(2, whole_units(101, 100)); + EXPECT_EQ(2, whole_units(102, 100)); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |