Changeset 3902


Ignore:
Timestamp:
May 5, 2020, 4:17:54 AM (3 years ago)
Author:
Peter
Message:

closes #915. New functions that read and write numbers in binary format.

Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Makefile.am

    r3901 r3902  
    4949  test/bam_pair_iterator.test \
    5050  test/bam_pair_iterator2.test \
     51  test/binary_read_write.test \
    5152  test/chi2.test \
    5253  test/cigar.test \
  • trunk/yat/random/random.cc

    r3894 r3902  
    2626#include "yat/statistics/Histogram.h"
    2727#include "yat/utility/Exception.h"
     28#include "yat/utility/utility.h"
    2829
    2930#include <boost/smart_ptr.hpp>
     
    126127  unsigned long RNG::seed_from_devurandom(void)
    127128  {
    128     unsigned char ulongsize=sizeof(unsigned long);
    129     boost::scoped_array<char> buffer(new char[ulongsize]);
    130129    std::ifstream is("/dev/urandom", std::ios::binary);
    131     is.read(buffer.get(),ulongsize);
     130    unsigned long s=0;
     131    utility::binary_read(is, s);
    132132    is.close();
    133     unsigned long s=0;
    134     memcpy(&s, buffer.get(), ulongsize);
    135133    seed(s);
    136134    return s;
  • trunk/yat/utility/Makefile.am

    r3717 r3902  
    6565  $(srcdir)/yat/utility/BasicVector.h \
    6666  $(srcdir)/yat/utility/BasicQueue.h \
     67  $(srcdir)/yat/utility/BinaryIstreamIterator.h \
     68  $(srcdir)/yat/utility/BinaryOstreamIterator.h \
    6769  $(srcdir)/yat/utility/BLAS_level1.h \
    6870  $(srcdir)/yat/utility/BLAS_level2.h \
  • trunk/yat/utility/utility.h

    r3855 r3902  
    5050#include <cmath>
    5151#include <cstdlib>
     52#include <cstring>
    5253#include <functional>
    5354#include <limits>
     
    7778   */
    7879  std::string basename(const std::string& fn);
     80
     81  /**
     82     Read data from \a is into variable \a x. Data has typically been
     83     written with binary_write(std::ostream& os, T).
     84
     85     \c T is expected to be a native numerical type.
     86
     87     \return \a is
     88
     89     \since New in yat 0.18
     90   */
     91  template<typename T>
     92  std::istream& binary_read(std::istream& is, T& x)
     93  {
     94    const unsigned char type_size=sizeof(T);
     95    YAT_ASSERT(type_size <= 16);
     96    char buffer[16];
     97    is.read(buffer, type_size);
     98    memcpy(&x, buffer, type_size);
     99    return is;
     100  }
     101
     102
     103  /**
     104     Write value of \a x into ostream \a os in binary format. Since
     105     the data is written in the native binary format, it may not be
     106     portable between different architectures.
     107
     108     \c T is expected to be a native numerical type.
     109
     110     \see binary_read(std::istream&, T&)
     111
     112     \since New in yat 0.18
     113   */
     114  template<typename T>
     115  void binary_write(std::ostream& os, T x)
     116  {
     117    const unsigned char type_size=sizeof(T);
     118    YAT_ASSERT(type_size <= 16);
     119    char buffer[16];
     120    memcpy(buffer, &x, type_size);
     121    os.write(buffer, type_size);
     122  }
    79123
    80124  /**
Note: See TracChangeset for help on using the changeset viewer.