🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1671

Re: Inversion-of-control and dependency-injection in C

#1671
From: fir
Date: Thu, 19 Nov 2026 15:03
142 lines
6366 bytes
Bonita Montero pisze:
> Am 19.11.2026 um 10:30 schrieb fir:
>> Bonita Montero pisze:
>>> There are no complex data structures in C and it's very hard
>>> to handle them as easy and as efficient as in C++. Imagine
>>> you have a unordered_map<string_view, intin C++ and now you
>>> should implement that in C. With C++ that's a one-liner, with
>>> C that's a week of work.
>>
>>
>> as this shot above you get it from ai - i never needed that so i dont
>> want to discus it (have may ovn more algebraic attitude to containers)
>>
>> writing it alosne is not a weeks of work arso adjusting it to some
>> needs not so hard
>>
>> this one cosed me like 2 * 3 seconds maybe here
>>
>> ofc c has some unconvenience in syntax and with some additions the 
>> containers in c could be more nice ofc (thats what im sometimes 
>> working on)
>>
>>
>> #include <stdlib.h>
>> #include <string.h>
>> #include <stdio.h>
>>
>> typedef struct {
>>      const char *ptr;
>>      int length;
>> } StringView;
>>
>> typedef struct {
>>      StringView key;
>>      int value;
>>      unsigned hash;
>>      int used;
>> } StringIntMapEntry;
>>
>> typedef struct {
>>      StringIntMapEntry *data;
>>      int size;
>>      int capacity;
>> } StringIntMap;
>>
>> unsigned HashStringView(StringView s)
>> {
>>      unsigned h=2166136261u;
>>      int i;
>>      for(i=0;i<s.length;i++) {
>>          h^=(unsigned char)s.ptr[i];
>>          h*=16777619u;
>>      }
>>      return h;
>> }
>>
>> int StringViewEqual(StringView a,StringView b)
>> {
>>      return a.length==b.length && memcmp(a.ptr,b.ptr,a.length)==0;
>> }
>>
>> void StringIntMapInit(StringIntMap *m,int capacity)
>> {
>>      m->size=0;
>>      m->capacity=capacity;
>>      m- 
>>  >data=(StringIntMapEntry*)calloc(capacity,sizeof(StringIntMapEntry));
>> }
>>
>> void StringIntMapFree(StringIntMap *m)
>> {
>>      free(m->data);
>>      m->data=NULL;
>>      m->size=0;
>>      m->capacity=0;
>> }
>>
>> void StringIntMapResize(StringIntMap *m,int capacity)
>> {
>>      StringIntMapEntry *old=m->data;
>>      int old_capacity=m->capacity;
>>      int i;
>>
>>      m- 
>>  >data=(StringIntMapEntry*)calloc(capacity,sizeof(StringIntMapEntry));
>>      m->capacity=capacity;
>>      m->size=0;
>>
>>      for(i=0;i<old_capacity;i++) {
>>          if(old[i].used)
>>              StringIntMapPut(m,old[i].key,old[i].value);
>>      }
>>
>>      free(old);
>> }
>>
>> void StringIntMapPut(StringIntMap *m,StringView key,int value)
>> {
>>      unsigned hash=HashStringView(key);
>>      int index;
>>
>>      if((m->size+1)*100>m->capacity*70)
>>          StringIntMapResize(m,m->capacity*2);
>>
>>      index=hash%m->capacity;
>>
>>      while(m->data[index].used) {
>>          if(m->data[index].hash==hash && StringViewEqual(m- 
>>  >data[index].key,key)) {
>>              m->data[index].value=value;
>>              return;
>>          }
>>          index=(index+1)%m->capacity;
>>      }
>>
>>      m->data[index].used=1;
>>      m->data[index].key=key;
>>      m->data[index].value=value;
>>      m->data[index].hash=hash;
>>      m->size++;
>> }
>>
>> int *StringIntMapGet(StringIntMap *m,StringView key)
>> {
>>      unsigned hash=HashStringView(key);
>>      int index=hash%m->capacity;
>>
>>      while(m->data[index].used) {
>>          if(m->data[index].hash==hash && StringViewEqual(m- 
>>  >data[index].key,key))
>>              return &m->data[index].value;
>>          index=(index+1)%m->capacity;
>>      }
>>
>>      return NULL;
>> }
> Try to implement that with the full functionality of std::unordered_map.
> You suck.
> 
primitive troll lame bonita told me i suck..i think we ended all 
possible conversation as your level of posting is near animal imo...
so good luck ..in your animal level world
Technical Details
Message-ID: <11dn34l$2bn7o$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <11d6vjp$qhu8$1@raubtier-asyl.eternal-september.org> <dsednTD-Fblclmr3nZ2dnZfqnPWdnZ2d@giganews.com> <11d738r$ru7a$1@dont-email.me> <11d75i7$spj0$1@dont-email.me> <11d7c8k$173nk$1@dont-email.me> <11d7fbv$1839o$2@dont-email.me> <PKGcne0xA53EC2D3nZ2dnZfqn_udnZ2d@giganews.com> <11dk8ib$1c198$1@dont-email.me> <BOmdncI_EeozMmD3nZ2dnZfqnPudnZ2d@giganews.com> <wZydnbpCy4bjnmP3nZ2dnZfqnPSdnZ2d@giganews.com> <11dl2g6$1ml3o$1@dont-email.me> <_OucnXHtiJG62GP3nZ2dnZfqn_ednZ2d@giganews.com> <11dm61j$213t5$1@dont-email.me> <11dm63d$213t5$2@dont-email.me> <11dm6lh$21ag2$1@raubtier-asyl.eternal-september.org> <11dma0n$229vg$1@dont-email.me> <11dmcsg$2361r$1@raubtier-asyl.eternal-september.org> <11dmfj1$246eg$1@dont-email.me> <11dn2hh$2bfcr$1@raubtier-asyl.eternal-september.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: fir <profesor.fir@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Inversion-of-control and dependency-injection in C
Date: Thu, 19 Nov 2026 16:03:42 +0100
Organization: A noiseless patient Spider
Lines: 142
Message-ID: <11dn34l$2bn7o$1@dont-email.me>
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com>
 <11d56gl$3ud5q$1@dont-email.me>
 <11d6vjp$qhu8$1@raubtier-asyl.eternal-september.org>
 <dsednTD-Fblclmr3nZ2dnZfqnPWdnZ2d@giganews.com>
 <11d738r$ru7a$1@dont-email.me> <11d75i7$spj0$1@dont-email.me>
 <11d7c8k$173nk$1@dont-email.me> <11d7fbv$1839o$2@dont-email.me>
 <PKGcne0xA53EC2D3nZ2dnZfqn_udnZ2d@giganews.com>
 <11dk8ib$1c198$1@dont-email.me>
 <BOmdncI_EeozMmD3nZ2dnZfqnPudnZ2d@giganews.com>
 <wZydnbpCy4bjnmP3nZ2dnZfqnPSdnZ2d@giganews.com>
 <11dl2g6$1ml3o$1@dont-email.me>
 <_OucnXHtiJG62GP3nZ2dnZfqn_ednZ2d@giganews.com>
 <11dm61j$213t5$1@dont-email.me> <11dm63d$213t5$2@dont-email.me>
 <11dm6lh$21ag2$1@raubtier-asyl.eternal-september.org>
 <11dma0n$229vg$1@dont-email.me>
 <11dmcsg$2361r$1@raubtier-asyl.eternal-september.org>
 <11dmfj1$246eg$1@dont-email.me>
 <11dn2hh$2bfcr$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 19 Nov 2026 15:03:50 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2481400"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1899eS1zst+9J1nCFcS+sZLdgO7luZTt3I="; posting-host="f1147b6e4ae1fa2903a1f60805897f84"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101
 Firefox/140.0 SeaMonkey/2.53.24
Cancel-Lock: sha1:LhooXNCQCc1zf1RNqEf7+FKqy00= sha256:HIDoAZkUEh0svsjn+pmqwg7APQl5zmk4pwQjvnUuOW4=
	sha1:uDzg2SLqKvxEYhCQh08vIm1HYWs= sha256:KL/5Dgp/0o/276F1OiFE5JZCrORZ55m8NG4VzuXse4Y=
In-Reply-To: <11dn2hh$2bfcr$1@raubtier-asyl.eternal-september.org>