vector and bool

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ioannis Vranos

    #16
    Re: vector and bool

    Nonenix (astalavista.ne t) wrote:[color=blue]
    > hmm not sure but i think somethink lieke that can be done with structures i
    > mena this:
    >
    >
    > struct item {
    >
    > unsigned int IsTrue:1;
    > unsigned int watever:1;
    > unsigned int number:14;
    >
    > }
    >
    > some cpu dosen't support that or the compiler doesen't use it because of
    > speed loss
    >
    > but if it work is should looks like this in the ram:
    >
    > 0 | 1 | 2.............1 5 |
    > IsTrue | waterver | number |
    >
    >
    > you could use it like an int but IMPORTANT
    >
    > if you forgot the unsignet the int will always be 0[/color]


    Why?

    [color=blue]
    > gnu says this:
    >
    > ..cpp: In function 'int main()':
    > ...cpp:13: warning: comparison is always 0 due to width of bitfield[/color]


    May you provide that code?


    Actually it could be even like this:


    struct item
    {
    bool a:1;
    bool b:1;
    bool c:1;
    bool d:1;
    };



    --
    Ioannis Vranos


    Comment

    • Richard Herring

      #17
      Re: vector and bool

      In message <1097606565.479 989@athnrd02>, Ioannis Vranos
      <ivr@guesswh.at .grad.com> writes[color=blue]
      >Richard Herring wrote:
      >[color=green]
      >> For a weak enough meaning of "should", I agree that that's the
      >>intention. But I don't think anything in the Standard rules out a
      >>naive implementation that makes no attempt to optimise, and simply
      >>implements vector<bool> in terms of an underlying vector<some_int _type>.[/color]
      >
      >
      >There is not an explicit prohibition on this, however explicit
      >prohibitions are rare in the standard.[/color]

      Just so. The standard specifies an interface and complexity (speed)
      requirements, not the mechanism used to implement them. But it says
      nothing prescriptive about the memory requirements for containers.[color=blue]
      >
      >Also the implementer has to implement this particular reference:
      >
      >
      >// bit reference:
      >class reference {
      >friend class vector;
      >reference();
      >public:
      >Ëœreference( );
      >operator bool() const;
      >reference& operator=(const bool x);
      >reference& operator=(const reference& x);
      >void flip(); // flips the bit
      >};
      >[/color]
      Yes, and the fact that this is what operator[] is mandated to return is
      what makes it *possible* for the implementor to provide access to data
      packed into something smaller than an addressable unit.[color=blue]
      >
      >with the note:
      >
      >reference is a class that simulates the behavior of references of a
      >single bit in vector<bool>.[/color]

      Yes; note the word "simulates" .[color=blue]
      >
      >and this particular member function:
      >
      >void flip(); // flips all bits
      >
      >Also vector<bool> is mentioned as a separate case in the standard, and
      >thus I think the legislator intended this to be implemented only with
      >bits.[/color]

      I'd say "intended it to be _possible_ to implement with bits".
      [color=blue]
      >:-)[/color]

      --
      Richard Herring

      Comment

      • Jeff Flinn

        #18
        Re: vector and bool


        "daniel" <danielheiserer @netscape.net> wrote in message
        news:b385c098.0 410121314.5b5c0 715@posting.goo gle.com...[color=blue]
        > Well I found something:
        > http://www.sgi.com/tech/stl/bit_vector.html states
        > ----------------
        > A bit_vector is essentially a vector<bool>: it is a Sequence that has
        > the same interface as vector. The main difference is that bit_vector
        > is optimized for space efficiency. A vector always requires at least
        > one byte per element, but a bit_vector only requires one bit per
        > element.
        >
        > Warning: The name bit_vector will be removed in a future release of
        > the STL. The only reason that bit_vector is a separate class, instead
        > of a template specialization of vector<bool>, is that this would
        > require partial specialization of templates. On compilers that support
        > partial specialization, bit_vector is a specialization of
        > vector<bool>. The name bit_vector is a typedef. This typedef is not
        > defined in the C++ standard, and is retained only for backward
        > compatibility.
        > ----------------
        > The std::bitset seems not to be useful because I cannot change its
        > size during runtime (no ->resize()).[/color]


        Which is why I also suggested boost::dynamic_ bitset. See


        Jeff F


        [color=blue]
        > So that should answer my first question.
        >
        > Thanks for the help, daniel
        >
        >
        >
        > "Jeff Flinn" <NONONE@nowhere .com> wrote in message[/color]
        news:<ckgnot$q0 h$1@bluegill.ad i.com>...[color=blue][color=green]
        > > "daniel" <danielheiserer @netscape.net> wrote in message
        > > news:b385c098.0 410112357.408a0 c8e@posting.goo gle.com...[color=darkred]
        > > > 1)
        > > > is C++ smart enough to automatically use "bits" for bool or will
        > > > a bool have the size of a charcter (byte).
        > > > 2) The index of a vector is it an integer (4 byte) or a "long long"
        > > > with 8 bytes or something else?
        > > >
        > > > I ask because I want to use
        > > >
        > > > vector<bool> with a few billion entries exceeding the int32 range for
        > > > indexing and I want to use as few memory as possible for the whole
        > > > vector<bool>[/color]
        > >
        > > Use either std::bitset or boost::dynamic_ bitset. Both "pack" the bits[/color][/color]
        for[color=blue][color=green]
        > > better memory usage. You'll most likely have to modify these classes to
        > > account for indices > 32 bit.
        > >
        > > Jeff F[/color][/color]


        Comment

        • Ioannis Vranos

          #19
          Re: vector and bool

          Richard Herring wrote:
          [color=blue]
          > I'd say "intended it to be _possible_ to implement with bits".[/color]


          Well in the reference type definition



          // bit reference:
          class reference {
          friend class vector;
          reference();
          public:
          Ëœreference();
          operator bool() const;
          reference& operator=(const bool x);
          reference& operator=(const reference& x);
          void flip(); // flips the bit
          };



          the remark for flip() is "flips the bit". Which bit?



          --
          Ioannis Vranos


          Comment

          • Richard Herring

            #20
            Re: vector and bool

            In message <1097678253.422 286@athnrd02>, Ioannis Vranos
            <ivr@guesswh.at .grad.com> writes[color=blue]
            >Richard Herring wrote:
            >[color=green]
            >> I'd say "intended it to be _possible_ to implement with bits".[/color]
            >
            >
            >Well in the reference type definition
            >
            >
            >// bit reference:
            >class reference {
            >friend class vector;
            >reference();
            >public:
            >Ëœreference( );
            >operator bool() const;
            >reference& operator=(const bool x);
            >reference& operator=(const reference& x);
            >void flip(); // flips the bit
            >};
            >
            >
            >the remark for flip() is "flips the bit". Which bit?[/color]

            The one "simulated" by the reference in the note you quoted earlier:
            [color=blue]
            >reference is a class that simulates the behavior of references of a
            >single bit in vector<bool>.[/color]


            --
            Richard Herring

            Comment

            Working...