Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8000793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:54:37+00:00 2026-06-04T15:54:37+00:00

Is there an way to zero out an array in with time complexsity O(1)?

  • 0

Is there an way to zero out an array in with time complexsity O(1)? It’s obvious that this can be done by for-loop, memset. But their time complexity are not O(1).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:54:39+00:00Added an answer on June 4, 2026 at 3:54 pm

    Yes

    However not any array. It takes an array that has been crafted for this to work.

    template <typename T, size_t N>
    class Array {
    public:
        Array(): generation(0) {}
    
        void clear() {
            // FIXME: deal with overflow
            ++generation;
        }
    
        T get(std::size_t i) const {
            if (i >= N) { throw std::runtime_error("out of range"); }
    
            TimedT const& t = data[i];
            return t.second == generation ? t.first : T{};
        }
    
        void set(std::size_t i, T t) {
            if (i >= N) { throw std::runtime_error("out of range"); }
    
            data[i] = std::make_pair(t, generation);
        }
    
    
    private:
        typedef std::pair<T, unsigned> TimedT;
    
        TimedT data[N];
        unsigned generation;
    };
    

    The principle is simple:

    • we define an epoch using the generation attribute
    • when an item is set, the epoch in which it has been set is recorded
    • only items of the current epoch can be seen
    • clearing is thus equivalent to incrementing the epoch counter

    The method has two issues:

    • storage increase: for each item we store an epoch
    • generation counter overflow: there is something as a maximum number of epochs

    The latter can be thwarted using a real big integer (uint64_t at the cost of more storage).

    The former is a natural consequence, one possible solution is to use buckets to downplay the issue by having for example up to 64 items associated to a single counter and a bitmask identifying which are valid within this counter.


    EDIT: just wanted to get back on the buckets idea.

    The original solution has an overhead of 8 bytes (64 bits) per element (if already 8-bytes aligned). Depending on the elements stored it might or might not be a big deal.

    If it is a big deal, the idea is to use buckets; of course like all trade-off it slows down access even more.

    template <typename T>
    class BucketArray {
    public:
         BucketArray(): generation(0), mask(0) {}
         
         T get(std::size_t index, std::size_t gen) const {
             assert(index < 64);
    
             return gen == generation and (mask & (1 << index)) ?
                    data[index] : T{};
         }
    
         void set(std::size_t index, T t, std::size_t gen) {
             assert(index < 64);
    
             if (generation < gen) { mask = 0; generation = gen; }
    
             mask |= (1 << index);
             data[index] = t;
         }
    
    private:
         std::uint64_t generation;
         std::uint64_t mask;
         T data[64];
    };
    

    Note that this small array of a fixed number of elements (we could actually template this and statically check it’s inferior or equal to 64) only has 16 bytes of overhead. This means we have an overhead of 2 bits per element.

    template <typename T, size_t N>
    class Array {
        typedef BucketArray<T> Bucket;
    public:
        Array(): generation(0) {}
        
        void clear() { ++generation; }
    
        T get(std::size_t i) const {
            if (i >= N) { throw ... }
    
            Bucket const& bucket = data[i / 64];
            return bucket.get(i % 64, generation);
        }
    
        void set(std::size_t i, T t) {
            if (i >= N) { throw ... }
    
            Bucket& bucket = data[i / 64];
            bucket.set(i % 64, t, generation);
        }
    
    private:
        std::uint64_t generation;
        Bucket data[N / 64 + 1];
    };
    

    We got the space overhead down by a factor of… 32. Now the array can even be used to store char for example, whereas before it would have been prohibitive. The cost is that access got slower, as we get a division and modulo (when we will get a standardized operation that returns both results in one shot ?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there way that I can read the file from remote server using fopen
My google-fu's failing me again. The information is (probably) out there, but I can't
Is there a way by which we can find out if a clip board
Is there way to set @include mixin(); to variable? I tried this @mixin bg-gradient($fallback,
Is there a way you can add a column to the Details view of
Is there a way I can know who holds a reference to an object?
In boost::numeric::ublas , there are three sparse vector types . I can see that
Q. Is there a way to find out if an object has any "strong
I'm aware that there are several Cocoa Touch/iPhone calendar implementations available online, but for
In MySQL, is there a way to set the total fields to zero if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.